From e3f3d5e3b1293a6086d86d0cabb91e986b82b73c Mon Sep 17 00:00:00 2001 From: Diadlo Date: Tue, 16 Jan 2018 02:40:00 +0300 Subject: [PATCH] refactor: Override only 'doOutput' and 'captureSamples' in OpenAL2 --- src/audio/backend/openal.cpp | 48 ++++++++++++++++++------- src/audio/backend/openal.h | 8 +++-- src/audio/backend/openal2.cpp | 66 +++++------------------------------ src/audio/backend/openal2.h | 5 ++- 4 files changed, 52 insertions(+), 75 deletions(-) diff --git a/src/audio/backend/openal.cpp b/src/audio/backend/openal.cpp index 1151ac62d..0b6e1d272 100644 --- a/src/audio/backend/openal.cpp +++ b/src/audio/backend/openal.cpp @@ -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(curSamples) < AUDIO_FRAME_SAMPLE_COUNT) { + if (curSamples < static_cast(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(std::numeric_limits::min(), - qRound(inputBuffer[i] * inputGainFactor()), + qRound(inputBuffer[i] * OpenAL::inputGainFactor()), std::numeric_limits::max()); inputBuffer[i] = static_cast(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); } /** diff --git a/src/audio/backend/openal.h b/src/audio/backend/openal.h index f9d603de6..a6a787b53 100644 --- a/src/audio/backend/openal.h +++ b/src/audio/backend/openal.h @@ -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; diff --git a/src/audio/backend/openal2.cpp b/src/audio/backend/openal2.cpp index 578915259..2d87a4004 100644 --- a/src/audio/backend/openal2.cpp +++ b/src/audio/backend/openal2.cpp @@ -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(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(std::numeric_limits::min(), - qRound(inputBuffer[i] * OpenAL::inputGainFactor()), - std::numeric_limits::max()); - - inputBuffer[i] = static_cast(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); } } diff --git a/src/audio/backend/openal2.h b/src/audio/backend/openal2.h index 71b936d1e..7ebe1db7f 100644 --- a/src/audio/backend/openal2.h +++ b/src/audio/backend/openal2.h @@ -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;