diff --git a/dist/wavefile.js b/dist/wavefile.js index 7ac5c4e..23956df 100644 --- a/dist/wavefile.js +++ b/dist/wavefile.js @@ -21,22 +21,22 @@ if(128>f)b[c]=f,c++;else{var g=0,k=0;2047>=f?(g=1,k=192):65535>=f?(g=2,k=224):11 (e=160),237===a[d]&&(f=159)):240<=k&&244>=k?(q=3,240===a[d]&&(e=144),244===a[d]&&(f=143)):g=!0;k&=(1<<8-q-1)-1;for(var z=0;zf)g=!0;k=k<<6|a[d]&63;d++}g?b+=String.fromCharCode(65533):65535>=k?b+=String.fromCharCode(k):(k-=65536,b+=String.fromCharCode((k>>10&1023)+55296,(k&1023)+56320))}}return b}function x(a){var b=[];X(a,b,0);return b}function ia(a,b,c,d,e){d=void 0===d?0:d;b=b||{};e=ja(b.g,b.N,b.F,void 0===e?!1:e);var f=Math.ceil(b.g/8),g=0,k=d;try{for(var q=a.length;ga||53a&&(a+=b);return a}function va(a,b){var c=2*(b-1);a=ca(a,c);a>b-1&&(a=c-a);return a}function la(a,b,c,d){d=void 0===d?{}:d;var e=new Float64Array(a.length*((c-b)/b+1));d.method=d.method|| -"cubic";var f=new v(a.length,e.length,{method:d.method,tension:d.tension||0,sincFilterSize:d.sincFilterSize||6,sincWindow:d.sincWindow||void 0,clip:d.clip||"mirror"});void 0===d.LPF&&(d.LPF=Na[d.method]);if(d.LPF){d.LPFType=d.LPFType||"IIR";var g=Oa[d.LPFType];if(c>b){b=new g(d.LPForder||ma[d.LPFType],c,b/2);c=0;for(d=e.length;cc?a[e]=c:a[e]b?b+Math.pow(2,this.g):b)&255;c++;for(var d=2,e=this.b;dthis.max||athis.max?this.max:athis.max&&(a-=2*this.max+2);return a};K.prototype.fa=function(a,b,c){if("number"!=typeof b)throw new TypeError;Math.abs(b)>this.h-2*this.j&&(b=0>b?-Infinity: -Infinity);var d=0>((b=+b)||1/b)?1:0>b?1:0;b=Math.abs(b);var e=Math.min(Math.floor(Math.log(b)/Math.LN2),1023),f=Y(b/Math.pow(2,e)*Math.pow(2,this.c));b!==b?(f=Math.pow(2,this.c-1),e=(1<=Math.pow(2,1-this.a)?(2<=f/Math.pow(2,this.c)&&(e+=1,f=1),e>this.a?(e=(1<=b;)a[f]=parseInt(e.substring(0, -8),2),e=e.substring(8),f--,d++;return d};B.prototype.ua=function(a){this.c=0;this.container=this.s(a,4);if(-1===this.Y.indexOf(this.container))throw Error("Not a supported format.");this.a.o="RIFX"===this.container;this.chunkSize=this.b(a);this.format=this.s(a,4);this.X={chunkId:this.container,chunkSize:this.chunkSize,format:this.format,subChunks:this.U(a)}};B.prototype.l=function(a,b){b=void 0===b?!1:b;for(var c=this.X.subChunks,d=[],e=0;ec?a[e]=c:a[e]a&&(a+=b);return a}function va(a,b){var c= +2*(b-1);a=ca(a,c);a>b-1&&(a=c-a);return a}function ma(a,b,c,d){d=void 0===d?{}:d;var e=new Float64Array(a.length*((c-b)/b+1));d.method=d.method||"cubic";var f=new v(a.length,e.length,{method:d.method,tension:d.tension||0,sincFilterSize:d.sincFilterSize||6,sincWindow:d.sincWindow||void 0,clip:d.clip||"mirror"});void 0===d.LPF&&(d.LPF=Na[d.method]);if(d.LPF){d.LPFType=d.LPFType||"IIR";var g=Oa[d.LPFType];if(c>b){b=new g(d.LPForder||na[d.LPFType],c,b/2);c=0;for(d=e.length;cb?b+Math.pow(2,this.g):b)&255;c++;for(var d=2,e=this.b;dthis.max||athis.max?this.max:athis.max&&(a-=2*this.max+2);return a};K.prototype.fa=function(a,b,c){if("number"!=typeof b)throw new TypeError;Math.abs(b)>this.h-2*this.j&& +(b=0>b?-Infinity:Infinity);var d=0>((b=+b)||1/b)?1:0>b?1:0;b=Math.abs(b);var e=Math.min(Math.floor(Math.log(b)/Math.LN2),1023),f=Y(b/Math.pow(2,e)*Math.pow(2,this.c));b!==b?(f=Math.pow(2,this.c-1),e=(1<=Math.pow(2,1-this.a)?(2<=f/Math.pow(2,this.c)&&(e+=1,f=1),e>this.a?(e=(1<=b;)a[f]= +parseInt(e.substring(0,8),2),e=e.substring(8),f--,d++;return d};B.prototype.ua=function(a){this.c=0;this.container=this.s(a,4);if(-1===this.Y.indexOf(this.container))throw Error("Not a supported format.");this.a.o="RIFX"===this.container;this.chunkSize=this.b(a);this.format=this.s(a,4);this.X={chunkId:this.container,chunkSize:this.chunkSize,format:this.format,subChunks:this.U(a)}};B.prototype.l=function(a,b){b=void 0===b?!1:b;for(var c=this.X.subChunks,d=[],e=0;ee[q]&&(e[q]=-1)}for(g= -0;ga||4294967295> 2],b+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(a[c]&3)<<4|a[c+1]>>4],b+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(a[c+1]&15)<<2|a[c+2]>>6],b+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[a[c+2]&63];2===a.length%3?b=b.substring(0,b.length-1)+"=":1===a.length%3&&(b=b.substring(0,b.length-2)+"==");return b};m.prototype.toDataURI=function(){return"data:audio/wav;base64,"+this.toBase64()};m.prototype.fromDataURI=function(a){this.fromBase64(a.replace("data:audio/wav;base64,", diff --git a/docs/lib_wavefile-converter.js.html b/docs/lib_wavefile-converter.js.html index 933caba..64d9515 100644 --- a/docs/lib_wavefile-converter.js.html +++ b/docs/lib_wavefile-converter.js.html @@ -76,6 +76,7 @@

lib/wavefile-converter.js

import * as alawmulaw from 'alawmulaw'; import { unpackArrayTo } from 'byte-data'; import { WaveFileCueEditor } from './wavefile-cue-editor'; +import { truncateIntSamples } from './parsers/truncate-samples'; import { validateSampleRate } from './validators/validate-sample-rate'; import { resample } from './resampler'; @@ -411,25 +412,6 @@

lib/wavefile-converter.js

} return outputSize; } - -/** - * Clamp integer samples. - * @param {!(Array<number>|TypedArray)} samples The samples to round. - * @param {number} bits The number of bits. - * @private - */ -function truncateIntSamples(samples, bits) { - let max = bits === 8 ? 255 : Math.pow(2, bits) / 2 - 1; - let min = bits === 8 ? 0 : -max - 1; - for (let i = 0, len = samples.length; i < len; i++) { - samples[i] = Math.round(samples[i]); - if (samples[i] > max) { - samples[i] = max; - } else if (samples[i] < min) { - samples[i] = min; - } - } -} diff --git a/docs/module-wavefile.WaveFile.html b/docs/module-wavefile.WaveFile.html index 40c3780..4248255 100644 --- a/docs/module-wavefile.WaveFile.html +++ b/docs/module-wavefile.WaveFile.html @@ -1147,7 +1147,7 @@

fromALawSource:
@@ -1932,7 +1932,7 @@

fromIMAAD
Source:
@@ -2081,7 +2081,7 @@

fromMuLawSource:
@@ -4597,7 +4597,7 @@

toALawSource:
@@ -4827,7 +4827,7 @@

toBitDepth<
Source:
@@ -5367,7 +5367,7 @@

toIMAADPCM<
Source:
@@ -5515,7 +5515,7 @@

toMuLawSource:
@@ -5607,7 +5607,7 @@

toRIFFSource:
@@ -5699,7 +5699,7 @@

toRIFXSource:
@@ -5791,7 +5791,7 @@

toSampleR
Source:
diff --git a/lib/parsers/truncate-samples.js b/lib/parsers/truncate-samples.js new file mode 100644 index 0000000..9a4ddeb --- /dev/null +++ b/lib/parsers/truncate-samples.js @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2017-2019 Rafael da Silva Rocha. + * + * 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 The truncateSamples function. + * @see https://github.com/rochars/wavefile + */ + +/** + * Truncate int samples on overflow. + * @param {!Array|!TypedArray} samples the samples. + * @param {number} bits The number of bits used by each sample. + */ +export function truncateIntSamples(samples, bits) { + let max = bits === 8 ? 255 : Math.pow(2, bits) / 2 - 1; + let min = bits === 8 ? 0 : -max - 1; + for (let i = 0, len = samples.length; i < len; i++) { + samples[i] = Math.round(samples[i]); + if (samples[i] > max) { + samples[i] = max; + } else if (samples[i] < min) { + samples[i] = min; + } + } +} diff --git a/lib/wavefile-converter.js b/lib/wavefile-converter.js index e020c34..723ed84 100644 --- a/lib/wavefile-converter.js +++ b/lib/wavefile-converter.js @@ -32,6 +32,7 @@ import * as imaadpcm from 'imaadpcm'; import * as alawmulaw from 'alawmulaw'; import { unpackArrayTo } from 'byte-data'; import { WaveFileCueEditor } from './wavefile-cue-editor'; +import { truncateIntSamples } from './parsers/truncate-samples'; import { validateSampleRate } from './validators/validate-sample-rate'; import { resample } from './resampler'; @@ -367,22 +368,3 @@ function outputSize_(byteLen, byteOffset) { } return outputSize; } - -/** - * Clamp integer samples. - * @param {!(Array|TypedArray)} samples The samples to round. - * @param {number} bits The number of bits. - * @private - */ -function truncateIntSamples(samples, bits) { - let max = bits === 8 ? 255 : Math.pow(2, bits) / 2 - 1; - let min = bits === 8 ? 0 : -max - 1; - for (let i = 0, len = samples.length; i < len; i++) { - samples[i] = Math.round(samples[i]); - if (samples[i] > max) { - samples[i] = max; - } else if (samples[i] < min) { - samples[i] = min; - } - } -} diff --git a/test/lib/truncate-samples.js b/test/lib/truncate-samples.js new file mode 100644 index 0000000..8f6532a --- /dev/null +++ b/test/lib/truncate-samples.js @@ -0,0 +1,26 @@ +/** + * WaveFile: https://github.com/rochars/wavefile + * Copyright (c) 2017-2020 Rafael da Silva Rocha. MIT License. + * + * Test the functions from lib/parsers/truncate-samples.js + * + */ + +const assert = require("assert"); +const truncateIntSamples = require( + '../../lib/parsers/truncate-samples').truncateIntSamples; + +describe("Truncate int samples", function() { + + it("should truncate 8-bit samples", function() { + let samples = new Float64Array([-1, 256]); + truncateIntSamples(samples, 8); + assert.deepEqual(samples, new Float64Array([0, 255])); + }); + + it("should truncate 16-bit samples", function() { + let samples = new Float64Array([-40000, 32777]); + truncateIntSamples(samples, 16); + assert.deepEqual(samples, new Float64Array([-32768, 32767])); + }); +});