Skip to content

Commit

Permalink
Back with lib/truncate-samples to not break tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rochars committed Jan 27, 2020
1 parent 826326d commit 13b3509
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 66 deletions.
36 changes: 18 additions & 18 deletions dist/wavefile.js

Large diffs are not rendered by default.

20 changes: 1 addition & 19 deletions docs/lib_wavefile-converter.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ <h1 class="page-title">lib/wavefile-converter.js</h1>
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';

Expand Down Expand Up @@ -411,25 +412,6 @@ <h1 class="page-title">lib/wavefile-converter.js</h1>
}
return outputSize;
}

/**
* Clamp integer samples.
* @param {!(Array&lt;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 &lt; len; i++) {
samples[i] = Math.round(samples[i]);
if (samples[i] > max) {
samples[i] = max;
} else if (samples[i] &lt; min) {
samples[i] = min;
}
}
}
</code></pre>
</article>
</section>
Expand Down
20 changes: 10 additions & 10 deletions docs/module-wavefile.WaveFile.html
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ <h4 class="name" id="fromALaw"><span class="type-signature"></span>fromALaw<span

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line148">line 148</a>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line149">line 149</a>
</li></ul></dd>


Expand Down Expand Up @@ -1932,7 +1932,7 @@ <h4 class="name" id="fromIMAADPCM"><span class="type-signature"></span>fromIMAAD

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line112">line 112</a>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line113">line 113</a>
</li></ul></dd>


Expand Down Expand Up @@ -2081,7 +2081,7 @@ <h4 class="name" id="fromMuLaw"><span class="type-signature"></span>fromMuLaw<sp

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line184">line 184</a>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line185">line 185</a>
</li></ul></dd>


Expand Down Expand Up @@ -4597,7 +4597,7 @@ <h4 class="name" id="toALaw"><span class="type-signature"></span>toALaw<span cla

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line127">line 127</a>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line128">line 128</a>
</li></ul></dd>


Expand Down Expand Up @@ -4827,7 +4827,7 @@ <h4 class="name" id="toBitDepth"><span class="type-signature"></span>toBitDepth<

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line204">line 204</a>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line205">line 205</a>
</li></ul></dd>


Expand Down Expand Up @@ -5367,7 +5367,7 @@ <h4 class="name" id="toIMAADPCM"><span class="type-signature"></span>toIMAADPCM<

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line83">line 83</a>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line84">line 84</a>
</li></ul></dd>


Expand Down Expand Up @@ -5515,7 +5515,7 @@ <h4 class="name" id="toMuLaw"><span class="type-signature"></span>toMuLaw<span c

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line163">line 163</a>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line164">line 164</a>
</li></ul></dd>


Expand Down Expand Up @@ -5607,7 +5607,7 @@ <h4 class="name" id="toRIFF"><span class="type-signature"></span>toRIFF<span cla

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line48">line 48</a>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line49">line 49</a>
</li></ul></dd>


Expand Down Expand Up @@ -5699,7 +5699,7 @@ <h4 class="name" id="toRIFX"><span class="type-signature"></span>toRIFX<span cla

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line64">line 64</a>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line65">line 65</a>
</li></ul></dd>


Expand Down Expand Up @@ -5791,7 +5791,7 @@ <h4 class="name" id="toSampleRate"><span class="type-signature"></span>toSampleR

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line244">line 244</a>
<a href="lib_wavefile-converter.js.html">lib/wavefile-converter.js</a>, <a href="lib_wavefile-converter.js.html#line245">line 245</a>
</li></ul></dd>


Expand Down
46 changes: 46 additions & 0 deletions lib/parsers/truncate-samples.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
20 changes: 1 addition & 19 deletions lib/wavefile-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -367,22 +368,3 @@ function outputSize_(byteLen, byteOffset) {
}
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;
}
}
}
26 changes: 26 additions & 0 deletions test/lib/truncate-samples.js
Original file line number Diff line number Diff line change
@@ -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]));
});
});

0 comments on commit 13b3509

Please sign in to comment.