changeset 162:fab88270bccc

Multi-channel spectrogram
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 22 Mar 2017 08:18:46 +0000
parents 30027e471338
children 8c882ee9d097
files src/app/spectrogram/Spectrogram.ts
diffstat 1 files changed, 23 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/app/spectrogram/Spectrogram.ts	Wed Mar 22 08:10:18 2017 +0000
+++ b/src/app/spectrogram/Spectrogram.ts	Wed Mar 22 08:18:46 2017 +0000
@@ -79,7 +79,7 @@
     const defaults = {
       normalise: 'hybrid',
       gain: 40.0,
-      channel: 0,
+      channel: -1,
       stepSize: 512,
       blockSize: 1024
     };
@@ -87,13 +87,29 @@
     const mergedOptions: Framing & Object & {channel: number} =
       Object.assign({}, defaults, options);
 
+    const getSamples = ((buffer, channel) => {
+      const nch = buffer.numberOfChannels;
+      if (channel >= 0 || nch == 1) {
+	return buffer.getChannelData(channel);
+      } else {
+	console.log("mixing down " + nch + " channels for spectrogram...");
+	const mixed = Float32Array.from(buffer.getChannelData(0));
+	const n = mixed.length;
+	for (let ch = 1; ch < nch; ++ch) {
+	  const buf = buffer.getChannelData(ch);
+	  for (let i = 0; i < n; ++i) mixed[i] += buf[i];
+	}
+	const scale = 1.0 / nch;
+	for (let i = 0; i < n; ++i) mixed[i] *= scale;
+	console.log("done");
+	return mixed;
+      }
+    });
+    
     super('entity',
-      new SpectrogramEntity(
-        buffer.getChannelData(mergedOptions.channel),
-        mergedOptions
-      ),
-      mergedOptions
-    );
+	  new SpectrogramEntity(getSamples(buffer, mergedOptions.channel),
+				mergedOptions),
+	  mergedOptions);
 
     this.configureShape(Waves.shapes.Matrix, {}, mergedOptions);
   }