From f6011b65d7604ef303ee94f4ba2884e4c9ebfb54 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Fri, 6 Jun 2014 11:26:04 -0700 Subject: [PATCH] fix input loudness for a stereo source --- assignment-client/src/audio/AudioMixer.cpp | 4 +--- .../src/audio/AudioMixerClientData.cpp | 3 ++- interface/src/Audio.cpp | 17 +++++++++++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index f8cfb3140c..61dee6c82b 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -307,9 +307,7 @@ void AudioMixer::addBufferToMixForListeningNodeWithBuffer(PositionalAudioRingBuf } } } else { - // stereo buffer - do attenuation but no sample delay for spatialization - qDebug() << "Adding a stereo buffer"; - + // stereo buffer - do attenuation but no sample delay for spatialization for (int s = 0; s < NETWORK_BUFFER_LENGTH_SAMPLES_STEREO; s += 4) { // use MMX to clamp four additions at a time diff --git a/assignment-client/src/audio/AudioMixerClientData.cpp b/assignment-client/src/audio/AudioMixerClientData.cpp index e21fadbd16..7fb2a7dcab 100644 --- a/assignment-client/src/audio/AudioMixerClientData.cpp +++ b/assignment-client/src/audio/AudioMixerClientData.cpp @@ -118,7 +118,8 @@ void AudioMixerClientData::pushBuffersAfterFrameSend() { PositionalAudioRingBuffer* audioBuffer = _ringBuffers[i]; if (audioBuffer->willBeAddedToMix()) { - audioBuffer->shiftReadPosition(NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL); + audioBuffer->shiftReadPosition(audioBuffer->isStereo() + ? NETWORK_BUFFER_LENGTH_SAMPLES_STEREO : NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL); audioBuffer->setWillBeAddedToMix(false); } else if (audioBuffer->getType() == PositionalAudioRingBuffer::Injector diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index 39392a5361..b012daa017 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -306,7 +306,7 @@ void linearResampling(int16_t* sourceSamples, int16_t* destinationSamples, } else { // this is a 48 to 24 resampling but both source and destination are two channels // squish two samples into one in each channel - for (int i = 0; i < numSourceSamples; i += 2) { + for (int i = 0; i < numSourceSamples; i += 4) { destinationSamples[i / 2] = (sourceSamples[i] / 2) + (sourceSamples[i + 2] / 2); destinationSamples[(i / 2) + 1] = (sourceSamples[i + 1] / 2) + (sourceSamples[i + 3] / 2); } @@ -585,6 +585,14 @@ void Audio::handleAudioInput() { _lastInputLoudness = 0; } } + } else { + float loudness = 0.0f; + + for (int i = 0; i < NETWORK_BUFFER_LENGTH_SAMPLES_STEREO; i++) { + loudness += fabsf(networkAudioSamples[i]); + } + + _lastInputLoudness = fabs(loudness / NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL); } } else { // our input loudness is 0, since we're muted @@ -626,11 +634,12 @@ void Audio::handleAudioInput() { packetType = PacketTypeSilentAudioFrame; // we need to indicate how many silent samples this is to the audio mixer - audioDataPacket[0] = NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL; + audioDataPacket[0] = _isStereoInput + ? NETWORK_BUFFER_LENGTH_SAMPLES_STEREO + : NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL; numAudioBytes = sizeof(int16_t); - } else { - numAudioBytes = NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL; + numAudioBytes = _isStereoInput ? NETWORK_BUFFER_LENGTH_BYTES_STEREO : NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL; if (Menu::getInstance()->isOptionChecked(MenuOption::EchoServerAudio)) { packetType = PacketTypeMicrophoneAudioWithEcho;