2014-09-04 07:33:44 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2014-12-01 10:47:38 +00:00
|
|
|
Copyright (c) 2014 John Preston, https://desktop.telegram.org
|
2014-09-04 07:33:44 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
|
|
|
|
void audioInit();
|
|
|
|
bool audioWorks();
|
|
|
|
void audioPlayNotify();
|
|
|
|
void audioFinish();
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
enum AudioPlayerState {
|
2015-07-03 08:47:16 +00:00
|
|
|
AudioPlayerStopped = 0x01,
|
|
|
|
AudioPlayerStoppedAtEnd = 0x02,
|
|
|
|
AudioPlayerStoppedAtError = 0x03,
|
|
|
|
AudioPlayerStoppedAtStart = 0x04,
|
|
|
|
AudioPlayerStoppedMask = 0x07,
|
|
|
|
|
|
|
|
AudioPlayerStarting = 0x08,
|
|
|
|
AudioPlayerPlaying = 0x10,
|
|
|
|
AudioPlayerFinishing = 0x18,
|
|
|
|
AudioPlayerPausing = 0x20,
|
|
|
|
AudioPlayerPaused = 0x28,
|
|
|
|
AudioPlayerPausedAtEnd = 0x30,
|
|
|
|
AudioPlayerResuming = 0x38,
|
2014-09-04 07:33:44 +00:00
|
|
|
};
|
|
|
|
|
2015-05-30 16:30:47 +00:00
|
|
|
class AudioPlayerFader;
|
|
|
|
class AudioPlayerLoaders;
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
class AudioPlayer : public QObject {
|
2014-09-04 07:33:44 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
AudioPlayer();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-07-03 08:47:16 +00:00
|
|
|
void play(const AudioMsgId &audio, int64 position = 0);
|
|
|
|
void play(const SongMsgId &song, int64 position = 0);
|
|
|
|
void pauseresume(MediaOverviewType type, bool fast = false);
|
|
|
|
void seek(int64 position); // type == OverviewDocuments
|
|
|
|
void stop(MediaOverviewType type);
|
2015-06-30 21:07:05 +00:00
|
|
|
|
|
|
|
void currentState(AudioMsgId *audio, AudioPlayerState *state = 0, int64 *position = 0, int64 *duration = 0, int32 *frequency = 0);
|
|
|
|
void currentState(SongMsgId *song, AudioPlayerState *state = 0, int64 *position = 0, int64 *duration = 0, int32 *frequency = 0);
|
|
|
|
|
|
|
|
void clearStoppedAtStart(const AudioMsgId &audio);
|
|
|
|
void clearStoppedAtStart(const SongMsgId &song);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-06-03 12:18:46 +00:00
|
|
|
void resumeDevice();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
~AudioPlayer();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
public slots:
|
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
void onError(const AudioMsgId &audio);
|
|
|
|
void onError(const SongMsgId &song);
|
|
|
|
|
|
|
|
void onStopped(const AudioMsgId &audio);
|
|
|
|
void onStopped(const SongMsgId &song);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
signals:
|
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
void updated(const AudioMsgId &audio);
|
|
|
|
void updated(const SongMsgId &song);
|
|
|
|
|
|
|
|
void stopped(const AudioMsgId &audio);
|
|
|
|
void stopped(const SongMsgId &song);
|
|
|
|
|
2015-07-03 08:47:16 +00:00
|
|
|
void stoppedOnError(const AudioMsgId &audio);
|
|
|
|
void stoppedOnError(const SongMsgId &song);
|
|
|
|
|
|
|
|
void loaderOnStart(const AudioMsgId &audio, qint64 position);
|
|
|
|
void loaderOnStart(const SongMsgId &song, qint64 position);
|
2015-06-30 21:07:05 +00:00
|
|
|
|
|
|
|
void loaderOnCancel(const AudioMsgId &audio);
|
|
|
|
void loaderOnCancel(const SongMsgId &song);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
void faderOnTimer();
|
2015-07-03 08:47:16 +00:00
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
void suppressSong();
|
|
|
|
void unsuppressSong();
|
|
|
|
void suppressAll();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-07-03 08:47:16 +00:00
|
|
|
void songVolumeChanged();
|
|
|
|
|
2014-09-04 07:33:44 +00:00
|
|
|
private:
|
|
|
|
|
2015-07-03 08:47:16 +00:00
|
|
|
bool fadedStop(MediaOverviewType type, bool *fadedStart = 0);
|
2015-06-30 21:07:05 +00:00
|
|
|
bool updateCurrentStarted(MediaOverviewType type, int32 pos = -1);
|
2015-07-03 08:47:16 +00:00
|
|
|
bool checkCurrentALError(MediaOverviewType type);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
struct Msg {
|
2015-06-30 21:07:05 +00:00
|
|
|
Msg() : position(0), duration(0), frequency(AudioVoiceMsgFrequency), skipStart(0), skipEnd(0), loading(0), started(0),
|
2015-05-29 18:52:43 +00:00
|
|
|
state(AudioPlayerStopped), source(0), nextBuffer(0) {
|
2014-09-04 07:33:44 +00:00
|
|
|
memset(buffers, 0, sizeof(buffers));
|
|
|
|
memset(samplesCount, 0, sizeof(samplesCount));
|
|
|
|
}
|
2015-06-30 21:07:05 +00:00
|
|
|
|
2014-09-04 07:33:44 +00:00
|
|
|
QString fname;
|
|
|
|
QByteArray data;
|
|
|
|
int64 position, duration;
|
2015-05-24 17:58:39 +00:00
|
|
|
int32 frequency;
|
2014-09-04 07:33:44 +00:00
|
|
|
int64 skipStart, skipEnd;
|
|
|
|
bool loading;
|
|
|
|
int64 started;
|
2015-05-29 18:52:43 +00:00
|
|
|
AudioPlayerState state;
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
uint32 source;
|
|
|
|
int32 nextBuffer;
|
|
|
|
uint32 buffers[3];
|
|
|
|
int64 samplesCount[3];
|
|
|
|
};
|
2015-06-30 21:07:05 +00:00
|
|
|
struct AudioMsg : public Msg {
|
|
|
|
AudioMsg() {
|
|
|
|
}
|
|
|
|
AudioMsgId audio;
|
|
|
|
};
|
|
|
|
struct SongMsg : public Msg {
|
|
|
|
SongMsg() {
|
|
|
|
}
|
|
|
|
SongMsgId song;
|
|
|
|
};
|
|
|
|
|
|
|
|
void currentState(Msg *current, AudioPlayerState *state, int64 *position, int64 *duration, int32 *frequency);
|
2015-07-03 08:47:16 +00:00
|
|
|
void setStoppedState(Msg *current, AudioPlayerState state = AudioPlayerStopped);
|
2015-06-30 21:07:05 +00:00
|
|
|
|
|
|
|
int32 _audioCurrent;
|
|
|
|
AudioMsg _audioData[AudioVoiceMsgSimultaneously];
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
int32 _songCurrent;
|
|
|
|
SongMsg _songData[AudioSongSimultaneously];
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
QMutex _mutex;
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
friend class AudioPlayerFader;
|
|
|
|
friend class AudioPlayerLoaders;
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
QThread _faderThread, _loaderThread;
|
|
|
|
AudioPlayerFader *_fader;
|
|
|
|
AudioPlayerLoaders *_loader;
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-05-30 16:30:47 +00:00
|
|
|
class AudioCaptureInner;
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
class AudioCapture : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
AudioCapture();
|
|
|
|
|
|
|
|
void start();
|
|
|
|
void stop(bool needResult);
|
|
|
|
|
2015-06-01 10:58:46 +00:00
|
|
|
bool check();
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
~AudioCapture();
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
|
|
|
void captureOnStart();
|
|
|
|
void captureOnStop(bool needResult);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
void onDone(QByteArray data, qint32 samples);
|
|
|
|
void onUpdate(qint16 level, qint32 samples);
|
|
|
|
void onError();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
friend class AudioCaptureInner;
|
|
|
|
|
|
|
|
QThread _captureThread;
|
|
|
|
AudioCaptureInner *_capture;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
AudioPlayer *audioPlayer();
|
|
|
|
AudioCapture *audioCapture();
|
|
|
|
|
|
|
|
class AudioPlayerFader : public QObject {
|
2014-09-04 07:33:44 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
AudioPlayerFader(QThread *thread);
|
2015-06-03 12:18:46 +00:00
|
|
|
void resumeDevice();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
signals:
|
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
void error(const AudioMsgId &audio);
|
|
|
|
void error(const SongMsgId &audio);
|
|
|
|
void playPositionUpdated(const AudioMsgId &audio);
|
|
|
|
void playPositionUpdated(const SongMsgId &audio);
|
|
|
|
void audioStopped(const AudioMsgId &audio);
|
|
|
|
void audioStopped(const SongMsgId &audio);
|
|
|
|
void needToPreload(const AudioMsgId &audio);
|
|
|
|
void needToPreload(const SongMsgId &audio);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-06-03 12:18:46 +00:00
|
|
|
void stopPauseDevice();
|
2015-01-10 13:08:30 +00:00
|
|
|
|
2014-09-04 07:33:44 +00:00
|
|
|
public slots:
|
|
|
|
|
|
|
|
void onInit();
|
|
|
|
void onTimer();
|
2015-06-03 12:18:46 +00:00
|
|
|
void onPauseTimer();
|
|
|
|
void onPauseTimerStop();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
void onSuppressSong();
|
|
|
|
void onUnsuppressSong();
|
|
|
|
void onSuppressAll();
|
2015-07-03 08:47:16 +00:00
|
|
|
void onSongVolumeChanged();
|
2015-06-30 21:07:05 +00:00
|
|
|
|
2014-09-04 07:33:44 +00:00
|
|
|
private:
|
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
enum {
|
|
|
|
EmitError = 0x01,
|
|
|
|
EmitStopped = 0x02,
|
|
|
|
EmitPositionUpdated = 0x04,
|
|
|
|
EmitNeedToPreload = 0x08,
|
|
|
|
};
|
|
|
|
int32 updateOnePlayback(AudioPlayer::Msg *m, bool &hasPlaying, bool &hasFading, float64 suppressGain, bool suppressGainChanged);
|
2015-07-03 08:47:16 +00:00
|
|
|
void setStoppedState(AudioPlayer::Msg *m, AudioPlayerState state = AudioPlayerStopped);
|
2015-06-30 21:07:05 +00:00
|
|
|
|
2015-06-03 12:18:46 +00:00
|
|
|
QTimer _timer, _pauseTimer;
|
|
|
|
QMutex _pauseMutex;
|
|
|
|
bool _pauseFlag, _paused;
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-07-03 08:47:16 +00:00
|
|
|
bool _suppressAll, _suppressAllAnim, _suppressSong, _suppressSongAnim, _songVolumeChanged;
|
2015-06-30 21:07:05 +00:00
|
|
|
anim::fvalue _suppressAllGain, _suppressSongGain;
|
|
|
|
uint64 _suppressAllStart, _suppressSongStart;
|
|
|
|
|
2014-09-04 07:33:44 +00:00
|
|
|
};
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
class AudioPlayerLoader;
|
|
|
|
class AudioPlayerLoaders : public QObject {
|
2014-09-04 07:33:44 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
AudioPlayerLoaders(QThread *thread);
|
|
|
|
~AudioPlayerLoaders();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
signals:
|
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
void error(const AudioMsgId &audio);
|
|
|
|
void error(const SongMsgId &song);
|
2014-09-04 07:33:44 +00:00
|
|
|
void needToCheck();
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
|
|
|
|
void onInit();
|
2015-06-30 21:07:05 +00:00
|
|
|
|
2015-07-03 08:47:16 +00:00
|
|
|
void onStart(const AudioMsgId &audio, qint64 position);
|
|
|
|
void onStart(const SongMsgId &audio, qint64 position);
|
2015-06-30 21:07:05 +00:00
|
|
|
|
|
|
|
void onLoad(const AudioMsgId &audio);
|
|
|
|
void onLoad(const SongMsgId &audio);
|
|
|
|
|
|
|
|
void onCancel(const AudioMsgId &audio);
|
|
|
|
void onCancel(const SongMsgId &audio);
|
|
|
|
|
2014-09-04 07:33:44 +00:00
|
|
|
private:
|
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
AudioMsgId _audio;
|
|
|
|
AudioPlayerLoader *_audioLoader;
|
|
|
|
|
|
|
|
SongMsgId _song;
|
|
|
|
AudioPlayerLoader *_songLoader;
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2015-06-30 21:07:05 +00:00
|
|
|
void emitError(MediaOverviewType type);
|
|
|
|
void clear(MediaOverviewType type);
|
2015-07-03 08:47:16 +00:00
|
|
|
void setStoppedState(AudioPlayer::Msg *m, AudioPlayerState state = AudioPlayerStopped);
|
2015-06-30 21:07:05 +00:00
|
|
|
AudioMsgId clearAudio();
|
|
|
|
SongMsgId clearSong();
|
|
|
|
|
|
|
|
enum SetupError {
|
|
|
|
SetupErrorAtStart = 0,
|
|
|
|
SetupErrorNotPlaying = 1,
|
|
|
|
SetupErrorLoadedFull = 2,
|
|
|
|
SetupNoErrorStarted = 3,
|
|
|
|
};
|
2015-07-03 08:47:16 +00:00
|
|
|
void loadData(MediaOverviewType type, const void *objId, qint64 position);
|
|
|
|
AudioPlayerLoader *setupLoader(MediaOverviewType type, const void *objId, SetupError &err, qint64 position);
|
2015-06-30 21:07:05 +00:00
|
|
|
AudioPlayer::Msg *checkLoader(MediaOverviewType type);
|
2014-09-04 07:33:44 +00:00
|
|
|
|
|
|
|
};
|
2015-05-29 18:52:43 +00:00
|
|
|
|
2015-05-30 16:30:47 +00:00
|
|
|
struct AudioCapturePrivate;
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
class AudioCaptureInner : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
AudioCaptureInner(QThread *thread);
|
|
|
|
~AudioCaptureInner();
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
|
|
|
void error();
|
|
|
|
void update(qint16 level, qint32 samples);
|
|
|
|
void done(QByteArray data, qint32 samples);
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
|
|
|
|
void onInit();
|
|
|
|
void onStart();
|
|
|
|
void onStop(bool needResult);
|
|
|
|
|
|
|
|
void onTimeout();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void writeFrame(int32 offset, int32 framesize);
|
|
|
|
|
|
|
|
AudioCapturePrivate *d;
|
|
|
|
QTimer _timer;
|
|
|
|
QByteArray _captured;
|
|
|
|
|
|
|
|
};
|
2015-06-30 21:07:05 +00:00
|
|
|
|
|
|
|
MTPDocumentAttribute audioReadSongAttributes(const QString &fname, const QByteArray &data, QImage &cover, QByteArray &coverBytes, QByteArray &coverFormat);
|