-
Notifications
You must be signed in to change notification settings - Fork 66
/
node-core-audio.js
334 lines (252 loc) · 11 KB
/
node-core-audio.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//////////////////////////////////////////////////////////////////////////
// node-core-audio - main module
//////////////////////////////////////////////////////////////////////////
//
// Main javascript audio API
/* ----------------------------------------------------------------------
Object Structures
-------------------------------------------------------------------------
*/
//////////////////////////////////////////////////////////////////////////
// Node.js Exports
var globalNamespace = {};
(function (exports) {
exports.createNewAudioEngine = function( options ) {
newAudioEngine= new AudioEngine( options );
return newAudioEngine;
};
}(typeof exports === 'object' && exports || globalNamespace));
var FFT = require("fft");
//////////////////////////////////////////////////////////////////////////
// Namespace (lol)
var SHOW_DEBUG_PRINTS = true;
var MAX_SUPPORTED_CHANNELS = 6; // We need to allocate our process audio for the max channels,
// so we have to set some reasonable limit
var log = function( a ) { if(SHOW_DEBUG_PRINTS) console.log(a); }; // A log function we can turn off
var exists = function(a) { return typeof(a) == "undefined" ? false : true; }; // Check whether a variable exists
//////////////////////////////////////////////////////////////////////////
// Constructor
function AudioEngine( options ) {
var audioEngineImpl = require( __dirname + "/build/Release/NodeCoreAudio" );
var defaultOptions = {
inputChannels: 1,
outputChannels: 2,
framesPerBuffer: 1024,
useMicrophone: true
};
this.options = options || defaultOptions;
this.audioEngine = audioEngineImpl.createAudioEngine( this.options );
this.options = this.audioEngine.getOptions();
this.audioStreamer;
this.processingCallbacks = [];
this.uiUpdateCallbacks = [];
this.outputBuffer = [];
this.tempBuffer = [];
this.processBuffer = [];
this.fft = new FFT.complex( this.audioEngine.getOptions().framesPerBuffer, false );
this.fftBuffer = [];
var _this = this;
function validateOutputBufferStructure( buffer ) {
if( buffer === undefined ) {
console.log( "Audio processing function didn't return an output buffer" );
return false;
}
if( !_this.audioEngine.getOptions().interleaved ) {
if( buffer.length > _this.options.inputChannels ) {
console.log( "Output buffer has info for too many channels" );
return false;
} else if( buffer.length < _this.options.inputChannels ) {
console.log( "Output buffer doesn't have data for enough channels" );
return false;
}
if( typeof(buffer[0]) != "object" ) {
console.log( "Output buffer not setup correctly, buffer[0] isn't an array" );
return false;
}
if( typeof(buffer[0][0]) != "number" ) {
console.log( "Output buffer not setup correctly, buffer[0][0] isn't a number" );
return false;
}
} else {
if( typeof(buffer[0]) != "number" ) {
console.log( "Output buffer not setup correctly, buffer[0] isn't a number" );
return false;
}
}
return true;
}
// Allocate a processing buffer for each of our channels
for( var iChannel = 0; iChannel<MAX_SUPPORTED_CHANNELS; ++iChannel ) {
this.processBuffer[iChannel] = [];
}
// Start polling the audio engine for data as fast as we can
var _this = this;
this.processAudio = this.getProcessAudio();
setInterval( function() {
if (_this.audioEngine.isBufferEmpty()) {
// Try to process audio
var input = _this.audioEngine.read();
var outputBuffer = _this.processAudio( input );
if( validateOutputBufferStructure(outputBuffer) )
_this.audioEngine.write( outputBuffer );
// Call our UI updates now that all the DSP work has been done
for( var iUpdate=0; iUpdate < _this.uiUpdateCallbacks.length; ++iUpdate ) {
_this.uiUpdateCallbacks[iUpdate]();
}
}
}, 1 );
} // end AudioEngine()
//////////////////////////////////////////////////////////////////////////
// Returns our main audio processing function
AudioEngine.prototype.getProcessAudio = function() {
var _this = this;
var options = this.audioEngine.getOptions(),
numChannels = options.inputChannels,
fftBuffer = this.fftBuffer;
var processAudio = function( inputBuffer ) {
// If we don't have any processing callbacks, just get out
if( _this.processingCallbacks.length == 0 )
return inputBuffer;
var processBuffer = inputBuffer;
//if( !_this.options.interleaved )
// deInterleave( inputBuffer, processBuffer, _this.options.framesPerBuffer, numChannels );
// Call through to all of our processing callbacks
for( var iCallback = 0; iCallback < _this.processingCallbacks.length; ++iCallback ) {
processBuffer = _this.processingCallbacks[iCallback]( processBuffer );
} // end for each callback
if( typeof(_this.audioStreamer) != "undefined" ) {
_this.audioStreamer.streamAudio( processBuffer, _this.options.framesPerBuffer, numChannels );
}
// Return our output audio to the sound card
return processBuffer;
} // end processAudio()
return processAudio;
} // end AudioEngine.getProcessAudio()
//////////////////////////////////////////////////////////////////////////
// Get the engine's options
AudioEngine.prototype.getOptions = function() {
this.options = this.audioEngine.getOptions();
return this.options;
} // end AudioEngine.getOptions()
//////////////////////////////////////////////////////////////////////////
// Get the engine's options
AudioEngine.prototype.setOptions = function( options ) {
this.audioEngine.setOptions( options );
this.options = this.audioEngine.getOptions();
} // end AudioEngine.setOptions()
//////////////////////////////////////////////////////////////////////////
// Add a processing callback
AudioEngine.prototype.createAudioHub = function( port ) {
this.audioStreamer = require("audio-streamer").createNewAudioStreamer( port );
} // end AudioEngine.createAudiohub()
//////////////////////////////////////////////////////////////////////////
// Add a processing callback
AudioEngine.prototype.addAudioCallback = function( callback ) {
this.processingCallbacks.push( callback );
} // end AudioEngine.addAudioCallback()
//////////////////////////////////////////////////////////////////////////
// Add a UI update callback
AudioEngine.prototype.addUpdateCallback = function( callback ) {
this.uiUpdateCallbacks.push( callback );
} // end AudioEngine.addUpdateCallback()
//////////////////////////////////////////////////////////////////////////
// Returns whether the audio engine is active
AudioEngine.prototype.isActive = function() {
return this.audioEngine.isActive();
} // end AudioEngine.isActive()
//////////////////////////////////////////////////////////////////////////
// Returns the sample rate of the audio engine
AudioEngine.prototype.getSampleRate = function() {
return this.audioEngine.getSampleRate();
} // end AudioEngine.getSampleRate()
//////////////////////////////////////////////////////////////////////////
// Returns the index of the input audio device
AudioEngine.prototype.getInputDeviceIndex = function() {
return this.audioEngine.getInputDeviceIndex();
} // end AudioEngine.getInputDeviceIndex()
//////////////////////////////////////////////////////////////////////////
// Returns the index of the output audio device
AudioEngine.prototype.getOutputDeviceIndex = function() {
return this.audioEngine.getOutputDeviceIndex();
} // end AudioEngine.getOutputDeviceIndex()
//////////////////////////////////////////////////////////////////////////
// Returns the name of a given device
AudioEngine.prototype.getDeviceName = function( deviceId ) {
return this.audioEngine.getDeviceName( deviceId );
} // end AudioEngine.getDeviceName()
//////////////////////////////////////////////////////////////////////////
// Returns the total number of audio devices
AudioEngine.prototype.getNumDevices = function() {
return this.audioEngine.getNumDevices();
} // end AudioEngine.getNumDevices()
//////////////////////////////////////////////////////////////////////////
// Sets the input audio device
AudioEngine.prototype.setInputDevice = function( deviceId ) {
return this.audioEngine.setInputDevice( deviceId );
} // end AudioEngine.setInputDevice()
//////////////////////////////////////////////////////////////////////////
// Sets the output audio device
AudioEngine.prototype.setOutputDevice = function( deviceId ) {
return this.audioEngine.setOutputDevice( deviceId );
} // end AudioEngine.setOutputDevice()
//////////////////////////////////////////////////////////////////////////
// Returns the number of input channels
AudioEngine.prototype.getNumInputChannels = function() {
return this.audioEngine.getNumInputChannels();
} // end AudioEngine.getNumInputChannels()
//////////////////////////////////////////////////////////////////////////
// Returns the number of output channels
AudioEngine.prototype.getNumOutputChannels = function() {
return this.audioEngine.getNumOutputChannels();
} // end AudioEngine.getNumOutputChannels()
//////////////////////////////////////////////////////////////////////////
// Read audio samples from the sound card
AudioEngine.prototype.read = function() {
return this.audioEngine.read();
} // end AudioEngine.read()
//////////////////////////////////////////////////////////////////////////
// Write some audio samples to the sound card
AudioEngine.prototype.write = function() {
this.audioEngine.write();
} // end AudioEngine.write()
//////////////////////////////////////////////////////////////////////////
// Splits a 1d buffer into its channel components
function deInterleave( inputBuffer, outputBuffer, numSamplesPerBuffer, numChannels ) {
// If the number of channels doesn't match, setup the output buffer
if( inputBuffer.length != outputBuffer.length ) {
outputBuffer = undefined;
outputBuffer = [];
for( var iChannel=0; iChannel<inputBuffer.length; ++iChannel )
outputBuffer[iChannel] = [];
}
if( numChannels < 2 ) {
outputBuffer[0] = inputBuffer;
return;
}
for( var iChannel = 0; iChannel < numChannels; iChannel += numChannels ) {
for( var iSample = 0; iSample < numSamplesPerBuffer; ++iSample ) {
outputBuffer[iChannel][iSample] = inputBuffer[iSample + iChannel];
} // end for each sample
} // end for each channel
} // end deInterleave()
//////////////////////////////////////////////////////////////////////////
// Joins multidimensional array into single buffer
function interleave( inputBuffer, outputBuffer, numSamplesPerBuffer, numChannels ) {
if( numChannels < 2 ) {
outputBuffer = inputBuffer;
return;
}
// If the number of channels doesn't match, setup the output buffer
if( inputBuffer.length != outputBuffer.length ) {
outputBuffer = undefined;
outputBuffer = [];
for( var iChannel=0; iChannel<inputBuffer.length; ++iChannel )
outputBuffer[iChannel] = [];
}
for( var iChannel = 0; iChannel < numChannels; ++iChannel ) {
if( inputBuffer[iChannel] === undefined ) break;
for( var iSample = 0; iSample < numSamplesPerBuffer; iSample += numChannels ) {
outputBuffer[iSample + iChannel] = inputBuffer[iChannel][iSample];
} // end for each sample position
} // end for each channel
} // end interleave()