1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/src/audio/audio.h

136 lines
3.5 KiB
C
Raw Normal View History

2014-11-17 03:49:20 +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.
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.
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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2014-11-17 03:49:20 +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
*/
#ifndef AUDIO_H
#define AUDIO_H
#include <atomic>
#include <cmath>
#include <QObject>
#include <QMutex>
#include <QTimer>
#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
2015-08-19 01:40:11 +08:00
class Audio : public QObject
{
Q_OBJECT
class Private;
public:
2015-10-18 21:51:38 +08:00
static Audio& getInstance();
qreal outputVolume() const;
void setOutputVolume(qreal volume);
2015-10-20 09:46:44 +08:00
qreal minInputGain() const;
void setMinInputGain(qreal dB);
2016-07-07 17:58:28 +08:00
qreal maxInputGain() const;
void setMaxInputGain(qreal dB);
2016-07-07 17:58:28 +08:00
qreal inputGain() const;
void setInputGain(qreal dB);
2015-10-20 09:46:44 +08:00
void reinitInput(const QString& inDevDesc);
bool reinitOutput(const QString& outDevDesc);
2015-11-09 04:09:38 +08:00
bool isOutputReady() const;
2015-10-20 09:46:44 +08:00
static QStringList outDeviceNames();
static QStringList inDeviceNames();
2016-07-07 17:58:28 +08:00
void subscribeOutput(ALuint& sid);
void unsubscribeOutput(ALuint& sid);
2016-07-07 17:58:28 +08:00
void subscribeInput();
void unsubscribeInput();
2015-10-05 08:36:50 +08:00
void startLoop();
void stopLoop();
2015-10-20 09:46:44 +08:00
void playMono16Sound(const QByteArray& data);
void playMono16Sound(const QString& path);
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
public:
// Public default audio settings
static constexpr uint32_t AUDIO_SAMPLE_RATE = 48000;
static constexpr uint32_t AUDIO_FRAME_DURATION = 20;
static constexpr ALint AUDIO_FRAME_SAMPLE_COUNT = AUDIO_FRAME_DURATION * AUDIO_SAMPLE_RATE/1000;
static constexpr uint32_t AUDIO_CHANNELS = 2;
2015-08-19 01:40:11 +08:00
signals:
void groupAudioPlayed(int group, int peer, unsigned short volume);
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
private:
Audio();
2015-01-24 22:31:11 +08:00
~Audio();
static void checkAlError() noexcept;
static void checkAlcError(ALCdevice *device) noexcept;
bool autoInitInput();
bool autoInitOutput();
bool initInput(const QString& deviceName);
bool initOutput(const QString& outDevDescr);
void cleanupInput();
void cleanupOutput();
void playMono16SoundCleanup();
void doCapture();
private:
Private* d;
private:
QThread* audioThread;
mutable QMutex audioLock;
ALCdevice* alInDev;
quint32 inSubscriptions;
QTimer captureTimer, playMono16Timer;
ALCdevice* alOutDev;
ALCcontext* alOutContext;
ALuint alMainSource;
ALuint alMainBuffer;
bool outputInitialized;
QList<ALuint> outSources;
};
#endif // AUDIO_H