-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsadpcm.js
203 lines (163 loc) · 6.09 KB
/
msadpcm.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
//This is adapted from https://github.com/Snack-X/node-ms-adpcm
//I tried to find a fast decoder for MSADPCM in nodejs and came up short.
//Maybe I didn't look hard enough.
//With some work, this did the job well for me.
const ADAPTATION_TABLE = [
230, 230, 230, 230, 307, 409, 512, 614,
768, 614, 512, 409, 307, 230, 230, 230,
];
function clamp(val, min, max) {
if(val < min) return min;
else if(val > max) return max;
else return val;
}
function expandNibble(nibble, state, channel) {
const signed = 8 <= nibble ? nibble - 16 : nibble;
let predictor = ((
state.sample1[channel] * state.coeff1[channel] +
state.sample2[channel] * state.coeff2[channel]
) >> 8) + (signed * state.delta[channel]);
predictor = clamp(predictor, -0x8000, 0x7fff);
state.sample2[channel] = state.sample1[channel];
state.sample1[channel] = predictor;
state.delta[channel] = Math.floor(ADAPTATION_TABLE[nibble] * state.delta[channel] / 256);
if(state.delta[channel] < 16) state.delta[channel] = 16;
return predictor;
}
/**
* Decode a block of MS-ADPCM data
* @param {Buffer} buf one block of MS-ADPCM data
* @param {number} channels number of channels (usually 1 or 2, never tested on upper values)
* @param {number[]} coefficient1 array of 7 UInt8 coefficient values
* usually, [ 256, 512, 0, 192, 240, 460, 392 ]
* @param {number[]} coefficient2 array of 7 UInt8 coefficient values
* usually, [ 0, -256, 0, 64, 0, -208, -232 ]
* @return {Buffer[]} array of decoded PCM buffer for each channels
*/
function decode(buf, channels, coefficient1, coefficient2) {
const state = {
coefficient: [ coefficient1, coefficient2 ],
coeff1: [],
coeff2: [],
delta: [],
sample1: [],
sample2: [],
};
let offset = 0;
// Read MS-ADPCM header
for(let i = 0 ; i < channels ; i++) {
const predictor = clamp(buf.readUInt8(offset), 0, 6);
offset += 1;
state.coeff1[i] = state.coefficient[0][predictor];
state.coeff2[i] = state.coefficient[1][predictor];
}
for(let i = 0 ; i < channels ; i++) { state.delta.push(buf.readInt16LE(offset)); offset += 2; }
for(let i = 0 ; i < channels ; i++) { state.sample1.push(buf.readInt16LE(offset)); offset += 2; }
for(let i = 0 ; i < channels ; i++) { state.sample2.push(buf.readInt16LE(offset)); offset += 2; }
// Decode
const output = [];
for(let i = 0 ; i < channels ; i++)
output[i] = [ state.sample2[i], state.sample1[i] ];
let channel = 0;
while(offset < buf.length) {
const byte = buf.readUInt8(offset);
offset += 1;
output[channel].push(expandNibble(byte >> 4, state, channel));
channel = (channel + 1) % channels;
output[channel].push(expandNibble(byte & 0xf, state, channel));
channel = (channel + 1) % channels;
}
//Converting all sound to stereo since it'll be easier later on.
if (channels == 1) {
output.push(output[0]);
}
return output;
}
function readWav(buf) {
let offset = 0;
// 'RIFF'
const magic = buf.readUInt32BE(offset); offset += 4;
if(magic !== 0x52494646) {
console.log(magic);
throw "0x0000:0x0004 != 52:49:46:46";
}
const dataSize = buf.readUInt32LE(offset); offset += 4;
// 'WAVE'
const format = buf.readUInt32BE(offset); offset += 4;
if(format !== 0x57415645) throw "0x0008:0x000B != 57:41:56:45";
let wavFormat, wavData;
while(offset < buf.length) {
const name = buf.readUInt32BE(offset); offset += 4;
const blockSize = buf.readUInt32LE(offset); offset += 4;
// 'fmt '
if(name === 0x666D7420) {
wavFormat = {
format: buf.readUInt16LE(offset + 0),
channels: buf.readUInt16LE(offset + 2),
sampleRate: buf.readUInt32LE(offset + 4),
byteRate: buf.readUInt32LE(offset + 8),
blockAlign: buf.readUInt16LE(offset + 12),
bitsPerSample: buf.readUInt16LE(offset + 14),
};
offset += 16;
if(wavFormat.format === 0x01) {
// console.log(`${filename} is PCM file`);
continue;
}
else if(wavFormat.format === 0x02) {
// console.log(`${filename} is MS-ADPCM file`);
const extraSize = buf.readUInt16LE(offset); offset += 2;
wavFormat.extraSize = extraSize;
wavFormat.extra = {
samplesPerBlock: buf.readUInt16LE(offset + 0),
coefficientCount: buf.readUInt16LE(offset + 2),
coefficient: [ [], [] ],
};
offset += 4;
for(let i = 0 ; i < wavFormat.extra.coefficientCount ; i++) {
wavFormat.extra.coefficient[0].push(buf.readInt16LE(offset + 0));
wavFormat.extra.coefficient[1].push(buf.readInt16LE(offset + 2));
offset += 4;
}
}
else throw `WAVE format ${wavFormat.format} is unknown`;
}
// 'data'
else if(name === 0x64617461) {
wavData = buf.slice(offset, offset + blockSize);
offset += blockSize;
}
else {
offset += blockSize;
}
}
if(wavFormat && wavData) return { format: wavFormat, data: wavData };
else throw "'fmt ' or/and 'data' block not found";
}
exports.decodeKeysoundOut = (buff, vol) => {
const adpcmData = readWav(buff);
const blockSize = adpcmData.format.blockAlign;
let totalBuff = Buffer.alloc(1);
const totalBlocks = adpcmData.data.length / blockSize;
let totalOffset = 0;
for(let i = 0 ; i < adpcmData.data.length ; i += blockSize) {
const adpcmBlock = adpcmData.data.slice(i, i + blockSize);
const decoded = decode(
adpcmBlock,
adpcmData.format.channels,
adpcmData.format.extra.coefficient[0],
adpcmData.format.extra.coefficient[1]
);
const pcmBlockSize = decoded[0].length * 2;
if (totalBuff.length == 1) {
totalBuff = Buffer.alloc(pcmBlockSize * totalBlocks * 2);
}
for(let s = 0 ; s < pcmBlockSize/2; s++) {
for(let c = 0 ; c < decoded.length ; c++) {
totalBuff.writeInt16LE(decoded[c][s], totalOffset);
totalOffset += 2;
}
}
}
return {data: totalBuff, channels: adpcmData.format.channels, samplingRate: adpcmData.format.sampleRate, volume: vol};
}