Skip to content

Commit e467227

Browse files
authored
Merge pull request #3 from PRX/feat/configure_pad_bytes
Configure pad bytes
2 parents 11b55a8 + 9933b72 commit e467227

File tree

8 files changed

+82
-28
lines changed

8 files changed

+82
-28
lines changed

dist/wavefile.js

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

externs/wavefile.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,12 @@ WaveFile.prototype._PMX = {
446446
* @type {string}
447447
*/
448448
WaveFile.prototype.bitDepth = "";
449+
/**
450+
* Whether to apply a pad byte or not
451+
* Defaults to 'true'
452+
* @type {boolean}
453+
*/
454+
WaveFile.prototype.padBytes = true;
449455

450456
/**
451457
* Return the samples packed in a Float64Array.

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ declare module wavefile {
101101
* @type {!Object<string, *>}
102102
*/
103103
_PMX: object;
104+
/**
105+
* Whether to apply a pad byte or not
106+
* Defaults to 'true'
107+
* @type {boolean}
108+
*/
109+
padBytes: boolean;
104110

105111
/**
106112
* @param {Uint8Array=} [wavBuffer=null] A wave file buffer.

lib/riff-file.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ export class RIFFFile {
7272
* @protected
7373
*/
7474
this.supported_containers = ['RIFF', 'RIFX'];
75+
/**
76+
* Whether to apply a pad byte or not
77+
* Defaults to 'true'
78+
* @type {boolean}
79+
*/
80+
this.padBytes = true;
7581
}
7682

7783
/**
@@ -167,7 +173,9 @@ export class RIFFFile {
167173
while(i <= buffer.length - 8) {
168174
chunks.push(this.getSubChunkIndex_(buffer, i));
169175
i += 8 + chunks[chunks.length - 1].chunkSize;
170-
i = i % 2 ? i + 1 : i;
176+
if (this.padBytes) {
177+
i = i % 2 ? i + 1 : i;
178+
}
171179
}
172180
return chunks;
173181
}
@@ -191,8 +199,10 @@ export class RIFFFile {
191199
chunk.subChunks = this.getSubChunksIndex_(buffer);
192200
} else {
193201
/** @type {number} */
194-
let realChunkSize = chunk.chunkSize % 2 ?
195-
chunk.chunkSize + 1 : chunk.chunkSize;
202+
let realChunkSize = chunk.chunkSize;
203+
if (this.padBytes) {
204+
realChunkSize = realChunkSize % 2 ? realChunkSize + 1 : realChunkSize;
205+
}
196206
this.head = index + 8 + realChunkSize;
197207
chunk.chunkData = {
198208
start: index + 8,

lib/wavefile-parser.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,10 @@ export class WaveFileParser extends WaveFileReader {
619619
* @private
620620
*/
621621
enforceByteLen_(bytes) {
622-
if (bytes.length % 2) {
623-
bytes.push(0);
622+
if (this.padBytes) {
623+
if (bytes.length % 2) {
624+
bytes.push(0);
625+
}
624626
}
625627
}
626628
}

test/dist/cart/cart-rw.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,36 @@ function cl(str) {
1414
return str.replace(/\0+$/, "");
1515
}
1616

17+
describe("Write file without pad bytes", function() {
18+
let wav = new WaveFile(fs.readFileSync(path + "cc_0101.wav"));
19+
wav.cart.tagText = "1";
20+
wav.padBytes = false;
21+
fs.writeFileSync(path + "out/cc_0101_nopad.wav", wav.toBuffer());
22+
fs.stat(path + "out/cc_0101_nopad.wav", (error, stats) => {
23+
if (error) {
24+
throw error;
25+
}
26+
else {
27+
assert.equal(stats.size, 806911);
28+
}
29+
});
30+
assert.equal(wav.cart.chunkSize, 2049);
31+
32+
wav = new WaveFile(fs.readFileSync(path + "cc_0101.wav"));
33+
wav.cart.tagText = "1";
34+
wav.padBytes = true;
35+
fs.writeFileSync(path + "out/cc_0101_yespad.wav", wav.toBuffer());
36+
fs.stat(path + "out/cc_0101_yespad.wav", (error, stats) => {
37+
if (error) {
38+
throw error;
39+
}
40+
else {
41+
assert.equal(stats.size, 806912);
42+
}
43+
});
44+
assert.equal(wav.cart.chunkSize, 2049);
45+
});
46+
1747
describe("Read and write cart chunk PCM wav file", function() {
1848
let wav = new WaveFile(fs.readFileSync(path + "cc_0101.wav"));
1949
fs.writeFileSync(path + "out/cc_0101.wav", wav.toBuffer());

test/files/out/cc_0101_nopad.wav

788 KB
Binary file not shown.

test/files/out/cc_0101_yespad.wav

788 KB
Binary file not shown.

0 commit comments

Comments
 (0)