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

refactor: Override only 'doOutput' and 'captureSamples' in OpenAL2

This commit is contained in:
Diadlo 2018-01-16 02:40:00 +03:00
parent c33592be06
commit e3f3d5e3b1
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
4 changed files with 52 additions and 75 deletions

View File

@ -554,23 +554,17 @@ void OpenAL::stopActive()
}
/**
* @brief Called on the captureTimer events to capture audio
* @brief handles recording of audio frames
*/
void OpenAL::doAudio()
void OpenAL::doInput()
{
QMutexLocker lock(&audioLock);
if (!alInDev || !inSubscriptions) {
return;
}
ALint curSamples = 0;
alcGetIntegerv(alInDev, ALC_CAPTURE_SAMPLES, sizeof(curSamples), &curSamples);
if (static_cast<ALuint>(curSamples) < AUDIO_FRAME_SAMPLE_COUNT) {
if (curSamples < static_cast<ALint>(AUDIO_FRAME_SAMPLE_COUNT)) {
return;
}
alcCaptureSamples(alInDev, inputBuffer, AUDIO_FRAME_SAMPLE_COUNT);
captureSamples(alInDev, inputBuffer, AUDIO_FRAME_SAMPLE_COUNT);
float volume = getVolume();
if (volume >= inputThreshold) {
@ -583,16 +577,44 @@ void OpenAL::doAudio()
return;
}
for (quint32 i = 0; i < AUDIO_FRAME_SAMPLE_COUNT * channels; ++i) {
for (quint32 i = 0; i < AUDIO_FRAME_SAMPLE_COUNT; ++i) {
// gain amplification with clipping to 16-bit boundaries
int ampPCM = qBound<int>(std::numeric_limits<int16_t>::min(),
qRound(inputBuffer[i] * inputGainFactor()),
qRound(inputBuffer[i] * OpenAL::inputGainFactor()),
std::numeric_limits<int16_t>::max());
inputBuffer[i] = static_cast<int16_t>(ampPCM);
}
emit Audio::frameAvailable(inputBuffer, AUDIO_FRAME_SAMPLE_COUNT, channels, AUDIO_SAMPLE_RATE);
emit Audio::frameAvailable(inputBuffer, AUDIO_FRAME_SAMPLE_COUNT, 1, AUDIO_SAMPLE_RATE);
}
void OpenAL::doOutput()
{
// Nothing
}
/**
* @brief Called on the captureTimer events to capture audio
*/
void OpenAL::doAudio()
{
QMutexLocker lock(&audioLock);
// Output section
if (outputInitialized && !peerSources.isEmpty()) {
doOutput();
}
// Input section
if (alInDev && inSubscriptions) {
doInput();
}
}
void OpenAL::captureSamples(ALCdevice* device, int16_t* buffer, ALCsizei samples)
{
alcCaptureSamples(device, buffer, samples);
}
/**

View File

@ -104,14 +104,18 @@ protected:
bool autoInitOutput();
bool initInput(const QString& deviceName, uint32_t channels);
virtual void doAudio();
float getVolume();
void doAudio();
virtual void doInput();
virtual void doOutput();
virtual void captureSamples(ALCdevice* device, int16_t* buffer, ALCsizei samples);
private:
virtual bool initInput(const QString& deviceName);
virtual bool initOutput(const QString& outDevDescr);
void playMono16SoundCleanup();
float getVolume();
protected:
QThread* audioThread;

View File

@ -297,6 +297,11 @@ void OpenAL2::cleanupOutput()
*/
void OpenAL2::doOutput()
{
if (!echoCancelSupported) {
kill_filter_audio(filterer);
filterer = nullptr;
}
alcMakeContextCurrent(alOutContext);
ALuint bufids[PROXY_BUFFER_COUNT];
ALint processed = 0, queued = 0;
@ -330,7 +335,7 @@ void OpenAL2::doOutput()
alSourceQueueBuffers(alProxySource, 1, bufids);
// initialize echo canceler if supported
if (!filterer) {
if (echoCancelSupported && !filterer) {
filterer = new_filter_audio(AUDIO_SAMPLE_RATE);
int16_t filterLatency = latency[1] * 1000 * 2 + AUDIO_FRAME_DURATION;
qDebug() << "Setting filter delay to: " << filterLatency << "ms";
@ -350,63 +355,10 @@ void OpenAL2::doOutput()
alcMakeContextCurrent(alProxyContext);
}
/**
* @brief handles recording of audio frames
*/
void OpenAL2::doInput()
void OpenAL2::captureSamples(ALCdevice* device, int16_t* buffer, ALCsizei samples)
{
ALint curSamples = 0;
alcGetIntegerv(alInDev, ALC_CAPTURE_SAMPLES, sizeof(curSamples), &curSamples);
if (curSamples < static_cast<ALint>(AUDIO_FRAME_SAMPLE_COUNT)) {
return;
}
alcCaptureSamples(alInDev, inputBuffer, AUDIO_FRAME_SAMPLE_COUNT);
alcCaptureSamples(device, buffer, samples);
if (echoCancelSupported && filterer) {
filter_audio(filterer, inputBuffer, AUDIO_FRAME_SAMPLE_COUNT);
}
float volume = getVolume();
if (volume >= inputThreshold) {
isActive = true;
emit startActive(voiceHold);
}
emit Audio::volumeAvailable(volume);
if (!isActive) {
return;
}
// gain amplification with clipping to 16-bit boundaries
for (quint32 i = 0; i < AUDIO_FRAME_SAMPLE_COUNT; ++i) {
int ampPCM = qBound<int>(std::numeric_limits<int16_t>::min(),
qRound(inputBuffer[i] * OpenAL::inputGainFactor()),
std::numeric_limits<int16_t>::max());
inputBuffer[i] = static_cast<int16_t>(ampPCM);
}
emit Audio::frameAvailable(inputBuffer, AUDIO_FRAME_SAMPLE_COUNT, 1, AUDIO_SAMPLE_RATE);
}
/**
* @brief Called on the captureTimer events to capture audio
*/
void OpenAL2::doAudio()
{
QMutexLocker lock(&audioLock);
// output section
if (echoCancelSupported && outputInitialized && !peerSources.isEmpty()) {
doOutput();
} else {
kill_filter_audio(filterer);
filterer = nullptr;
}
// input section
if (alInDev && inSubscriptions) {
doInput();
filter_audio(filterer, buffer, samples);
}
}

View File

@ -60,11 +60,10 @@ protected:
bool initOutput(const QString& outDevDescr) override;
void cleanupOutput() override;
void playMono16SoundCleanup();
void doAudio() override;
void doInput();
void doOutput();
void doOutput() override;
bool loadOpenALExtensions(ALCdevice* dev);
bool initOutputEchoCancel();
void captureSamples(ALCdevice* device, int16_t* buffer, ALCsizei samples) override;
private:
ALCdevice* alProxyDev;