Mercurial > hg > svcore
diff data/fileio/CodedAudioFileReader.cpp @ 920:f3cda3280398 tonioni
Add normalised option to CodedAudioFileReader
author | Chris Cannam |
---|---|
date | Fri, 13 Jun 2014 16:03:48 +0100 |
parents | f5cd33909744 |
children | 2896b8872834 |
line wrap: on
line diff
--- a/data/fileio/CodedAudioFileReader.cpp Fri Jun 13 12:56:31 2014 +0100 +++ b/data/fileio/CodedAudioFileReader.cpp Fri Jun 13 16:03:48 2014 +0100 @@ -28,7 +28,8 @@ #include <QMutexLocker> CodedAudioFileReader::CodedAudioFileReader(CacheMode cacheMode, - size_t targetRate) : + size_t targetRate, + bool normalised) : m_cacheMode(cacheMode), m_initialised(false), m_serialiser(0), @@ -40,9 +41,12 @@ m_cacheWriteBufferSize(16384), m_resampler(0), m_resampleBuffer(0), - m_fileFrameCount(0) + m_fileFrameCount(0), + m_normalised(normalised), + m_max(0.f), + m_gain(1.f) { - SVDEBUG << "CodedAudioFileReader::CodedAudioFileReader: rate " << targetRate << endl; + SVDEBUG << "CodedAudioFileReader::CodedAudioFileReader: rate " << targetRate << ", normalised = " << normalised << endl; m_frameCount = 0; m_sampleRate = targetRate; @@ -270,11 +274,9 @@ return; } -// if (m_cacheWriteBufferIndex > 0) { - pushBuffer(m_cacheWriteBuffer, - m_cacheWriteBufferIndex / m_channelCount, - true); -// } + pushBuffer(m_cacheWriteBuffer, + m_cacheWriteBufferIndex / m_channelCount, + true); delete[] m_cacheWriteBuffer; m_cacheWriteBuffer = 0; @@ -312,14 +314,24 @@ void CodedAudioFileReader::pushBufferNonResampling(float *buffer, size_t sz) { - float max = 1.0; + float clip = 1.0; size_t count = sz * m_channelCount; - for (size_t i = 0; i < count; ++i) { - if (buffer[i] > max) buffer[i] = max; - } - for (size_t i = 0; i < count; ++i) { - if (buffer[i] < -max) buffer[i] = -max; + if (m_normalised) { + for (size_t i = 0; i < count; ++i) { + float v = fabsf(buffer[i]); + if (v > m_max) { + m_max = v; + m_gain = 1.f / m_max; + } + } + } else { + for (size_t i = 0; i < count; ++i) { + if (buffer[i] > clip) buffer[i] = clip; + } + for (size_t i = 0; i < count; ++i) { + if (buffer[i] < -clip) buffer[i] = -clip; + } } m_frameCount += sz; @@ -432,5 +444,11 @@ m_dataLock.unlock(); } } + + if (m_normalised) { + for (int i = 0; i < (int)(count * m_channelCount); ++i) { + frames[i] *= m_gain; + } + } }