1
|
|
|
/* |
2
|
|
|
* Copyright (c) 2017-2018 Rafael da Silva Rocha. |
3
|
|
|
* |
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
5
|
|
|
* a copy of this software and associated documentation files (the |
6
|
|
|
* "Software"), to deal in the Software without restriction, including |
7
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
8
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
9
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
10
|
|
|
* the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be |
13
|
|
|
* included in all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
18
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
19
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
20
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
21
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @fileoverview The WavBuffer class. |
27
|
|
|
* @see https://github.com/rochars/wavefile |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* A class representing the data in a wav buffer. |
32
|
|
|
*/ |
33
|
|
|
export default class WavBuffer { |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @throws {Error} If no 'RIFF' chunk is found. |
37
|
|
|
* @throws {Error} If no 'fmt ' chunk is found. |
38
|
|
|
* @throws {Error} If no 'data' chunk is found. |
39
|
|
|
*/ |
40
|
|
|
constructor() { |
41
|
|
|
/** |
42
|
|
|
* The container identifier. |
43
|
|
|
* 'RIFF', 'RIFX' and 'RF64' are supported. |
44
|
|
|
* @type {string} |
45
|
|
|
*/ |
46
|
|
|
this.container = ''; |
47
|
|
|
/** |
48
|
|
|
* @type {number} |
49
|
|
|
*/ |
50
|
|
|
this.chunkSize = 0; |
51
|
|
|
/** |
52
|
|
|
* The format. |
53
|
|
|
* Always 'WAVE'. |
54
|
|
|
* @type {string} |
55
|
|
|
*/ |
56
|
|
|
this.format = ''; |
57
|
|
|
/** |
58
|
|
|
* The data of the 'fmt' chunk. |
59
|
|
|
* @type {!Object<string, *>} |
60
|
|
|
*/ |
61
|
|
|
this.fmt = { |
62
|
|
|
/** @type {string} */ |
63
|
|
|
chunkId: '', |
64
|
|
|
/** @type {number} */ |
65
|
|
|
chunkSize: 0, |
66
|
|
|
/** @type {number} */ |
67
|
|
|
audioFormat: 0, |
68
|
|
|
/** @type {number} */ |
69
|
|
|
numChannels: 0, |
70
|
|
|
/** @type {number} */ |
71
|
|
|
sampleRate: 0, |
72
|
|
|
/** @type {number} */ |
73
|
|
|
byteRate: 0, |
74
|
|
|
/** @type {number} */ |
75
|
|
|
blockAlign: 0, |
76
|
|
|
/** @type {number} */ |
77
|
|
|
bitsPerSample: 0, |
78
|
|
|
/** @type {number} */ |
79
|
|
|
cbSize: 0, |
80
|
|
|
/** @type {number} */ |
81
|
|
|
validBitsPerSample: 0, |
82
|
|
|
/** @type {number} */ |
83
|
|
|
dwChannelMask: 0, |
84
|
|
|
/** |
85
|
|
|
* 4 32-bit values representing a 128-bit ID |
86
|
|
|
* @type {!Array<number>} |
87
|
|
|
*/ |
88
|
|
|
subformat: [] |
89
|
|
|
}; |
90
|
|
|
/** |
91
|
|
|
* The data of the 'fact' chunk. |
92
|
|
|
* @type {!Object<string, *>} |
93
|
|
|
*/ |
94
|
|
|
this.fact = { |
95
|
|
|
/** @type {string} */ |
96
|
|
|
chunkId: '', |
97
|
|
|
/** @type {number} */ |
98
|
|
|
chunkSize: 0, |
99
|
|
|
/** @type {number} */ |
100
|
|
|
dwSampleLength: 0 |
101
|
|
|
}; |
102
|
|
|
/** |
103
|
|
|
* The data of the 'cue ' chunk. |
104
|
|
|
* @type {!Object<string, *>} |
105
|
|
|
*/ |
106
|
|
|
this.cue = { |
107
|
|
|
/** @type {string} */ |
108
|
|
|
chunkId: '', |
109
|
|
|
/** @type {number} */ |
110
|
|
|
chunkSize: 0, |
111
|
|
|
/** @type {number} */ |
112
|
|
|
dwCuePoints: 0, |
113
|
|
|
/** @type {!Array<!Object>} */ |
114
|
|
|
points: [], |
115
|
|
|
}; |
116
|
|
|
/** |
117
|
|
|
* The data of the 'smpl' chunk. |
118
|
|
|
* @type {!Object<string, *>} |
119
|
|
|
*/ |
120
|
|
|
this.smpl = { |
121
|
|
|
/** @type {string} */ |
122
|
|
|
chunkId: '', |
123
|
|
|
/** @type {number} */ |
124
|
|
|
chunkSize: 0, |
125
|
|
|
/** @type {number} */ |
126
|
|
|
dwManufacturer: 0, |
127
|
|
|
/** @type {number} */ |
128
|
|
|
dwProduct: 0, |
129
|
|
|
/** @type {number} */ |
130
|
|
|
dwSamplePeriod: 0, |
131
|
|
|
/** @type {number} */ |
132
|
|
|
dwMIDIUnityNote: 0, |
133
|
|
|
/** @type {number} */ |
134
|
|
|
dwMIDIPitchFraction: 0, |
135
|
|
|
/** @type {number} */ |
136
|
|
|
dwSMPTEFormat: 0, |
137
|
|
|
/** @type {number} */ |
138
|
|
|
dwSMPTEOffset: 0, |
139
|
|
|
/** @type {number} */ |
140
|
|
|
dwNumSampleLoops: 0, |
141
|
|
|
/** @type {number} */ |
142
|
|
|
dwSamplerData: 0, |
143
|
|
|
/** @type {!Array<!Object>} */ |
144
|
|
|
loops: [] |
145
|
|
|
}; |
146
|
|
|
/** |
147
|
|
|
* The data of the 'bext' chunk. |
148
|
|
|
* @type {!Object<string, *>} |
149
|
|
|
*/ |
150
|
|
|
this.bext = { |
151
|
|
|
/** @type {string} */ |
152
|
|
|
chunkId: '', |
153
|
|
|
/** @type {number} */ |
154
|
|
|
chunkSize: 0, |
155
|
|
|
/** @type {string} */ |
156
|
|
|
description: '', //256 |
157
|
|
|
/** @type {string} */ |
158
|
|
|
originator: '', //32 |
159
|
|
|
/** @type {string} */ |
160
|
|
|
originatorReference: '', //32 |
161
|
|
|
/** @type {string} */ |
162
|
|
|
originationDate: '', //10 |
163
|
|
|
/** @type {string} */ |
164
|
|
|
originationTime: '', //8 |
165
|
|
|
/** |
166
|
|
|
* 2 32-bit values, timeReference high and low |
167
|
|
|
* @type {!Array<number>} |
168
|
|
|
*/ |
169
|
|
|
timeReference: [0, 0], |
170
|
|
|
/** @type {number} */ |
171
|
|
|
version: 0, //WORD |
172
|
|
|
/** @type {string} */ |
173
|
|
|
UMID: '', // 64 chars |
174
|
|
|
/** @type {number} */ |
175
|
|
|
loudnessValue: 0, //WORD |
176
|
|
|
/** @type {number} */ |
177
|
|
|
loudnessRange: 0, //WORD |
178
|
|
|
/** @type {number} */ |
179
|
|
|
maxTruePeakLevel: 0, //WORD |
180
|
|
|
/** @type {number} */ |
181
|
|
|
maxMomentaryLoudness: 0, //WORD |
182
|
|
|
/** @type {number} */ |
183
|
|
|
maxShortTermLoudness: 0, //WORD |
184
|
|
|
/** @type {string} */ |
185
|
|
|
reserved: '', //180 |
186
|
|
|
/** @type {string} */ |
187
|
|
|
codingHistory: '' // string, unlimited |
188
|
|
|
}; |
189
|
|
|
/** |
190
|
|
|
* The data of the 'ds64' chunk. |
191
|
|
|
* Used only with RF64 files. |
192
|
|
|
* @type {!Object<string, *>} |
193
|
|
|
*/ |
194
|
|
|
this.ds64 = { |
195
|
|
|
/** @type {string} */ |
196
|
|
|
chunkId: '', |
197
|
|
|
/** @type {number} */ |
198
|
|
|
chunkSize: 0, |
199
|
|
|
/** @type {number} */ |
200
|
|
|
riffSizeHigh: 0, // DWORD |
201
|
|
|
/** @type {number} */ |
202
|
|
|
riffSizeLow: 0, // DWORD |
203
|
|
|
/** @type {number} */ |
204
|
|
|
dataSizeHigh: 0, // DWORD |
205
|
|
|
/** @type {number} */ |
206
|
|
|
dataSizeLow: 0, // DWORD |
207
|
|
|
/** @type {number} */ |
208
|
|
|
originationTime: 0, // DWORD |
209
|
|
|
/** @type {number} */ |
210
|
|
|
sampleCountHigh: 0, // DWORD |
211
|
|
|
/** @type {number} */ |
212
|
|
|
sampleCountLow: 0 // DWORD |
213
|
|
|
/** @type {number} */ |
214
|
|
|
//'tableLength': 0, // DWORD |
215
|
|
|
/** @type {!Array<number>} */ |
216
|
|
|
//'table': [] |
217
|
|
|
}; |
218
|
|
|
/** |
219
|
|
|
* The data of the 'data' chunk. |
220
|
|
|
* @type {!Object<string, *>} |
221
|
|
|
*/ |
222
|
|
|
this.data = { |
223
|
|
|
/** @type {string} */ |
224
|
|
|
chunkId: '', |
225
|
|
|
/** @type {number} */ |
226
|
|
|
chunkSize: 0, |
227
|
|
|
/** @type {!Uint8Array} */ |
228
|
|
|
samples: new Uint8Array(0) |
229
|
|
|
}; |
230
|
|
|
/** |
231
|
|
|
* The data of the 'LIST' chunks. |
232
|
|
|
* Each item in this list look like this: |
233
|
|
|
* { |
234
|
|
|
* chunkId: '', |
235
|
|
|
* chunkSize: 0, |
236
|
|
|
* format: '', |
237
|
|
|
* subChunks: [] |
238
|
|
|
* } |
239
|
|
|
* @type {!Array<!Object>} |
240
|
|
|
*/ |
241
|
|
|
this.LIST = []; |
242
|
|
|
/** |
243
|
|
|
* The data of the 'junk' chunk. |
244
|
|
|
* @type {!Object<string, *>} |
245
|
|
|
*/ |
246
|
|
|
this.junk = { |
247
|
|
|
/** @type {string} */ |
248
|
|
|
chunkId: '', |
249
|
|
|
/** @type {number} */ |
250
|
|
|
chunkSize: 0, |
251
|
|
|
/** @type {!Array<number>} */ |
252
|
|
|
chunkData: [] |
253
|
|
|
}; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|