2014-11-17 03:49:20 +08:00
|
|
|
/*
|
2015-06-06 09:40:08 +08:00
|
|
|
Copyright © 2014-2015 by The qTox Project
|
|
|
|
|
2014-11-17 03:49:20 +08:00
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
2014-11-17 03:49:20 +08:00
|
|
|
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.
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
2014-11-17 03:49:20 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-06-06 09:40:08 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-11-17 03:49:20 +08:00
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
2014-11-17 03:49:20 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
#ifndef AUDIO_H
|
|
|
|
#define AUDIO_H
|
|
|
|
|
|
|
|
#include <atomic>
|
2015-10-22 13:57:46 +08:00
|
|
|
#include <cmath>
|
2015-12-10 13:48:28 +08:00
|
|
|
|
|
|
|
#include <QObject>
|
2016-01-21 23:43:13 +08:00
|
|
|
#include <QMutex>
|
2016-01-22 03:17:18 +08:00
|
|
|
#include <QTimer>
|
2016-01-21 23:43:13 +08:00
|
|
|
|
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
|
|
|
#include <OpenAL/al.h>
|
|
|
|
#include <OpenAL/alc.h>
|
|
|
|
#else
|
|
|
|
#include <AL/al.h>
|
|
|
|
#include <AL/alc.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef ALC_ALL_DEVICES_SPECIFIER
|
|
|
|
// compatibility with older versions of OpenAL
|
|
|
|
#include <AL/alext.h>
|
|
|
|
#endif
|
2014-11-17 02:04:45 +08:00
|
|
|
|
2016-01-22 03:17:18 +08:00
|
|
|
#ifdef QTOX_FILTER_AUDIO
|
|
|
|
#include "audiofilterer.h"
|
|
|
|
#endif
|
2014-11-17 02:04:45 +08:00
|
|
|
|
2015-09-28 05:16:40 +08:00
|
|
|
// Public default audio settings
|
|
|
|
static constexpr uint32_t AUDIO_SAMPLE_RATE = 48000; ///< The next best Opus would take is 24k
|
|
|
|
static constexpr uint32_t AUDIO_FRAME_DURATION = 20; ///< In milliseconds
|
2016-01-22 03:17:18 +08:00
|
|
|
static constexpr ALint AUDIO_FRAME_SAMPLE_COUNT = AUDIO_FRAME_DURATION * AUDIO_SAMPLE_RATE/1000;
|
2015-09-28 05:16:40 +08:00
|
|
|
static constexpr uint32_t AUDIO_CHANNELS = 2; ///< Ideally, we'd auto-detect, but that's a sane default
|
|
|
|
|
2015-08-19 01:40:11 +08:00
|
|
|
class Audio : public QObject
|
2014-11-17 02:04:45 +08:00
|
|
|
{
|
2016-01-21 23:43:13 +08:00
|
|
|
Q_OBJECT
|
2015-11-15 04:02:29 +08:00
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
public:
|
2015-10-18 21:51:38 +08:00
|
|
|
static Audio& getInstance();
|
2014-11-20 05:26:04 +08:00
|
|
|
|
2016-01-22 03:17:18 +08:00
|
|
|
ALfloat outputVolume();
|
|
|
|
void setOutputVolume(ALfloat volume);
|
2015-10-20 09:46:44 +08:00
|
|
|
|
2016-01-22 03:17:18 +08:00
|
|
|
ALfloat inputVolume();
|
|
|
|
void setInputVolume(ALfloat volume);
|
2015-10-20 09:46:44 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
void reinitInput(const QString& inDevDesc);
|
|
|
|
bool reinitOutput(const QString& outDevDesc);
|
2015-11-09 04:09:38 +08:00
|
|
|
|
2015-10-20 09:46:44 +08:00
|
|
|
bool isInputReady();
|
2015-10-24 08:59:12 +08:00
|
|
|
bool isOutputReady();
|
2015-10-20 09:46:44 +08:00
|
|
|
|
2015-12-11 07:16:21 +08:00
|
|
|
static const char* outDeviceNames();
|
|
|
|
static const char* inDeviceNames();
|
2016-01-21 23:43:13 +08:00
|
|
|
void subscribeOutput(ALuint& sid);
|
|
|
|
void unsubscribeOutput(ALuint& sid);
|
2016-01-22 03:17:18 +08:00
|
|
|
void subscribeInput();
|
|
|
|
void unsubscribeInput();
|
2015-10-05 08:36:50 +08:00
|
|
|
|
2015-10-24 08:26:32 +08:00
|
|
|
void startLoop();
|
|
|
|
void stopLoop();
|
2015-10-20 09:46:44 +08:00
|
|
|
void playMono16Sound(const QByteArray& data);
|
2015-11-15 09:38:16 +08:00
|
|
|
void playMono16Sound(const QString& path);
|
2014-11-17 02:04:45 +08:00
|
|
|
|
2016-01-22 03:17:18 +08:00
|
|
|
void playAudioBuffer(ALuint alSource, const int16_t *data, int samples,
|
2015-12-11 07:16:21 +08:00
|
|
|
unsigned channels, int sampleRate);
|
2015-09-28 03:34:14 +08:00
|
|
|
|
2015-08-19 01:40:11 +08:00
|
|
|
signals:
|
|
|
|
void groupAudioPlayed(int group, int peer, unsigned short volume);
|
2016-01-22 03:17:18 +08:00
|
|
|
/// When there are input subscribers, we regularly emit captured audio frames with this signal
|
|
|
|
/// Always connect with a blocking queued connection or a lambda, or the behavior is undefined
|
|
|
|
void frameAvailable(const int16_t *pcm, size_t sample_count, uint8_t channels, uint32_t sampling_rate);
|
2015-08-19 01:40:11 +08:00
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
private:
|
2015-10-18 21:23:33 +08:00
|
|
|
Audio();
|
2015-01-24 22:31:11 +08:00
|
|
|
~Audio();
|
2015-10-18 21:23:33 +08:00
|
|
|
|
2016-01-22 07:58:50 +08:00
|
|
|
static void checkAlError() noexcept;
|
|
|
|
static void checkAlcError(ALCdevice *device) noexcept;
|
|
|
|
|
2016-01-21 23:43:13 +08:00
|
|
|
bool autoInitInput();
|
|
|
|
bool autoInitOutput();
|
|
|
|
bool initInput(QString inDevDescr);
|
|
|
|
bool initOutput(QString outDevDescr);
|
|
|
|
void cleanupInput();
|
|
|
|
void cleanupOutput();
|
2016-01-22 07:10:54 +08:00
|
|
|
/// Called after a mono16 sound stopped playing
|
|
|
|
void playMono16SoundCleanup();
|
2016-01-22 03:17:18 +08:00
|
|
|
/// Called on the captureTimer events to capture audio
|
|
|
|
void doCapture();
|
|
|
|
#if defined(QTOX_FILTER_AUDIO) && defined(ALC_LOOPBACK_CAPTURE_SAMPLES)
|
|
|
|
void getEchoesToFilter(AudioFilterer* filter, int samples);
|
|
|
|
#endif
|
2016-01-21 23:43:13 +08:00
|
|
|
|
2015-11-15 04:50:32 +08:00
|
|
|
private:
|
2016-01-21 23:43:13 +08:00
|
|
|
QThread* audioThread;
|
|
|
|
QMutex audioLock;
|
|
|
|
|
|
|
|
ALCdevice* alInDev;
|
2016-01-22 03:17:18 +08:00
|
|
|
ALfloat inGain;
|
2016-01-21 23:43:13 +08:00
|
|
|
quint32 inSubscriptions;
|
2016-01-22 07:10:54 +08:00
|
|
|
QTimer captureTimer, playMono16Timer;
|
2016-01-21 23:43:13 +08:00
|
|
|
|
|
|
|
ALCdevice* alOutDev;
|
|
|
|
ALCcontext* alOutContext;
|
|
|
|
ALuint alMainSource;
|
2016-01-22 07:10:54 +08:00
|
|
|
ALuint alMainBuffer;
|
2016-01-21 23:43:13 +08:00
|
|
|
bool outputInitialized;
|
|
|
|
|
2016-01-22 03:17:18 +08:00
|
|
|
QList<ALuint> outSources;
|
|
|
|
#ifdef QTOX_FILTER_AUDIO
|
|
|
|
AudioFilterer filterer;
|
|
|
|
#endif
|
2014-11-17 02:04:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // AUDIO_H
|