8000 Changed to Float64Array · JavaScriptIOT/dsp.js@af45825 · GitHub
[go: up one dir, main page]

Skip to content

Commit af45825

Browse files
committed
Changed to Float64Array
Float32Arrays do not represent numbers as accurately (Ref: http://stackoverflow.com/questions/8542414/javascript-float32array-wierdness). It results in noticable difference when converting the FFT output to dB scale.
1 parent a7b2e97 commit af45825

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

dsp.js

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function setupTypedArray(name, fallback) {
7474
}
7575
}
7676

77-
setupTypedArray("Float32Array", "WebGLFloatArray");
77+
setupTypedArray("Float64Array", "WebGLFloatArray");
7878
setupTypedArray("Int32Array", "WebGLIntArray");
7979
setupTypedArray("Uint16Array", "WebGLUnsignedShortArray");
8080
setupTypedArray("Uint8Array", "WebGLUnsignedByteArray");
@@ -112,7 +112,7 @@ DSP.interleave = function(left, right) {
112112
throw "Can not interleave. Channel lengths differ.";
113113
}
114114

115-
var stereoInterleaved = new Float32Array(left.length * 2);
115+
var stereoInterleaved = new Float64Array(left.length * 2);
116116

117117
for (var i = 0, len = left.length; i < len; i++) {
118118
stereoInterleaved[2*i] = left[i];
@@ -154,14 +154,14 @@ DSP.deinterleave = (function() {
154154
};
155155

156156
return function(channel, buffer) {
157-
left = left || new Float32Array(buffer.length/2);
158-
right = right || new Float32Array(buffer.length/2);
159-
mix = mix || new Float32Array(buffer.length/2);
157+
left = left || new Float64Array(buffer.length/2);
158+
right = right || new Float64Array(buffer.length/2);
159+
mix = mix || new Float64Array(buffer.length/2);
160160

161161
if (buffer.length/2 !== left.length) {
162-
left = new Float32Array(buffer.length/2);
163-
right = new Float32Array(buffer.length/2);
164-
mix = new Float32Array(buffer.length/2);
162+
left = new Float64Array(buffer.length/2);
163+
right = new Float64Array(buffer.length/2);
164+
mix = new Float64Array(buffer.length/2);
165165
}
166166

167167
return deinterleaveChannel[channel](buffer);
@@ -183,15 +183,15 @@ DSP.getChannel = DSP.deinterleave;
183183
* to negate the second buffer while mixing and to perform a volume correction
184184
* on the final signal.
185185
*
186-
* @param {Array} sampleBuffer1 Array containing Float values or a Float32Array
187-
* @param {Array} sampleBuffer2 Array containing Float values or a Float32Array
186+
* @param {Array} sampleBuffer1 Array containing Float values or a Float64Array
187+
* @param {Array} sampleBuffer2 Array containing Float values or a Float64Array
188188
* @param {Boolean} negate When true inverts/flips the audio signal
189189
* @param {Number} volumeCorrection When you add multiple sample buffers, use this to tame your signal ;)
190190
*
191-
* @returns A new Float32Array interleaved buffer.
191+
* @returns A new Float64Array interleaved buffer.
192192
*/
193193
DSP.mixSampleBuffers = function(sampleBuffer1, sampleBuffer2, negate, volumeCorrection){
194-
var outputSamples = new Float32Array(sampleBuffer1);
194+
var outputSamples = new Float64Array(sampleBuffer1);
195195

196196
for(var i = 0; i<sampleBuffer1.length; i++){
197197
outputSamples[i] += (negate ? -sampleBuffer2[i] : sampleBuffer2[i]) / volumeCorrection;
@@ -244,9 +244,9 @@ function FourierTransform(bufferSize, sampleRate) {
244244
this.sampleRate = sampleRate;
245245
this.bandwidth = 2 / bufferSize * sampleRate / 2;
246246

247-
this.spectrum = new Float32Array(bufferSize/2);
248-
this.real = new Float32Array(bufferSize);
249-
this.imag = new Float32Array(bufferSize);
247+
this.spectrum = new Float64Array(bufferSize/2);
248+
this.real = new Float64Array(bufferSize);
249+
this.imag = new Float64Array(bufferSize);
250250

251251
this.peakBand = 0;
252252
this.peak = 0;
@@ -301,8 +301,8 @@ function DFT(bufferSize, sampleRate) {
301301
var N = bufferSize/2 * bufferSize;
302302
var TWO_PI = 2 * Math.PI;
303303

304-
this.sinTable = new Float32Array(N);
305-
this.cosTable = new Float32Array(N);
304+
this.sinTable = new Float64Array(N);
305+
this.cosTable = new Float64Array(N);
306306

307307
for (var i = 0; i < N; i++) {
308308
this.sinTable[i] = Math.sin(i * TWO_PI / bufferSize);
@@ -369,8 +369,8 @@ function FFT(bufferSize, sampleRate) {
369369
bit = bit >> 1;
370370
}
371371

372-
this.sinTable = new Float32Array(bufferSize);
373-
this.cosTable = new Float32Array(bufferSize);
372+
this.sinTable = new Float64Array(bufferSize);
373+
this.cosTable = new Float64Array(bufferSize);
374374

375375
for (i = 0; i < bufferSize; i++) {
376376
this.sinTable[i] = Math.sin(-Math.PI/i);
@@ -479,8 +479,8 @@ FFT.prototype.inverse = function(real, imag) {
479479
imag[i] *= -1;
480480
}
481481

482-
var revReal = new Float32Array(bufferSize);
483-
var revImag = new Float32Array(bufferSize);
482+
var revReal = new Float64Array(bufferSize);
483+
var revImag = new Float64Array(bufferSize);
484484

485485
for (i = 0; i < real.length; i++) {
486486
revReal[i] = real[reverseTable[i]];
@@ -520,7 +520,7 @@ FFT.prototype.inverse = function(real, imag) {
520520
halfSize = halfSize << 1;
521521
}
522522

523-
var buffer = new Float32Array(bufferSize); // this should be reused instead
523+
var buffer = new Float64Array(bufferSize); // this should be reused instead
524524
for (i = 0; i < bufferSize; i++) {
525525
buffer[i] = real[i] / bufferSize;
526526
}
@@ -554,7 +554,7 @@ FFT.prototype.inverse = function(real, imag) {
554554
function RFFT(bufferSize, sampleRate) {
555555
FourierTransform.call(this, bufferSize, sampleRate);
556556

557-
this.trans = new Float32Array(bufferSize);
557+
this.trans = new Float64Array(bufferSize);
558558

559559
this.reverseTable = new Uint32Array(bufferSize);
560560

@@ -838,7 +838,7 @@ function Sampler(file, bufferSize, sampleRate, playStart, playEnd, loopStart, lo
838838
this.loopMode = loopMode || DSP.OFF;
839839
this.loaded = false;
840840
this.samples = [];
841-
this.signal = new Float32Array(bufferSize);
841+
this.signal = new Float64Array(bufferSize);
842842
this.frameCount = 0;
843843
this.envelope = null;
844844
this.amplitude = 1;
@@ -861,7 +861,7 @@ function Sampler(file, bufferSize, sampleRate, playStart, playEnd, loopStart, lo
861861

862862
this.loadComplete = function() {
863863
// convert flexible js array into a fast typed array
864-
self.samples = new Float32Array(self.samples);
864+
self.samples = new Float64Array(self.samples);
865865
self.loaded = true;
866866
};
867867

@@ -969,7 +969,7 @@ function Oscillator(type, frequency, amplitude, bufferSize, sampleRate) {
969969

970970
this.cyclesPerSample = frequency / sampleRate;
971971

972-
this.signal = new Float32Array(bufferSize);
972+
this.signal = new Float64Array(bufferSize);
973973
this.envelope = null;
974974

975975
switch(parseInt(type, 10)) {
@@ -992,7 +992,7 @@ function Oscillator(type, frequency, amplitude, bufferSize, sampleRate) {
992992
}
993993

994994
this.generateWaveTable = function() {
995-
Oscillator.waveTable[this.func] = new Float32Array(2048);
995+
Oscillator.waveTable[this.func] = new Float64Array(2048);
996996
var waveTableTime = this.waveTableLength / this.sampleRate;
997997
var waveTableHz = 1 / waveTableTime;
998998

@@ -1310,7 +1310,7 @@ function IIRFilter2(type, cutoff, resonance, sampleRate) {
13101310
this.resonance = resonance;
13111311
this.sampleRate = sampleRate;
13121312

1313-
this.f = Float32Array(4);
1313+
this.f = Float64Array(4);
13141314
this.f[0] = 0.0; // lp
13151315
this.f[1] = 0.0; // hp
13161316
this.f[2] = 0.0; // bp
@@ -1720,7 +1720,7 @@ function Biquad(type, sampleRate) {
17201720
// - (a1/a0)*y[n-1] - (a2/a0)*y[n-2]
17211721

17221722
var len = buffer.length;
1723-
var output = new Float32Array(len);
1723+
var output = new Float64Array(len);
17241724

17251725
for ( var i=0; i<buffer.length; i++ ) {
17261726
output[i] = this.b0a0*buffer[i] + this.b1a0*this.x_1_l + this.b2a0*this.x_2_l - this.a1a0*this.y_1_l - this.a2a0*this.y_2_l;
@@ -1738,7 +1738,7 @@ function Biquad(type, sampleRate) {
17381738
// - (a1/a0)*y[n-1] - (a2/a0)*y[n-2]
17391739

17401740
var len = buffer.length;
1741-
var output = new Float32Array(len);
1741+
var output = new Float64Array(len);
17421742

17431743
for (var i = 0; i < len/2; i++) {
17441744
output[2*i] = this.b0a0*buffer[2*i] + this.b1a0*this.x_1_l + this.b2a0*this.x_2_l - this.a1a0*this.y_1_l - this.a2a0*this.y_2_l;
@@ -1776,7 +1776,7 @@ DSP.mag2db = function(buffer) {
17761776
var log = Math.log;
17771777
var max = Math.max;
17781778

1779-
var result = Float32Array(buffer.length);
1779+
var result = Float64Array(buffer.length);
17801780
for (var i=0; i<buffer.length; i++) {
17811781
result[i] = 20.0*log(max(buffer 1241 [i], minMag));
17821782
}
@@ -1803,13 +1803,13 @@ DSP.freqz = function(b, a, w) {
18031803
var i, j;
18041804

18051805
if (!w) {
1806-
w = Float32Array(200);
1806+
w = Float64Array(200);
18071807
for (i=0;i<w.length; i++) {
18081808
w[i] = DSP.TWO_PI/w.length * i - Math.PI;
18091809
}
18101810
}
18111811

1812-
var result = Float32Array(w.length);
1812+
var result = Float64Array(w.length);
18131813

18141814
var sqrt = Math.sqrt;
18151815
var cos = Math.cos;
@@ -1909,7 +1909,7 @@ function GraphicalEq(sampleRate) {
19091909
}
19101910

19111911
if (!this.w) {
1912-
this.w = Float32Array(400);
1912+
this.w = Float64Array(400);
19131913
for (var i=0; i<this.w.length; i++) {
19141914
this.w[i] = Math.PI/this.w.length * i;
19151915
}
@@ -1961,7 +1961,7 @@ function GraphicalEq(sampleRate) {
19611961
* @constructor
19621962
*/
19631963
function MultiDelay(maxDelayInSamplesSize, delayInSamples, masterVolume, delayVolume) {
1964-
this.delayBufferSamples = new Float32Array(maxDelayInSamplesSize); // The maximum size of delay
1964+
this.delayBufferSamples = new Float64Array(maxDelayInSamplesSize); // The maximum size of delay
19651965
this.delayInputPointer = delayInSamples;
19661966
this.delayOutputPointer = 0;
19671967

@@ -2006,13 +2006,13 @@ MultiDelay.prototype.setDelayVolume = function(delayVolume) {
20062006
/**
20072007
* Process a given interleaved or mono non-interleaved float value Array and adds the delayed audio.
20082008
*
2009-
* @param {Array} samples Array containing Float values or a Float32Array
2009+
* @param {Array} samples Array containing Float values or a Float64Array
20102010
*
2011-
* @returns A new Float32Array interleaved or mono non-interleaved as was fed to this function.
2011+
* @returns A new Float64Array interleaved or mono non-interleaved as was fed to this function.
20122012
*/
20132013
MultiDelay.prototype.process = function(samples) {
20142014
// NB. Make a copy to put in the output samples to return.
2015-
var outputSamples = new Float32Array(samples.length);
2015+
var outputSamples = new Float64Array(samples.length);
20162016

20172017
for (var i=0; i<samples.length; i++) {
20182018
// delayBufferSamples could contain initial NULL's, return silence in that case
@@ -2062,7 +2062,7 @@ MultiDelay.prototype.process = function(samples) {
20622062
*/
20632063

20642064
function SingleDelay(maxDelayInSamplesSize, delayInSamples, delayVolume) {
2065-
this.delayBufferSamples = new Float32Array(maxDelayInSamplesSize); // The maximum size of delay
2065+
this.delayBufferSamples = new Float64Array(maxDelayInSamplesSize); // The maximum size of delay
20662066
this.delayInputPointer = delayInSamples;
20672067
this.delayOutputPointer = 0;
20682068

@@ -2097,13 +2097,13 @@ SingleDelay.prototype.setDelayVolume = function(delayVolume) {
20972097
* Process a given interleaved or mono non-interleaved float value Array and
20982098
* returns the delayed audio.
20992099
*
2100-
* @param {Array} samples Array containing Float values or a Float32Array
2100+
* @param {Array} samples Array containing Float values or a Float64Array
21012101
*
2102-
* @returns A new Float32Array interleaved or mono non-interleaved as was fed to this function.
2102+
* @returns A new Float64Array interleaved or mono non-interleaved as was fed to this function.
21032103
*/
21042104
SingleDelay.prototype.process = function(samples) {
21052105
// NB. Make a copy to put in the output samples to return.
2106-
var outputSamples = new Float32Array(samples.length);
2106+
var outputSamples = new Float64Array(samples.length);
21072107

21082108
for (var i=0; i<samples.length; i++) {
21092109

@@ -2255,13 +2255,13 @@ Reverb.prototype.setDampFrequency = function (dampFrequency){
22552255
/**
22562256
* Process a given interleaved float value Array and copies and adds the reverb signal.
22572257
*
2258-
* @param {Array} samples Array containing Float values or a Float32Array
2258+
* @param {Array} samples Array containing Float values or a Float64Array
22592259
*
2260-
* @returns A new Float32Array interleaved buffer.
2260+
* @returns A new Float64Array interleaved buffer.
22612261
*/
22622262
Reverb.prototype.process = function (interleavedSamples){
22632263
// NB. Make a copy to put in the output samples to return.
2264-
var outputSamples = new Float32Array(interleavedSamples.length);
2264+
var outputSamples = new Float64Array(interleavedSamples.length);
22652265

22662266
// Perform low pass on the input samples to mimick damp
22672267
var leftRightMix = DSP.deinterleave(interleavedSamples);
@@ -2278,7 +2278,7 @@ Reverb.prototype.process = function (interleavedSamples){
22782278
}
22792279

22802280
// Process SingleDelays in series
2281-
var singleDelaySamples = new Float32Array(outputSamples.length);
2281+
var singleDelaySamples = new Float64Array(outputSamples.length);
22822282
for (i = 0; i<this.NR_OF_SINGLEDELAYS; i++) {
22832283
// Invert the signal of every even singleDelay
22842284
singleDelaySamples = DSP.mixSampleBuffers(singleDelaySamples, this.singleDelays[i].process(outputSamples), 2%i === 0, 1);

0 commit comments

Comments
 (0)
0