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

603 lines
15 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
*/
// 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
#include "audio.h"
#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"
#include <QDebug>
2015-08-05 20:45:02 +08:00
#include <QTimer>
#include <QThread>
2015-06-26 19:00:16 +08:00
#include <QMutexLocker>
2015-10-24 08:37:27 +08:00
#include <QFile>
#include <cassert>
Audio* Audio::instance{nullptr};
2015-10-18 21:51:38 +08:00
/**
Returns the singleton's instance. Will construct on first call.
*/
Audio& Audio::getInstance()
{
if (!instance)
{
instance = new Audio();
instance->startAudioThread();
}
return *instance;
}
Audio::Audio()
: audioThread(new QThread())
, audioInLock(QMutex::Recursive)
, audioOutLock(QMutex::Recursive)
, inputSubscriptions(0)
, alOutDev(nullptr)
, alInDev(nullptr)
, outputVolume(1.0)
, inputVolume(1.0)
, alMainSource(0)
, alContext(nullptr)
{
audioThread->setObjectName("qTox Audio");
connect(audioThread, &QThread::finished, audioThread, &QThread::deleteLater);
}
2015-01-24 22:31:11 +08:00
Audio::~Audio()
{
audioThread->exit();
audioThread->wait();
if (audioThread->isRunning())
audioThread->terminate();
}
2015-04-24 19:01:50 +08:00
2015-10-20 09:41:18 +08:00
/**
Start the audio thread for capture and playback.
*/
void Audio::startAudioThread()
{
if (!audioThread->isRunning())
audioThread->start();
else
qWarning("Audio thread already started -> ignored.");
moveToThread(audioThread);
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-10-20 09:46:44 +08:00
qreal Audio::getOutputVolume()
{
QMutexLocker locker(&audioOutLock);
return outputVolume;
}
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)
{
QMutexLocker locker(&audioOutLock);
outputVolume = volume;
alSourcef(alMainSource, AL_GAIN, outputVolume);
2015-06-26 19:00:16 +08:00
for (const ToxGroupCall& call : CoreAV::groupCalls)
{
2015-10-05 08:36:50 +08:00
alSourcef(call.alSource, AL_GAIN, outputVolume);
}
2015-10-05 08:36:50 +08:00
for (const ToxFriendCall& call : CoreAV::calls)
{
alSourcef(call.alSource, AL_GAIN, outputVolume);
}
}
2015-10-18 21:51:38 +08:00
/**
The volume must be between 0 and 2
*/
2015-10-20 09:46:44 +08:00
void Audio::setInputVolume(qreal volume)
{
QMutexLocker locker(&audioInLock);
inputVolume = volume;
}
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-10-20 09:46:44 +08:00
void Audio::subscribeInput()
{
QMutexLocker locker(&audioInLock);
if (!alInDev)
initInput(Settings::getInstance().getInDev());
inputSubscriptions++;
qDebug() << "Subscribed to audio input device [" << inputSubscriptions << " subscriptions ]";
}
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-10-20 09:46:44 +08:00
void Audio::unsubscribeInput()
{
qDebug() << "unsubscribing input" << inputSubscriptions;
QMutexLocker locker(&audioInLock);
if (inputSubscriptions > 0)
inputSubscriptions--;
2015-08-05 23:42:10 +08:00
if (!inputSubscriptions)
cleanupInput();
}
2015-10-18 21:51:38 +08:00
/**
Open an input device, use before suscribing
*/
2015-10-20 09:46:44 +08:00
void Audio::openInput(const QString& inDevDescr)
{
QMutexLocker lock(&audioInLock);
initInput(inDevDescr);
}
2015-10-21 05:12:55 +08:00
void Audio::initInput(const QString& inDevDescr)
{
qDebug() << "Opening audio input" << inDevDescr;
2015-04-24 19:01:50 +08:00
if (inDevDescr == "none")
return;
assert(!alInDev);
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;
if (inDevDescr.isEmpty())
{
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);
}
}
else
2015-10-21 05:12:55 +08:00
alInDev = alcCaptureOpenDevice(inDevDescr.toStdString().c_str(),
sampleRate, stereoFlag, bufSize);
if (alInDev)
qDebug() << "Opened audio input" << inDevDescr;
2015-10-21 05:12:55 +08:00
else
qWarning() << "Cannot open input audio device" << inDevDescr;
Core* core = Core::getInstance();
if (core)
core->getAv()->resetCallSources(); // Force to regen each group call's sources
// Restart the capture if necessary
2015-10-18 17:14:26 +08:00
if (alInDev)
{
alcCaptureStart(alInDev);
}
else
{
#if (FIX_SND_PCM_PREPARE_BUG)
2015-10-21 05:12:55 +08:00
alcCaptureStart(alInDev);
#endif
}
}
2015-10-18 21:51:38 +08:00
/**
Open an output device
*/
2015-10-20 09:46:44 +08:00
bool Audio::openOutput(const QString &outDevDescr)
{
QMutexLocker lock(&audioOutLock);
return initOutput(outDevDescr);
}
2015-10-21 05:14:07 +08:00
bool Audio::initOutput(const QString& outDevDescr)
{
qDebug() << "Opening audio output" << outDevDescr;
if (outDevDescr == "none")
return true;
2015-04-24 19:01:50 +08:00
assert(!alOutDev);
if (outDevDescr.isEmpty())
{
// 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);
}
}
else
alOutDev = alcOpenDevice(outDevDescr.toStdString().c_str());
2015-04-24 19:01:50 +08:00
if (alOutDev)
{
alContext = alcCreateContext(alOutDev, nullptr);
if (alcMakeContextCurrent(alContext))
{
alGenSources(1, &alMainSource);
}
else
2015-04-24 19:01:50 +08:00
{
qWarning() << "Cannot create output audio context";
alcCloseDevice(alOutDev);
2015-10-21 05:14:07 +08:00
return false;
2015-04-24 19:01:50 +08:00
}
2015-10-21 05:14:07 +08:00
}
else
{
qWarning() << "Cannot open output audio device" << outDevDescr;
return false;
}
Core* core = Core::getInstance();
if (core)
core->getAv()->resetCallSources(); // Force to regen each group call's sources
return alOutDev;
}
2015-10-18 21:51:38 +08:00
/**
Close an input device, please don't use unless everyone's unsuscribed
*/
2015-10-20 09:46:44 +08:00
void Audio::closeInput()
{
qDebug() << "Closing input";
QMutexLocker locker(&audioInLock);
cleanupInput();
}
2015-10-18 21:51:38 +08:00
/**
Close an output device
*/
2015-10-20 09:46:44 +08:00
void Audio::closeOutput()
{
qDebug() << "Closing output";
QMutexLocker locker(&audioOutLock);
cleanupOutput();
}
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)
{
QMutexLocker lock(&audioOutLock);
2015-01-05 00:55:39 +08:00
if (!alOutDev)
{
if (!initOutput(Settings::getInstance().getOutDev()))
return;
}
2015-01-05 00:55:39 +08:00
ALuint buffer;
alGenBuffers(1, &buffer);
alBufferData(buffer, AL_FORMAT_MONO16, data.data(), data.size(), 44100);
alSourcef(alMainSource, AL_GAIN, outputVolume);
alSourcei(alMainSource, AL_BUFFER, buffer);
alSourcePlay(alMainSource);
2015-08-05 20:45:02 +08:00
ALint sizeInBytes;
ALint channels;
ALint bits;
alGetBufferi(buffer, AL_SIZE, &sizeInBytes);
alGetBufferi(buffer, AL_CHANNELS, &channels);
alGetBufferi(buffer, AL_BITS, &bits);
int lengthInSamples = sizeInBytes * 8 / (channels * bits);
ALint frequency;
alGetBufferi(buffer, AL_FREQUENCY, &frequency);
alDeleteBuffers(1, &buffer);
}
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
*/
void Audio::playMono16Sound(const char *path)
{
QFile sndFile(path);
sndFile.open(QIODevice::ReadOnly);
playMono16Sound(sndFile.readAll());
}
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,
unsigned samples, uint8_t channels, unsigned sample_rate, void* core)
{
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));
emit static_cast<Core*>(core)->groupPeerAudioPlaying(group, peer);
}
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,
unsigned samples, uint8_t channels, unsigned sample_rate)
{
assert(QThread::currentThread() == audioThread);
QMutexLocker lock(&audioOutLock);
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];
2015-10-05 08:36:50 +08:00
if (call.inactive || call.muteVol)
return;
2015-10-05 08:36:50 +08:00
if (!call.alSource)
{
2015-10-05 08:36:50 +08:00
alGenSources(1, &call.alSource);
alSourcef(call.alSource, AL_GAIN, outputVolume);
}
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
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);
}
void Audio::playAudioBuffer(ALuint alSource, const int16_t *data, int samples, unsigned channels, int sampleRate)
{
assert(channels == 1 || channels == 2);
2015-09-28 03:34:14 +08:00
QMutexLocker lock(&getInstance().audioOutLock);
2015-01-05 00:55:39 +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);
if (processed)
{
ALuint bufids[processed];
alSourceUnqueueBuffers(alSource, processed, bufids);
alDeleteBuffers(processed - 1, bufids + 1);
bufid = bufids[0];
}
else if (queued < 16)
{
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-09-28 03:34:14 +08:00
alSourcef(alSource, AL_GAIN, getInstance().outputVolume);
if (state != AL_PLAYING)
alSourcePlay(alSource);
}
2015-01-05 00:55:39 +08:00
void Audio::cleanupInput()
{
if (alInDev)
{
#if (!FIX_SND_PCM_PREPARE_BUG)
qDebug() << "stopping capture";
alcCaptureStop(alInDev);
#endif
if (alcCaptureCloseDevice(alInDev))
{
alInDev = nullptr;
inputSubscriptions = 0;
}
else
{
qWarning() << "Failed to close input";
}
}
}
void Audio::cleanupOutput()
{
if (inputSubscriptions)
cleanupInput();
if (alOutDev) {
alSourcei(alMainSource, AL_LOOPING, AL_FALSE);
alSourceStop(alMainSource);
alDeleteSources(1, &alMainSource);
ALCdevice* device = alcGetContextsDevice(alContext);
if (!alcMakeContextCurrent(nullptr))
qWarning("Failed to clear current audio context.");
alcDestroyContext(alContext);
alContext = nullptr;
if (!alcCloseDevice(device))
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()
{
QMutexLocker locker(&audioInLock);
return alInDev && inputSubscriptions;
2015-01-05 00:55:39 +08:00
}
bool Audio::isInputSubscribed()
{
// No lock, inputSubscriptions is atomic!
return inputSubscriptions;
}
2015-10-18 21:51:38 +08:00
/**
Returns true if the output device is open
*/
bool Audio::isOutputReady()
{
QMutexLocker locker(&audioOutLock);
2015-10-19 03:20:39 +08:00
return alOutDev;
2015-01-05 00:55:39 +08:00
}
2015-10-05 08:36:50 +08:00
void Audio::createSource(ALuint* source)
{
alGenSources(1, source);
alSourcef(*source, AL_GAIN, getInstance().outputVolume);
}
void Audio::deleteSource(ALuint* source)
{
if (alIsSource(*source))
alDeleteSources(1, source);
else
qWarning() << "Trying to delete invalid audio source"<<*source;
}
void Audio::startLoop()
{
alSourcei(alMainSource, AL_LOOPING, AL_TRUE);
}
void Audio::stopLoop()
{
alSourcei(alMainSource, AL_LOOPING, AL_FALSE);
alSourceStop(alMainSource);
}
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)
{
QMutexLocker lock(&audioInLock);
2015-01-05 00:55:39 +08:00
2015-09-28 05:16:40 +08:00
ALint curSamples=0;
alcGetIntegerv(Audio::alInDev, ALC_CAPTURE_SAMPLES, sizeof(curSamples), &curSamples);
if (curSamples < samples)
2015-01-05 00:55:39 +08:00
return false;
2015-09-28 05:16:40 +08:00
alcCaptureSamples(Audio::alInDev, buf, samples);
2015-10-30 05:18:52 +08:00
for (size_t i = 0; i < samples * AUDIO_CHANNELS; ++i)
{
2015-10-30 05:18:52 +08:00
int sample = buf[i] * pow(inputVolume, 2);
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-30 05:18:52 +08:00
buf[i] = sample;
}
2015-01-05 00:55:39 +08:00
return true;
}
#ifdef QTOX_FILTER_AUDIO
#include "audiofilterer.h"
2015-02-19 01:17:06 +08:00
/* include for compatibility with older versions of OpenAL */
#ifndef ALC_ALL_DEVICES_SPECIFIER
#include <AL/alext.h>
2015-02-19 01:17:06 +08:00
#endif
void Audio::getEchoesToFilter(AudioFilterer* filterer, int framesize)
{
#ifdef ALC_LOOPBACK_CAPTURE_SAMPLES
ALint samples;
2015-12-11 05:01:49 +08:00
alcGetIntegerv(Audio::getInstance().alOutDev, ALC_LOOPBACK_CAPTURE_SAMPLES, sizeof(samples), &samples);
if (samples >= framesize)
{
int16_t buf[framesize];
2015-12-11 05:01:49 +08:00
alcCaptureSamplesLoopback(Audio::getInstance().alOutDev, buf, framesize);
filterer->passAudioOutput(buf, framesize);
filterer->setEchoDelayMs(5); // This 5ms is configurable I believe
}
#else
Q_UNUSED(filterer);
Q_UNUSED(framesize);
#endif
}
#endif