Skip to content

Commit

Permalink
truncate float samples on toBitDepth when they overflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
rochars committed Jun 12, 2018
1 parent 7502395 commit e6d923c
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 75 deletions.
115 changes: 58 additions & 57 deletions dist/wavefile-min.js

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions docs/WaveFile.html
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ <h4 class="name" id="deInterleave"><span class="type-signature"></span>deInterle

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line543">line 543</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line544">line 544</a>
</li></ul></dd>


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

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line787">line 787</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line788">line 788</a>
</li></ul></dd>


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

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line729">line 729</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line730">line 730</a>
</li></ul></dd>


Expand Down Expand Up @@ -1626,7 +1626,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="index.js.html">index.js</a>, <a href="index.js.html#line627">line 627</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line628">line 628</a>
</li></ul></dd>


Expand Down Expand Up @@ -2334,7 +2334,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="index.js.html">index.js</a>, <a href="index.js.html#line593">line 593</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line594">line 594</a>
</li></ul></dd>


Expand Down Expand Up @@ -2475,7 +2475,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="index.js.html">index.js</a>, <a href="index.js.html#line661">line 661</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line662">line 662</a>
</li></ul></dd>


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

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line1931">line 1931</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line1950">line 1950</a>
</li></ul></dd>


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

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line714">line 714</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line715">line 715</a>
</li></ul></dd>


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

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line525">line 525</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line526">line 526</a>
</li></ul></dd>


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

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line745">line 745</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line746">line 746</a>
</li></ul></dd>


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

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line681">line 681</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line682">line 682</a>
</li></ul></dd>


Expand Down Expand Up @@ -3600,7 +3600,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="index.js.html">index.js</a>, <a href="index.js.html#line609">line 609</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line610">line 610</a>
</li></ul></dd>


Expand Down Expand Up @@ -4286,7 +4286,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="index.js.html">index.js</a>, <a href="index.js.html#line568">line 568</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line569">line 569</a>
</li></ul></dd>


Expand Down Expand Up @@ -4426,7 +4426,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="index.js.html">index.js</a>, <a href="index.js.html#line643">line 643</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line644">line 644</a>
</li></ul></dd>


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

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line818">line 818</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line819">line 819</a>
</li></ul></dd>


Expand Down Expand Up @@ -4835,7 +4835,7 @@ <h5>Parameters:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Jun 12 2018 15:30:30 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Jun 12 2018 16:15:48 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ <h2>LICENSE</h2><p>Copyright (c) 2017-2018 Rafael da Silva Rocha.</p>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Jun 12 2018 15:30:30 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Jun 12 2018 16:15:48 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
21 changes: 20 additions & 1 deletion docs/index.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ <h1 class="page-title">index.js</h1>
}
this.assureInterleaved_();
this.assureUncompressed_();
this.truncateSamples();
bitDepth_.toBitDepth(this.data.samples, thisBitDepth, toBitDepth);
this.fromScratch(
this.fmt.numChannels,
Expand Down Expand Up @@ -1718,6 +1719,24 @@ <h1 class="page-title">index.js</h1>
this.data.samples, this.getSamplesType_());
}

/**
* Truncate float samples on over and underflow.
* @private
*/
truncateSamples() {
if (this.fmt.audioFormat == 3) {
/** @type {number} */
let len = this.data.samples.length;
for (let i=0; i&lt;len; i++) {
if (this.data.samples[i] > 1) {
this.data.samples[i] = 1;
} else if (this.data.samples[i] &lt; -1) {
this.data.samples[i] = -1;
}
}
}
}

