From 47c6ba708fcfaf4dae9d943078dfeaa2c7f49b55 Mon Sep 17 00:00:00 2001 From: Craig Hansen-Sturm Date: Sat, 20 Sep 2014 04:45:58 -0700 Subject: [PATCH 1/7] new audio edit buffer object / click removal / apply linear fade to sound clip endpoints --- libraries/audio/src/AudioEditBuffer.h | 108 ++++++++++++++++++++++++++ libraries/audio/src/Sound.cpp | 25 +++++- libraries/audio/src/Sound.h | 1 + 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 libraries/audio/src/AudioEditBuffer.h diff --git a/libraries/audio/src/AudioEditBuffer.h b/libraries/audio/src/AudioEditBuffer.h new file mode 100644 index 0000000000..1fa2794e79 --- /dev/null +++ b/libraries/audio/src/AudioEditBuffer.h @@ -0,0 +1,108 @@ +// +// AudioEditBuffer.h +// hifi +// +// Created by Craig Hansen-Sturm on 8/29/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_AudioEditBuffer_h +#define hifi_AudioEditBuffer_h + +template< typename T > +class AudioEditBuffer + : public AudioFrameBuffer { + +public: + + AudioEditBuffer() : + AudioFrameBuffer() { + } + + AudioEditBuffer(const uint32_t channelCount, const uint32_t frameCount) : + AudioFrameBuffer(channelCount, frameCount) { + } + + ~AudioEditBuffer() { + } + + bool getZeroCrossing(uint32_t start, bool direction, float32_t epsilon, uint32_t& zero); + + void linearFade(uint32_t start, uint32_t stop, bool slope); + void exponentialFade(uint32_t start, uint32_t stop, bool slope); +}; + +template< typename T > +bool AudioEditBuffer::getZeroCrossing(uint32_t start, bool direction, float32_t epsilon, uint32_t& zero) { + + zero = this->_frameCount; + + if (direction) { // scan from the left + if (start < this->_frameCount) { + for (uint32_t i = start; i < this->_frameCount; ++i) { + for (uint32_t j = 0; j < this->_channelCount; ++j) { + if (this->_frameBuffer[j][i] >= -epsilon && this->_frameBuffer[j][i] <= epsilon) { + zero = i; + return true; + } + } + } + } + } + else { // scan from the right + if (start != 0 && start < this->_frameCount) { + for (uint32_t i = start; i != 0; --i) { + for (uint32_t j = 0; j < this->_channelCount; ++j) { + if (this->_frameBuffer[j][i] >= -epsilon && this->_frameBuffer[j][i] <= epsilon) { + zero = i; + return true; + } + } + } + } + } + + return false; +} + +template< typename T > +void AudioEditBuffer::linearFade(uint32_t start, uint32_t stop, bool slope) { + + if (start >= stop || start > this->_frameCount || stop > this->_frameCount ) { + return; + } + + uint32_t count = stop - start; + float32_t delta; + float32_t gain; + + if (slope) { // 0.0 to 1.0f in delta increments + delta = 1.0f / (float32_t)count; + gain = 0.0f; + } + else { // 1.0f to 0.0f in delta increments + delta = -1.0f / (float32_t)count; + gain = 1.0f; + } + + for (uint32_t i = start; i < stop; ++i) { + for (uint32_t j = 0; j < this->_channelCount; ++j) { + this->_frameBuffer[j][i] *= gain; + gain += delta; + } + } +} + +template< typename T > +void AudioEditBuffer::exponentialFade(uint32_t start, uint32_t stop, bool slope) { + // TBD +} + +typedef AudioEditBuffer< float32_t > AudioEditBufferFloat32; +typedef AudioEditBuffer< int32_t > AudioEditBufferSInt32; + +#endif // hifi_AudioEditBuffer_h + diff --git a/libraries/audio/src/Sound.cpp b/libraries/audio/src/Sound.cpp index f52f5c04dd..9edb04aa2c 100644 --- a/libraries/audio/src/Sound.cpp +++ b/libraries/audio/src/Sound.cpp @@ -24,6 +24,9 @@ #include #include "AudioRingBuffer.h" +#include "AudioFormat.h" +#include "AudioBuffer.h" +#include "AudioEditBuffer.h" #include "Sound.h" // procedural audio version of Sound @@ -120,6 +123,7 @@ void Sound::replyFinished() { // Process as RAW file downSample(rawAudioByteArray); } + trimFrames(); } else { qDebug() << "Network reply without 'Content-Type'."; } @@ -133,7 +137,6 @@ void Sound::replyError(QNetworkReply::NetworkError code) { } void Sound::downSample(const QByteArray& rawAudioByteArray) { - // assume that this was a RAW file and is now an array of samples that are // signed, 16-bit, 48Khz, mono @@ -155,6 +158,26 @@ void Sound::downSample(const QByteArray& rawAudioByteArray) { } } +void Sound::trimFrames() { + + const uint32_t inputFrameCount = _byteArray.size() / sizeof(int16_t); + const uint32_t trimCount = 1024; // number of leading and trailing frames to trim + + if (inputFrameCount <= (2 * trimCount)) { + return; + } + + int16_t* inputFrameData = (int16_t*)_byteArray.data(); + + AudioEditBufferFloat32 editBuffer(1, inputFrameCount); + editBuffer.copyFrames(1, inputFrameCount, inputFrameData, false /*copy in*/); + + editBuffer.linearFade(0, trimCount, true); + editBuffer.linearFade(inputFrameCount - trimCount, inputFrameCount, false); + + editBuffer.copyFrames(1, inputFrameCount, inputFrameData, true /*copy out*/); +} + // // Format description from https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ // diff --git a/libraries/audio/src/Sound.h b/libraries/audio/src/Sound.h index 7dae3679f1..fa2dd97903 100644 --- a/libraries/audio/src/Sound.h +++ b/libraries/audio/src/Sound.h @@ -33,6 +33,7 @@ private: QByteArray _byteArray; bool _hasDownloaded; + void trimFrames(); void downSample(const QByteArray& rawAudioByteArray); void interpretAsWav(const QByteArray& inputAudioByteArray, QByteArray& outputAudioByteArray); From 90379ee7ebfb5c3e06e14404dc0f65f3860f9d03 Mon Sep 17 00:00:00 2001 From: Craig Hansen-Sturm Date: Sat, 20 Sep 2014 04:50:11 -0700 Subject: [PATCH 2/7] frame and channel counts are now consistently uint32_t / float32_t replaces float --- interface/src/Audio.cpp | 2 +- libraries/audio/src/AudioBuffer.h | 46 ++++---- libraries/audio/src/AudioFilter.h | 150 ++++++++++++------------- libraries/audio/src/AudioFilterBank.h | 61 +++++----- libraries/audio/src/AudioGain.h | 8 +- libraries/audio/src/AudioPan.h | 4 +- libraries/audio/src/AudioSourceNoise.h | 4 +- libraries/audio/src/AudioSourceTone.h | 4 +- 8 files changed, 141 insertions(+), 138 deletions(-) diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index 79704eebf1..e9cc6f9271 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -509,7 +509,7 @@ void Audio::handleAudioInput() { if (!_muted && (_audioSourceInjectEnabled || _peqEnabled)) { int16_t* inputFrameData = (int16_t*)inputByteArray.data(); - const int inputFrameCount = inputByteArray.size() / sizeof(int16_t); + const uint32_t inputFrameCount = inputByteArray.size() / sizeof(int16_t); _inputFrameBuffer.copyFrames(1, inputFrameCount, inputFrameData, false /*copy in*/); diff --git a/libraries/audio/src/AudioBuffer.h b/libraries/audio/src/AudioBuffer.h index 863bbdf22a..cf6a2fb55a 100644 --- a/libraries/audio/src/AudioBuffer.h +++ b/libraries/audio/src/AudioBuffer.h @@ -17,17 +17,19 @@ template< typename T > class AudioFrameBuffer { - uint16_t _channelCount; - uint16_t _channelCountMax; - uint16_t _frameCount; - uint16_t _frameCountMax; +protected: + + uint32_t _channelCount; + uint32_t _channelCountMax; + uint32_t _frameCount; + uint32_t _frameCountMax; T** _frameBuffer; void allocateFrames() { _frameBuffer = new T*[_channelCountMax]; if (_frameBuffer) { - for (uint16_t i = 0; i < _channelCountMax; ++i) { + for (uint32_t i = 0; i < _channelCountMax; ++i) { _frameBuffer[i] = new T[_frameCountMax]; } } @@ -35,7 +37,7 @@ class AudioFrameBuffer { void deallocateFrames() { if (_frameBuffer) { - for (uint16_t i = 0; i < _channelCountMax; ++i) { + for (uint32_t i = 0; i < _channelCountMax; ++i) { delete _frameBuffer[i]; } delete _frameBuffer; @@ -52,7 +54,7 @@ public: _frameBuffer(NULL) { } - AudioFrameBuffer(const uint16_t channelCount, const uint16_t frameCount) : + AudioFrameBuffer(const uint32_t channelCount, const uint32_t frameCount) : _channelCount(channelCount), _channelCountMax(channelCount), _frameCount(frameCount), @@ -61,11 +63,11 @@ public: allocateFrames(); } - ~AudioFrameBuffer() { + virtual ~AudioFrameBuffer() { finalize(); } - void initialize(const uint16_t channelCount, const uint16_t frameCount) { + void initialize(const uint32_t channelCount, const uint32_t frameCount) { if (_frameBuffer) { finalize(); } @@ -88,25 +90,25 @@ public: return _frameBuffer; } - uint16_t getChannelCount() { + uint32_t getChannelCount() { return _channelCount; } - uint16_t getFrameCount() { + uint32_t getFrameCount() { return _frameCount; } - + void zeroFrames() { if (!_frameBuffer) { return; } - for (uint16_t i = 0; i < _channelCountMax; ++i) { + for (uint32_t i = 0; i < _channelCountMax; ++i) { memset(_frameBuffer[i], 0, sizeof(T)*_frameCountMax); } } template< typename S > - void copyFrames(uint16_t channelCount, const uint16_t frameCount, S* frames, const bool copyOut = false) { + void copyFrames(uint32_t channelCount, const uint32_t frameCount, S* frames, const bool copyOut = false) { if ( !_frameBuffer || !frames) { return; } @@ -152,8 +154,8 @@ public: S* dst = frames; if(typeid(T) == typeid(S)) { // source and destination types are the same - for (int i = 0; i < _frameCount; ++i) { - for (int j = 0; j < _channelCount; ++j) { + for (uint32_t i = 0; i < _frameCount; ++i) { + for (uint32_t j = 0; j < _channelCount; ++j) { *dst++ = _frameBuffer[j][i]; } } @@ -164,8 +166,8 @@ public: const int scale = (2 << ((8 * sizeof(S)) - 1)); - for (int i = 0; i < _frameCount; ++i) { - for (int j = 0; j < _channelCount; ++j) { + for (uint32_t i = 0; i < _frameCount; ++i) { + for (uint32_t j = 0; j < _channelCount; ++j) { *dst++ = (S)(_frameBuffer[j][i] * scale); } } @@ -179,8 +181,8 @@ public: S* src = frames; if(typeid(T) == typeid(S)) { // source and destination types are the same - for (int i = 0; i < _frameCount; ++i) { - for (int j = 0; j < _channelCount; ++j) { + for (uint32_t i = 0; i < _frameCount; ++i) { + for (uint32_t j = 0; j < _channelCount; ++j) { _frameBuffer[j][i] = *src++; } } @@ -191,8 +193,8 @@ public: const int scale = (2 << ((8 * sizeof(S)) - 1)); - for (int i = 0; i < _frameCount; ++i) { - for (int j = 0; j < _channelCount; ++j) { + for (uint32_t i = 0; i < _frameCount; ++i) { + for (uint32_t j = 0; j < _channelCount; ++j) { _frameBuffer[j][i] = ((T)(*src++)) / scale; } } diff --git a/libraries/audio/src/AudioFilter.h b/libraries/audio/src/AudioFilter.h index c792b5fec4..ee823c00da 100644 --- a/libraries/audio/src/AudioFilter.h +++ b/libraries/audio/src/AudioFilter.h @@ -21,16 +21,16 @@ class AudioBiquad { // // private data // - float _a0; // gain - float _a1; // feedforward 1 - float _a2; // feedforward 2 - float _b1; // feedback 1 - float _b2; // feedback 2 + float32_t _a0; // gain + float32_t _a1; // feedforward 1 + float32_t _a2; // feedforward 2 + float32_t _b1; // feedback 1 + float32_t _b2; // feedback 2 - float _xm1; - float _xm2; - float _ym1; - float _ym2; + float32_t _xm1; + float32_t _xm2; + float32_t _ym1; + float32_t _ym2; public: @@ -51,20 +51,20 @@ public: // // public interface // - void setParameters(const float a0, const float a1, const float a2, const float b1, const float b2) { + void setParameters(const float32_t a0, const float32_t a1, const float32_t a2, const float32_t b1, const float32_t b2) { _a0 = a0; _a1 = a1; _a2 = a2; _b1 = b1; _b2 = b2; } - void getParameters(float& a0, float& a1, float& a2, float& b1, float& b2) { + void getParameters(float32_t& a0, float32_t& a1, float32_t& a2, float32_t& b1, float32_t& b2) { a0 = _a0; a1 = _a1; a2 = _a2; b1 = _b1; b2 = _b2; } - void render(const float* in, float* out, const int frames) { + void render(const float32_t* in, float32_t* out, const uint32_t frames) { - float x; - float y; + float32_t x; + float32_t y; - for (int i = 0; i < frames; ++i) { + for (uint32_t i = 0; i < frames; ++i) { x = *in++; @@ -105,10 +105,10 @@ protected: // data // AudioBiquad _kernel; - float _sampleRate; - float _frequency; - float _gain; - float _slope; + float32_t _sampleRate; + float32_t _frequency; + float32_t _gain; + float32_t _slope; // // helpers @@ -131,7 +131,7 @@ public: // // public interface // - void setParameters(const float sampleRate, const float frequency, const float gain, const float slope) { + void setParameters(const float32_t sampleRate, const float32_t frequency, const float32_t gain, const float32_t slope) { _sampleRate = std::max(sampleRate, 1.0f); _frequency = std::max(frequency, 2.0f); @@ -141,11 +141,11 @@ public: updateKernel(); } - void getParameters(float& sampleRate, float& frequency, float& gain, float& slope) { + void getParameters(float32_t& sampleRate, float32_t& frequency, float32_t& gain, float32_t& slope) { sampleRate = _sampleRate; frequency = _frequency; gain = _gain; slope = _slope; } - void render(const float* in, float* out, const int frames) { + void render(const float32_t* in, float32_t* out, const uint32_t frames) { _kernel.render(in,out,frames); } @@ -166,14 +166,14 @@ public: // void updateKernel() { - const float a = _gain; - const float aAdd1 = a + 1.0f; - const float aSub1 = a - 1.0f; - const float omega = TWO_PI * _frequency / _sampleRate; - const float aAdd1TimesCosOmega = aAdd1 * cosf(omega); - const float aSub1TimesCosOmega = aSub1 * cosf(omega); - const float alpha = 0.5f * sinf(omega) / _slope; - const float zeta = 2.0f * sqrtf(a) * alpha; + const float32_t a = _gain; + const float32_t aAdd1 = a + 1.0f; + const float32_t aSub1 = a - 1.0f; + const float32_t omega = TWO_PI * _frequency / _sampleRate; + const float32_t aAdd1TimesCosOmega = aAdd1 * cosf(omega); + const float32_t aSub1TimesCosOmega = aSub1 * cosf(omega); + const float32_t alpha = 0.5f * sinf(omega) / _slope; + const float32_t zeta = 2.0f * sqrtf(a) * alpha; /* b0 = A*( (A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha ) b1 = 2*A*( (A-1) - (A+1)*cos(w0) ) @@ -182,14 +182,14 @@ public: a1 = -2*( (A-1) + (A+1)*cos(w0) ) a2 = (A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha */ - const float b0 = +1.0f * (aAdd1 - aSub1TimesCosOmega + zeta) * a; - const float b1 = +2.0f * (aSub1 - aAdd1TimesCosOmega + ZERO) * a; - const float b2 = +1.0f * (aAdd1 - aSub1TimesCosOmega - zeta) * a; - const float a0 = +1.0f * (aAdd1 + aSub1TimesCosOmega + zeta); - const float a1 = -2.0f * (aSub1 + aAdd1TimesCosOmega + ZERO); - const float a2 = +1.0f * (aAdd1 + aSub1TimesCosOmega - zeta); + const float32_t b0 = +1.0f * (aAdd1 - aSub1TimesCosOmega + zeta) * a; + const float32_t b1 = +2.0f * (aSub1 - aAdd1TimesCosOmega + ZERO) * a; + const float32_t b2 = +1.0f * (aAdd1 - aSub1TimesCosOmega - zeta) * a; + const float32_t a0 = +1.0f * (aAdd1 + aSub1TimesCosOmega + zeta); + const float32_t a1 = -2.0f * (aSub1 + aAdd1TimesCosOmega + ZERO); + const float32_t a2 = +1.0f * (aAdd1 + aSub1TimesCosOmega - zeta); - const float normA0 = 1.0f / a0; + const float32_t normA0 = 1.0f / a0; _kernel.setParameters(b0 * normA0, b1 * normA0 , b2 * normA0, a1 * normA0, a2 * normA0); } @@ -207,14 +207,14 @@ public: // void updateKernel() { - const float a = _gain; - const float aAdd1 = a + 1.0f; - const float aSub1 = a - 1.0f; - const float omega = TWO_PI * _frequency / _sampleRate; - const float aAdd1TimesCosOmega = aAdd1 * cosf(omega); - const float aSub1TimesCosOmega = aSub1 * cosf(omega); - const float alpha = 0.5f * sinf(omega) / _slope; - const float zeta = 2.0f * sqrtf(a) * alpha; + const float32_t a = _gain; + const float32_t aAdd1 = a + 1.0f; + const float32_t aSub1 = a - 1.0f; + const float32_t omega = TWO_PI * _frequency / _sampleRate; + const float32_t aAdd1TimesCosOmega = aAdd1 * cosf(omega); + const float32_t aSub1TimesCosOmega = aSub1 * cosf(omega); + const float32_t alpha = 0.5f * sinf(omega) / _slope; + const float32_t zeta = 2.0f * sqrtf(a) * alpha; /* b0 = A*( (A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha ) b1 = -2*A*( (A-1) + (A+1)*cos(w0) ) @@ -223,14 +223,14 @@ public: a1 = 2*( (A-1) - (A+1)*cos(w0) ) a2 = (A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha */ - const float b0 = +1.0f * (aAdd1 + aSub1TimesCosOmega + zeta) * a; - const float b1 = -2.0f * (aSub1 + aAdd1TimesCosOmega + ZERO) * a; - const float b2 = +1.0f * (aAdd1 + aSub1TimesCosOmega - zeta) * a; - const float a0 = +1.0f * (aAdd1 - aSub1TimesCosOmega + zeta); - const float a1 = +2.0f * (aSub1 - aAdd1TimesCosOmega + ZERO); - const float a2 = +1.0f * (aAdd1 - aSub1TimesCosOmega - zeta); + const float32_t b0 = +1.0f * (aAdd1 + aSub1TimesCosOmega + zeta) * a; + const float32_t b1 = -2.0f * (aSub1 + aAdd1TimesCosOmega + ZERO) * a; + const float32_t b2 = +1.0f * (aAdd1 + aSub1TimesCosOmega - zeta) * a; + const float32_t a0 = +1.0f * (aAdd1 - aSub1TimesCosOmega + zeta); + const float32_t a1 = +2.0f * (aSub1 - aAdd1TimesCosOmega + ZERO); + const float32_t a2 = +1.0f * (aAdd1 - aSub1TimesCosOmega - zeta); - const float normA0 = 1.0f / a0; + const float32_t normA0 = 1.0f / a0; _kernel.setParameters(b0 * normA0, b1 * normA0 , b2 * normA0, a1 * normA0, a2 * normA0); } @@ -248,9 +248,9 @@ public: // void updateKernel() { - const float omega = TWO_PI * _frequency / _sampleRate; - const float cosOmega = cosf(omega); - const float alpha = 0.5f * sinf(omega) / _slope; + const float32_t omega = TWO_PI * _frequency / _sampleRate; + const float32_t cosOmega = cosf(omega); + const float32_t alpha = 0.5f * sinf(omega) / _slope; /* b0 = 1 - alpha b1 = -2*cos(w0) @@ -259,14 +259,14 @@ public: a1 = -2*cos(w0) a2 = 1 - alpha */ - const float b0 = +1.0f - alpha; - const float b1 = -2.0f * cosOmega; - const float b2 = +1.0f + alpha; - const float a0 = +1.0f + alpha; - const float a1 = -2.0f * cosOmega; - const float a2 = +1.0f - alpha; + const float32_t b0 = +1.0f - alpha; + const float32_t b1 = -2.0f * cosOmega; + const float32_t b2 = +1.0f + alpha; + const float32_t a0 = +1.0f + alpha; + const float32_t a1 = -2.0f * cosOmega; + const float32_t a2 = +1.0f - alpha; - const float normA0 = 1.0f / a0; + const float32_t normA0 = 1.0f / a0; _kernel.setParameters(b0 * normA0, b1 * normA0 , b2 * normA0, a1 * normA0, a2 * normA0); } @@ -284,12 +284,12 @@ public: // void updateKernel() { - const float a = _gain; - const float omega = TWO_PI * _frequency / _sampleRate; - const float cosOmega = cosf(omega); - const float alpha = 0.5f * sinf(omega) / _slope; - const float alphaMulA = alpha * a; - const float alphaDivA = alpha / a; + const float32_t a = _gain; + const float32_t omega = TWO_PI * _frequency / _sampleRate; + const float32_t cosOmega = cosf(omega); + const float32_t alpha = 0.5f * sinf(omega) / _slope; + const float32_t alphaMulA = alpha * a; + const float32_t alphaDivA = alpha / a; /* b0 = 1 + alpha*A b1 = -2*cos(w0) @@ -298,14 +298,14 @@ public: a1 = -2*cos(w0) a2 = 1 - alpha/A */ - const float b0 = +1.0f + alphaMulA; - const float b1 = -2.0f * cosOmega; - const float b2 = +1.0f - alphaMulA; - const float a0 = +1.0f + alphaDivA; - const float a1 = -2.0f * cosOmega; - const float a2 = +1.0f - alphaDivA; + const float32_t b0 = +1.0f + alphaMulA; + const float32_t b1 = -2.0f * cosOmega; + const float32_t b2 = +1.0f - alphaMulA; + const float32_t a0 = +1.0f + alphaDivA; + const float32_t a1 = -2.0f * cosOmega; + const float32_t a2 = +1.0f - alphaDivA; - const float normA0 = 1.0f / a0; + const float32_t normA0 = 1.0f / a0; _kernel.setParameters(b0 * normA0, b1 * normA0 , b2 * normA0, a1 * normA0, a2 * normA0); } diff --git a/libraries/audio/src/AudioFilterBank.h b/libraries/audio/src/AudioFilterBank.h index b9546999d9..41b2985cbe 100644 --- a/libraries/audio/src/AudioFilterBank.h +++ b/libraries/audio/src/AudioFilterBank.h @@ -15,24 +15,24 @@ // // Helper/convenience class that implements a bank of Filter objects // -template< typename T, const int N, const int C > +template< typename T, const uint32_t N, const uint32_t C > class AudioFilterBank { // // types // struct FilterParameter { - float _p1; - float _p2; - float _p3; + float32_t _p1; + float32_t _p2; + float32_t _p3; }; // // private static data // - static const int _filterCount = N; - static const int _channelCount = C; - static const int _profileCount = 4; + static const uint32_t _filterCount = N; + static const uint32_t _channelCount = C; + static const uint32_t _profileCount = 4; static FilterParameter _profiles[ _profileCount ][ _filterCount ]; @@ -40,9 +40,9 @@ class AudioFilterBank { // private data // T _filters[ _filterCount ][ _channelCount ]; - float* _buffer[ _channelCount ]; - float _sampleRate; - uint16_t _frameCount; + float32_t* _buffer[ _channelCount ]; + float32_t _sampleRate; + uint32_t _frameCount; public: @@ -64,11 +64,11 @@ public: // // public interface // - void initialize(const float sampleRate, const int frameCount = 0) { + void initialize(const float32_t sampleRate, const uint32_t frameCount = 0) { finalize(); - for (int i = 0; i < _channelCount; ++i) { - _buffer[i] = (float*)malloc(frameCount * sizeof(float)); + for (uint32_t i = 0; i < _channelCount; ++i) { + _buffer[i] = (float32_t*)malloc(frameCount * sizeof(float32_t)); } _sampleRate = sampleRate; @@ -79,7 +79,7 @@ public: } void finalize() { - for (int i = 0; i < _channelCount; ++i) { + for (uint32_t i = 0; i < _channelCount; ++i) { if (_buffer[i]) { free (_buffer[i]); _buffer[i] = NULL; @@ -90,52 +90,53 @@ public: void loadProfile(int profileIndex) { if (profileIndex >= 0 && profileIndex < _profileCount) { - for (int i = 0; i < _filterCount; ++i) { + for (uint32_t i = 0; i < _filterCount; ++i) { FilterParameter p = _profiles[profileIndex][i]; - for (int j = 0; j < _channelCount; ++j) { + for (uint32_t j = 0; j < _channelCount; ++j) { _filters[i][j].setParameters(_sampleRate,p._p1,p._p2,p._p3); } } } } - void setParameters(int filterStage, int filterChannel, const float sampleRate, const float frequency, const float gain, - const float slope) { + void setParameters(uint32_t filterStage, uint32_t filterChannel, const float32_t sampleRate, const float32_t frequency, + const float32_t gain, const float32_t slope) { if (filterStage >= 0 && filterStage < _filterCount && filterChannel >= 0 && filterChannel < _channelCount) { _filters[filterStage][filterChannel].setParameters(sampleRate,frequency,gain,slope); } } - void getParameters(int filterStage, int filterChannel, float& sampleRate, float& frequency, float& gain, float& slope) { + void getParameters(uint32_t filterStage, uint32_t filterChannel, float32_t& sampleRate, float32_t& frequency, + float32_t& gain, float32_t& slope) { if (filterStage >= 0 && filterStage < _filterCount && filterChannel >= 0 && filterChannel < _channelCount) { _filters[filterStage][filterChannel].getParameters(sampleRate,frequency,gain,slope); } } - void render(const int16_t* in, int16_t* out, const int frameCount) { + void render(const int16_t* in, int16_t* out, const uint32_t frameCount) { if (!_buffer || (frameCount > _frameCount)) return; const int scale = (2 << ((8 * sizeof(int16_t)) - 1)); // de-interleave and convert int16_t to float32 (normalized to -1. ... 1.) - for (int i = 0; i < frameCount; ++i) { - for (int j = 0; j < _channelCount; ++j) { + for (uint32_t i = 0; i < frameCount; ++i) { + for (uint32_t j = 0; j < _channelCount; ++j) { _buffer[j][i] = ((float)(*in++)) / scale; } } // now step through each filter - for (int i = 0; i < _channelCount; ++i) { - for (int j = 0; j < _filterCount; ++j) { + for (uint32_t i = 0; i < _channelCount; ++i) { + for (uint32_t j = 0; j < _filterCount; ++j) { _filters[j][i].render( &_buffer[i][0], &_buffer[i][0], frameCount ); } } // convert float32 to int16_t and interleave - for (int i = 0; i < frameCount; ++i) { - for (int j = 0; j < _channelCount; ++j) { + for (uint32_t i = 0; i < frameCount; ++i) { + for (uint32_t j = 0; j < _channelCount; ++j) { *out++ = (int16_t)(_buffer[j][i] * scale); } } @@ -144,16 +145,16 @@ public: void render(AudioBufferFloat32& frameBuffer) { float32_t** samples = frameBuffer.getFrameData(); - for (uint16_t j = 0; j < frameBuffer.getChannelCount(); ++j) { - for (int i = 0; i < _filterCount; ++i) { + for (uint32_t j = 0; j < frameBuffer.getChannelCount(); ++j) { + for (uint32_t i = 0; i < _filterCount; ++i) { _filters[i][j].render( samples[j], samples[j], frameBuffer.getFrameCount() ); } } } void reset() { - for (int i = 0; i < _filterCount; ++i) { - for (int j = 0; j < _channelCount; ++j) { + for (uint32_t i = 0; i < _filterCount; ++i) { + for (uint32_t j = 0; j < _channelCount; ++j) { _filters[i][j].reset(); } } diff --git a/libraries/audio/src/AudioGain.h b/libraries/audio/src/AudioGain.h index ac1923528b..485e239128 100644 --- a/libraries/audio/src/AudioGain.h +++ b/libraries/audio/src/AudioGain.h @@ -61,7 +61,7 @@ public: if (frameBuffer.getChannelCount() == 1) { - for (uint16_t i = 0; i < frameBuffer.getFrameCount(); i += 16) { + for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 16) { samples[0][i + 0] *= _gain; samples[0][i + 1] *= _gain; samples[0][i + 2] *= _gain; @@ -82,7 +82,7 @@ public: } else if (frameBuffer.getChannelCount() == 2) { - for (uint16_t i = 0; i < frameBuffer.getFrameCount(); i += 16) { + for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 16) { samples[0][i + 0] *= _gain; samples[0][i + 1] *= _gain; samples[0][i + 2] *= _gain; @@ -123,8 +123,8 @@ public: } else { - for (uint16_t j = 0; j < frameBuffer.getChannelCount(); ++j) { - for (uint16_t i = 0; i < frameBuffer.getFrameCount(); i += 1) { + for (uint32_t j = 0; j < frameBuffer.getChannelCount(); ++j) { + for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 1) { samples[j][i] *= _gain; } } diff --git a/libraries/audio/src/AudioPan.h b/libraries/audio/src/AudioPan.h index 85e739b255..78e42d2ed5 100644 --- a/libraries/audio/src/AudioPan.h +++ b/libraries/audio/src/AudioPan.h @@ -88,7 +88,7 @@ public: if (frameBuffer.getChannelCount() == 2) { - for (uint16_t i = 0; i < frameBuffer.getFrameCount(); i += 16) { + for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 16) { samples[0][i + 0] *= _gainLeft; samples[0][i + 1] *= _gainLeft; samples[0][i + 2] *= _gainLeft; @@ -128,7 +128,7 @@ public: } } else { - for (uint16_t i = 0; i < frameBuffer.getFrameCount(); i += 1) { + for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 1) { samples[0][i] *= _gainLeft; samples[1][i] *= _gainRight; } diff --git a/libraries/audio/src/AudioSourceNoise.h b/libraries/audio/src/AudioSourceNoise.h index 3e71703893..b7f6889179 100644 --- a/libraries/audio/src/AudioSourceNoise.h +++ b/libraries/audio/src/AudioSourceNoise.h @@ -70,8 +70,8 @@ public: uint32_t randomNumber; float32_t** samples = frameBuffer.getFrameData(); - for (uint16_t i = 0; i < frameBuffer.getFrameCount(); ++i) { - for (uint16_t j = 0; j < frameBuffer.getChannelCount(); ++j) { + for (uint32_t i = 0; i < frameBuffer.getFrameCount(); ++i) { + for (uint32_t j = 0; j < frameBuffer.getChannelCount(); ++j) { _index = (_index + 1) & _indexMask; // increment and mask index. if (_index != 0) { // if index is zero, don't update any random values. diff --git a/libraries/audio/src/AudioSourceTone.h b/libraries/audio/src/AudioSourceTone.h index 22ec95496f..f0c72ca9b3 100644 --- a/libraries/audio/src/AudioSourceTone.h +++ b/libraries/audio/src/AudioSourceTone.h @@ -44,7 +44,7 @@ inline void AudioSourceTone::render(AudioBufferFloat32& frameBuffer) { float32_t** samples = frameBuffer.getFrameData(); float32_t yq; float32_t y; - for (uint16_t i = 0; i < frameBuffer.getFrameCount(); ++i) { + for (uint32_t i = 0; i < frameBuffer.getFrameCount(); ++i) { yq = _yq1 - (_epsilon * _y1); y = _y1 + (_epsilon * yq); @@ -53,7 +53,7 @@ inline void AudioSourceTone::render(AudioBufferFloat32& frameBuffer) { _yq1 = yq; _y1 = y; - for (uint16_t j = 0; j < frameBuffer.getChannelCount(); ++j) { + for (uint32_t j = 0; j < frameBuffer.getChannelCount(); ++j) { samples[j][i] = _amplitude * y; } } From 1b2ee4023aacc1ebf34e4fb2d08c6e51fe0f61b0 Mon Sep 17 00:00:00 2001 From: Craig Hansen-Sturm Date: Sat, 20 Sep 2014 05:46:27 -0700 Subject: [PATCH 3/7] out of line inline members for coding standard --- libraries/audio/src/AudioBuffer.h | 346 ++++++++++++++------------ libraries/audio/src/AudioEditBuffer.h | 36 +-- libraries/audio/src/AudioGain.cpp | 48 ++++ libraries/audio/src/AudioGain.h | 181 ++++++-------- libraries/audio/src/AudioPan.cpp | 33 ++- libraries/audio/src/AudioPan.h | 186 +++++++------- 6 files changed, 456 insertions(+), 374 deletions(-) create mode 100644 libraries/audio/src/AudioGain.cpp diff --git a/libraries/audio/src/AudioBuffer.h b/libraries/audio/src/AudioBuffer.h index cf6a2fb55a..277466ac89 100644 --- a/libraries/audio/src/AudioBuffer.h +++ b/libraries/audio/src/AudioBuffer.h @@ -26,186 +26,216 @@ protected: T** _frameBuffer; - void allocateFrames() { - _frameBuffer = new T*[_channelCountMax]; - if (_frameBuffer) { - for (uint32_t i = 0; i < _channelCountMax; ++i) { - _frameBuffer[i] = new T[_frameCountMax]; - } - } - } - - void deallocateFrames() { - if (_frameBuffer) { - for (uint32_t i = 0; i < _channelCountMax; ++i) { - delete _frameBuffer[i]; - } - delete _frameBuffer; - } - _frameBuffer = NULL; - } + void allocateFrames(); + void deallocateFrames(); public: - AudioFrameBuffer() : - _channelCount(0), - _frameCount(0), - _frameCountMax(0), - _frameBuffer(NULL) { - } + AudioFrameBuffer(); + AudioFrameBuffer(const uint32_t channelCount, const uint32_t frameCount); + virtual ~AudioFrameBuffer(); - AudioFrameBuffer(const uint32_t channelCount, const uint32_t frameCount) : - _channelCount(channelCount), - _channelCountMax(channelCount), - _frameCount(frameCount), - _frameCountMax(frameCount), - _frameBuffer(NULL) { - allocateFrames(); + void initialize(const uint32_t channelCount, const uint32_t frameCount); + void finalize(); + + T**& getFrameData(); + uint32_t getChannelCount(); + uint32_t getFrameCount(); + + template< typename S > + void copyFrames(uint32_t channelCount, const uint32_t frameCount, S* frames, const bool copyOut = false); + void zeroFrames(); +}; + +template< typename T > +AudioFrameBuffer< T >::AudioFrameBuffer() : + _channelCount(0), + _frameCount(0), + _frameCountMax(0), + _frameBuffer(NULL) { +} + +template< typename T > +AudioFrameBuffer< T >::AudioFrameBuffer(const uint32_t channelCount, const uint32_t frameCount) : + _channelCount(channelCount), + _channelCountMax(channelCount), + _frameCount(frameCount), + _frameCountMax(frameCount), + _frameBuffer(NULL) { + allocateFrames(); +} + +template< typename T > +AudioFrameBuffer< T >::~AudioFrameBuffer() { + finalize(); +} + +template< typename T > +void AudioFrameBuffer< T >::allocateFrames() { + _frameBuffer = new T*[_channelCountMax]; + if (_frameBuffer) { + for (uint32_t i = 0; i < _channelCountMax; ++i) { + _frameBuffer[i] = new T[_frameCountMax]; + } } - - virtual ~AudioFrameBuffer() { +} + +template< typename T > +void AudioFrameBuffer< T >::deallocateFrames() { + if (_frameBuffer) { + for (uint32_t i = 0; i < _channelCountMax; ++i) { + delete _frameBuffer[i]; + } + delete _frameBuffer; + } + _frameBuffer = NULL; +} + +template< typename T > +void AudioFrameBuffer< T >::initialize(const uint32_t channelCount, const uint32_t frameCount) { + if (_frameBuffer) { finalize(); } + _channelCount = channelCount; + _channelCountMax = channelCount; + _frameCount = frameCount; + _frameCountMax = frameCount; + allocateFrames(); +} + +template< typename T > +void AudioFrameBuffer< T >::finalize() { + deallocateFrames(); + _channelCount = 0; + _channelCountMax = 0; + _frameCount = 0; + _frameCountMax = 0; +} + +template< typename T > +inline T**& AudioFrameBuffer< T >::getFrameData() { + return _frameBuffer; +} + +template< typename T > +inline uint32_t AudioFrameBuffer< T >::getChannelCount() { + return _channelCount; +} + +template< typename T > +inline uint32_t AudioFrameBuffer< T >::getFrameCount() { + return _frameCount; +} + +template< typename T > +inline void AudioFrameBuffer< T >::zeroFrames() { + if (!_frameBuffer) { + return; + } + for (uint32_t i = 0; i < _channelCountMax; ++i) { + memset(_frameBuffer[i], 0, sizeof(T)*_frameCountMax); + } +} + +template< typename T > +template< typename S > +inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint32_t frameCount, S* frames, const bool copyOut) { + if ( !_frameBuffer || !frames) { + return; + } - void initialize(const uint32_t channelCount, const uint32_t frameCount) { - if (_frameBuffer) { - finalize(); - } - _channelCount = channelCount; - _channelCountMax = channelCount; + if (channelCount <=_channelCountMax && frameCount <=_frameCountMax) { + // We always allow copying fewer frames than we have allocated _frameCount = frameCount; - _frameCountMax = frameCount; - allocateFrames(); + _channelCount = channelCount; + } + else { + // + // However we do not attempt to copy more frames than we've allocated ;-) This is a framing error caused by either + // a/ the platform audio driver not correctly queuing and regularly smoothing device IO capture frames -or- + // b/ our IO processing thread (currently running on a Qt GUI thread) has been delayed/scheduled too late. + // + // The fix is not to make the problem worse by allocating additional frames on this thread, rather, it is to handle + // dynamic re-sizing off the IO processing thread. While a/ is not in our control, we will address the off thread + // re-sizing,, as well as b/, in later releases. + // + // For now, we log this condition, and do our best to recover by copying as many frames as we have allocated. + // Unfortunately, this will result (temporarily), in an audible discontinuity. + // + // If you repeatedly receive this error, contact craig@highfidelity.io and send me what audio device you are using, + // what audio-stack you are using (pulse/alsa, core audio, ...), what OS, and what the reported frame/channel + // counts are. In addition, any information about what you were doing at the time of the discontinuity, would be + // useful (e.g., accessing any client features/menus) + // + qDebug() << "Audio framing error: _channelCount=" + << _channelCount + << "channelCountMax=" + << _channelCountMax + << "_frameCount=" + << _frameCount + << "frameCountMax=" + << _frameCountMax; + + + _channelCount = std::min(_channelCount,_channelCountMax); + _frameCount = std::min(_frameCount,_frameCountMax); } - void finalize() { - deallocateFrames(); - _channelCount = 0; - _channelCountMax = 0; - _frameCount = 0; - _frameCountMax = 0; - } - - T**& getFrameData() { - return _frameBuffer; - } - - uint32_t getChannelCount() { - return _channelCount; - } - - uint32_t getFrameCount() { - return _frameCount; - } - - void zeroFrames() { - if (!_frameBuffer) { - return; - } - for (uint32_t i = 0; i < _channelCountMax; ++i) { - memset(_frameBuffer[i], 0, sizeof(T)*_frameCountMax); - } - } - - template< typename S > - void copyFrames(uint32_t channelCount, const uint32_t frameCount, S* frames, const bool copyOut = false) { - if ( !_frameBuffer || !frames) { - return; - } - - if (channelCount <=_channelCountMax && frameCount <=_frameCountMax) { - // We always allow copying fewer frames than we have allocated - _frameCount = frameCount; - _channelCount = channelCount; + if (copyOut) { + S* dst = frames; + + if(typeid(T) == typeid(S)) { // source and destination types are the same + for (uint32_t i = 0; i < _frameCount; ++i) { + for (uint32_t j = 0; j < _channelCount; ++j) { + *dst++ = _frameBuffer[j][i]; + } + } } else { - // - // However we do not attempt to copy more frames than we've allocated ;-) This is a framing error caused by either - // a/ the platform audio driver not correctly queuing and regularly smoothing device IO capture frames -or- - // b/ our IO processing thread (currently running on a Qt GUI thread) has been delayed/scheduled too late. - // - // The fix is not to make the problem worse by allocating additional frames on this thread, rather, it is to handle - // dynamic re-sizing off the IO processing thread. While a/ is not in our control, we will address the off thread - // re-sizing,, as well as b/, in later releases. - // - // For now, we log this condition, and do our best to recover by copying as many frames as we have allocated. - // Unfortunately, this will result (temporarily), in an audible discontinuity. - // - // If you repeatedly receive this error, contact craig@highfidelity.io and send me what audio device you are using, - // what audio-stack you are using (pulse/alsa, core audio, ...), what OS, and what the reported frame/channel - // counts are. In addition, any information about what you were doing at the time of the discontinuity, would be - // useful (e.g., accessing any client features/menus) - // - qDebug() << "Audio framing error: _channelCount=" - << _channelCount - << "channelCountMax=" - << _channelCountMax - << "_frameCount=" - << _frameCount - << "frameCountMax=" - << _frameCountMax; - - - _channelCount = std::min(_channelCount,_channelCountMax); - _frameCount = std::min(_frameCount,_frameCountMax); - } - - if (copyOut) { - S* dst = frames; - - if(typeid(T) == typeid(S)) { // source and destination types are the same + if(typeid(T) == typeid(float32_t) && + typeid(S) == typeid(int16_t)) { + + const int scale = (2 << ((8 * sizeof(S)) - 1)); + for (uint32_t i = 0; i < _frameCount; ++i) { for (uint32_t j = 0; j < _channelCount; ++j) { - *dst++ = _frameBuffer[j][i]; + *dst++ = (S)(_frameBuffer[j][i] * scale); } } } else { - if(typeid(T) == typeid(float32_t) && - typeid(S) == typeid(int16_t)) { - - const int scale = (2 << ((8 * sizeof(S)) - 1)); - - for (uint32_t i = 0; i < _frameCount; ++i) { - for (uint32_t j = 0; j < _channelCount; ++j) { - *dst++ = (S)(_frameBuffer[j][i] * scale); - } - } - } - else { - assert(0); // currently unsupported conversion - } - } - } - else { // copyIn - S* src = frames; - - if(typeid(T) == typeid(S)) { // source and destination types are the same - for (uint32_t i = 0; i < _frameCount; ++i) { - for (uint32_t j = 0; j < _channelCount; ++j) { - _frameBuffer[j][i] = *src++; - } - } - } - else { - if(typeid(T) == typeid(float32_t) && - typeid(S) == typeid(int16_t)) { - - const int scale = (2 << ((8 * sizeof(S)) - 1)); - - for (uint32_t i = 0; i < _frameCount; ++i) { - for (uint32_t j = 0; j < _channelCount; ++j) { - _frameBuffer[j][i] = ((T)(*src++)) / scale; - } - } - } - else { - assert(0); // currently unsupported conversion - } + assert(0); // currently unsupported conversion } } } -}; + else { // copyIn + S* src = frames; + + if(typeid(T) == typeid(S)) { // source and destination types are the same + for (uint32_t i = 0; i < _frameCount; ++i) { + for (uint32_t j = 0; j < _channelCount; ++j) { + _frameBuffer[j][i] = *src++; + } + } + } + else { + if(typeid(T) == typeid(float32_t) && + typeid(S) == typeid(int16_t)) { + + const int scale = (2 << ((8 * sizeof(S)) - 1)); + + for (uint32_t i = 0; i < _frameCount; ++i) { + for (uint32_t j = 0; j < _channelCount; ++j) { + _frameBuffer[j][i] = ((T)(*src++)) / scale; + } + } + } + else { + assert(0); // currently unsupported conversion + } + } + } +} typedef AudioFrameBuffer< float32_t > AudioBufferFloat32; typedef AudioFrameBuffer< int32_t > AudioBufferSInt32; diff --git a/libraries/audio/src/AudioEditBuffer.h b/libraries/audio/src/AudioEditBuffer.h index 1fa2794e79..2761eee97c 100644 --- a/libraries/audio/src/AudioEditBuffer.h +++ b/libraries/audio/src/AudioEditBuffer.h @@ -13,21 +13,13 @@ #define hifi_AudioEditBuffer_h template< typename T > -class AudioEditBuffer - : public AudioFrameBuffer { +class AudioEditBuffer : public AudioFrameBuffer { public: - AudioEditBuffer() : - AudioFrameBuffer() { - } - - AudioEditBuffer(const uint32_t channelCount, const uint32_t frameCount) : - AudioFrameBuffer(channelCount, frameCount) { - } - - ~AudioEditBuffer() { - } + AudioEditBuffer(); + AudioEditBuffer(const uint32_t channelCount, const uint32_t frameCount); + ~AudioEditBuffer(); bool getZeroCrossing(uint32_t start, bool direction, float32_t epsilon, uint32_t& zero); @@ -36,7 +28,21 @@ public: }; template< typename T > -bool AudioEditBuffer::getZeroCrossing(uint32_t start, bool direction, float32_t epsilon, uint32_t& zero) { +AudioEditBuffer::AudioEditBuffer() : + AudioFrameBuffer() { +} + +template< typename T > +AudioEditBuffer::AudioEditBuffer(const uint32_t channelCount, const uint32_t frameCount) : + AudioFrameBuffer(channelCount, frameCount) { +} + +template< typename T > + AudioEditBuffer::~AudioEditBuffer() { +} + +template< typename T > +inline bool AudioEditBuffer::getZeroCrossing(uint32_t start, bool direction, float32_t epsilon, uint32_t& zero) { zero = this->_frameCount; @@ -69,7 +75,7 @@ bool AudioEditBuffer::getZeroCrossing(uint32_t start, bool direction, float32 } template< typename T > -void AudioEditBuffer::linearFade(uint32_t start, uint32_t stop, bool slope) { +inline void AudioEditBuffer::linearFade(uint32_t start, uint32_t stop, bool slope) { if (start >= stop || start > this->_frameCount || stop > this->_frameCount ) { return; @@ -97,7 +103,7 @@ void AudioEditBuffer::linearFade(uint32_t start, uint32_t stop, bool slope) { } template< typename T > -void AudioEditBuffer::exponentialFade(uint32_t start, uint32_t stop, bool slope) { +inline void AudioEditBuffer::exponentialFade(uint32_t start, uint32_t stop, bool slope) { // TBD } diff --git a/libraries/audio/src/AudioGain.cpp b/libraries/audio/src/AudioGain.cpp new file mode 100644 index 0000000000..8bd2b5b7a3 --- /dev/null +++ b/libraries/audio/src/AudioGain.cpp @@ -0,0 +1,48 @@ +// +// AudioGain.cpp +// hifi +// +// Created by Craig Hansen-Sturm on 9/10/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include +#include +#include +#include "AudioRingBuffer.h" +#include "AudioFormat.h" +#include "AudioBuffer.h" +#include "AudioGain.h" + +AudioGain::AudioGain() { + initialize(); +} + +AudioGain::~AudioGain() { + finalize(); +} + +void AudioGain::initialize() { + setParameters(1.0f,0.0f); +} + +void AudioGain::finalize() { +} + +void AudioGain::reset() { + initialize(); +} + +void AudioGain::setParameters(const float gain, const float mute) { + _gain = std::min(std::max(gain, 0.0f), 1.0f); + _mute = mute != 0.0f; + +} + +void AudioGain::getParameters(float& gain, float& mute) { + gain = _gain; + mute = _mute ? 1.0f : 0.0f; +} diff --git a/libraries/audio/src/AudioGain.h b/libraries/audio/src/AudioGain.h index 485e239128..a321ec2eba 100644 --- a/libraries/audio/src/AudioGain.h +++ b/libraries/audio/src/AudioGain.h @@ -18,119 +18,102 @@ class AudioGain bool _mute; public: - AudioGain() { - initialize(); - } + AudioGain(); + ~AudioGain(); - ~AudioGain() { - finalize(); - } + void initialize(); + void finalize(); + void reset(); - void initialize() { - setParameters(1.0f,0.0f); - } + void setParameters(const float gain, const float mute); + void getParameters(float& gain, float& mute); - void finalize() { - } + void render(AudioBufferFloat32& frameBuffer); +}; + + +inline void AudioGain::render(AudioBufferFloat32& frameBuffer) { + if (_mute) { + frameBuffer.zeroFrames(); + return; + } - void reset() { - initialize(); - } + float32_t** samples = frameBuffer.getFrameData(); - void setParameters(const float gain, const float mute) { - _gain = std::min(std::max(gain, 0.0f), 1.0f); - _mute = mute != 0.0f; + bool frameAlignment16 = (frameBuffer.getFrameCount() & 0x0F) == 0; + if (frameAlignment16) { - } - - void getParameters(float& gain, float& mute) { - gain = _gain; - mute = _mute ? 1.0f : 0.0f; - } - - void render(AudioBufferFloat32& frameBuffer) { - if (_mute) { - frameBuffer.zeroFrames(); - return; - } - - float32_t** samples = frameBuffer.getFrameData(); - - bool frameAlignment16 = (frameBuffer.getFrameCount() & 0x0F) == 0; - if (frameAlignment16) { + if (frameBuffer.getChannelCount() == 1) { - if (frameBuffer.getChannelCount() == 1) { - - for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 16) { - samples[0][i + 0] *= _gain; - samples[0][i + 1] *= _gain; - samples[0][i + 2] *= _gain; - samples[0][i + 3] *= _gain; - samples[0][i + 4] *= _gain; - samples[0][i + 5] *= _gain; - samples[0][i + 6] *= _gain; - samples[0][i + 7] *= _gain; - samples[0][i + 8] *= _gain; - samples[0][i + 9] *= _gain; - samples[0][i + 10] *= _gain; - samples[0][i + 11] *= _gain; - samples[0][i + 12] *= _gain; - samples[0][i + 13] *= _gain; - samples[0][i + 14] *= _gain; - samples[0][i + 15] *= _gain; - } + for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 16) { + samples[0][i + 0] *= _gain; + samples[0][i + 1] *= _gain; + samples[0][i + 2] *= _gain; + samples[0][i + 3] *= _gain; + samples[0][i + 4] *= _gain; + samples[0][i + 5] *= _gain; + samples[0][i + 6] *= _gain; + samples[0][i + 7] *= _gain; + samples[0][i + 8] *= _gain; + samples[0][i + 9] *= _gain; + samples[0][i + 10] *= _gain; + samples[0][i + 11] *= _gain; + samples[0][i + 12] *= _gain; + samples[0][i + 13] *= _gain; + samples[0][i + 14] *= _gain; + samples[0][i + 15] *= _gain; } - else if (frameBuffer.getChannelCount() == 2) { - - for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 16) { - samples[0][i + 0] *= _gain; - samples[0][i + 1] *= _gain; - samples[0][i + 2] *= _gain; - samples[0][i + 3] *= _gain; - samples[0][i + 4] *= _gain; - samples[0][i + 5] *= _gain; - samples[0][i + 6] *= _gain; - samples[0][i + 7] *= _gain; - samples[0][i + 8] *= _gain; - samples[0][i + 9] *= _gain; - samples[0][i + 10] *= _gain; - samples[0][i + 11] *= _gain; - samples[0][i + 12] *= _gain; - samples[0][i + 13] *= _gain; - samples[0][i + 14] *= _gain; - samples[0][i + 15] *= _gain; - samples[1][i + 0] *= _gain; - samples[1][i + 1] *= _gain; - samples[1][i + 2] *= _gain; - samples[1][i + 3] *= _gain; - samples[1][i + 4] *= _gain; - samples[1][i + 5] *= _gain; - samples[1][i + 6] *= _gain; - samples[1][i + 7] *= _gain; - samples[1][i + 8] *= _gain; - samples[1][i + 9] *= _gain; - samples[1][i + 10] *= _gain; - samples[1][i + 11] *= _gain; - samples[1][i + 12] *= _gain; - samples[1][i + 13] *= _gain; - samples[1][i + 14] *= _gain; - samples[1][i + 15] *= _gain; - } - } - else { - assert("unsupported channel format"); + } + else if (frameBuffer.getChannelCount() == 2) { + + for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 16) { + samples[0][i + 0] *= _gain; + samples[0][i + 1] *= _gain; + samples[0][i + 2] *= _gain; + samples[0][i + 3] *= _gain; + samples[0][i + 4] *= _gain; + samples[0][i + 5] *= _gain; + samples[0][i + 6] *= _gain; + samples[0][i + 7] *= _gain; + samples[0][i + 8] *= _gain; + samples[0][i + 9] *= _gain; + samples[0][i + 10] *= _gain; + samples[0][i + 11] *= _gain; + samples[0][i + 12] *= _gain; + samples[0][i + 13] *= _gain; + samples[0][i + 14] *= _gain; + samples[0][i + 15] *= _gain; + samples[1][i + 0] *= _gain; + samples[1][i + 1] *= _gain; + samples[1][i + 2] *= _gain; + samples[1][i + 3] *= _gain; + samples[1][i + 4] *= _gain; + samples[1][i + 5] *= _gain; + samples[1][i + 6] *= _gain; + samples[1][i + 7] *= _gain; + samples[1][i + 8] *= _gain; + samples[1][i + 9] *= _gain; + samples[1][i + 10] *= _gain; + samples[1][i + 11] *= _gain; + samples[1][i + 12] *= _gain; + samples[1][i + 13] *= _gain; + samples[1][i + 14] *= _gain; + samples[1][i + 15] *= _gain; } } else { - - for (uint32_t j = 0; j < frameBuffer.getChannelCount(); ++j) { - for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 1) { - samples[j][i] *= _gain; - } + assert("unsupported channel format"); + } + } + else { + + for (uint32_t j = 0; j < frameBuffer.getChannelCount(); ++j) { + for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 1) { + samples[j][i] *= _gain; } } } -}; +} #endif // AudioGain_h diff --git a/libraries/audio/src/AudioPan.cpp b/libraries/audio/src/AudioPan.cpp index 8f9b568b6a..d5728762ea 100644 --- a/libraries/audio/src/AudioPan.cpp +++ b/libraries/audio/src/AudioPan.cpp @@ -1,8 +1,8 @@ // -// AudioSourceTone.cpp +// AudioPan.cpp // hifi // -// Created by Craig Hansen-Sturm on 8/10/14. +// Created by Craig Hansen-Sturm on 9/10/14. // Copyright 2014 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. @@ -21,3 +21,32 @@ float32_t AudioPan::ONE_MINUS_EPSILON = 1.0f - EPSILON; float32_t AudioPan::ZERO_PLUS_EPSILON = 0.0f + EPSILON; float32_t AudioPan::ONE_HALF_MINUS_EPSILON = 0.5f - EPSILON; float32_t AudioPan::ONE_HALF_PLUS_EPSILON = 0.5f + EPSILON; + +AudioPan::AudioPan() { + initialize(); +} + +AudioPan::~AudioPan() { + finalize(); +} + +void AudioPan::initialize() { + setParameters(0.5f); +} + +void AudioPan::finalize() { +} + +void AudioPan::reset() { + initialize(); +} + +void AudioPan::setParameters(const float32_t pan) { + // pan ranges between 0.0 and 1.0f inclusive. 0.5f is midpoint between full left and full right + _pan = std::min(std::max(pan, 0.0f), 1.0f); + updateCoefficients(); +} + +void AudioPan::getParameters(float32_t& pan) { + pan = _pan; +} diff --git a/libraries/audio/src/AudioPan.h b/libraries/audio/src/AudioPan.h index 78e42d2ed5..ea5e90a7f7 100644 --- a/libraries/audio/src/AudioPan.h +++ b/libraries/audio/src/AudioPan.h @@ -23,118 +23,104 @@ class AudioPan static float32_t ONE_HALF_MINUS_EPSILON; static float32_t ONE_HALF_PLUS_EPSILON; - void updateCoefficients() { - - // implement constant power sin^2 + cos^2 = 1 panning law - - if (_pan >= ONE_MINUS_EPSILON) { // full right - _gainLeft = 0.0f; - _gainRight = 1.0f; - } - else if (_pan <= ZERO_PLUS_EPSILON) { // full left - _gainLeft = 1.0f; - _gainRight = 0.0f; - } - else if ((_pan >= ONE_HALF_MINUS_EPSILON) && (_pan <= ONE_HALF_PLUS_EPSILON)) { // center - _gainLeft = 1.0f / SQUARE_ROOT_OF_2; - _gainRight = 1.0f / SQUARE_ROOT_OF_2; - } - else { // intermediate cases - _gainLeft = cosf( TWO_PI * _pan ); - _gainRight = sinf( TWO_PI * _pan ); - } - } + void updateCoefficients(); public: - AudioPan() { - initialize(); + AudioPan(); + ~AudioPan(); + + void initialize(); + void finalize(); + void reset(); + + void setParameters(const float32_t pan); + void getParameters(float32_t& pan); + + void render(AudioBufferFloat32& frameBuffer); +}; + + +inline void AudioPan::render(AudioBufferFloat32& frameBuffer) { + + if (frameBuffer.getChannelCount() != 2) { + return; } - ~AudioPan() { - finalize(); - } + float32_t** samples = frameBuffer.getFrameData(); - void initialize() { - setParameters(0.5f); - } - - void finalize() { - } - - void reset() { - initialize(); - } - - void setParameters(const float32_t pan) { - // pan ranges between 0.0 and 1.0f inclusive. 0.5f is midpoint between full left and full right - _pan = std::min(std::max(pan, 0.0f), 1.0f); - updateCoefficients(); - } - - void getParameters(float32_t& pan) { - pan = _pan; - } - - void render(AudioBufferFloat32& frameBuffer) { + bool frameAlignment16 = (frameBuffer.getFrameCount() & 0x0F) == 0; + if (frameAlignment16) { - if (frameBuffer.getChannelCount() != 2) { - return; - } - - float32_t** samples = frameBuffer.getFrameData(); - - bool frameAlignment16 = (frameBuffer.getFrameCount() & 0x0F) == 0; - if (frameAlignment16) { + if (frameBuffer.getChannelCount() == 2) { - if (frameBuffer.getChannelCount() == 2) { - - for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 16) { - samples[0][i + 0] *= _gainLeft; - samples[0][i + 1] *= _gainLeft; - samples[0][i + 2] *= _gainLeft; - samples[0][i + 3] *= _gainLeft; - samples[0][i + 4] *= _gainLeft; - samples[0][i + 5] *= _gainLeft; - samples[0][i + 6] *= _gainLeft; - samples[0][i + 7] *= _gainLeft; - samples[0][i + 8] *= _gainLeft; - samples[0][i + 9] *= _gainLeft; - samples[0][i + 10] *= _gainLeft; - samples[0][i + 11] *= _gainLeft; - samples[0][i + 12] *= _gainLeft; - samples[0][i + 13] *= _gainLeft; - samples[0][i + 14] *= _gainLeft; - samples[0][i + 15] *= _gainLeft; - samples[1][i + 0] *= _gainRight; - samples[1][i + 1] *= _gainRight; - samples[1][i + 2] *= _gainRight; - samples[1][i + 3] *= _gainRight; - samples[1][i + 4] *= _gainRight; - samples[1][i + 5] *= _gainRight; - samples[1][i + 6] *= _gainRight; - samples[1][i + 7] *= _gainRight; - samples[1][i + 8] *= _gainRight; - samples[1][i + 9] *= _gainRight; - samples[1][i + 10] *= _gainRight; - samples[1][i + 11] *= _gainRight; - samples[1][i + 12] *= _gainRight; - samples[1][i + 13] *= _gainRight; - samples[1][i + 14] *= _gainRight; - samples[1][i + 15] *= _gainRight; - } - } - else { - assert("unsupported channel format"); + for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 16) { + samples[0][i + 0] *= _gainLeft; + samples[0][i + 1] *= _gainLeft; + samples[0][i + 2] *= _gainLeft; + samples[0][i + 3] *= _gainLeft; + samples[0][i + 4] *= _gainLeft; + samples[0][i + 5] *= _gainLeft; + samples[0][i + 6] *= _gainLeft; + samples[0][i + 7] *= _gainLeft; + samples[0][i + 8] *= _gainLeft; + samples[0][i + 9] *= _gainLeft; + samples[0][i + 10] *= _gainLeft; + samples[0][i + 11] *= _gainLeft; + samples[0][i + 12] *= _gainLeft; + samples[0][i + 13] *= _gainLeft; + samples[0][i + 14] *= _gainLeft; + samples[0][i + 15] *= _gainLeft; + samples[1][i + 0] *= _gainRight; + samples[1][i + 1] *= _gainRight; + samples[1][i + 2] *= _gainRight; + samples[1][i + 3] *= _gainRight; + samples[1][i + 4] *= _gainRight; + samples[1][i + 5] *= _gainRight; + samples[1][i + 6] *= _gainRight; + samples[1][i + 7] *= _gainRight; + samples[1][i + 8] *= _gainRight; + samples[1][i + 9] *= _gainRight; + samples[1][i + 10] *= _gainRight; + samples[1][i + 11] *= _gainRight; + samples[1][i + 12] *= _gainRight; + samples[1][i + 13] *= _gainRight; + samples[1][i + 14] *= _gainRight; + samples[1][i + 15] *= _gainRight; } } else { - for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 1) { - samples[0][i] *= _gainLeft; - samples[1][i] *= _gainRight; - } + assert("unsupported channel format"); } } -}; + else { + for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 1) { + samples[0][i] *= _gainLeft; + samples[1][i] *= _gainRight; + } + } +} + +inline void AudioPan::updateCoefficients() { + + // implement constant power sin^2 + cos^2 = 1 panning law + + if (_pan >= ONE_MINUS_EPSILON) { // full right + _gainLeft = 0.0f; + _gainRight = 1.0f; + } + else if (_pan <= ZERO_PLUS_EPSILON) { // full left + _gainLeft = 1.0f; + _gainRight = 0.0f; + } + else if ((_pan >= ONE_HALF_MINUS_EPSILON) && (_pan <= ONE_HALF_PLUS_EPSILON)) { // center + _gainLeft = 1.0f / SQUARE_ROOT_OF_2; + _gainRight = 1.0f / SQUARE_ROOT_OF_2; + } + else { // intermediate cases + _gainLeft = cosf( TWO_PI * _pan ); + _gainRight = sinf( TWO_PI * _pan ); + } +} #endif // AudioPan_h From 4372410fa765783c63a43798d4c5035a632adc64 Mon Sep 17 00:00:00 2001 From: Craig Hansen-Sturm Date: Sat, 20 Sep 2014 14:49:41 -0700 Subject: [PATCH 4/7] unrolling for int16_t<->float32_t conversion/scaling and copy in/out --- libraries/audio/src/AudioBuffer.h | 304 ++++++++++++++++++++++++++---- 1 file changed, 267 insertions(+), 37 deletions(-) diff --git a/libraries/audio/src/AudioBuffer.h b/libraries/audio/src/AudioBuffer.h index 277466ac89..b0d9d7da8e 100644 --- a/libraries/audio/src/AudioBuffer.h +++ b/libraries/audio/src/AudioBuffer.h @@ -150,23 +150,6 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 _channelCount = channelCount; } else { - // - // However we do not attempt to copy more frames than we've allocated ;-) This is a framing error caused by either - // a/ the platform audio driver not correctly queuing and regularly smoothing device IO capture frames -or- - // b/ our IO processing thread (currently running on a Qt GUI thread) has been delayed/scheduled too late. - // - // The fix is not to make the problem worse by allocating additional frames on this thread, rather, it is to handle - // dynamic re-sizing off the IO processing thread. While a/ is not in our control, we will address the off thread - // re-sizing,, as well as b/, in later releases. - // - // For now, we log this condition, and do our best to recover by copying as many frames as we have allocated. - // Unfortunately, this will result (temporarily), in an audible discontinuity. - // - // If you repeatedly receive this error, contact craig@highfidelity.io and send me what audio device you are using, - // what audio-stack you are using (pulse/alsa, core audio, ...), what OS, and what the reported frame/channel - // counts are. In addition, any information about what you were doing at the time of the discontinuity, would be - // useful (e.g., accessing any client features/menus) - // qDebug() << "Audio framing error: _channelCount=" << _channelCount << "channelCountMax=" @@ -176,30 +159,154 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 << "frameCountMax=" << _frameCountMax; - _channelCount = std::min(_channelCount,_channelCountMax); _frameCount = std::min(_frameCount,_frameCountMax); } + bool frameAlignment16 = (_frameCount & 0x0F) == 0; + if (copyOut) { S* dst = frames; - if(typeid(T) == typeid(S)) { // source and destination types are the same - for (uint32_t i = 0; i < _frameCount; ++i) { - for (uint32_t j = 0; j < _channelCount; ++j) { - *dst++ = _frameBuffer[j][i]; + if(typeid(T) == typeid(S)) { // source and destination types are the same, just copy out + + if (frameAlignment16 && (_channelCount == 1 || _channelCount == 2)) { + + if (_channelCount == 1) { + for (uint32_t i = 0; i < _frameCount; i += 16) { + *dst++ = _frameBuffer[0][0]; + *dst++ = _frameBuffer[0][1]; + *dst++ = _frameBuffer[0][2]; + *dst++ = _frameBuffer[0][3]; + *dst++ = _frameBuffer[0][4]; + *dst++ = _frameBuffer[0][5]; + *dst++ = _frameBuffer[0][6]; + *dst++ = _frameBuffer[0][7]; + *dst++ = _frameBuffer[0][8]; + *dst++ = _frameBuffer[0][9]; + *dst++ = _frameBuffer[0][10]; + *dst++ = _frameBuffer[0][11]; + *dst++ = _frameBuffer[0][12]; + *dst++ = _frameBuffer[0][13]; + *dst++ = _frameBuffer[0][14]; + *dst++ = _frameBuffer[0][15]; + } + } + else if (_channelCount == 2) { + for (uint32_t i = 0; i < _frameCount; i += 16) { + *dst++ = _frameBuffer[0][0]; + *dst++ = _frameBuffer[1][0]; + *dst++ = _frameBuffer[0][1]; + *dst++ = _frameBuffer[1][1]; + *dst++ = _frameBuffer[0][2]; + *dst++ = _frameBuffer[1][2]; + *dst++ = _frameBuffer[0][3]; + *dst++ = _frameBuffer[1][3]; + *dst++ = _frameBuffer[0][4]; + *dst++ = _frameBuffer[1][4]; + *dst++ = _frameBuffer[0][5]; + *dst++ = _frameBuffer[1][5]; + *dst++ = _frameBuffer[0][6]; + *dst++ = _frameBuffer[1][6]; + *dst++ = _frameBuffer[0][7]; + *dst++ = _frameBuffer[1][7]; + *dst++ = _frameBuffer[0][8]; + *dst++ = _frameBuffer[1][8]; + *dst++ = _frameBuffer[0][9]; + *dst++ = _frameBuffer[1][9]; + *dst++ = _frameBuffer[0][10]; + *dst++ = _frameBuffer[1][10]; + *dst++ = _frameBuffer[0][11]; + *dst++ = _frameBuffer[1][11]; + *dst++ = _frameBuffer[0][12]; + *dst++ = _frameBuffer[1][12]; + *dst++ = _frameBuffer[0][13]; + *dst++ = _frameBuffer[1][13]; + *dst++ = _frameBuffer[0][14]; + *dst++ = _frameBuffer[1][14]; + *dst++ = _frameBuffer[0][15]; + *dst++ = _frameBuffer[1][15]; + } + } + } + else { + for (uint32_t i = 0; i < _frameCount; ++i) { + for (uint32_t j = 0; j < _channelCount; ++j) { + *dst++ = _frameBuffer[j][i]; + } } } } else { if(typeid(T) == typeid(float32_t) && - typeid(S) == typeid(int16_t)) { + typeid(S) == typeid(int16_t)) { // source and destination aare not the same, convert from float32_t to int16_t and copy out const int scale = (2 << ((8 * sizeof(S)) - 1)); - - for (uint32_t i = 0; i < _frameCount; ++i) { - for (uint32_t j = 0; j < _channelCount; ++j) { - *dst++ = (S)(_frameBuffer[j][i] * scale); + + if (frameAlignment16 && (_channelCount == 1 || _channelCount == 2)) { + + if (_channelCount == 1) { + for (uint32_t i = 0; i < _frameCount; i += 16) { + *dst++ = (S)(_frameBuffer[0][0] * scale); + *dst++ = (S)(_frameBuffer[0][1] * scale); + *dst++ = (S)(_frameBuffer[0][2] * scale); + *dst++ = (S)(_frameBuffer[0][3] * scale); + *dst++ = (S)(_frameBuffer[0][4] * scale); + *dst++ = (S)(_frameBuffer[0][5] * scale); + *dst++ = (S)(_frameBuffer[0][6] * scale); + *dst++ = (S)(_frameBuffer[0][7] * scale); + *dst++ = (S)(_frameBuffer[0][8] * scale); + *dst++ = (S)(_frameBuffer[0][9] * scale); + *dst++ = (S)(_frameBuffer[0][10] * scale); + *dst++ = (S)(_frameBuffer[0][11] * scale); + *dst++ = (S)(_frameBuffer[0][12] * scale); + *dst++ = (S)(_frameBuffer[0][13] * scale); + *dst++ = (S)(_frameBuffer[0][14] * scale); + *dst++ = (S)(_frameBuffer[0][15] * scale); + } + } + else if (_channelCount == 2) { + for (uint32_t i = 0; i < _frameCount; i += 16) { + *dst++ = (S)(_frameBuffer[0][0] * scale); + *dst++ = (S)(_frameBuffer[1][0] * scale); + *dst++ = (S)(_frameBuffer[0][1] * scale); + *dst++ = (S)(_frameBuffer[1][1] * scale); + *dst++ = (S)(_frameBuffer[0][2] * scale); + *dst++ = (S)(_frameBuffer[1][2] * scale); + *dst++ = (S)(_frameBuffer[0][3] * scale); + *dst++ = (S)(_frameBuffer[1][3] * scale); + *dst++ = (S)(_frameBuffer[0][4] * scale); + *dst++ = (S)(_frameBuffer[1][4] * scale); + *dst++ = (S)(_frameBuffer[0][5] * scale); + *dst++ = (S)(_frameBuffer[1][5] * scale); + *dst++ = (S)(_frameBuffer[0][6] * scale); + *dst++ = (S)(_frameBuffer[1][6] * scale); + *dst++ = (S)(_frameBuffer[0][7] * scale); + *dst++ = (S)(_frameBuffer[1][7] * scale); + *dst++ = (S)(_frameBuffer[0][8] * scale); + *dst++ = (S)(_frameBuffer[1][8] * scale); + *dst++ = (S)(_frameBuffer[0][9] * scale); + *dst++ = (S)(_frameBuffer[1][9] * scale); + *dst++ = (S)(_frameBuffer[0][10] * scale); + *dst++ = (S)(_frameBuffer[1][10] * scale); + *dst++ = (S)(_frameBuffer[0][11] * scale); + *dst++ = (S)(_frameBuffer[1][11] * scale); + *dst++ = (S)(_frameBuffer[0][12] * scale); + *dst++ = (S)(_frameBuffer[1][12] * scale); + *dst++ = (S)(_frameBuffer[0][13] * scale); + *dst++ = (S)(_frameBuffer[1][13] * scale); + *dst++ = (S)(_frameBuffer[0][14] * scale); + *dst++ = (S)(_frameBuffer[1][14] * scale); + *dst++ = (S)(_frameBuffer[0][15] * scale); + *dst++ = (S)(_frameBuffer[1][15] * scale); + } + } + } + else { + for (uint32_t i = 0; i < _frameCount; ++i) { + for (uint32_t j = 0; j < _channelCount; ++j) { + *dst++ = (S)(_frameBuffer[j][i] * scale); + } } } } @@ -208,25 +315,148 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 } } } - else { // copyIn + else { // copyIn S* src = frames; - if(typeid(T) == typeid(S)) { // source and destination types are the same - for (uint32_t i = 0; i < _frameCount; ++i) { - for (uint32_t j = 0; j < _channelCount; ++j) { - _frameBuffer[j][i] = *src++; + if(typeid(T) == typeid(S)) { // source and destination types are the same, copy in + + if (frameAlignment16 && (_channelCount == 1 || _channelCount == 2)) { + + if (_channelCount == 1) { + for (uint32_t i = 0; i < _frameCount; i += 16) { + _frameBuffer[0][0] = *src++; + _frameBuffer[0][1] = *src++; + _frameBuffer[0][2] = *src++; + _frameBuffer[0][3] = *src++; + _frameBuffer[0][4] = *src++; + _frameBuffer[0][5] = *src++; + _frameBuffer[0][6] = *src++; + _frameBuffer[0][7] = *src++; + _frameBuffer[0][8] = *src++; + _frameBuffer[0][9] = *src++; + _frameBuffer[0][10] = *src++; + _frameBuffer[0][11] = *src++; + _frameBuffer[0][12] = *src++; + _frameBuffer[0][13] = *src++; + _frameBuffer[0][14] = *src++; + _frameBuffer[0][15] = *src++; + } + } + else if (_channelCount == 2) { + for (uint32_t i = 0; i < _frameCount; i += 16) { + _frameBuffer[0][0] = *src++; + _frameBuffer[1][0] = *src++; + _frameBuffer[0][1] = *src++; + _frameBuffer[1][1] = *src++; + _frameBuffer[0][2] = *src++; + _frameBuffer[1][2] = *src++; + _frameBuffer[0][3] = *src++; + _frameBuffer[1][3] = *src++; + _frameBuffer[0][4] = *src++; + _frameBuffer[1][4] = *src++; + _frameBuffer[0][5] = *src++; + _frameBuffer[1][5] = *src++; + _frameBuffer[0][6] = *src++; + _frameBuffer[1][6] = *src++; + _frameBuffer[0][7] = *src++; + _frameBuffer[1][7] = *src++; + _frameBuffer[0][8] = *src++; + _frameBuffer[1][8] = *src++; + _frameBuffer[0][9] = *src++; + _frameBuffer[1][9] = *src++; + _frameBuffer[0][10] = *src++; + _frameBuffer[1][10] = *src++; + _frameBuffer[0][11] = *src++; + _frameBuffer[1][11] = *src++; + _frameBuffer[0][12] = *src++; + _frameBuffer[1][12] = *src++; + _frameBuffer[0][13] = *src++; + _frameBuffer[1][13] = *src++; + _frameBuffer[0][14] = *src++; + _frameBuffer[1][14] = *src++; + _frameBuffer[0][15] = *src++; + _frameBuffer[1][15] = *src++; + } + } + } + else { + for (uint32_t i = 0; i < _frameCount; ++i) { + for (uint32_t j = 0; j < _channelCount; ++j) { + _frameBuffer[j][i] = *src++; + } } } } - else { + else { if(typeid(T) == typeid(float32_t) && - typeid(S) == typeid(int16_t)) { + typeid(S) == typeid(int16_t)) { // source and destination aare not the same, convert from int16_t to float32_t and copy in const int scale = (2 << ((8 * sizeof(S)) - 1)); - for (uint32_t i = 0; i < _frameCount; ++i) { - for (uint32_t j = 0; j < _channelCount; ++j) { - _frameBuffer[j][i] = ((T)(*src++)) / scale; + if (frameAlignment16 && (_channelCount == 1 || _channelCount == 2)) { + + if (_channelCount == 1) { + for (uint32_t i = 0; i < _frameCount; i += 16) { + _frameBuffer[0][0] = ((T)(*src++)) / scale; + _frameBuffer[0][1] = ((T)(*src++)) / scale; + _frameBuffer[0][2] = ((T)(*src++)) / scale; + _frameBuffer[0][3] = ((T)(*src++)) / scale; + _frameBuffer[0][4] = ((T)(*src++)) / scale; + _frameBuffer[0][5] = ((T)(*src++)) / scale; + _frameBuffer[0][6] = ((T)(*src++)) / scale; + _frameBuffer[0][7] = ((T)(*src++)) / scale; + _frameBuffer[0][8] = ((T)(*src++)) / scale; + _frameBuffer[0][9] = ((T)(*src++)) / scale; + _frameBuffer[0][10] = ((T)(*src++)) / scale; + _frameBuffer[0][11] = ((T)(*src++)) / scale; + _frameBuffer[0][12] = ((T)(*src++)) / scale; + _frameBuffer[0][13] = ((T)(*src++)) / scale; + _frameBuffer[0][14] = ((T)(*src++)) / scale; + _frameBuffer[0][15] = ((T)(*src++)) / scale; + } + } + else if (_channelCount == 2) { + for (uint32_t i = 0; i < _frameCount; i += 16) { + _frameBuffer[0][0] = ((T)(*src++)) / scale; + _frameBuffer[1][0] = ((T)(*src++)) / scale; + _frameBuffer[0][1] = ((T)(*src++)) / scale; + _frameBuffer[1][1] = ((T)(*src++)) / scale; + _frameBuffer[0][2] = ((T)(*src++)) / scale; + _frameBuffer[1][2] = ((T)(*src++)) / scale; + _frameBuffer[0][3] = ((T)(*src++)) / scale; + _frameBuffer[1][3] = ((T)(*src++)) / scale; + _frameBuffer[0][4] = ((T)(*src++)) / scale; + _frameBuffer[1][4] = ((T)(*src++)) / scale; + _frameBuffer[0][5] = ((T)(*src++)) / scale; + _frameBuffer[1][5] = ((T)(*src++)) / scale; + _frameBuffer[0][6] = ((T)(*src++)) / scale; + _frameBuffer[1][6] = ((T)(*src++)) / scale; + _frameBuffer[0][7] = ((T)(*src++)) / scale; + _frameBuffer[1][7] = ((T)(*src++)) / scale; + _frameBuffer[0][8] = ((T)(*src++)) / scale; + _frameBuffer[1][8] = ((T)(*src++)) / scale; + _frameBuffer[0][9] = ((T)(*src++)) / scale; + _frameBuffer[1][9] = ((T)(*src++)) / scale; + _frameBuffer[0][10] = ((T)(*src++)) / scale; + _frameBuffer[1][10] = ((T)(*src++)) / scale; + _frameBuffer[0][11] = ((T)(*src++)) / scale; + _frameBuffer[1][11] = ((T)(*src++)) / scale; + _frameBuffer[0][12] = ((T)(*src++)) / scale; + _frameBuffer[1][12] = ((T)(*src++)) / scale; + _frameBuffer[0][13] = ((T)(*src++)) / scale; + _frameBuffer[1][13] = ((T)(*src++)) / scale; + _frameBuffer[0][14] = ((T)(*src++)) / scale; + _frameBuffer[1][14] = ((T)(*src++)) / scale; + _frameBuffer[0][15] = ((T)(*src++)) / scale; + _frameBuffer[1][15] = ((T)(*src++)) / scale; + } + } + } + else { + for (uint32_t i = 0; i < _frameCount; ++i) { + for (uint32_t j = 0; j < _channelCount; ++j) { + _frameBuffer[j][i] = ((T)(*src++)) / scale; + } } } } From 49e3f4bdb0a6e25b9aa799e7cc0546e8299d17c9 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 22 Sep 2014 09:20:34 -0700 Subject: [PATCH 5/7] use 10.8 only in deployment target, base SDK should be latest --- CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 28ff00cc96..4c0bfb0892 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,8 +35,7 @@ endif () set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${QT_CMAKE_PREFIX_PATH}) -# set our Base SDK to 10.8 -set(CMAKE_OSX_SYSROOT /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk) +# set our OS X deployment target to set(CMAKE_OSX_DEPLOYMENT_TARGET 10.8) # Find includes in corresponding build directories From 66ec43c06615b276acb81e17751309c3273c164c Mon Sep 17 00:00:00 2001 From: Craig Hansen-Sturm Date: Mon, 22 Sep 2014 11:04:21 -0700 Subject: [PATCH 6/7] coding standard - else clause --- libraries/audio/src/AudioBuffer.h | 44 +++++++++------------------ libraries/audio/src/AudioEditBuffer.h | 3 +- libraries/audio/src/AudioGain.h | 9 ++---- libraries/audio/src/AudioPan.h | 15 +++------ 4 files changed, 24 insertions(+), 47 deletions(-) diff --git a/libraries/audio/src/AudioBuffer.h b/libraries/audio/src/AudioBuffer.h index b0d9d7da8e..b8ff1635a8 100644 --- a/libraries/audio/src/AudioBuffer.h +++ b/libraries/audio/src/AudioBuffer.h @@ -148,8 +148,7 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 // We always allow copying fewer frames than we have allocated _frameCount = frameCount; _channelCount = channelCount; - } - else { + } else { qDebug() << "Audio framing error: _channelCount=" << _channelCount << "channelCountMax=" @@ -163,7 +162,7 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 _frameCount = std::min(_frameCount,_frameCountMax); } - bool frameAlignment16 = (_frameCount & 0x0F) == 0; + bool frameAlignment16 = false; // (_frameCount & 0x0F) == 0; if (copyOut) { S* dst = frames; @@ -191,8 +190,7 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 *dst++ = _frameBuffer[0][14]; *dst++ = _frameBuffer[0][15]; } - } - else if (_channelCount == 2) { + } else if (_channelCount == 2) { for (uint32_t i = 0; i < _frameCount; i += 16) { *dst++ = _frameBuffer[0][0]; *dst++ = _frameBuffer[1][0]; @@ -228,16 +226,14 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 *dst++ = _frameBuffer[1][15]; } } - } - else { + } else { for (uint32_t i = 0; i < _frameCount; ++i) { for (uint32_t j = 0; j < _channelCount; ++j) { *dst++ = _frameBuffer[j][i]; } } } - } - else { + } else { if(typeid(T) == typeid(float32_t) && typeid(S) == typeid(int16_t)) { // source and destination aare not the same, convert from float32_t to int16_t and copy out @@ -264,8 +260,7 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 *dst++ = (S)(_frameBuffer[0][14] * scale); *dst++ = (S)(_frameBuffer[0][15] * scale); } - } - else if (_channelCount == 2) { + } else if (_channelCount == 2) { for (uint32_t i = 0; i < _frameCount; i += 16) { *dst++ = (S)(_frameBuffer[0][0] * scale); *dst++ = (S)(_frameBuffer[1][0] * scale); @@ -301,21 +296,18 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 *dst++ = (S)(_frameBuffer[1][15] * scale); } } - } - else { + } else { for (uint32_t i = 0; i < _frameCount; ++i) { for (uint32_t j = 0; j < _channelCount; ++j) { *dst++ = (S)(_frameBuffer[j][i] * scale); } } } - } - else { + } else { assert(0); // currently unsupported conversion } } - } - else { // copyIn + } else { // copyIn S* src = frames; if(typeid(T) == typeid(S)) { // source and destination types are the same, copy in @@ -341,8 +333,7 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 _frameBuffer[0][14] = *src++; _frameBuffer[0][15] = *src++; } - } - else if (_channelCount == 2) { + } else if (_channelCount == 2) { for (uint32_t i = 0; i < _frameCount; i += 16) { _frameBuffer[0][0] = *src++; _frameBuffer[1][0] = *src++; @@ -378,16 +369,14 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 _frameBuffer[1][15] = *src++; } } - } - else { + } else { for (uint32_t i = 0; i < _frameCount; ++i) { for (uint32_t j = 0; j < _channelCount; ++j) { _frameBuffer[j][i] = *src++; } } } - } - else { + } else { if(typeid(T) == typeid(float32_t) && typeid(S) == typeid(int16_t)) { // source and destination aare not the same, convert from int16_t to float32_t and copy in @@ -414,8 +403,7 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 _frameBuffer[0][14] = ((T)(*src++)) / scale; _frameBuffer[0][15] = ((T)(*src++)) / scale; } - } - else if (_channelCount == 2) { + } else if (_channelCount == 2) { for (uint32_t i = 0; i < _frameCount; i += 16) { _frameBuffer[0][0] = ((T)(*src++)) / scale; _frameBuffer[1][0] = ((T)(*src++)) / scale; @@ -451,16 +439,14 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 _frameBuffer[1][15] = ((T)(*src++)) / scale; } } - } - else { + } else { for (uint32_t i = 0; i < _frameCount; ++i) { for (uint32_t j = 0; j < _channelCount; ++j) { _frameBuffer[j][i] = ((T)(*src++)) / scale; } } } - } - else { + } else { assert(0); // currently unsupported conversion } } diff --git a/libraries/audio/src/AudioEditBuffer.h b/libraries/audio/src/AudioEditBuffer.h index 2761eee97c..f93d338e47 100644 --- a/libraries/audio/src/AudioEditBuffer.h +++ b/libraries/audio/src/AudioEditBuffer.h @@ -88,8 +88,7 @@ inline void AudioEditBuffer::linearFade(uint32_t start, uint32_t stop, bool s if (slope) { // 0.0 to 1.0f in delta increments delta = 1.0f / (float32_t)count; gain = 0.0f; - } - else { // 1.0f to 0.0f in delta increments + } else { // 1.0f to 0.0f in delta increments delta = -1.0f / (float32_t)count; gain = 1.0f; } diff --git a/libraries/audio/src/AudioGain.h b/libraries/audio/src/AudioGain.h index a321ec2eba..1d54d76f7f 100644 --- a/libraries/audio/src/AudioGain.h +++ b/libraries/audio/src/AudioGain.h @@ -63,8 +63,7 @@ inline void AudioGain::render(AudioBufferFloat32& frameBuffer) { samples[0][i + 14] *= _gain; samples[0][i + 15] *= _gain; } - } - else if (frameBuffer.getChannelCount() == 2) { + } else if (frameBuffer.getChannelCount() == 2) { for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 16) { samples[0][i + 0] *= _gain; @@ -100,12 +99,10 @@ inline void AudioGain::render(AudioBufferFloat32& frameBuffer) { samples[1][i + 14] *= _gain; samples[1][i + 15] *= _gain; } - } - else { + } else { assert("unsupported channel format"); } - } - else { + } else { for (uint32_t j = 0; j < frameBuffer.getChannelCount(); ++j) { for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 1) { diff --git a/libraries/audio/src/AudioPan.h b/libraries/audio/src/AudioPan.h index ea5e90a7f7..2fe8c5cc28 100644 --- a/libraries/audio/src/AudioPan.h +++ b/libraries/audio/src/AudioPan.h @@ -87,12 +87,10 @@ inline void AudioPan::render(AudioBufferFloat32& frameBuffer) { samples[1][i + 14] *= _gainRight; samples[1][i + 15] *= _gainRight; } - } - else { + } else { assert("unsupported channel format"); } - } - else { + } else { for (uint32_t i = 0; i < frameBuffer.getFrameCount(); i += 1) { samples[0][i] *= _gainLeft; samples[1][i] *= _gainRight; @@ -107,16 +105,13 @@ inline void AudioPan::updateCoefficients() { if (_pan >= ONE_MINUS_EPSILON) { // full right _gainLeft = 0.0f; _gainRight = 1.0f; - } - else if (_pan <= ZERO_PLUS_EPSILON) { // full left + } else if (_pan <= ZERO_PLUS_EPSILON) { // full left _gainLeft = 1.0f; _gainRight = 0.0f; - } - else if ((_pan >= ONE_HALF_MINUS_EPSILON) && (_pan <= ONE_HALF_PLUS_EPSILON)) { // center + } else if ((_pan >= ONE_HALF_MINUS_EPSILON) && (_pan <= ONE_HALF_PLUS_EPSILON)) { // center _gainLeft = 1.0f / SQUARE_ROOT_OF_2; _gainRight = 1.0f / SQUARE_ROOT_OF_2; - } - else { // intermediate cases + } else { // intermediate cases _gainLeft = cosf( TWO_PI * _pan ); _gainRight = sinf( TWO_PI * _pan ); } From 75858b07c6a64d1869837fbace5454f2c32d6ea8 Mon Sep 17 00:00:00 2001 From: Craig Hansen-Sturm Date: Mon, 22 Sep 2014 11:21:18 -0700 Subject: [PATCH 7/7] coding standard + missing frame-offsets in audiobuffer --- libraries/audio/src/AudioBuffer.h | 386 +++++++++++++------------- libraries/audio/src/AudioEditBuffer.h | 3 +- 2 files changed, 194 insertions(+), 195 deletions(-) diff --git a/libraries/audio/src/AudioBuffer.h b/libraries/audio/src/AudioBuffer.h index b8ff1635a8..c489c169a7 100644 --- a/libraries/audio/src/AudioBuffer.h +++ b/libraries/audio/src/AudioBuffer.h @@ -162,7 +162,7 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 _frameCount = std::min(_frameCount,_frameCountMax); } - bool frameAlignment16 = false; // (_frameCount & 0x0F) == 0; + bool frameAlignment16 = (_frameCount & 0x0F) == 0; if (copyOut) { S* dst = frames; @@ -173,57 +173,57 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 if (_channelCount == 1) { for (uint32_t i = 0; i < _frameCount; i += 16) { - *dst++ = _frameBuffer[0][0]; - *dst++ = _frameBuffer[0][1]; - *dst++ = _frameBuffer[0][2]; - *dst++ = _frameBuffer[0][3]; - *dst++ = _frameBuffer[0][4]; - *dst++ = _frameBuffer[0][5]; - *dst++ = _frameBuffer[0][6]; - *dst++ = _frameBuffer[0][7]; - *dst++ = _frameBuffer[0][8]; - *dst++ = _frameBuffer[0][9]; - *dst++ = _frameBuffer[0][10]; - *dst++ = _frameBuffer[0][11]; - *dst++ = _frameBuffer[0][12]; - *dst++ = _frameBuffer[0][13]; - *dst++ = _frameBuffer[0][14]; - *dst++ = _frameBuffer[0][15]; + *dst++ = _frameBuffer[0][i + 0]; + *dst++ = _frameBuffer[0][i + 1]; + *dst++ = _frameBuffer[0][i + 2]; + *dst++ = _frameBuffer[0][i + 3]; + *dst++ = _frameBuffer[0][i + 4]; + *dst++ = _frameBuffer[0][i + 5]; + *dst++ = _frameBuffer[0][i + 6]; + *dst++ = _frameBuffer[0][i + 7]; + *dst++ = _frameBuffer[0][i + 8]; + *dst++ = _frameBuffer[0][i + 9]; + *dst++ = _frameBuffer[0][i + 10]; + *dst++ = _frameBuffer[0][i + 11]; + *dst++ = _frameBuffer[0][i + 12]; + *dst++ = _frameBuffer[0][i + 13]; + *dst++ = _frameBuffer[0][i + 14]; + *dst++ = _frameBuffer[0][i + 15]; } } else if (_channelCount == 2) { for (uint32_t i = 0; i < _frameCount; i += 16) { - *dst++ = _frameBuffer[0][0]; - *dst++ = _frameBuffer[1][0]; - *dst++ = _frameBuffer[0][1]; - *dst++ = _frameBuffer[1][1]; - *dst++ = _frameBuffer[0][2]; - *dst++ = _frameBuffer[1][2]; - *dst++ = _frameBuffer[0][3]; - *dst++ = _frameBuffer[1][3]; - *dst++ = _frameBuffer[0][4]; - *dst++ = _frameBuffer[1][4]; - *dst++ = _frameBuffer[0][5]; - *dst++ = _frameBuffer[1][5]; - *dst++ = _frameBuffer[0][6]; - *dst++ = _frameBuffer[1][6]; - *dst++ = _frameBuffer[0][7]; - *dst++ = _frameBuffer[1][7]; - *dst++ = _frameBuffer[0][8]; - *dst++ = _frameBuffer[1][8]; - *dst++ = _frameBuffer[0][9]; - *dst++ = _frameBuffer[1][9]; - *dst++ = _frameBuffer[0][10]; - *dst++ = _frameBuffer[1][10]; - *dst++ = _frameBuffer[0][11]; - *dst++ = _frameBuffer[1][11]; - *dst++ = _frameBuffer[0][12]; - *dst++ = _frameBuffer[1][12]; - *dst++ = _frameBuffer[0][13]; - *dst++ = _frameBuffer[1][13]; - *dst++ = _frameBuffer[0][14]; - *dst++ = _frameBuffer[1][14]; - *dst++ = _frameBuffer[0][15]; - *dst++ = _frameBuffer[1][15]; + *dst++ = _frameBuffer[0][i + 0]; + *dst++ = _frameBuffer[1][i + 0]; + *dst++ = _frameBuffer[0][i + 1]; + *dst++ = _frameBuffer[1][i + 1]; + *dst++ = _frameBuffer[0][i + 2]; + *dst++ = _frameBuffer[1][i + 2]; + *dst++ = _frameBuffer[0][i + 3]; + *dst++ = _frameBuffer[1][i + 3]; + *dst++ = _frameBuffer[0][i + 4]; + *dst++ = _frameBuffer[1][i + 4]; + *dst++ = _frameBuffer[0][i + 5]; + *dst++ = _frameBuffer[1][i + 5]; + *dst++ = _frameBuffer[0][i + 6]; + *dst++ = _frameBuffer[1][i + 6]; + *dst++ = _frameBuffer[0][i + 7]; + *dst++ = _frameBuffer[1][i + 7]; + *dst++ = _frameBuffer[0][i + 8]; + *dst++ = _frameBuffer[1][i + 8]; + *dst++ = _frameBuffer[0][i + 9]; + *dst++ = _frameBuffer[1][i + 9]; + *dst++ = _frameBuffer[0][i + 10]; + *dst++ = _frameBuffer[1][i + 10]; + *dst++ = _frameBuffer[0][i + 11]; + *dst++ = _frameBuffer[1][i + 11]; + *dst++ = _frameBuffer[0][i + 12]; + *dst++ = _frameBuffer[1][i + 12]; + *dst++ = _frameBuffer[0][i + 13]; + *dst++ = _frameBuffer[1][i + 13]; + *dst++ = _frameBuffer[0][i + 14]; + *dst++ = _frameBuffer[1][i + 14]; + *dst++ = _frameBuffer[0][i + 15]; + *dst++ = _frameBuffer[1][i + 15]; } } } else { @@ -243,57 +243,57 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 if (_channelCount == 1) { for (uint32_t i = 0; i < _frameCount; i += 16) { - *dst++ = (S)(_frameBuffer[0][0] * scale); - *dst++ = (S)(_frameBuffer[0][1] * scale); - *dst++ = (S)(_frameBuffer[0][2] * scale); - *dst++ = (S)(_frameBuffer[0][3] * scale); - *dst++ = (S)(_frameBuffer[0][4] * scale); - *dst++ = (S)(_frameBuffer[0][5] * scale); - *dst++ = (S)(_frameBuffer[0][6] * scale); - *dst++ = (S)(_frameBuffer[0][7] * scale); - *dst++ = (S)(_frameBuffer[0][8] * scale); - *dst++ = (S)(_frameBuffer[0][9] * scale); - *dst++ = (S)(_frameBuffer[0][10] * scale); - *dst++ = (S)(_frameBuffer[0][11] * scale); - *dst++ = (S)(_frameBuffer[0][12] * scale); - *dst++ = (S)(_frameBuffer[0][13] * scale); - *dst++ = (S)(_frameBuffer[0][14] * scale); - *dst++ = (S)(_frameBuffer[0][15] * scale); + *dst++ = (S)(_frameBuffer[0][i + 0] * scale); + *dst++ = (S)(_frameBuffer[0][i + 1] * scale); + *dst++ = (S)(_frameBuffer[0][i + 2] * scale); + *dst++ = (S)(_frameBuffer[0][i + 3] * scale); + *dst++ = (S)(_frameBuffer[0][i + 4] * scale); + *dst++ = (S)(_frameBuffer[0][i + 5] * scale); + *dst++ = (S)(_frameBuffer[0][i + 6] * scale); + *dst++ = (S)(_frameBuffer[0][i + 7] * scale); + *dst++ = (S)(_frameBuffer[0][i + 8] * scale); + *dst++ = (S)(_frameBuffer[0][i + 9] * scale); + *dst++ = (S)(_frameBuffer[0][i + 10] * scale); + *dst++ = (S)(_frameBuffer[0][i + 11] * scale); + *dst++ = (S)(_frameBuffer[0][i + 12] * scale); + *dst++ = (S)(_frameBuffer[0][i + 13] * scale); + *dst++ = (S)(_frameBuffer[0][i + 14] * scale); + *dst++ = (S)(_frameBuffer[0][i + 15] * scale); } } else if (_channelCount == 2) { for (uint32_t i = 0; i < _frameCount; i += 16) { - *dst++ = (S)(_frameBuffer[0][0] * scale); - *dst++ = (S)(_frameBuffer[1][0] * scale); - *dst++ = (S)(_frameBuffer[0][1] * scale); - *dst++ = (S)(_frameBuffer[1][1] * scale); - *dst++ = (S)(_frameBuffer[0][2] * scale); - *dst++ = (S)(_frameBuffer[1][2] * scale); - *dst++ = (S)(_frameBuffer[0][3] * scale); - *dst++ = (S)(_frameBuffer[1][3] * scale); - *dst++ = (S)(_frameBuffer[0][4] * scale); - *dst++ = (S)(_frameBuffer[1][4] * scale); - *dst++ = (S)(_frameBuffer[0][5] * scale); - *dst++ = (S)(_frameBuffer[1][5] * scale); - *dst++ = (S)(_frameBuffer[0][6] * scale); - *dst++ = (S)(_frameBuffer[1][6] * scale); - *dst++ = (S)(_frameBuffer[0][7] * scale); - *dst++ = (S)(_frameBuffer[1][7] * scale); - *dst++ = (S)(_frameBuffer[0][8] * scale); - *dst++ = (S)(_frameBuffer[1][8] * scale); - *dst++ = (S)(_frameBuffer[0][9] * scale); - *dst++ = (S)(_frameBuffer[1][9] * scale); - *dst++ = (S)(_frameBuffer[0][10] * scale); - *dst++ = (S)(_frameBuffer[1][10] * scale); - *dst++ = (S)(_frameBuffer[0][11] * scale); - *dst++ = (S)(_frameBuffer[1][11] * scale); - *dst++ = (S)(_frameBuffer[0][12] * scale); - *dst++ = (S)(_frameBuffer[1][12] * scale); - *dst++ = (S)(_frameBuffer[0][13] * scale); - *dst++ = (S)(_frameBuffer[1][13] * scale); - *dst++ = (S)(_frameBuffer[0][14] * scale); - *dst++ = (S)(_frameBuffer[1][14] * scale); - *dst++ = (S)(_frameBuffer[0][15] * scale); - *dst++ = (S)(_frameBuffer[1][15] * scale); + *dst++ = (S)(_frameBuffer[0][i + 0] * scale); + *dst++ = (S)(_frameBuffer[1][i + 0] * scale); + *dst++ = (S)(_frameBuffer[0][i + 1] * scale); + *dst++ = (S)(_frameBuffer[1][i + 1] * scale); + *dst++ = (S)(_frameBuffer[0][i + 2] * scale); + *dst++ = (S)(_frameBuffer[1][i + 2] * scale); + *dst++ = (S)(_frameBuffer[0][i + 3] * scale); + *dst++ = (S)(_frameBuffer[1][i + 3] * scale); + *dst++ = (S)(_frameBuffer[0][i + 4] * scale); + *dst++ = (S)(_frameBuffer[1][i + 4] * scale); + *dst++ = (S)(_frameBuffer[0][i + 5] * scale); + *dst++ = (S)(_frameBuffer[1][i + 5] * scale); + *dst++ = (S)(_frameBuffer[0][i + 6] * scale); + *dst++ = (S)(_frameBuffer[1][i + 6] * scale); + *dst++ = (S)(_frameBuffer[0][i + 7] * scale); + *dst++ = (S)(_frameBuffer[1][i + 7] * scale); + *dst++ = (S)(_frameBuffer[0][i + 8] * scale); + *dst++ = (S)(_frameBuffer[1][i + 8] * scale); + *dst++ = (S)(_frameBuffer[0][i + 9] * scale); + *dst++ = (S)(_frameBuffer[1][i + 9] * scale); + *dst++ = (S)(_frameBuffer[0][i + 10] * scale); + *dst++ = (S)(_frameBuffer[1][i + 10] * scale); + *dst++ = (S)(_frameBuffer[0][i + 11] * scale); + *dst++ = (S)(_frameBuffer[1][i + 11] * scale); + *dst++ = (S)(_frameBuffer[0][i + 12] * scale); + *dst++ = (S)(_frameBuffer[1][i + 12] * scale); + *dst++ = (S)(_frameBuffer[0][i + 13] * scale); + *dst++ = (S)(_frameBuffer[1][i + 13] * scale); + *dst++ = (S)(_frameBuffer[0][i + 14] * scale); + *dst++ = (S)(_frameBuffer[1][i + 14] * scale); + *dst++ = (S)(_frameBuffer[0][i + 15] * scale); + *dst++ = (S)(_frameBuffer[1][i + 15] * scale); } } } else { @@ -316,57 +316,57 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 if (_channelCount == 1) { for (uint32_t i = 0; i < _frameCount; i += 16) { - _frameBuffer[0][0] = *src++; - _frameBuffer[0][1] = *src++; - _frameBuffer[0][2] = *src++; - _frameBuffer[0][3] = *src++; - _frameBuffer[0][4] = *src++; - _frameBuffer[0][5] = *src++; - _frameBuffer[0][6] = *src++; - _frameBuffer[0][7] = *src++; - _frameBuffer[0][8] = *src++; - _frameBuffer[0][9] = *src++; - _frameBuffer[0][10] = *src++; - _frameBuffer[0][11] = *src++; - _frameBuffer[0][12] = *src++; - _frameBuffer[0][13] = *src++; - _frameBuffer[0][14] = *src++; - _frameBuffer[0][15] = *src++; + _frameBuffer[0][i + 0] = *src++; + _frameBuffer[0][i + 1] = *src++; + _frameBuffer[0][i + 2] = *src++; + _frameBuffer[0][i + 3] = *src++; + _frameBuffer[0][i + 4] = *src++; + _frameBuffer[0][i + 5] = *src++; + _frameBuffer[0][i + 6] = *src++; + _frameBuffer[0][i + 7] = *src++; + _frameBuffer[0][i + 8] = *src++; + _frameBuffer[0][i + 9] = *src++; + _frameBuffer[0][i + 10] = *src++; + _frameBuffer[0][i + 11] = *src++; + _frameBuffer[0][i + 12] = *src++; + _frameBuffer[0][i + 13] = *src++; + _frameBuffer[0][i + 14] = *src++; + _frameBuffer[0][i + 15] = *src++; } } else if (_channelCount == 2) { for (uint32_t i = 0; i < _frameCount; i += 16) { - _frameBuffer[0][0] = *src++; - _frameBuffer[1][0] = *src++; - _frameBuffer[0][1] = *src++; - _frameBuffer[1][1] = *src++; - _frameBuffer[0][2] = *src++; - _frameBuffer[1][2] = *src++; - _frameBuffer[0][3] = *src++; - _frameBuffer[1][3] = *src++; - _frameBuffer[0][4] = *src++; - _frameBuffer[1][4] = *src++; - _frameBuffer[0][5] = *src++; - _frameBuffer[1][5] = *src++; - _frameBuffer[0][6] = *src++; - _frameBuffer[1][6] = *src++; - _frameBuffer[0][7] = *src++; - _frameBuffer[1][7] = *src++; - _frameBuffer[0][8] = *src++; - _frameBuffer[1][8] = *src++; - _frameBuffer[0][9] = *src++; - _frameBuffer[1][9] = *src++; - _frameBuffer[0][10] = *src++; - _frameBuffer[1][10] = *src++; - _frameBuffer[0][11] = *src++; - _frameBuffer[1][11] = *src++; - _frameBuffer[0][12] = *src++; - _frameBuffer[1][12] = *src++; - _frameBuffer[0][13] = *src++; - _frameBuffer[1][13] = *src++; - _frameBuffer[0][14] = *src++; - _frameBuffer[1][14] = *src++; - _frameBuffer[0][15] = *src++; - _frameBuffer[1][15] = *src++; + _frameBuffer[0][i + 0] = *src++; + _frameBuffer[1][i + 0] = *src++; + _frameBuffer[0][i + 1] = *src++; + _frameBuffer[1][i + 1] = *src++; + _frameBuffer[0][i + 2] = *src++; + _frameBuffer[1][i + 2] = *src++; + _frameBuffer[0][i + 3] = *src++; + _frameBuffer[1][i + 3] = *src++; + _frameBuffer[0][i + 4] = *src++; + _frameBuffer[1][i + 4] = *src++; + _frameBuffer[0][i + 5] = *src++; + _frameBuffer[1][i + 5] = *src++; + _frameBuffer[0][i + 6] = *src++; + _frameBuffer[1][i + 6] = *src++; + _frameBuffer[0][i + 7] = *src++; + _frameBuffer[1][i + 7] = *src++; + _frameBuffer[0][i + 8] = *src++; + _frameBuffer[1][i + 8] = *src++; + _frameBuffer[0][i + 9] = *src++; + _frameBuffer[1][i + 9] = *src++; + _frameBuffer[0][i + 10] = *src++; + _frameBuffer[1][i + 10] = *src++; + _frameBuffer[0][i + 11] = *src++; + _frameBuffer[1][i + 11] = *src++; + _frameBuffer[0][i + 12] = *src++; + _frameBuffer[1][i + 12] = *src++; + _frameBuffer[0][i + 13] = *src++; + _frameBuffer[1][i + 13] = *src++; + _frameBuffer[0][i + 14] = *src++; + _frameBuffer[1][i + 14] = *src++; + _frameBuffer[0][i + 15] = *src++; + _frameBuffer[1][i + 15] = *src++; } } } else { @@ -386,57 +386,57 @@ inline void AudioFrameBuffer< T >::copyFrames(uint32_t channelCount, const uint3 if (_channelCount == 1) { for (uint32_t i = 0; i < _frameCount; i += 16) { - _frameBuffer[0][0] = ((T)(*src++)) / scale; - _frameBuffer[0][1] = ((T)(*src++)) / scale; - _frameBuffer[0][2] = ((T)(*src++)) / scale; - _frameBuffer[0][3] = ((T)(*src++)) / scale; - _frameBuffer[0][4] = ((T)(*src++)) / scale; - _frameBuffer[0][5] = ((T)(*src++)) / scale; - _frameBuffer[0][6] = ((T)(*src++)) / scale; - _frameBuffer[0][7] = ((T)(*src++)) / scale; - _frameBuffer[0][8] = ((T)(*src++)) / scale; - _frameBuffer[0][9] = ((T)(*src++)) / scale; - _frameBuffer[0][10] = ((T)(*src++)) / scale; - _frameBuffer[0][11] = ((T)(*src++)) / scale; - _frameBuffer[0][12] = ((T)(*src++)) / scale; - _frameBuffer[0][13] = ((T)(*src++)) / scale; - _frameBuffer[0][14] = ((T)(*src++)) / scale; - _frameBuffer[0][15] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 0] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 1] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 2] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 3] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 4] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 5] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 6] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 7] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 8] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 9] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 10] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 11] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 12] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 13] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 14] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 15] = ((T)(*src++)) / scale; } } else if (_channelCount == 2) { for (uint32_t i = 0; i < _frameCount; i += 16) { - _frameBuffer[0][0] = ((T)(*src++)) / scale; - _frameBuffer[1][0] = ((T)(*src++)) / scale; - _frameBuffer[0][1] = ((T)(*src++)) / scale; - _frameBuffer[1][1] = ((T)(*src++)) / scale; - _frameBuffer[0][2] = ((T)(*src++)) / scale; - _frameBuffer[1][2] = ((T)(*src++)) / scale; - _frameBuffer[0][3] = ((T)(*src++)) / scale; - _frameBuffer[1][3] = ((T)(*src++)) / scale; - _frameBuffer[0][4] = ((T)(*src++)) / scale; - _frameBuffer[1][4] = ((T)(*src++)) / scale; - _frameBuffer[0][5] = ((T)(*src++)) / scale; - _frameBuffer[1][5] = ((T)(*src++)) / scale; - _frameBuffer[0][6] = ((T)(*src++)) / scale; - _frameBuffer[1][6] = ((T)(*src++)) / scale; - _frameBuffer[0][7] = ((T)(*src++)) / scale; - _frameBuffer[1][7] = ((T)(*src++)) / scale; - _frameBuffer[0][8] = ((T)(*src++)) / scale; - _frameBuffer[1][8] = ((T)(*src++)) / scale; - _frameBuffer[0][9] = ((T)(*src++)) / scale; - _frameBuffer[1][9] = ((T)(*src++)) / scale; - _frameBuffer[0][10] = ((T)(*src++)) / scale; - _frameBuffer[1][10] = ((T)(*src++)) / scale; - _frameBuffer[0][11] = ((T)(*src++)) / scale; - _frameBuffer[1][11] = ((T)(*src++)) / scale; - _frameBuffer[0][12] = ((T)(*src++)) / scale; - _frameBuffer[1][12] = ((T)(*src++)) / scale; - _frameBuffer[0][13] = ((T)(*src++)) / scale; - _frameBuffer[1][13] = ((T)(*src++)) / scale; - _frameBuffer[0][14] = ((T)(*src++)) / scale; - _frameBuffer[1][14] = ((T)(*src++)) / scale; - _frameBuffer[0][15] = ((T)(*src++)) / scale; - _frameBuffer[1][15] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 0] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 0] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 1] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 1] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 2] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 2] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 3] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 3] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 4] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 4] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 5] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 5] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 6] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 6] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 7] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 7] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 8] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 8] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 9] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 9] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 10] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 10] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 11] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 11] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 12] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 12] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 13] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 13] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 14] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 14] = ((T)(*src++)) / scale; + _frameBuffer[0][i + 15] = ((T)(*src++)) / scale; + _frameBuffer[1][i + 15] = ((T)(*src++)) / scale; } } } else { diff --git a/libraries/audio/src/AudioEditBuffer.h b/libraries/audio/src/AudioEditBuffer.h index f93d338e47..b773fedfb8 100644 --- a/libraries/audio/src/AudioEditBuffer.h +++ b/libraries/audio/src/AudioEditBuffer.h @@ -57,8 +57,7 @@ inline bool AudioEditBuffer::getZeroCrossing(uint32_t start, bool direction, } } } - } - else { // scan from the right + } else { // scan from the right if (start != 0 && start < this->_frameCount) { for (uint32_t i = start; i != 0; --i) { for (uint32_t j = 0; j < this->_channelCount; ++j) {