forked from Moddable-OpenSource/moddable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwav2maud.js
233 lines (200 loc) · 7.5 KB
/
wav2maud.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright (c) 2016-2023 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Tools.
*
* The Moddable SDK Tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Moddable SDK Tools. If not, see <http://www.gnu.org/licenses/>.
*
*/
import {TOOL, FILE} from "tool";
import Resampler from "resampler";
import WavReader from "wavreader"
export default class extends TOOL {
constructor(argv) {
super(argv);
this.inputPath = "";
this.outputPath = "";
this.output = {
sampleRate: 11025,
numChannels: 1,
bitsPerSample: 8,
format: "uncompressed"
};
let argc = argv.length;
for (let argi = 1; argi < argc; argi++) {
let option = argv[argi], name, path;
switch (option) {
case "-o":
argi++;
if (argi >= argc)
throw new Error("-o: no directory!");
name = argv[argi];
if (this.outputPath)
throw new Error("-o '" + name + "': too many directories!");
path = this.resolveDirectoryPath(name);
if (!path)
throw new Error("-o '" + name + "': directory not found!");
this.outputPath = path;
break;
case "-r":
argi++;
if (argi >= argc)
throw new Error("-o: no sample rate!");
this.output.sampleRate = parseInt(argv[argi]);
if ((this.output.sampleRate < 8000) || (this.output.sampleRate > 48000))
throw new Error("sample rate must be from 8000 to 48000");
break;
case "-s":
argi++;
if (argi >= argc)
throw new Error("-o: no bitsPerSample!");
this.output.bitsPerSample = parseInt(argv[argi]);
if ((8 != this.output.bitsPerSample) && (16 != this.output.bitsPerSample))
throw new Error("bitsPerSample must be 8 or 16");
break;
case "-c":
argi++;
if (argi >= argc)
throw new Error("-o: no channels!");
this.output.numChannels = parseInt(argv[argi]);
if ((1 != this.output.numChannels) && (2 != this.output.numChannels))
throw new Error("channels must be 1 or 2");
break;
case "-f":
argi++;
if (argi >= argc)
throw new Error("-o: no format!");
if (("uncompressed" !== argv[argi]) && ("ima" !== argv[argi]))
throw new Error("unknown format!");
this.output.format = argv[argi];
break;
default:
name = argv[argi];
if (this.inputPath)
throw new Error("'" + name + "': too many files!");
path = this.resolveFilePath(name);
if (!path)
throw new Error("'" + name + "': file not found!");
this.inputPath = path;
break;
}
}
if (!this.inputPath)
throw new Error("no file!");
if (!this.outputPath)
this.outputPath = this.splitPath(this.inputPath).directory;
if ("ima" === this.output.format) {
if (1 !== this.output.numChannels)
throw new Error("ima must be mono");
this.output.bitsPerSample = 16;
}
let parts = this.splitPath(this.inputPath);
parts.extension = ".maud";
parts.directory = this.outputPath;
this.outputPath = this.joinPath(parts);
this.wav = new WavReader(this.readFileBuffer(this.inputPath));
if (1 !== this.wav.audioFormat)
throw new Error("unsupported format");
if ((8 !== this.wav.bitsPerSample) && (16 !== this.wav.bitsPerSample))
throw new Error("unsupported bitsPerSample");
if ((1 !== this.wav.numChannels) && (2 !== this.wav.numChannels))
throw new Error("unsupported channels");
this.wavSamples = new Float32Array(8192 * this.wav.numChannels);
this.resampler = new Resampler(this.wav.sampleRate, this.output.sampleRate, this.wav.numChannels, this.wavSamples);
if (this.output.numChannels > this.wav.numChannels)
throw Error("no mono to stereo conversion");
}
run() {
const imaSamplesPerChunk = 129;
const imaOutput = "ima" === this.output.format;
let outputFile = new FILE(this.outputPath, "wb");
let header = new DataView(new ArrayBuffer(12));
header.setUint8(0, 'm'.charCodeAt(0));
header.setUint8(1, 'a'.charCodeAt(0));
header.setUint8(2, 1); // version
header.setUint8(3, this.output.bitsPerSample);
header.setUint16(4, this.output.sampleRate, true); // little-endian
header.setUint8(6, this.output.numChannels);
header.setUint8(7, imaOutput ? 1 : 0); // sample format
let sampleCount = Math.floor((this.wav.samples / this.wav.sampleRate) * this.output.sampleRate);
if (imaOutput)
sampleCount = Math.idiv(sampleCount, imaSamplesPerChunk) * imaSamplesPerChunk; // quantize
header.setUint32(8, sampleCount, true)
outputFile.writeBuffer(header.buffer);
let resampled = this.resampler.outputBuffer;
let finalSamples = (8 === this.output.bitsPerSample) ? new Int8Array(this.wavSamples.length) : new Int16Array(this.wavSamples.length);
let imaBuffer = new Int16Array(imaSamplesPerChunk);
imaBuffer.samplesInBuffer = 0;
let totalOut = 0;
while (this.wav.samples) {
// get next buffer of samples
let use = this.wavSamples.length / this.wav.numChannels;
if (use > this.wav.samples)
use = this.wav.samples;
use = Math.min(1024, use);
this.wav.getSamples(this.wavSamples, use);
// apply output sample rate
use = this.resampler.resampler(use * this.wav.numChannels) / this.wav.numChannels;
// apply output numChannels (stereo to mono)
if ((2 == this.wav.numChannels) && (1 == this.output.numChannels)) {
for (let i = 0, j = 0; i < use; i++, j += 2)
resampled[i] = (resampled[j] + resampled[j + 1]) / 2;
}
// apply output bitsPerSample to generate finalSamples
if (16 == this.output.bitsPerSample) { // -32768 to 32767
for (let i = 0, length = use * this.wav.numChannels; i < length; i++)
finalSamples[i] = resampled[i];
}
else if (8 == this.output.bitsPerSample) { // -128 to 127
for (let i = 0, length = use * this.wav.numChannels; i < length; i++)
finalSamples[i] = resampled[i] >> 8;
}
// output samples
if ("uncompressed" === this.output.format) {
let byteLength = use * ((this.output.bitsPerSample * this.output.numChannels) >> 3);
outputFile.writeBuffer(finalSamples.buffer.slice(0, byteLength));
totalOut += use;
}
else if (imaOutput) {
let pos = 0;
while (pos < use) {
let needed = imaSamplesPerChunk - imaBuffer.samplesInBuffer;
if (needed > (use - pos))
needed = (use - pos);
imaBuffer.set(new Int16Array(finalSamples.buffer, pos << 1, needed), imaBuffer.samplesInBuffer);
imaBuffer.samplesInBuffer += needed;
if (imaBuffer.samplesInBuffer < imaSamplesPerChunk)
break;
outputFile.writeBuffer(this.compressIMA(imaBuffer.buffer, imaSamplesPerChunk, this.output.sampleRate));
imaBuffer.samplesInBuffer = 0;
pos += needed;
totalOut += imaSamplesPerChunk;
}
}
}
if (totalOut > sampleCount)
trace("too much output!\n");
else if (totalOut < sampleCount) {
if ("uncompressed" === this.output.format) {
const needed = (sampleCount - totalOut) * ((this.output.bitsPerSample * this.output.numChannels) >> 3);
outputFile.writeBuffer(new ArrayBuffer(needed));
}
else
trace("IMA padding needed!\n");
}
// if IMA, may be a partial buffer pending
outputFile.close();
}
compressIMA(samples) @ "Tool_compressIMA";
}