# HG changeset patch # User Chris Cannam # Date 1490170726 0 # Node ID fab88270bcccedf4061b9b872e73229778c819e8 # Parent 30027e4713384cc81c6ed05dd1e81a417239c3bb Multi-channel spectrogram diff -r 30027e471338 -r fab88270bccc src/app/spectrogram/Spectrogram.ts --- 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); }