/**
* Turn bytes to samples and load them in the data.samples property.
* @param {!Array&lt;number>} bytes The bytes.
Expand Down Expand Up @@ -2131,7 +2150,7 @@ <h1 class="page-title">index.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Jun 12 2018 15:30:30 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Jun 12 2018 16:15:48 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
19 changes: 19 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ class WaveFile {
}
this.assureInterleaved_();
this.assureUncompressed_();
this.truncateSamples();
bitDepth_.toBitDepth(this.data.samples, thisBitDepth, toBitDepth);
this.fromScratch(
this.fmt.numChannels,
Expand Down Expand Up @@ -1679,6 +1680,24 @@ class WaveFile {
this.data.samples, this.getSamplesType_());
}

/**
* Truncate float samples on over and underflow.
* @private
*/
truncateSamples() {
if (this.fmt.audioFormat == 3) {
/** @type {number} */
let len = this.data.samples.length;
for (let i=0; i<len; i++) {
if (this.data.samples[i] > 1) {
this.data.samples[i] = 1;
} else if (this.data.samples[i] < -1) {
this.data.samples[i] = -1;
}
}
}
}

/**
* Turn bytes to samples and load them in the data.samples property.
* @param {!Array<number>} bytes The bytes.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"test-adtl": "nyc ./node_modules/mocha/bin/_mocha test/list/read-list-adtl.js --src --recursive",
"test-info": "nyc ./node_modules/mocha/bin/_mocha test/list/read-list-info.js --src --recursive",
"test-cue": "nyc ./node_modules/mocha/bin/_mocha test/cue --src --recursive",
"test-overflow": "nyc ./node_modules/mocha/bin/_mocha test/overflow.js --src --recursive",
"test": "nyc ./node_modules/mocha/bin/_mocha test --src --recursive",
"test-dist": "nyc ./node_modules/mocha/bin/_mocha test --dist --recursive",
"coverage": "nyc report --reporter=lcov > coverage.lcov && codecov",
Expand Down
Binary file added test/files/out/to-bit-depth/64-to-8-overflow.wav
Binary file not shown.
64 changes: 64 additions & 0 deletions test/overflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* WaveFile: https://github.com/rochars/wavefile
* Copyright (c) 2017-2018 Rafael da Silva Rocha. MIT License.
*
* Test the toBitDepth() method to convert an 64-bit file to 8-bit.
* The 64-bit file have values > 1, so after the conversion the
* values will cause overflow when packed to 8-bit. WaveFile should
* truncate the values.
*
*/

const assert = require("assert");
const fs = require("fs");
const WaveFile = require("../test/loader.js");
const path = "test/files/";
describe("32-bit IEEE from file to 8-bit", function() {

let wav = new WaveFile(
fs.readFileSync(path + "64bit-48kHz-noBext-mono-overflow.wav"));
wav.data.samples[0] = -1.5;
wav.toBitDepth("8");
fs.writeFileSync(
path + "/out/to-bit-depth/64-to-8-overflow.wav", wav.toBuffer());

it("chunkId should be 'RIFF'", function() {
assert.equal(wav.container, "RIFF");
});
it("fmtChunkId should be 'fmt '", function() {
assert.equal(wav.fmt.chunkId, "fmt ");
});
it("format should be 'WAVE'", function() {
assert.equal(wav.format, "WAVE");
});
it("fmtChunkSize should be 16", function() {
assert.equal(wav.fmt.chunkSize, 16);
});
it("audioFormat should be 1 (PCM)", function() {
assert.equal(wav.fmt.audioFormat, 1);
});
it("numChannels should be 1", function() {
assert.equal(wav.fmt.numChannels, 1);
});
it("sampleRate should be 48000", function() {
assert.equal(wav.fmt.sampleRate, 48000);
});
it("byteRate be 48000", function() {
assert.equal(wav.fmt.byteRate, 48000);
});
it("blockAlign should be 1", function() {
assert.equal(wav.fmt.blockAlign, 1);
});
it("bitsPerSample should be 8", function() {
assert.equal(wav.fmt.bitsPerSample, 8);
});
it("dataChunkId should be 'data'", function() {
assert.equal(wav.data.chunkId, 'data');
});
it("dataChunkSize should be > 0", function() {
assert.ok(wav.data.chunkSize > 0);
});
it("samples.length should be > 0", function() {
assert.ok(wav.data.samples.length > 0);
});
});

0 comments on commit e6d923c

Please sign in to comment.