-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
146 lines (124 loc) · 3.89 KB
/
index.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
const A = require('arcsecond');
const B = require('arcsecond-binary');
const C = require('construct-js');
const fs = require('fs');
const path = require('path');
const file = fs.readFileSync(path.join(__dirname, './test.wav'));
const riffChunkSize = B.u32LE.chain(size => {
if (size !== file.length - 8) {
return A.fail(`Invalid file size: ${file.length}. Expected ${size}`);
}
return A.succeedWith(size);
});
const riffChunk = A.sequenceOf([
A.str('RIFF'),
riffChunkSize,
A.str('WAVE')
]);
const fmtSubChunk = A.coroutine(function* () {
const id = yield A.str('fmt ');
const subChunk1Size = yield B.u32LE;
const audioFormat = yield B.u16LE;
const numChannels = yield B.u16LE;
const sampleRate = yield B.u32LE;
const byteRate = yield B.u32LE;
const blockAlign = yield B.u16LE;
const bitsPerSample = yield B.u16LE;
const expectedByteRate = sampleRate * numChannels * bitsPerSample / 8;
if (byteRate !== expectedByteRate) {
yield A.fail(`Invalid byte rate: ${byteRate}, expected ${expectedByteRate}`);
}
const expectedBlockAlign = numChannels * bitsPerSample / 8;
if (blockAlign !== expectedBlockAlign) {
yield A.fail(`Invalid block align: ${blockAlign}, expected ${expectedBlockAlign}`);
}
const fmtChunkData = {
id,
subChunk1Size,
audioFormat,
numChannels,
sampleRate,
byteRate,
blockAlign,
bitsPerSample
};
yield A.setData(fmtChunkData);
return fmtChunkData;
});
const dataSubChunk = A.coroutine(function* () {
const id = yield A.str('data');
const size = yield B.u32LE;
const fmtData = yield A.getData;
const samples = size / fmtData.numChannels / (fmtData.bitsPerSample / 8);
const channelData = Array.from({length: fmtData.numChannels}, () => []);
let sampleParser;
if (fmtData.bitsPerSample === 8) {
sampleParser = B.s8;
} else if (fmtData.bitsPerSample === 16) {
sampleParser = B.s16LE;
} else if (fmtData.bitsPerSample === 32) {
sampleParser = B.s32LE;
} else {
yield A.fail(`Unsupported bits per sample: ${fmtData.bitsPerSample}`);
}
for (let sampleIndex = 0; sampleIndex < samples; sampleIndex++) {
for (let i = 0; i < fmtData.numChannels; i++) {
const sampleValue = yield sampleParser;
channelData[i].push(sampleValue);
}
}
return {
id,
size,
channelData
};
});
const parser = A.sequenceOf([
riffChunk,
fmtSubChunk,
dataSubChunk,
A.endOfInput
]).map(([riffChunk, fmtSubChunk, dataSubChunk]) => ({
riffChunk,
fmtSubChunk,
dataSubChunk
}));
const output = parser.run(file.buffer);
if (output.isError) {
throw new Error(output.error);
}
const riffChunkStruct = C.Struct('riffChunk')
.field('magic', C.RawString('RIFF'))
.field('size', C.U32LE(0))
.field('fmtName', C.RawString('WAVE'));
const fmtSubChunkStruct = C.Struct('fmtSubChunk')
.field('id', C.RawString('fmt '))
.field('subChunk1Size', C.U32LE(0))
.field('audioFormat', C.U16LE(1))
.field('numChannels', C.U16LE(1))
.field('sampleRate', C.U32LE(44100))
.field('byteRate', C.U32LE(44100 * 2))
.field('blockAlign', C.U16LE(2))
.field('bitsPerSample', C.U16LE(16));
const totalSubChunkSize = fmtSubChunkStruct.computeBufferSize();
fmtSubChunkStruct.get('subChunk1Size').set(totalSubChunkSize - 8);
const dataSubChunkStruct = C.Struct('dataSubChunk')
.field('id', C.RawString('data'))
.field('size', C.U32LE(0))
.field('data', C.S16LEs([0]));
const soundData = [];
let isUp = true;
for (let i = 0; i < 44100; i++) {
if (i % 100 === 0) {
isUp = !isUp;
}
const sampleValue = isUp ? 16383 : -16383;
soundData[i] = sampleValue;
}
dataSubChunkStruct.get('data').set(soundData);
dataSubChunkStruct.get('size').set(soundData.length * 2);
const fileStruct = C.Struct('waveFile')
.field('riffChunk', riffChunkStruct)
.field('fmtSubChunk', fmtSubChunkStruct)
.field('dataSubChunk', dataSubChunkStruct);
fs.writeFileSync(path.join(__dirname, './new.wav'), fileStruct.toBuffer());