-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
705 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright (c) 2019 Rafael da Silva Rocha. | ||
* Copyright (c) 2014 Florian Markert | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
|
||
/** | ||
* @fileoverview Butterworth LPF. Based on the Butterworth LPF from Fili.js. | ||
* @see https://github.com/rochars/wavefile | ||
* @see https://github.com/markert/fili.js | ||
*/ | ||
|
||
/** | ||
* Butterworth LPF. | ||
*/ | ||
export class ButterworthLPF { | ||
|
||
/** | ||
* @param {number} order The order of the filter. | ||
* @param {number} sampleRate The sample rate. | ||
* @param {number} cutOff The cut off frequency. | ||
*/ | ||
constructor(order, sampleRate, cutOff) { | ||
let filters = []; | ||
for (let i = 0; i < order; i++) { | ||
filters.push(this.getCoeffs_({ | ||
Fs: sampleRate, | ||
Fc: cutOff, | ||
Q: 0.5 / (Math.sin((Math.PI / (order * 2)) * (i + 0.5))) | ||
})); | ||
} | ||
this.stages = []; | ||
for (let i = 0; i < filters.length; i++) { | ||
this.stages[i] = { | ||
b0 : filters[i].b[0], | ||
b1 : filters[i].b[1], | ||
b2 : filters[i].b[2], | ||
a1 : filters[i].a[0], | ||
a2 : filters[i].a[1], | ||
k : filters[i].k, | ||
z : [0, 0] | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* @param {number} sample A sample of a sequence. | ||
* @return {number} | ||
*/ | ||
filter(sample) { | ||
let out = sample; | ||
for (let i = 0, len = this.stages.length; i < len; i++) { | ||
out = this.runStage_(i, out); | ||
} | ||
return out; | ||
} | ||
|
||
getCoeffs_(params) { | ||
let coeffs = {}; | ||
coeffs.z = [0, 0]; | ||
coeffs.a = []; | ||
coeffs.b = []; | ||
let p = this.preCalc_(params, coeffs); | ||
coeffs.k = 1; | ||
coeffs.b.push((1 - p.cw) / (2 * p.a0)); | ||
coeffs.b.push(2 * coeffs.b[0]); | ||
coeffs.b.push(coeffs.b[0]); | ||
return coeffs; | ||
} | ||
|
||
preCalc_(params, coeffs) { | ||
let pre = {}; | ||
let w = 2 * Math.PI * params.Fc / params.Fs; | ||
pre.alpha = Math.sin(w) / (2 * params.Q); | ||
pre.cw = Math.cos(w); | ||
pre.a0 = 1 + pre.alpha; | ||
coeffs.a0 = pre.a0; | ||
coeffs.a.push((-2 * pre.cw) / pre.a0); | ||
coeffs.k = 1; | ||
coeffs.a.push((1 - pre.alpha) / pre.a0); | ||
return pre; | ||
} | ||
|
||
runStage_(i, input) { | ||
let temp = | ||
input * this.stages[i].k - this.stages[i].a1 * this.stages[i].z[0] - | ||
this.stages[i].a2 * this.stages[i].z[1]; | ||
let out = | ||
this.stages[i].b0 * temp + this.stages[i].b1 * this.stages[i].z[0] + | ||
this.stages[i].b2 * this.stages[i].z[1]; | ||
this.stages[i].z[1] = this.stages[i].z[0]; | ||
this.stages[i].z[0] = temp; | ||
return out; | ||
} | ||
|
||
/** | ||
* Reset the filter. | ||
*/ | ||
reset() { | ||
for (let i = 0; i < this.stages.length; i++) { | ||
this.stages[i].z = [0, 0]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+47 KB
test/files/out/to-sample-rate/cubic-IIR-16-bit-16001kHz-noBext-mono.wav
Binary file not shown.
Binary file added
BIN
+250 KB
test/files/out/to-sample-rate/cubic-IIR-16bit-16000Hz-mono-chirp-1-22050-linear.wav
Binary file not shown.
Binary file added
BIN
+250 KB
test/files/out/to-sample-rate/cubic-IIR-16bit-16000Hz-mono-chirp-1-22050-log.wav
Binary file not shown.
Binary file added
BIN
+1.46 MB
test/files/out/to-sample-rate/cubic-IIR-16bit-96000Hz-mono-chirp-1-22050-linear.wav
Binary file not shown.
Binary file added
BIN
+1.46 MB
test/files/out/to-sample-rate/cubic-IIR-16bit-96000Hz-mono-chirp-1-22050-log.wav
Binary file not shown.
Binary file added
BIN
+500 KB
test/files/out/to-sample-rate/cubic-IIR-32fp-16000Hz-mono-chirp-1-22050-linear.wav
Binary file not shown.
Binary file added
BIN
+500 KB
test/files/out/to-sample-rate/cubic-IIR-32fp-16000Hz-mono-chirp-1-22050-log.wav
Binary file not shown.
Binary file added
BIN
+2.93 MB
test/files/out/to-sample-rate/cubic-IIR-32fp-96000Hz-mono-chirp-1-22050-linear.wav
Binary file not shown.
Binary file added
BIN
+2.93 MB
test/files/out/to-sample-rate/cubic-IIR-32fp-96000Hz-mono-chirp-1-22050-log.wav
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+250 KB
test/files/out/to-sample-rate/sinc-IIR-16bit-16000Hz-mono-chirp-1-22050-linear.wav
Binary file not shown.
Binary file added
BIN
+250 KB
test/files/out/to-sample-rate/sinc-IIR-16bit-16000Hz-mono-chirp-1-22050-log.wav
Binary file not shown.
Binary file added
BIN
+1.46 MB
test/files/out/to-sample-rate/sinc-IIR-16bit-96000Hz-mono-chirp-1-22050-linear.wav
Binary file not shown.
Binary file added
BIN
+1.46 MB
test/files/out/to-sample-rate/sinc-IIR-16bit-96000Hz-mono-chirp-1-22050-log.wav
Binary file not shown.
Binary file added
BIN
+500 KB
test/files/out/to-sample-rate/sinc-IIR-32fp-16000Hz-mono-chirp-1-22050-linear.wav
Binary file not shown.
Binary file added
BIN
+500 KB
test/files/out/to-sample-rate/sinc-IIR-32fp-16000Hz-mono-chirp-1-22050-log.wav
Binary file not shown.
Binary file added
BIN
+2.93 MB
test/files/out/to-sample-rate/sinc-IIR-32fp-96000Hz-mono-chirp-1-22050-linear.wav
Binary file not shown.
Binary file added
BIN
+2.93 MB
test/files/out/to-sample-rate/sinc-IIR-32fp-96000Hz-mono-chirp-1-22050-log.wav
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/** | ||
* WaveFile: https://github.com/rochars/wavefile | ||
* Copyright (c) 2017-2018 Rafael da Silva Rocha. MIT License. | ||
* | ||
* Tests for sample rate conversion, cubic method. | ||
* | ||
*/ | ||
|
||
const assert = require('assert'); | ||
const fs = require("fs"); | ||
const WaveFile = require("../../test/loader.js"); | ||
const path = "./test/files/"; | ||
|
||
console.log('cubic + IIR'); | ||
|
||
let hrstart = process.hrtime(); | ||
|
||
// Chirps, FP | ||
|
||
describe('Downsample a 32-bit 44.1kHz log sine sweep', function() { | ||
// Read a 8kHz wav | ||
let wav = new WaveFile( | ||
fs.readFileSync(path + "32fp-44100Hz-mono-chirp-1-22050-log.wav")); | ||
|
||
// Convert to another sample rate | ||
wav.toSampleRate(16000, {method: 'cubic', LPFType: 'IIR'}); | ||
|
||
// Write the file | ||
fs.writeFileSync( | ||
path + "/out/to-sample-rate/cubic-IIR-32fp-16000Hz-mono-chirp-1-22050-log.wav", | ||
wav.toBuffer()); | ||
}); | ||
|
||
describe('Upsample a 32-bit 44.1kHz log sine sweep', function() { | ||
// Read a 8kHz wav | ||
let wav = new WaveFile( | ||
fs.readFileSync(path + "32fp-44100Hz-mono-chirp-1-22050-log.wav")); | ||
|
||
// Convert to another sample rate | ||
wav.toSampleRate(96000, {method: 'cubic', LPFType: 'IIR'}); | ||
|
||
// Write the file | ||
fs.writeFileSync( | ||
path + "/out/to-sample-rate/cubic-IIR-32fp-96000Hz-mono-chirp-1-22050-log.wav", | ||
wav.toBuffer()); | ||
}); | ||
|
||
describe('Downsample a 32-bit 44.1kHz linear sine sweep', function() { | ||
// Read a 8kHz wav | ||
let wav = new WaveFile( | ||
fs.readFileSync(path + "32fp-44100Hz-mono-chirp-1-22050-linear.wav")); | ||
|
||
// Convert to another sample rate | ||
wav.toSampleRate(16000, {method: 'cubic', LPFType: 'IIR'}); | ||
|
||
// Write the file | ||
fs.writeFileSync( | ||
path + "/out/to-sample-rate/cubic-IIR-32fp-16000Hz-mono-chirp-1-22050-linear.wav", | ||
wav.toBuffer()); | ||
}); | ||
|
||
describe('Upsample a 32-bit 44.1kHz linear sine sweep', function() { | ||
// Read a 8kHz wav | ||
let wav = new WaveFile( | ||
fs.readFileSync(path + "32fp-44100Hz-mono-chirp-1-22050-linear.wav")); | ||
|
||
// Convert to another sample rate | ||
wav.toSampleRate(96000, {method: 'cubic', LPFType: 'IIR'}); | ||
|
||
// Write the file | ||
fs.writeFileSync( | ||
path + "/out/to-sample-rate/cubic-IIR-32fp-96000Hz-mono-chirp-1-22050-linear.wav", | ||
wav.toBuffer()); | ||
}); | ||
|
||
// Chirps, int | ||
|
||
describe('Downsample a 16-bit 44.1kHz log sine sweep', function() { | ||
// Read a 8kHz wav | ||
let wav = new WaveFile( | ||
fs.readFileSync(path + "16bit-44100Hz-mono-chirp-1-22050-log.wav")); | ||
|
||
// Convert to another sample rate | ||
wav.toSampleRate(16000, {method: 'cubic', LPFType: 'IIR'}); | ||
|
||
// Write the file | ||
fs.writeFileSync( | ||
path + "/out/to-sample-rate/cubic-IIR-16bit-16000Hz-mono-chirp-1-22050-log.wav", | ||
wav.toBuffer()); | ||
}); | ||
|
||
describe('Upsample a 16-bit 44.1kHz log sine sweep', function() { | ||
// Read a 8kHz wav | ||
let wav = new WaveFile( | ||
fs.readFileSync(path + "16bit-44100Hz-mono-chirp-1-22050-log.wav")); | ||
|
||
// Convert to another sample rate | ||
wav.toSampleRate(96000, {method: 'cubic', LPFType: 'IIR'}); | ||
|
||
// Write the file | ||
fs.writeFileSync( | ||
path + "/out/to-sample-rate/cubic-IIR-16bit-96000Hz-mono-chirp-1-22050-log.wav", | ||
wav.toBuffer()); | ||
}); | ||
|
||
describe('Downsample a 16-bit 44.1kHz linear sine sweep', function() { | ||
// Read a 8kHz wav | ||
let wav = new WaveFile( | ||
fs.readFileSync(path + "16bit-44100Hz-mono-chirp-1-22050-linear.wav")); | ||
|
||
// Convert to another sample rate | ||
wav.toSampleRate(16000, {method: 'cubic', LPFType: 'IIR'}); | ||
|
||
// Write the file | ||
fs.writeFileSync( | ||
path + "/out/to-sample-rate/cubic-IIR-16bit-16000Hz-mono-chirp-1-22050-linear.wav", | ||
wav.toBuffer()); | ||
}); | ||
|
||
describe('Upsample a 16-bit 44.1kHz linear sine sweep', function() { | ||
// Read a 8kHz wav | ||
let wav = new WaveFile( | ||
fs.readFileSync(path + "16bit-44100Hz-mono-chirp-1-22050-linear.wav")); | ||
|
||
// Convert to another sample rate | ||
wav.toSampleRate(96000, {method: 'cubic', LPFType: 'IIR'}); | ||
|
||
// Write the file | ||
fs.writeFileSync( | ||
path + "/out/to-sample-rate/cubic-IIR-16bit-96000Hz-mono-chirp-1-22050-linear.wav", | ||
wav.toBuffer()); | ||
}); | ||
|
||
// Songs | ||
|
||
describe('Downsample a 16bit 44.1kHz file', function() { | ||
// Read a 8kHz wav | ||
let wav = new WaveFile( | ||
fs.readFileSync(path + "song1.wav")); | ||
|
||
// Convert to another sample rate | ||
wav.toSampleRate(16000, {method: 'cubic', LPFType: 'IIR'}); | ||
|
||
// Write the file | ||
fs.writeFileSync( | ||
path + "/out/to-sample-rate/cubic-IIR-song1-16kHz.wav", | ||
wav.toBuffer()); | ||
}); | ||
|
||
describe('Upsample a 16bit 44.1kHz file', function() { | ||
// Read a 8kHz wav | ||
let wav = new WaveFile( | ||
fs.readFileSync(path + "song1.wav")); | ||
|
||
// Convert to another sample rate | ||
wav.toSampleRate(96000, {method: 'cubic', LPFType: 'IIR'}); | ||
|
||
// Write the file | ||
fs.writeFileSync( | ||
path + "/out/to-sample-rate/cubic-IIR-song1-96kHz.wav", | ||
wav.toBuffer()); | ||
}); | ||
|
||
hrend = process.hrtime(hrstart); | ||
console.info('%ds %dms', hrend[0], hrend[1] / 1000000); |
Oops, something went wrong.