changeset 34:76ea8ffc4dfa

* Fix mixdown for multi-channel source -> 1-channel plugin
author Chris Cannam
date Mon, 24 May 2010 15:06:30 +0000
parents 44dc87d1059d
children c1b3af62bc2a
files runner/FeatureExtractionManager.cpp
diffstat 1 files changed, 29 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/runner/FeatureExtractionManager.cpp	Fri May 21 10:21:33 2010 +0000
+++ b/runner/FeatureExtractionManager.cpp	Mon May 24 15:06:30 2010 +0000
@@ -429,7 +429,7 @@
 
     cerr << "Opened " << channels << "-channel file or URL \"" << audioSource.toStdString() << "\"" << endl;
 
-    // reject file if it has too few channels, plugin will handle if it has too many
+    // reject file if it has too few channels
     if ((int)channels < m_channels) {
         delete reader;
         throw FileOperationFailed
@@ -574,24 +574,40 @@
         // leave it to the plugin adapter because the same plugin
         // adapter may have to serve for input files with various
         // numbers of channels (so the adapter is simply configured
-        // with a fixed channel count, generally 1).
+        // with a fixed channel count).
 
         int rc = reader->getChannelCount();
 
-        for (int j = 0; j < m_blockSize; ++j) {
-            for (int c = 0; c < m_channels; ++c) {
-                int index;
-                if (c < rc) {
+        // m_channels is the number of channels we need for the plugin
+
+        int index;
+        int fc = (int)frames.size();
+        if (m_channels == 1) { // only case in which we can sensibly mix down
+            for (int j = 0; j < m_blockSize; ++j) {
+                data[0][j] = 0.f;
+            }
+            for (int c = 0; c < rc; ++c) {
+                for (int j = 0; j < m_blockSize; ++j) {
                     index = j * rc + c;
-                    data[c][j] = 0.f;
-                } else {
-                    index = j * rc + (c % rc);
-                }
-                if (index < (int)frames.size()) {
-                    data[c][j] += frames[index];
+                    if (index < fc) data[0][j] += frames[index];
                 }
             }
-        }    
+            for (int j = 0; j < m_blockSize; ++j) {
+                data[0][j] /= rc;
+            }
+        } else {                
+            for (int c = 0; c < m_channels; ++c) {
+                for (int j = 0; j < m_blockSize; ++j) {
+                    data[c][j] = 0.f;
+                }
+                if (c < rc) {
+                    for (int j = 0; j < m_blockSize; ++j) {
+                        index = j * rc + c;
+                        if (index < fc) data[c][j] += frames[index];
+                    }
+                }
+            }
+        }                
 
         Vamp::RealTime timestamp = Vamp::RealTime::frame2RealTime
             (i, m_sampleRate);