-
Notifications
You must be signed in to change notification settings - Fork 4
/
mgreel.js
executable file
·88 lines (77 loc) · 2.48 KB
/
mgreel.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
#! /usr/bin/env node
/*
* ================================== mgreel.js ===============================
*
* mgreel.js is a JavaScript/Node script to convert one or more Wave files to a
* single Make Noise Morphagene reel (Wave file 32bit/48khz/stereo).
*
* All files are concatenated and splice markers are set at the end of each
* input files. Mono Wave files are converted to stereo.
*
* Example:
* mgreel --out out.wav mywav/*.wav
*
*/
var fs = require('fs');
var path = require('path');
var argv = require('minimist')(process.argv.slice(2));
var wavefile = require('wavefile');
const sampleRate = 48000.0;
const bitDepth = '32f';
if (!argv._.length) {
console.error("call `mgreel --out out.wav input1.wav input2.wav ...`");
process.exit();
}
// splicesL|R is an array of samples for each input file (splice). Samples are
// resampled to 32f/48khz.
let splicesL = [];
let splicesR = [];
argv._.forEach(function(file) {
let buf = fs.readFileSync(file);
let wav = new wavefile.WaveFile(buf);
wav.toBitDepth(bitDepth);
wav.toSampleRate(sampleRate);
let samples = wav.getSamples(false);
if (wav.fmt.numChannels == 2) {
splicesL.push(samples[0]);
splicesR.push(samples[1]);
} else {
splicesL.push(samples);
splicesR.push(samples);
}
});
// totalLength contains total number of samples.
let totalLength = splicesL.reduce(function(total, splice) { return total + splice.length }, 0);
// samplesL|R are arrays with the final samples.
let samplesL = new Float64Array(totalLength);
let samplesR = new Float64Array(totalLength);
let currentOffset = 0;
splicesL.forEach(function(splice) {
samplesL.set(splice, currentOffset);
currentOffset += splice.length;
});
currentOffset = 0;
splicesR.forEach(function(splice) {
samplesR.set(splice, currentOffset);
currentOffset += splice.length;
});
// Assemble final wav.
let wav = new wavefile.WaveFile();
wav.fromScratch(2, sampleRate, bitDepth, [samplesL, samplesR]);
// Set cue points based on splices length.
currentOffset = 0;
splicesL.forEach(function(splice) {
if (currentOffset > 0) {
wav.setCuePoint({position: currentOffset/sampleRate*1000});
}
currentOffset += splice.length;
});
// MG requires that dwPosition is set to same value as dwSampleOffset.
wav.cue.points.forEach(function(point) {
point.dwPosition = point.dwSampleOffset
});
if (argv.out) {
fs.writeFileSync(argv.out, wav.toBuffer());
} else {
process.stdout.write(wav.toBuffer());
}