-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
233 lines (198 loc) · 6.95 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
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
const stream = require('stream');
const native = require('./libopenmpt.js');
module.exports = function(buffer, options) {
const hasOptions = options != null && typeof options == 'object';
const duplex = buffer == null || !Buffer.isBuffer(buffer);
if(duplex && !hasOptions && typeof buffer == 'object') {
options = buffer;
} else if(!hasOptions) {
options = {};
}
const samplerate = options['sampleRate'] || 48000;
const channels = options['channels'] || 2;
const maxFramesPerChunk = options['readSize'] || 1024;
if(channels != 1 && channels != 2 && channels != 4) {
throw new Error('Invalid number of channels');
}
const bytesPerFrame = 2 * channels;
if(duplex) {
return createDuplexStream(samplerate, channels, maxFramesPerChunk, bytesPerFrame);
} else {
return createReadableStream(buffer, samplerate, channels, maxFramesPerChunk, bytesPerFrame);
}
};
function createDuplexStream(samplerate, channels, maxFramesPerChunk, bytesPerFrame) {
// TODO: change duplex to something better
const duplex = stream.Duplex();
var mod_ptr = null;
var buf_ptr = null;
var data = [];
var superPipe = duplex.pipe;
var toPipe = [];
var destroyed = false;
// Once it finishes writing, lets decode the music
duplex.once('finish', function() {
mod_ptr = initModule(Buffer.concat(data));
buf_ptr = initBuffer(bytesPerFrame, maxFramesPerChunk);
data = null;
duplex.emit('readable');
try {
for(var i = 0; i < toPipe.length; i++) {
superPipe.apply(duplex, toPipe[i]);
}
duplex.pipe = superPipe;
toPipe = null;
superPipe = null;
} catch(e) {}
});
duplex._write = function(chunk, enc, next) {
data.push(chunk);
next();
};
duplex._read = function(m) {
if(mod_ptr != null) {
const buf = readModule(mod_ptr, buf_ptr, samplerate, channels, maxFramesPerChunk, bytesPerFrame);
if(buf == null) {
cleanupModule(mod_ptr, buf_ptr);
mod_ptr = null;
buf_ptr = null;
destroyed = true;
}
duplex.push(buf);
} else if(destroyed) {
duplex.push(null);
duplex.emit('end');
} else {
duplex.push(null);
}
};
// Dirty trick. TODO: Change it to something better
duplex.pipe = function(dest, options) {
if(mod_ptr != null) {
superPipe.apply(duplex, arguments);
} else {
toPipe.push(arguments);
}
};
duplex.destroy = function() {
duplex.end();
destroyed = true;
cleanupModule(mod_ptr, buf_ptr);
mod_ptr = null;
buf_ptr = null;
};
createProperties(duplex, mod_ptr);
return duplex;
}
function createReadableStream(buffer, samplerate, channels, maxFramesPerChunk, bytesPerFrame) {
const readable = stream.Readable();
const mod_ptr = initModule(buffer);
const buf_ptr = initBuffer(maxFramesPerChunk, bytesPerFrame);
var destroyed = false;
readable._read = function() {
if(destroyed) {
readable.push(null);
return;
}
const buf = readModule(mod_ptr, buf_ptr, samplerate, channels, maxFramesPerChunk, bytesPerFrame);
if(buf == null) {
cleanupModule(mod_ptr, buf_ptr);
destroyed = true;
}
readable.push(buf);
};
readable.destroy = function() {
destroyed = true;
cleanupModule(mod_ptr, buf_ptr);
};
createProperties(readable, mod_ptr);
return readable;
}
function initModule(buffer) {
var array = new Int8Array(buffer);
var allocMem = native._malloc(array.byteLength);
native.HEAPU8.set(array, allocMem);
return native._openmpt_module_create_from_memory(allocMem, array.byteLength, 0, 0, 0);
}
function initBuffer(bytesPerFrame, maxFramesPerChunk) {
return native._malloc(bytesPerFrame * maxFramesPerChunk);
}
function readModule(mod_ptr, buf_ptr, samplerate, channels, maxFramesPerChunk, bytesPerFrame) {
var frames = 0;
switch(channels) {
case 1:
frames = native._openmpt_module_read_mono(mod_ptr, samplerate, maxFramesPerChunk, buf_ptr);
break;
case 2:
frames = native._openmpt_module_read_interleaved_stereo(mod_ptr, samplerate, maxFramesPerChunk, buf_ptr);
break;
case 4:
frames = native._openmpt_module_read_interleaved_quad(mod_ptr, samplerate, maxFramesPerChunk, buf_ptr);
break;
}
if(frames <= 0) return null;
var rawpcm = native.HEAPU8.subarray(buf_ptr, buf_ptr + bytesPerFrame * frames);
return new Buffer(rawpcm.buffer).slice(rawpcm.byteOffset, rawpcm.byteOffset + rawpcm.byteLength);
}
function cleanupModule(mod_ptr, buf_ptr) {
native._free(buf_ptr);
native._openmpt_module_destroy(mod_ptr);
}
function createProperties(obj, mod_ptr) {
Object.defineProperties(obj, {
'repeat': {
get: function() {
return native._openmpt_module_get_repeat_count(mod_ptr);
},
set: function(count) {
native._openmpt_module_set_repeat_count(mod_ptr, count);
}
},
'position': {
get: function() {
return native._openmpt_module_get_position_seconds(mod_ptr);
},
set: function(seconds) {
native._openmpt_module_set_position_seconds(mod_ptr, seconds);
}
},
'duration': {
get: function() {
return native._openmpt_module_get_duration_seconds(mod_ptr);
}
},
'current_speed': {
get: function() {
return native._openmpt_module_get_current_speed(mod_ptr);
}
},
'current_tempo': {
get: function() {
return native._openmpt_module_get_current_tempo(mod_ptr);
}
},
'num_channels': {
get: function() {
return native._openmpt_module_get_num_channels(mod_ptr);
}
},
'num_instruments': {
get: function() {
return native._openmpt_module_get_num_instruments(mod_ptr);
}
},
'metadata': {
get: function() {
const metadata = {};
const keys = native.Pointer_stringify(native._openmpt_module_get_metadata_keys(mod_ptr)).split(';');
for(var i = 0; i < keys.length; i++) {
var buf = native._malloc(keys[i].length + 1);
native.writeStringToMemory(keys[i], buf);
metadata[keys[i]] = native.Pointer_stringify(native._openmpt_module_get_metadata(mod_ptr, buf));
native._free(buf);
}
return metadata;
}
}
});
}