1
|
|
|
/*! |
2
|
|
|
* wavefile |
3
|
|
|
* Read & write wave files with 8, 16, 24, 32 PCM, 32 IEEE & 64-bit data. |
4
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. MIT License. |
5
|
|
|
* https://github.com/rochars/wavefile |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
const bitDepthLib = require("bitdepth"); |
10
|
|
|
const waveFileReaderWriter = require("./src/wavefile-reader-writer"); |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* WaveFile |
14
|
|
|
*/ |
15
|
|
|
class WaveFile extends waveFileReaderWriter.WaveFileReaderWriter { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param {Uint8Array} bytes The file bytes. |
|
|
|
|
19
|
|
|
* @param {Uint8Array} buffer A wave file buffer. |
20
|
|
|
* @param {boolean} enforceFact True if it should throw a error |
21
|
|
|
* if no "fact" chunk is found. |
22
|
|
|
* @param {boolean} enforceBext True if it should throw a error |
23
|
|
|
* if no "bext" chunk is found. |
24
|
|
|
*/ |
25
|
|
|
constructor(buffer, enforceFact=false, enforceBext=false) { |
26
|
|
|
super(enforceFact, enforceBext); |
27
|
|
|
if(buffer) { |
28
|
|
|
this.fromBuffer(buffer); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Turn the file to RIFF. |
34
|
|
|
* All values will be little-endian when writing. |
35
|
|
|
*/ |
36
|
|
|
toRIFF() { |
37
|
|
|
this.chunkId = "RIFF"; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Turn the file to RIFX. |
42
|
|
|
* All values but FourCCs will be big-endian when writing. |
43
|
|
|
*/ |
44
|
|
|
toRIFX() { |
45
|
|
|
this.chunkId = "RIFX"; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Change the bit depth of the data. |
50
|
|
|
* @param {string} bitDepth The new bit depth of the data. |
51
|
|
|
* One of "8", "16", "24", "32", "32f", "64" |
52
|
|
|
*/ |
53
|
|
|
toBitDepth(bitDepth) { |
54
|
|
|
bitDepthLib.toBitDepth(this.samples_, this.bitDepth_, bitDepth); |
55
|
|
|
this.fromScratch( |
56
|
|
|
this.numChannels, |
57
|
|
|
this.sampleRate, |
58
|
|
|
bitDepth, |
59
|
|
|
this.samples_, |
60
|
|
|
{"container": this.chunkId} |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Interleave multi-channel samples. |
66
|
|
|
*/ |
67
|
|
|
interleave() { |
68
|
|
|
let finalSamples = []; |
69
|
|
|
let i; |
70
|
|
|
let j; |
71
|
|
|
let numChannels = this.samples_[0].length; |
72
|
|
|
for (i = 0; i < numChannels; i++) { |
73
|
|
|
for (j = 0; j < this.samples_.length; j++) { |
74
|
|
|
finalSamples.push(this.samples_[j][i]); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
this.samples_ = finalSamples; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* De-interleave samples into multiple channels. |
82
|
|
|
*/ |
83
|
|
|
deInterleave() { |
84
|
|
|
let finalSamples = []; |
85
|
|
|
let i; |
86
|
|
|
for (i = 0; i < this.numChannels; i++) { |
87
|
|
|
finalSamples[i] = []; |
88
|
|
|
} |
89
|
|
|
i = 0; |
90
|
|
|
let j; |
91
|
|
|
while (i < this.samples_.length) { |
92
|
|
|
for (j = 0; j < this.numChannels; j++) { |
93
|
|
|
finalSamples[j].push(this.samples_[i+j]); |
94
|
|
|
} |
95
|
|
|
i += j; |
96
|
|
|
} |
97
|
|
|
this.samples_ = finalSamples; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
module.exports.WaveFile = WaveFile; |
102
|
|
|
|
103
|
|
|
|