diff js/core.js @ 2642:77204f78386a

#3: Added resampler from JS-xtract into resampler.js. Automatically use to match sample rates
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Thu, 19 Jan 2017 14:48:02 +0000
parents e3cf3c24149e
children 52e51b15f808
line wrap: on
line diff
--- a/js/core.js	Thu Jan 19 13:12:31 2017 +0000
+++ b/js/core.js	Thu Jan 19 14:48:02 2017 +0000
@@ -1507,6 +1507,22 @@
             function processAudio(response) {
                 var self = this;
                 return audioContext.decodeAudioData(response, function (decodedData) {
+                    if (decodedData.sampleRate !== audioContext.sampleRate) {
+                        // Resample
+                        var num_channels = decodedData.numberOfChannels,
+                            num_samples = decodedData.length,
+                            r = audioContext.sampleRate / decodedData.sampleRate,
+                            new_buffer = audioContext.createBuffer(num_channels, Math.floor(num_samples * r), audioContext.sampleRate),
+                            channel;
+                        for (channel = 0; channel < num_channels; channel++) {
+                            var buffer = new Float32Array(decodedData.length);
+                            decodedData.copyFromChannel(buffer, channel);
+                            buffer = xtract_resample(buffer, audioContext.sampleRate, decodedData.sampleRate);
+                            new_buffer.copyToChannel(buffer, channel);
+                        }
+                        decodedData = new_buffer;
+                        buffer = new_buffer = undefined;
+                    }
                     self.buffer = decodedData;
                     self.status = 2;
                     calculateLoudness(self, "I");