diff data/fileio/CodedAudioFileReader.cpp @ 933:d03b3d956358 warnfix_no_size_t

Merge from branch tony_integration
author Chris Cannam
date Wed, 18 Jun 2014 08:34:46 +0100
parents 59e7fe1b1003 9911b8cb53bc
children cc27f35aa75c
line wrap: on
line diff
--- a/data/fileio/CodedAudioFileReader.cpp	Tue Jun 17 16:42:51 2014 +0100
+++ b/data/fileio/CodedAudioFileReader.cpp	Wed Jun 18 08:34:46 2014 +0100
@@ -28,7 +28,8 @@
 #include <QMutexLocker>
 
 CodedAudioFileReader::CodedAudioFileReader(CacheMode cacheMode,
-                                           int targetRate) :
+                                           int 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, int sz)
 {
-    float max = 1.0;
+    float clip = 1.0;
     int count = sz * m_channelCount;
 
-    for (int i = 0; i < count; ++i) {
-        if (buffer[i] >  max) buffer[i] =  max;
-    }
-    for (int i = 0; i < count; ++i) {
-        if (buffer[i] < -max) buffer[i] = -max;
+    if (m_normalised) {
+        for (int 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 (int i = 0; i < count; ++i) {
+            if (buffer[i] >  clip) buffer[i] =  clip;
+        }
+        for (int 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;
+        }
+    }
 }