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
|
|
|
*/
|
|
|
|
|
|
|
|
|
2015-01-05 03:24:11 +08:00
|
|
|
// Output some extra debug info
|
|
|
|
#define AUDIO_DEBUG 1
|
|
|
|
|
|
|
|
// Fix a 7 years old openal-soft/alsa bug
|
|
|
|
// http://blog.gmane.org/gmane.comp.lib.openal.devel/month=20080501
|
|
|
|
// If set to 1, the capture will be started as long as the device is open
|
|
|
|
#define FIX_SND_PCM_PREPARE_BUG 0
|
|
|
|
|
2014-11-17 02:04:45 +08:00
|
|
|
#include "audio.h"
|
2015-04-24 08:32:09 +08:00
|
|
|
#include "src/core/core.h"
|
2015-08-05 20:45:02 +08:00
|
|
|
#include "src/persistence/settings.h"
|
2015-06-26 19:00:16 +08:00
|
|
|
#include "src/core/coreav.h"
|
2014-11-17 02:04:45 +08:00
|
|
|
|
|
|
|
#include <QDebug>
|
2015-10-24 08:37:27 +08:00
|
|
|
#include <QFile>
|
2015-11-21 19:40:24 +08:00
|
|
|
#include <QMutexLocker>
|
2015-11-17 07:14:50 +08:00
|
|
|
#include <QPointer>
|
2015-11-21 19:40:24 +08:00
|
|
|
#include <QThread>
|
2014-11-20 05:26:04 +08:00
|
|
|
|
|
|
|
#include <cassert>
|
2014-11-17 02:04:45 +08:00
|
|
|
|
2015-12-11 07:16:21 +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
|
|
|
|
|
2015-12-08 11:52:19 +08:00
|
|
|
#ifndef ALC_ALL_DEVICES_SPECIFIER
|
|
|
|
// compatibility with older versions of OpenAL
|
|
|
|
#include <AL/alext.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef QTOX_FILTER_AUDIO
|
|
|
|
#include "audiofilterer.h"
|
|
|
|
#endif
|
|
|
|
|
2015-11-17 07:14:50 +08:00
|
|
|
Audio* Audio::instance{nullptr};
|
|
|
|
|
2015-12-11 07:16:21 +08:00
|
|
|
class AudioPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AudioPrivate()
|
|
|
|
: alInDev(nullptr)
|
|
|
|
, alOutDev(nullptr)
|
|
|
|
, alContext(nullptr)
|
|
|
|
, inputVolume(1.f)
|
|
|
|
, outputVolume(1.f)
|
2015-11-21 19:40:24 +08:00
|
|
|
, audioThread(new QThread())
|
|
|
|
, inputInitialized(false)
|
|
|
|
, outputInitialized(false)
|
2015-12-11 07:16:21 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
audioThread->setObjectName("qTox Audio");
|
|
|
|
QObject::connect(audioThread, &QThread::finished, audioThread, &QThread::deleteLater);
|
2015-12-11 07:16:21 +08:00
|
|
|
}
|
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
void initInput(const QString& inDevDescr);
|
|
|
|
bool initOutput(const QString& outDevDescr);
|
|
|
|
void cleanupInput();
|
|
|
|
void cleanupOutput();
|
|
|
|
|
2015-12-11 07:16:21 +08:00
|
|
|
public:
|
|
|
|
ALCdevice* alInDev;
|
|
|
|
ALCdevice* alOutDev;
|
|
|
|
ALCcontext* alContext;
|
|
|
|
|
|
|
|
ALuint alMainSource;
|
2015-11-21 19:40:24 +08:00
|
|
|
|
2015-12-11 07:16:21 +08:00
|
|
|
qreal inputVolume;
|
|
|
|
qreal outputVolume;
|
2015-11-21 19:40:24 +08:00
|
|
|
|
|
|
|
QThread* audioThread;
|
|
|
|
QMutex audioLock;
|
|
|
|
Audio::PtrList inputSubscriptions;
|
|
|
|
Audio::PtrList outputSubscriptions;
|
|
|
|
bool inputInitialized;
|
|
|
|
bool outputInitialized;
|
2015-11-17 07:14:50 +08:00
|
|
|
|
|
|
|
QPointer<AudioMeter> mAudioMeter;
|
2015-11-21 19:40:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
@class AudioPlayer
|
|
|
|
|
|
|
|
@brief Non-blocking audio player.
|
|
|
|
|
|
|
|
The audio data is played from start to finish (no streaming).
|
|
|
|
*/
|
|
|
|
class AudioPlayer : public QThread
|
|
|
|
{
|
|
|
|
public:
|
2015-11-15 20:11:00 +08:00
|
|
|
AudioPlayer(ALuint source, const QByteArray& data)
|
|
|
|
: mSource(source)
|
2015-11-21 19:40:24 +08:00
|
|
|
{
|
2015-11-15 20:11:00 +08:00
|
|
|
alGenBuffers(1, &mBuffer);
|
|
|
|
alBufferData(mBuffer, AL_FORMAT_MONO16, data.constData(), data.size(), 44100);
|
|
|
|
alSourcei(mSource, AL_BUFFER, mBuffer);
|
2015-11-21 19:40:24 +08:00
|
|
|
|
2015-11-15 20:11:00 +08:00
|
|
|
connect(this, &AudioPlayer::finished, this, &AudioPlayer::deleteLater);
|
2015-11-21 19:40:24 +08:00
|
|
|
}
|
|
|
|
|
2015-11-15 20:11:00 +08:00
|
|
|
private:
|
|
|
|
void run() override final
|
2015-11-21 19:40:24 +08:00
|
|
|
{
|
2015-11-15 20:11:00 +08:00
|
|
|
alSourceRewind(mSource);
|
|
|
|
alSourcePlay(mSource);
|
2015-11-21 19:40:24 +08:00
|
|
|
|
|
|
|
QMutexLocker locker(&playLock);
|
|
|
|
ALint state = AL_PLAYING;
|
|
|
|
while (state == AL_PLAYING) {
|
2015-11-15 20:11:00 +08:00
|
|
|
alGetSourcei(mSource, AL_SOURCE_STATE, &state);
|
2015-11-21 19:40:24 +08:00
|
|
|
waitPlaying.wait(&playLock, 2000);
|
|
|
|
}
|
2015-11-15 20:11:00 +08:00
|
|
|
|
|
|
|
alSourceStop(mSource);
|
|
|
|
alDeleteBuffers(1, &mBuffer);
|
2015-11-21 19:40:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
QMutex playLock;
|
|
|
|
QWaitCondition waitPlaying;
|
|
|
|
|
|
|
|
private:
|
2015-11-15 20:11:00 +08:00
|
|
|
ALuint mBuffer;
|
|
|
|
ALuint mSource;
|
2015-12-11 07:16:21 +08:00
|
|
|
};
|
|
|
|
|
2015-11-17 07:14:50 +08:00
|
|
|
class AudioMeter : public QThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AudioMeter()
|
|
|
|
{
|
|
|
|
connect(this, &AudioMeter::finished, this, &AudioMeter::deleteLater);
|
|
|
|
}
|
|
|
|
|
2015-11-30 06:37:42 +08:00
|
|
|
inline void stop()
|
2015-11-17 07:14:50 +08:00
|
|
|
{
|
2015-11-30 06:37:42 +08:00
|
|
|
requestInterruption();
|
2015-11-17 07:14:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void run() override final
|
|
|
|
{
|
|
|
|
static const int framesize = AUDIO_FRAME_SAMPLE_COUNT * AUDIO_CHANNELS;
|
|
|
|
|
|
|
|
Audio& audio = Audio::getInstance();
|
|
|
|
|
|
|
|
mNewMaxGain = 0.f;
|
|
|
|
|
2015-11-30 06:45:52 +08:00
|
|
|
QMutexLocker locker(&mMeterLock);
|
2015-11-30 06:37:42 +08:00
|
|
|
while (!isInterruptionRequested()) {
|
2015-11-30 06:45:52 +08:00
|
|
|
mListenerReady.wait(locker.mutex());
|
|
|
|
locker.relock();
|
|
|
|
|
2015-11-17 07:14:50 +08:00
|
|
|
int16_t buff[framesize] = {0};
|
|
|
|
if (audio.tryCaptureSamples(buff, AUDIO_FRAME_SAMPLE_COUNT)) {
|
|
|
|
mNewMaxGain = 0.f;
|
|
|
|
for (int i = 0; i < framesize; ++i) {
|
|
|
|
mNewMaxGain = qMax(mNewMaxGain, qAbs(buff[i]) / 32767.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 06:45:52 +08:00
|
|
|
mGainMeasured.wakeAll();
|
2015-11-17 07:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
QMutex mMeterLock;
|
2015-11-30 06:45:52 +08:00
|
|
|
QWaitCondition mListenerReady;
|
|
|
|
QWaitCondition mGainMeasured;
|
2015-11-17 07:14:50 +08:00
|
|
|
qreal mNewMaxGain;
|
|
|
|
};
|
|
|
|
|
|
|
|
AudioMeterListener::AudioMeterListener(AudioMeter* measureThread)
|
|
|
|
: mActive(false)
|
|
|
|
, mAudioMeter(measureThread)
|
|
|
|
{
|
|
|
|
assert(mAudioMeter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioMeterListener::start()
|
|
|
|
{
|
2015-11-30 06:45:52 +08:00
|
|
|
if (!mAudioMeter->isRunning()) {
|
|
|
|
mAudioMeter->start();
|
|
|
|
// TODO: ensure that audiometer is running
|
|
|
|
// -> Start listeners from AudioMeter::started signal
|
|
|
|
while (!mAudioMeter->isRunning())
|
|
|
|
QThread::msleep(10);
|
|
|
|
}
|
|
|
|
|
2015-11-17 07:14:50 +08:00
|
|
|
QThread* listener = new QThread;
|
|
|
|
connect(listener, &QThread::started, this, &AudioMeterListener::doListen);
|
|
|
|
connect(listener, &QThread::finished, listener, &QThread::deleteLater);
|
|
|
|
moveToThread(listener);
|
|
|
|
|
|
|
|
listener->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioMeterListener::stop()
|
|
|
|
{
|
|
|
|
mActive = false;
|
|
|
|
}
|
|
|
|
|
2015-11-30 06:45:52 +08:00
|
|
|
void AudioMeterListener::processed()
|
|
|
|
{
|
|
|
|
mGainProcessed.wakeAll();
|
|
|
|
}
|
|
|
|
|
2015-11-17 07:14:50 +08:00
|
|
|
void AudioMeterListener::doListen()
|
|
|
|
{
|
|
|
|
mMaxGain = 0.f;
|
|
|
|
mActive = true;
|
|
|
|
|
|
|
|
QMutexLocker locker(&mAudioMeter->mMeterLock);
|
|
|
|
while (mActive) {
|
2015-11-30 06:45:52 +08:00
|
|
|
mAudioMeter->mListenerReady.wakeAll();
|
|
|
|
mAudioMeter->mGainMeasured.wait(&mAudioMeter->mMeterLock);
|
2015-11-17 07:14:50 +08:00
|
|
|
locker.relock();
|
|
|
|
|
2015-11-30 06:45:52 +08:00
|
|
|
if (mAudioMeter->mNewMaxGain > mMaxGain) {
|
|
|
|
mMaxGain = mAudioMeter->mNewMaxGain;
|
|
|
|
emit gainChanged(mMaxGain);
|
|
|
|
} else if (mMaxGain > 0.02f) {
|
|
|
|
mMaxGain -= 0.008f;
|
|
|
|
emit gainChanged(mMaxGain);
|
|
|
|
}
|
|
|
|
|
|
|
|
mGainProcessed.wait(locker.mutex(), 10);
|
|
|
|
locker.relock();
|
2015-11-17 07:14:50 +08:00
|
|
|
}
|
|
|
|
|
2015-11-30 06:37:42 +08:00
|
|
|
mAudioMeter->requestInterruption();
|
2015-11-17 07:14:50 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Returns the singleton's instance. Will construct on first call.
|
|
|
|
*/
|
2014-11-20 05:26:04 +08:00
|
|
|
Audio& Audio::getInstance()
|
|
|
|
{
|
|
|
|
if (!instance)
|
|
|
|
{
|
|
|
|
instance = new Audio();
|
2015-10-18 21:23:33 +08:00
|
|
|
instance->startAudioThread();
|
2014-11-20 05:26:04 +08:00
|
|
|
}
|
|
|
|
return *instance;
|
|
|
|
}
|
|
|
|
|
2015-11-17 07:14:50 +08:00
|
|
|
AudioMeterListener* Audio::createAudioMeterListener() const
|
|
|
|
{
|
|
|
|
if (!d->mAudioMeter)
|
|
|
|
d->mAudioMeter = new AudioMeter;
|
|
|
|
|
|
|
|
return new AudioMeterListener(d->mAudioMeter);
|
|
|
|
}
|
|
|
|
|
2015-10-18 21:23:33 +08:00
|
|
|
Audio::Audio()
|
2015-12-11 07:16:21 +08:00
|
|
|
: d(new AudioPrivate)
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-24 22:31:11 +08:00
|
|
|
Audio::~Audio()
|
|
|
|
{
|
2015-11-30 06:37:42 +08:00
|
|
|
if (d->mAudioMeter)
|
|
|
|
d->mAudioMeter->stop();
|
2015-11-21 19:40:24 +08:00
|
|
|
d->audioThread->exit();
|
|
|
|
d->audioThread->wait();
|
|
|
|
d->cleanupInput();
|
|
|
|
d->cleanupOutput();
|
2015-10-18 21:23:33 +08:00
|
|
|
}
|
2015-04-24 19:01:50 +08:00
|
|
|
|
2015-10-20 09:41:18 +08:00
|
|
|
/**
|
|
|
|
Start the audio thread for capture and playback.
|
|
|
|
*/
|
2015-10-18 21:23:33 +08:00
|
|
|
void Audio::startAudioThread()
|
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
moveToThread(d->audioThread);
|
2015-11-15 04:50:32 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
if (!d->audioThread->isRunning())
|
|
|
|
d->audioThread->start();
|
2015-10-20 09:42:02 +08:00
|
|
|
else
|
|
|
|
qWarning("Audio thread already started -> ignored.");
|
2015-10-18 21:23:33 +08:00
|
|
|
|
2015-01-24 22:31:11 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Returns the current output volume, between 0 and 1
|
|
|
|
*/
|
2015-12-11 07:16:21 +08:00
|
|
|
qreal Audio::outputVolume()
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
2015-12-11 07:16:21 +08:00
|
|
|
return d->outputVolume;
|
2015-05-12 07:27:32 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
The volume must be between 0 and 1
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
void Audio::setOutputVolume(qreal volume)
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
2015-11-14 17:08:58 +08:00
|
|
|
|
2015-12-11 07:16:21 +08:00
|
|
|
d->outputVolume = volume;
|
|
|
|
alSourcef(d->alMainSource, AL_GAIN, volume);
|
2015-05-12 07:27:32 +08:00
|
|
|
|
2015-06-26 19:00:16 +08:00
|
|
|
for (const ToxGroupCall& call : CoreAV::groupCalls)
|
2015-05-12 07:27:32 +08:00
|
|
|
{
|
2015-12-11 07:16:21 +08:00
|
|
|
alSourcef(call.alSource, AL_GAIN, volume);
|
2015-05-12 07:27:32 +08:00
|
|
|
}
|
|
|
|
|
2015-10-05 08:36:50 +08:00
|
|
|
for (const ToxFriendCall& call : CoreAV::calls)
|
2015-05-12 07:27:32 +08:00
|
|
|
{
|
2015-12-11 07:16:21 +08:00
|
|
|
alSourcef(call.alSource, AL_GAIN, volume);
|
2015-05-12 07:27:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 07:16:21 +08:00
|
|
|
qreal Audio::inputVolume()
|
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
2015-12-11 07:16:21 +08:00
|
|
|
|
|
|
|
return d->inputVolume;
|
|
|
|
}
|
|
|
|
|
2015-10-20 09:46:44 +08:00
|
|
|
void Audio::setInputVolume(qreal volume)
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
2015-12-11 07:16:21 +08:00
|
|
|
d->inputVolume = volume;
|
2015-10-18 16:15:02 +08:00
|
|
|
}
|
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
void Audio::reinitInput(const QString& inDevDesc)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&d->audioLock);
|
|
|
|
d->cleanupInput();
|
|
|
|
d->initInput(inDevDesc);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Audio::reinitOutput(const QString& outDevDesc)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&d->audioLock);
|
|
|
|
d->cleanupOutput();
|
|
|
|
return d->initOutput(outDevDesc);
|
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
2015-10-20 09:41:18 +08:00
|
|
|
@brief Subscribe to capture sound from the opened input device.
|
|
|
|
|
|
|
|
If the input device is not open, it will be opened before capturing.
|
2015-10-18 21:51:38 +08:00
|
|
|
*/
|
2015-11-15 04:02:29 +08:00
|
|
|
void Audio::subscribeInput(const void* inListener)
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
2015-11-14 17:08:58 +08:00
|
|
|
|
2015-12-11 07:16:21 +08:00
|
|
|
if (!d->alInDev)
|
2015-11-21 19:40:24 +08:00
|
|
|
d->initInput(Settings::getInstance().getInDev());
|
2015-11-08 13:00:05 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
if (!d->inputSubscriptions.contains(inListener)) {
|
|
|
|
d->inputSubscriptions << inListener;
|
|
|
|
qDebug() << "Subscribed to audio input device [" << d->inputSubscriptions.size() << "subscriptions ]";
|
2015-11-15 04:02:29 +08:00
|
|
|
}
|
2015-10-18 21:23:33 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
2015-10-20 09:41:18 +08:00
|
|
|
@brief Unsubscribe from capturing from an opened input device.
|
|
|
|
|
|
|
|
If the input device has no more subscriptions, it will be closed.
|
2015-10-18 21:51:38 +08:00
|
|
|
*/
|
2015-11-15 04:02:29 +08:00
|
|
|
void Audio::unsubscribeInput(const void* inListener)
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
2015-11-09 00:19:14 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
if (inListener && d->inputSubscriptions.size())
|
2015-11-09 04:08:55 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
d->inputSubscriptions.removeOne(inListener);
|
|
|
|
qDebug() << "Unsubscribed from audio input device [" << d->inputSubscriptions.size() << "subscriptions left ]";
|
2015-11-09 04:08:55 +08:00
|
|
|
}
|
2015-08-05 23:42:10 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
if (d->inputSubscriptions.isEmpty())
|
|
|
|
d->cleanupInput();
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
2015-11-15 04:02:29 +08:00
|
|
|
void Audio::subscribeOutput(const void* outListener)
|
2015-11-09 04:05:34 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
2015-11-09 04:05:34 +08:00
|
|
|
|
2015-12-11 07:16:21 +08:00
|
|
|
if (!d->alOutDev)
|
2015-11-21 19:40:24 +08:00
|
|
|
d->initOutput(Settings::getInstance().getOutDev());
|
2015-11-09 04:05:34 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
if (!d->outputSubscriptions.contains(outListener)) {
|
|
|
|
d->outputSubscriptions << outListener;
|
|
|
|
qDebug() << "Subscribed to audio output device [" << d->outputSubscriptions.size() << "subscriptions ]";
|
2015-11-15 04:02:29 +08:00
|
|
|
}
|
2015-11-09 04:05:34 +08:00
|
|
|
}
|
|
|
|
|
2015-11-15 04:02:29 +08:00
|
|
|
void Audio::unsubscribeOutput(const void* outListener)
|
2015-11-09 04:05:34 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
2015-11-14 17:08:58 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
if (outListener && d->outputSubscriptions.size())
|
2015-11-09 04:05:34 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
d->outputSubscriptions.removeOne(outListener);
|
|
|
|
qDebug() << "Unsubscribed from audio output device [" << d->outputSubscriptions.size() << " subscriptions left ]";
|
2015-11-09 04:05:34 +08:00
|
|
|
}
|
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
if (d->outputSubscriptions.isEmpty())
|
|
|
|
d->cleanupOutput();
|
2015-11-09 04:05:34 +08:00
|
|
|
}
|
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
void AudioPrivate::initInput(const QString& inDevDescr)
|
2015-11-09 04:03:33 +08:00
|
|
|
{
|
|
|
|
qDebug() << "Opening audio input" << inDevDescr;
|
2015-04-24 19:01:50 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
inputInitialized = false;
|
2015-11-08 01:23:10 +08:00
|
|
|
if (inDevDescr == "none")
|
|
|
|
return;
|
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
assert(!alInDev);
|
2015-11-09 04:03:33 +08:00
|
|
|
|
2015-06-26 19:00:16 +08:00
|
|
|
/// TODO: Try to actually detect if our audio source is stereo
|
2015-09-28 05:16:40 +08:00
|
|
|
int stereoFlag = AUDIO_CHANNELS == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
|
|
|
|
const uint32_t sampleRate = AUDIO_SAMPLE_RATE;
|
|
|
|
const uint16_t frameDuration = AUDIO_FRAME_DURATION;
|
|
|
|
const uint32_t chnls = AUDIO_CHANNELS;
|
2015-10-21 05:12:55 +08:00
|
|
|
const ALCsizei bufSize = (frameDuration * sampleRate * 4) / 1000 * chnls;
|
2014-11-17 02:04:45 +08:00
|
|
|
if (inDevDescr.isEmpty())
|
2015-12-14 18:09:34 +08:00
|
|
|
{
|
|
|
|
const ALchar *pDeviceList = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
|
|
|
|
if (pDeviceList)
|
|
|
|
{
|
|
|
|
alInDev = alcCaptureOpenDevice(pDeviceList, sampleRate, stereoFlag, bufSize);
|
|
|
|
int len = strlen(pDeviceList);
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
QString inDev = QString::fromUtf8(pDeviceList, len);
|
|
|
|
#else
|
|
|
|
QString inDev = QString::fromLocal8Bit(pDeviceList, len);
|
|
|
|
#endif
|
|
|
|
Settings::getInstance().setInDev(inDev);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
alInDev = alcCaptureOpenDevice(nullptr, sampleRate, stereoFlag, bufSize);
|
|
|
|
}
|
|
|
|
}
|
2014-11-17 02:04:45 +08:00
|
|
|
else
|
2015-11-21 19:40:24 +08:00
|
|
|
alInDev = alcCaptureOpenDevice(inDevDescr.toStdString().c_str(),
|
2015-10-21 05:12:55 +08:00
|
|
|
sampleRate, stereoFlag, bufSize);
|
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
if (alInDev)
|
2015-11-09 04:03:33 +08:00
|
|
|
qDebug() << "Opened audio input" << inDevDescr;
|
2015-10-21 05:12:55 +08:00
|
|
|
else
|
2015-11-09 04:03:33 +08:00
|
|
|
qWarning() << "Cannot open input audio device" << inDevDescr;
|
2014-11-17 02:04:45 +08:00
|
|
|
|
2015-05-20 01:40:01 +08:00
|
|
|
Core* core = Core::getInstance();
|
|
|
|
if (core)
|
2015-09-28 01:59:26 +08:00
|
|
|
core->getAv()->resetCallSources(); // Force to regen each group call's sources
|
2014-11-17 02:04:45 +08:00
|
|
|
|
|
|
|
// Restart the capture if necessary
|
2015-11-21 19:40:24 +08:00
|
|
|
if (alInDev)
|
2015-01-05 03:24:11 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
alcCaptureStart(alInDev);
|
2015-01-05 03:24:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if (FIX_SND_PCM_PREPARE_BUG)
|
2015-10-21 05:12:55 +08:00
|
|
|
alcCaptureStart(alInDev);
|
2015-01-05 03:24:11 +08:00
|
|
|
#endif
|
|
|
|
}
|
2015-11-13 06:08:17 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
inputInitialized = true;
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
2015-11-11 08:30:41 +08:00
|
|
|
@internal
|
2015-10-21 05:14:07 +08:00
|
|
|
|
2015-11-11 08:30:41 +08:00
|
|
|
Open an audio output device
|
|
|
|
*/
|
2015-11-21 19:40:24 +08:00
|
|
|
bool AudioPrivate::initOutput(const QString& outDevDescr)
|
2015-11-09 04:03:33 +08:00
|
|
|
{
|
|
|
|
qDebug() << "Opening audio output" << outDevDescr;
|
2015-11-09 00:19:14 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
outputInitialized = false;
|
2015-11-09 04:03:33 +08:00
|
|
|
if (outDevDescr == "none")
|
|
|
|
return true;
|
2015-04-24 19:01:50 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
assert(!alOutDev);
|
2015-11-09 04:03:33 +08:00
|
|
|
|
|
|
|
if (outDevDescr.isEmpty())
|
2015-12-07 14:08:50 +08:00
|
|
|
{
|
|
|
|
// Attempt to default to the first available audio device.
|
|
|
|
const ALchar *pDeviceList;
|
|
|
|
if (alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
|
|
|
|
pDeviceList = alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
|
|
|
|
else
|
|
|
|
pDeviceList = alcGetString(NULL, ALC_DEVICE_SPECIFIER);
|
|
|
|
if (pDeviceList)
|
|
|
|
{
|
|
|
|
alOutDev = alcOpenDevice(pDeviceList);
|
|
|
|
int len = strlen(pDeviceList);
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
QString outDev = QString::fromUtf8(pDeviceList, len);
|
|
|
|
#else
|
|
|
|
QString outDev = QString::fromLocal8Bit(pDeviceList, len);
|
|
|
|
#endif
|
|
|
|
Settings::getInstance().setOutDev(outDev);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
alOutDev = alcOpenDevice(nullptr);
|
|
|
|
}
|
|
|
|
}
|
2015-11-09 04:03:33 +08:00
|
|
|
else
|
2015-11-21 19:40:24 +08:00
|
|
|
alOutDev = alcOpenDevice(outDevDescr.toStdString().c_str());
|
2015-04-24 19:01:50 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
if (alOutDev)
|
2015-11-09 04:03:33 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
alContext = alcCreateContext(alOutDev, nullptr);
|
|
|
|
if (alcMakeContextCurrent(alContext))
|
2014-11-17 02:04:45 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
alGenSources(1, &alMainSource);
|
|
|
|
alSourcef(alMainSource, AL_GAIN, outputVolume);
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
2015-04-24 19:01:50 +08:00
|
|
|
{
|
2015-11-09 04:03:33 +08:00
|
|
|
qWarning() << "Cannot create output audio context";
|
2015-11-09 05:23:36 +08:00
|
|
|
cleanupOutput();
|
2015-10-21 05:14:07 +08:00
|
|
|
return false;
|
2015-04-24 19:01:50 +08:00
|
|
|
}
|
2014-11-17 02:04:45 +08:00
|
|
|
|
2015-11-13 06:08:17 +08:00
|
|
|
Core* core = Core::getInstance();
|
|
|
|
if (core)
|
|
|
|
core->getAv()->resetCallSources(); // Force to regen each group call's sources
|
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
outputInitialized = true;
|
2015-11-13 06:08:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-05 22:36:57 +08:00
|
|
|
|
2015-11-13 06:08:17 +08:00
|
|
|
qWarning() << "Cannot open output audio device" << outDevDescr;
|
|
|
|
return false;
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Play a 44100Hz mono 16bit PCM sound
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
void Audio::playMono16Sound(const QByteArray& data)
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
2015-08-05 22:36:57 +08:00
|
|
|
|
2015-12-11 07:16:21 +08:00
|
|
|
if (!d->alOutDev)
|
2015-11-21 19:40:24 +08:00
|
|
|
d->initOutput(Settings::getInstance().getOutDev());
|
2015-01-05 00:55:39 +08:00
|
|
|
|
2015-12-11 07:16:21 +08:00
|
|
|
alSourcef(d->alMainSource, AL_GAIN, d->outputVolume);
|
2015-11-15 04:02:29 +08:00
|
|
|
|
2015-11-15 20:11:00 +08:00
|
|
|
AudioPlayer *player = new AudioPlayer(d->alMainSource, data);
|
|
|
|
connect(player, &AudioPlayer::finished, [=]() {
|
|
|
|
QMutexLocker locker(&d->audioLock);
|
|
|
|
|
|
|
|
if (d->outputSubscriptions.isEmpty())
|
|
|
|
d->cleanupOutput();
|
|
|
|
else
|
|
|
|
qDebug("Audio output not closed -> there are pending subscriptions.");
|
|
|
|
});
|
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
player->start();
|
2014-11-17 02:04:45 +08:00
|
|
|
}
|
2015-10-18 15:50:55 +08:00
|
|
|
|
2015-10-24 08:37:27 +08:00
|
|
|
/**
|
|
|
|
Play a 44100Hz mono 16bit PCM sound from a file
|
|
|
|
*/
|
2015-11-15 09:38:16 +08:00
|
|
|
void Audio::playMono16Sound(const QString& path)
|
2015-10-24 08:37:27 +08:00
|
|
|
{
|
|
|
|
QFile sndFile(path);
|
|
|
|
sndFile.open(QIODevice::ReadOnly);
|
2015-11-15 09:38:16 +08:00
|
|
|
playMono16Sound(QByteArray(sndFile.readAll()));
|
2015-10-24 08:37:27 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
@brief May be called from any thread, will always queue a call to playGroupAudio.
|
|
|
|
|
|
|
|
The first and last argument are ignored, but allow direct compatibility with toxcore.
|
|
|
|
*/
|
2015-06-26 19:00:16 +08:00
|
|
|
void Audio::playGroupAudioQueued(void*,int group, int peer, const int16_t* data,
|
2015-03-13 14:00:12 +08:00
|
|
|
unsigned samples, uint8_t channels, unsigned sample_rate, void* core)
|
2014-11-20 05:26:04 +08:00
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(instance, "playGroupAudio", Qt::BlockingQueuedConnection,
|
|
|
|
Q_ARG(int,group), Q_ARG(int,peer), Q_ARG(const int16_t*,data),
|
|
|
|
Q_ARG(unsigned,samples), Q_ARG(uint8_t,channels), Q_ARG(unsigned,sample_rate));
|
2015-03-13 14:00:12 +08:00
|
|
|
emit static_cast<Core*>(core)->groupPeerAudioPlaying(group, peer);
|
2014-11-20 05:26:04 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Must be called from the audio thread, plays a group call's received audio
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
void Audio::playGroupAudio(int group, int peer, const int16_t* data,
|
2015-10-18 21:23:33 +08:00
|
|
|
unsigned samples, uint8_t channels, unsigned sample_rate)
|
2014-11-20 05:26:04 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
assert(QThread::currentThread() == d->audioThread);
|
|
|
|
QMutexLocker locker(&d->audioLock);
|
2015-01-05 00:55:39 +08:00
|
|
|
|
2015-10-05 08:36:50 +08:00
|
|
|
if (!CoreAV::groupCalls.contains(group))
|
|
|
|
return;
|
|
|
|
|
2015-06-26 19:00:16 +08:00
|
|
|
ToxGroupCall& call = CoreAV::groupCalls[group];
|
2014-11-20 05:26:04 +08:00
|
|
|
|
2015-10-05 08:36:50 +08:00
|
|
|
if (call.inactive || call.muteVol)
|
2014-11-20 05:26:04 +08:00
|
|
|
return;
|
|
|
|
|
2015-10-05 08:36:50 +08:00
|
|
|
if (!call.alSource)
|
2015-02-01 23:58:59 +08:00
|
|
|
{
|
2015-10-05 08:36:50 +08:00
|
|
|
alGenSources(1, &call.alSource);
|
2015-12-11 07:16:21 +08:00
|
|
|
alSourcef(call.alSource, AL_GAIN, d->outputVolume);
|
2015-02-01 23:58:59 +08:00
|
|
|
}
|
2014-11-20 05:26:04 +08:00
|
|
|
|
2015-10-15 01:53:46 +08:00
|
|
|
qreal volume = 0.;
|
2015-08-19 01:40:11 +08:00
|
|
|
int bufsize = samples * 2 * channels;
|
|
|
|
for (int i = 0; i < bufsize; ++i)
|
2015-10-21 05:14:07 +08:00
|
|
|
volume += abs(data[i]);
|
2015-08-19 01:40:11 +08:00
|
|
|
|
2015-10-15 01:53:46 +08:00
|
|
|
emit groupAudioPlayed(group, peer, volume / bufsize);
|
2015-08-19 01:40:11 +08:00
|
|
|
|
2015-10-05 08:36:50 +08:00
|
|
|
playAudioBuffer(call.alSource, data, samples, channels, sample_rate);
|
2014-11-20 05:26:04 +08:00
|
|
|
}
|
|
|
|
|
2015-12-11 07:16:21 +08:00
|
|
|
void Audio::playAudioBuffer(quint32 alSource, const int16_t *data, int samples, unsigned channels, int sampleRate)
|
2014-11-20 05:26:04 +08:00
|
|
|
{
|
|
|
|
assert(channels == 1 || channels == 2);
|
2015-11-21 19:40:24 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
2015-01-05 00:55:39 +08:00
|
|
|
|
2014-11-20 05:26:04 +08:00
|
|
|
ALuint bufid;
|
|
|
|
ALint processed = 0, queued = 16;
|
|
|
|
alGetSourcei(alSource, AL_BUFFERS_PROCESSED, &processed);
|
|
|
|
alGetSourcei(alSource, AL_BUFFERS_QUEUED, &queued);
|
|
|
|
alSourcei(alSource, AL_LOOPING, AL_FALSE);
|
|
|
|
|
2014-12-12 02:05:52 +08:00
|
|
|
if (processed)
|
2014-11-20 05:26:04 +08:00
|
|
|
{
|
|
|
|
ALuint bufids[processed];
|
|
|
|
alSourceUnqueueBuffers(alSource, processed, bufids);
|
|
|
|
alDeleteBuffers(processed - 1, bufids + 1);
|
|
|
|
bufid = bufids[0];
|
|
|
|
}
|
2014-12-12 02:05:52 +08:00
|
|
|
else if (queued < 16)
|
2014-11-20 05:26:04 +08:00
|
|
|
{
|
|
|
|
alGenBuffers(1, &bufid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
alBufferData(bufid, (channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, data,
|
|
|
|
samples * 2 * channels, sampleRate);
|
|
|
|
alSourceQueueBuffers(alSource, 1, &bufid);
|
|
|
|
|
|
|
|
ALint state;
|
|
|
|
alGetSourcei(alSource, AL_SOURCE_STATE, &state);
|
2015-12-11 07:16:21 +08:00
|
|
|
alSourcef(alSource, AL_GAIN, d->outputVolume);
|
2014-12-12 02:05:52 +08:00
|
|
|
if (state != AL_PLAYING)
|
2014-11-20 05:26:04 +08:00
|
|
|
alSourcePlay(alSource);
|
|
|
|
}
|
2015-01-05 00:55:39 +08:00
|
|
|
|
2015-11-11 08:30:41 +08:00
|
|
|
/**
|
|
|
|
@internal
|
|
|
|
|
|
|
|
Close active audio input device.
|
|
|
|
*/
|
2015-11-21 19:40:24 +08:00
|
|
|
void AudioPrivate::cleanupInput()
|
2015-11-09 00:19:14 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
inputInitialized = false;
|
2015-11-13 06:08:17 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
if (alInDev)
|
2015-11-09 00:19:14 +08:00
|
|
|
{
|
|
|
|
#if (!FIX_SND_PCM_PREPARE_BUG)
|
2015-11-09 04:08:55 +08:00
|
|
|
qDebug() << "stopping audio capture";
|
2015-11-21 19:40:24 +08:00
|
|
|
alcCaptureStop(alInDev);
|
2015-11-09 00:19:14 +08:00
|
|
|
#endif
|
|
|
|
|
2015-11-09 04:08:55 +08:00
|
|
|
qDebug() << "Closing audio input";
|
2015-11-21 19:40:24 +08:00
|
|
|
if (alcCaptureCloseDevice(alInDev) == ALC_TRUE)
|
|
|
|
alInDev = nullptr;
|
2015-11-09 00:19:14 +08:00
|
|
|
else
|
|
|
|
qWarning() << "Failed to close input";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-11 08:30:41 +08:00
|
|
|
/**
|
|
|
|
@internal
|
|
|
|
|
|
|
|
Close active audio output device
|
|
|
|
*/
|
2015-11-21 19:40:24 +08:00
|
|
|
void AudioPrivate::cleanupOutput()
|
2015-11-09 00:19:14 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
outputInitialized = false;
|
2015-11-13 06:08:17 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
if (alOutDev) {
|
2015-11-09 04:08:55 +08:00
|
|
|
qDebug() << "Closing audio output";
|
2015-11-21 19:40:24 +08:00
|
|
|
alSourcei(alMainSource, AL_LOOPING, AL_FALSE);
|
|
|
|
alSourceStop(alMainSource);
|
|
|
|
alDeleteSources(1, &alMainSource);
|
2015-11-09 00:19:14 +08:00
|
|
|
|
|
|
|
if (!alcMakeContextCurrent(nullptr))
|
|
|
|
qWarning("Failed to clear current audio context.");
|
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
alcDestroyContext(alContext);
|
|
|
|
alContext = nullptr;
|
2015-11-09 00:19:14 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
if (alcCloseDevice(alOutDev))
|
|
|
|
alOutDev = nullptr;
|
2015-11-09 04:08:55 +08:00
|
|
|
else
|
2015-11-09 00:19:14 +08:00
|
|
|
qWarning("Failed to close output.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Returns true if the input device is open and suscribed to
|
|
|
|
*/
|
2015-10-20 09:46:44 +08:00
|
|
|
bool Audio::isInputReady()
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
|
|
|
return d->alInDev && d->inputInitialized;
|
2015-01-05 00:55:39 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Returns true if the output device is open
|
|
|
|
*/
|
2015-10-24 08:59:12 +08:00
|
|
|
bool Audio::isOutputReady()
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
|
|
|
return d->alOutDev && d->outputInitialized;
|
2015-12-11 07:16:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* Audio::outDeviceNames()
|
|
|
|
{
|
|
|
|
const char* pDeviceList;
|
|
|
|
if (alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
|
|
|
|
pDeviceList = alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
|
|
|
|
else
|
|
|
|
pDeviceList = alcGetString(NULL, ALC_DEVICE_SPECIFIER);
|
|
|
|
|
|
|
|
return pDeviceList;
|
2015-01-05 00:55:39 +08:00
|
|
|
}
|
|
|
|
|
2015-12-11 07:16:21 +08:00
|
|
|
const char* Audio::inDeviceNames()
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2015-12-11 07:16:21 +08:00
|
|
|
return alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
2015-12-11 07:16:21 +08:00
|
|
|
void Audio::createSource(quint32* sid)
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2015-12-11 07:16:21 +08:00
|
|
|
alGenSources(1, sid);
|
|
|
|
alSourcef(*sid, AL_GAIN, 1.f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::deleteSource(quint32 sid)
|
|
|
|
{
|
|
|
|
if (alIsSource(sid)) {
|
|
|
|
alDeleteSources(1, &sid);
|
|
|
|
} else {
|
|
|
|
qWarning() << "Trying to delete invalid audio source" << sid;
|
|
|
|
}
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
2015-10-24 08:26:32 +08:00
|
|
|
void Audio::startLoop()
|
|
|
|
{
|
2015-11-21 19:06:22 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
2015-12-11 07:16:21 +08:00
|
|
|
alSourcei(d->alMainSource, AL_LOOPING, AL_TRUE);
|
2015-10-24 08:26:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::stopLoop()
|
|
|
|
{
|
2015-11-21 19:06:22 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
2015-12-11 07:16:21 +08:00
|
|
|
alSourcei(d->alMainSource, AL_LOOPING, AL_FALSE);
|
|
|
|
alSourceStop(d->alMainSource);
|
2015-10-24 08:26:32 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 21:51:38 +08:00
|
|
|
/**
|
|
|
|
Does nothing and return false on failure
|
|
|
|
*/
|
2015-09-28 05:16:40 +08:00
|
|
|
bool Audio::tryCaptureSamples(int16_t* buf, int samples)
|
2015-10-18 21:23:33 +08:00
|
|
|
{
|
2015-11-21 19:40:24 +08:00
|
|
|
QMutexLocker lock(&d->audioLock);
|
2015-01-05 00:55:39 +08:00
|
|
|
|
2015-11-21 19:40:24 +08:00
|
|
|
if (!(d->alInDev && d->inputInitialized))
|
2015-11-13 06:08:17 +08:00
|
|
|
return false;
|
|
|
|
|
2015-12-08 11:52:19 +08:00
|
|
|
ALint curSamples = 0;
|
2015-12-11 07:16:21 +08:00
|
|
|
alcGetIntegerv(d->alInDev, ALC_CAPTURE_SAMPLES, sizeof(curSamples), &curSamples);
|
2015-09-28 05:16:40 +08:00
|
|
|
if (curSamples < samples)
|
2015-01-05 00:55:39 +08:00
|
|
|
return false;
|
|
|
|
|
2015-12-11 07:16:21 +08:00
|
|
|
alcCaptureSamples(d->alInDev, buf, samples);
|
2015-10-18 16:15:02 +08:00
|
|
|
|
2015-10-30 05:18:52 +08:00
|
|
|
for (size_t i = 0; i < samples * AUDIO_CHANNELS; ++i)
|
2015-10-18 16:15:02 +08:00
|
|
|
{
|
2015-12-11 07:16:21 +08:00
|
|
|
int sample = buf[i] * pow(d->inputVolume, 2);
|
2015-10-18 16:15:02 +08:00
|
|
|
|
2015-10-30 05:18:52 +08:00
|
|
|
if (sample < std::numeric_limits<int16_t>::min())
|
|
|
|
sample = std::numeric_limits<int16_t>::min();
|
|
|
|
else if (sample > std::numeric_limits<int16_t>::max())
|
|
|
|
sample = std::numeric_limits<int16_t>::max();
|
2015-10-18 16:15:02 +08:00
|
|
|
|
2015-10-30 05:18:52 +08:00
|
|
|
buf[i] = sample;
|
2015-10-18 16:15:02 +08:00
|
|
|
}
|
|
|
|
|
2015-01-05 00:55:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-02-18 14:25:25 +08:00
|
|
|
|
2015-12-08 11:52:19 +08:00
|
|
|
#if defined(QTOX_FILTER_AUDIO) && defined(ALC_LOOPBACK_CAPTURE_SAMPLES)
|
|
|
|
void Audio::getEchoesToFilter(AudioFilterer* filterer, int samples)
|
2015-02-18 14:25:25 +08:00
|
|
|
{
|
2015-12-08 11:52:19 +08:00
|
|
|
QMutexLocker locker(&d->audioLock);
|
|
|
|
|
2015-02-18 14:25:25 +08:00
|
|
|
ALint samples;
|
2015-12-08 11:52:19 +08:00
|
|
|
alcGetIntegerv(&d->alOutDev, ALC_LOOPBACK_CAPTURE_SAMPLES, sizeof(samples), &samples);
|
|
|
|
if (samples >= samples)
|
2015-02-18 14:25:25 +08:00
|
|
|
{
|
2015-12-08 11:52:19 +08:00
|
|
|
int16_t buf[samples];
|
|
|
|
alcCaptureSamplesLoopback(&d->alOutDev, buf, samples);
|
|
|
|
filterer->passAudioOutput(buf, samples);
|
2015-02-18 14:25:25 +08:00
|
|
|
filterer->setEchoDelayMs(5); // This 5ms is configurable I believe
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|