-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudioconvert.js
52 lines (45 loc) · 1.34 KB
/
audioconvert.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
const AIFF = require('./aiff');
const Wave = require('./wave');
function aiffToWave(aiffFile) {
const parsedAIFF = AIFF.parse(aiffFile);
if (parsedAIFF.formType !== 'AIFF') {
throw new Error(`AIFC not supported`);
}
const waveSoundData = parsedAIFF.soundData.slice();
// to little endian
if (parsedAIFF.sampleSize === 16) {
waveSoundData.swap16();
} else if (parsedAIFF.sampleSize !== 8) {
throw new Error('unsupported bit depth: ' + parsedAIFF.sampleSize);
}
return Wave.serialize({
audioFormat: 1, // linear PCM
numChannels: parsedAIFF.numChannels,
sampleRate: parsedAIFF.sampleRate,
sampleSize: parsedAIFF.sampleSize,
soundData: waveSoundData,
});
}
function waveToAiff(waveFile) {
const parsedWave = Wave.parse(waveFile);
if (parsedWave.audioFormat !== 1) {
throw new Error(`non PCM wave not supported`);
}
const aiffSoundData = parsedWave.soundData.slice();
// to big endian
if (parsedWave.sampleSize === 16) {
aiffSoundData.swap16();
} else if (parsedWave.sampleSize !== 8) {
throw new Error('unsupported bit depth: ' + parsedWave.sampleSize);
}
return AIFF.serialize({
numChannels: parsedWave.numChannels,
sampleRate: parsedWave.sampleRate,
sampleSize: parsedWave.sampleSize,
soundData: aiffSoundData,
});
}
module.exports = {
aiffToWave,
waveToAiff,
};