-
Notifications
You must be signed in to change notification settings - Fork 3
/
Audiolet.js
executable file
·7600 lines (6646 loc) · 190 KB
/
Audiolet.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function AudioFileRequest(url, async) {
this.url = url;
if (typeof async == 'undefined' || async == null) {
async = true;
}
this.async = async;
var splitURL = url.split('.');
this.extension = splitURL[splitURL.length - 1].toLowerCase();
}
AudioFileRequest.prototype.onSuccess = function(decoded) {
};
AudioFileRequest.prototype.onFailure = function(decoded) {
};
AudioFileRequest.prototype.send = function() {
if (this.extension != 'wav' &&
this.extension != 'aiff' &&
this.extension != 'aif') {
this.onFailure();
return;
}
var request = new XMLHttpRequest();
request.open('GET', this.url, this.async);
request.overrideMimeType('text/plain; charset=x-user-defined');
request.onreadystatechange = function(event) {
if (request.readyState == 4) {
if (request.status == 200 || request.status == 0) {
this.handleResponse(request.responseText);
}
else {
this.onFailure();
}
}
}.bind(this);
request.send(null);
};
AudioFileRequest.prototype.handleResponse = function(data) {
var decoder, decoded;
if (this.extension == 'wav') {
decoder = new WAVDecoder();
decoded = decoder.decode(data);
}
else if (this.extension == 'aiff' || this.extension == 'aif') {
decoder = new AIFFDecoder();
decoded = decoder.decode(data);
}
this.onSuccess(decoded);
};
function Decoder() {
}
Decoder.prototype.readString = function(data, offset, length) {
return data.slice(offset, offset + length);
};
Decoder.prototype.readIntL = function(data, offset, length) {
var value = 0;
for (var i = 0; i < length; i++) {
value = value + ((data.charCodeAt(offset + i) & 0xFF) *
Math.pow(2, 8 * i));
}
return value;
};
Decoder.prototype.readChunkHeaderL = function(data, offset) {
var chunk = {};
chunk.name = this.readString(data, offset, 4);
chunk.length = this.readIntL(data, offset + 4, 4);
return chunk;
};
Decoder.prototype.readIntB = function(data, offset, length) {
var value = 0;
for (var i = 0; i < length; i++) {
value = value + ((data.charCodeAt(offset + i) & 0xFF) *
Math.pow(2, 8 * (length - i - 1)));
}
return value;
};
Decoder.prototype.readChunkHeaderB = function(data, offset) {
var chunk = {};
chunk.name = this.readString(data, offset, 4);
chunk.length = this.readIntB(data, offset + 4, 4);
return chunk;
};
Decoder.prototype.readFloatB = function(data, offset) {
var expon = this.readIntB(data, offset, 2);
var range = 1 << 16 - 1;
if (expon >= range) {
expon |= ~(range - 1);
}
var sign = 1;
if (expon < 0) {
sign = -1;
expon += range;
}
var himant = this.readIntB(data, offset + 2, 4);
var lomant = this.readIntB(data, offset + 6, 4);
var value;
if (expon == himant == lomant == 0) {
value = 0;
}
else if (expon == 0x7FFF) {
value = Number.MAX_VALUE;
}
else {
expon -= 16383;
value = (himant * 0x100000000 + lomant) * Math.pow(2, expon - 63);
}
return sign * value;
};
function WAVDecoder(data) {
}
WAVDecoder.prototype.__proto__ = Decoder.prototype;
WAVDecoder.prototype.decode = function(data) {
var decoded = {};
var offset = 0;
// Header
var chunk = this.readChunkHeaderL(data, offset);
offset += 8;
if (chunk.name != 'RIFF') {
console.error('File is not a WAV');
return null;
}
var fileLength = chunk.length;
fileLength += 8;
var wave = this.readString(data, offset, 4);
offset += 4;
if (wave != 'WAVE') {
console.error('File is not a WAV');
return null;
}
while (offset < fileLength) {
var chunk = this.readChunkHeaderL(data, offset);
offset += 8;
if (chunk.name == 'fmt ') {
// File encoding
var encoding = this.readIntL(data, offset, 2);
offset += 2;
if (encoding != 0x0001) {
// Only support PCM
console.error('Cannot decode non-PCM encoded WAV file');
return null;
}
// Number of channels
var numberOfChannels = this.readIntL(data, offset, 2);
offset += 2;
// Sample rate
var sampleRate = this.readIntL(data, offset, 4);
offset += 4;
// Ignore bytes/sec - 4 bytes
offset += 4;
// Ignore block align - 2 bytes
offset += 2;
// Bit depth
var bitDepth = this.readIntL(data, offset, 2);
var bytesPerSample = bitDepth / 8;
offset += 2;
}
else if (chunk.name == 'data') {
// Data must come after fmt, so we are okay to use it's variables
// here
var length = chunk.length / (bytesPerSample * numberOfChannels);
var channels = [];
for (var i = 0; i < numberOfChannels; i++) {
channels.push(new Float32Array(length));
}
for (var i = 0; i < numberOfChannels; i++) {
var channel = channels[i];
for (var j = 0; j < length; j++) {
var index = offset;
index += (j * numberOfChannels + i) * bytesPerSample;
// Sample
var value = this.readIntL(data, index, bytesPerSample);
// Scale range from 0 to 2**bitDepth -> -2**(bitDepth-1) to
// 2**(bitDepth-1)
var range = 1 << bitDepth - 1;
if (value >= range) {
value |= ~(range - 1);
}
// Scale range to -1 to 1
channel[j] = value / range;
}
}
offset += chunk.length;
}
else {
offset += chunk.length;
}
}
decoded.sampleRate = sampleRate;
decoded.bitDepth = bitDepth;
decoded.channels = channels;
decoded.length = length;
return decoded;
};
function AIFFDecoder() {
}
AIFFDecoder.prototype.__proto__ = Decoder.prototype;
AIFFDecoder.prototype.decode = function(data) {
var decoded = {};
var offset = 0;
// Header
var chunk = this.readChunkHeaderB(data, offset);
offset += 8;
if (chunk.name != 'FORM') {
console.error('File is not an AIFF');
return null;
}
var fileLength = chunk.length;
fileLength += 8;
var aiff = this.readString(data, offset, 4);
offset += 4;
if (aiff != 'AIFF') {
console.error('File is not an AIFF');
return null;
}
while (offset < fileLength) {
var chunk = this.readChunkHeaderB(data, offset);
offset += 8;
if (chunk.name == 'COMM') {
// Number of channels
var numberOfChannels = this.readIntB(data, offset, 2);
offset += 2;
// Number of samples
var length = this.readIntB(data, offset, 4);
offset += 4;
var channels = [];
for (var i = 0; i < numberOfChannels; i++) {
channels.push(new Float32Array(length));
}
// Bit depth
var bitDepth = this.readIntB(data, offset, 2);
var bytesPerSample = bitDepth / 8;
offset += 2;
// Sample rate
var sampleRate = this.readFloatB(data, offset);
offset += 10;
}
else if (chunk.name == 'SSND') {
// Data offset
var dataOffset = this.readIntB(data, offset, 4);
offset += 4;
// Ignore block size
offset += 4;
// Skip over data offset
offset += dataOffset;
for (var i = 0; i < numberOfChannels; i++) {
var channel = channels[i];
for (var j = 0; j < length; j++) {
var index = offset;
index += (j * numberOfChannels + i) * bytesPerSample;
// Sample
var value = this.readIntB(data, index, bytesPerSample);
// Scale range from 0 to 2**bitDepth -> -2**(bitDepth-1) to
// 2**(bitDepth-1)
var range = 1 << bitDepth - 1;
if (value >= range) {
value |= ~(range - 1);
}
// Scale range to -1 to 1
channel[j] = value / range;
}
}
offset += chunk.length - dataOffset - 8;
}
else {
offset += chunk.length;
}
}
decoded.sampleRate = sampleRate;
decoded.bitDepth = bitDepth;
decoded.channels = channels;
decoded.length = length;
return decoded;
};
/*
* @depends ../audiofile/audiofile.js
*/
/**
* A variable size multi-channel audio buffer.
*
* @constructor
* @param {Number} numberOfChannels The initial number of channels.
* @param {Number} length The length in samples of each channel.
*/
var AudioletBuffer = function(numberOfChannels, length) {
this.numberOfChannels = numberOfChannels;
this.length = length;
this.channels = [];
for (var i = 0; i < this.numberOfChannels; i++) {
this.channels.push(new Float32Array(length));
}
this.unslicedChannels = [];
for (var i = 0; i < this.numberOfChannels; i++) {
this.unslicedChannels.push(this.channels[i]);
}
this.isEmpty = false;
this.channelOffset = 0;
};
/**
* Get a single channel of data
*
* @param {Number} channel The index of the channel.
* @return {Float32Array} The requested channel.
*/
AudioletBuffer.prototype.getChannelData = function(channel) {
return (this.channels[channel]);
};
/**
* Set the data in the buffer by copying data from a second buffer
*
* @param {AudioletBuffer} buffer The buffer to copy data from.
*/
AudioletBuffer.prototype.set = function(buffer) {
var numberOfChannels = buffer.numberOfChannels;
for (var i = 0; i < numberOfChannels; i++) {
this.channels[i].set(buffer.getChannelData(i));
}
};
/**
* Set the data in a section of the buffer by copying data from a second buffer
*
* @param {AudioletBuffer} buffer The buffer to copy data from.
* @param {Number} length The number of samples to copy.
* @param {Number} [inputOffset=0] An offset to read data from.
* @param {Number} [outputOffset=0] An offset to write data to.
*/
AudioletBuffer.prototype.setSection = function(buffer, length, inputOffset,
outputOffset) {
inputOffset = inputOffset || 0;
outputOffset = outputOffset || 0;
var numberOfChannels = buffer.numberOfChannels;
for (var i = 0; i < numberOfChannels; i++) {
// Begin subarray-of-subarray fix
inputOffset += buffer.channelOffset;
outputOffset += this.channelOffset;
var channel1 = this.unslicedChannels[i].subarray(outputOffset,
outputOffset +
length);
var channel2 = buffer.unslicedChannels[i].subarray(inputOffset,
inputOffset +
length);
// End subarray-of-subarray fix
// Uncomment the following lines when subarray-of-subarray is fixed
/*!
var channel1 = this.getChannelData(i).subarray(outputOffset,
outputOffset +
length);
var channel2 = buffer.getChannelData(i).subarray(inputOffset,
inputOffset +
length);
*/
channel1.set(channel2);
}
};
/**
* Add the data from a second buffer to the data in this buffer
*
* @param {AudioletBuffer} buffer The buffer to add data from.
*/
AudioletBuffer.prototype.add = function(buffer) {
var length = this.length;
var numberOfChannels = buffer.numberOfChannels;
for (var i = 0; i < numberOfChannels; i++) {
var channel1 = this.getChannelData(i);
var channel2 = buffer.getChannelData(i);
for (var j = 0; j < length; j++) {
channel1[j] += channel2[j];
}
}
};
/**
* Add the data from a section of a second buffer to the data in this buffer
*
* @param {AudioletBuffer} buffer The buffer to add data from.
* @param {Number} length The number of samples to add.
* @param {Number} [inputOffset=0] An offset to read data from.
* @param {Number} [outputOffset=0] An offset to write data to.
*/
AudioletBuffer.prototype.addSection = function(buffer, length, inputOffset,
outputOffset) {
inputOffset = inputOffset || 0;
outputOffset = outputOffset || 0;
var numberOfChannels = buffer.numberOfChannels;
for (var i = 0; i < numberOfChannels; i++) {
var channel1 = this.getChannelData(i);
var channel2 = buffer.getChannelData(i);
for (var j = 0; j < length; j++) {
channel1[j + outputOffset] += channel2[j + inputOffset];
}
}
};
/**
* Resize the buffer. This operation can optionally be lazy, which is
* generally faster but doesn't necessarily result in an empty buffer.
*
* @param {Number} numberOfChannel The new number of channels.
* @param {Number} length The new length of each channel.
* @param {Boolean} [lazy=false] If true a resized buffer may not be empty.
* @param {Number} [offset=0] An offset to resize from.
*/
AudioletBuffer.prototype.resize = function(numberOfChannels, length, lazy,
offset) {
offset = offset || 0;
// Local variables
var channels = this.channels;
var unslicedChannels = this.unslicedChannels;
var oldLength = this.length;
var channelOffset = this.channelOffset + offset;
for (var i = 0; i < numberOfChannels; i++) {
// Get the current channels
var channel = channels[i];
var unslicedChannel = unslicedChannels[i];
if (length > oldLength) {
// We are increasing the size of the buffer
var oldChannel = channel;
if (!lazy ||
!unslicedChannel ||
unslicedChannel.length < length) {
// Unsliced channel is not empty when it needs to be,
// does not exist, or is not large enough, so needs to be
// (re)created
unslicedChannel = new Float32Array(length);
}
channel = unslicedChannel.subarray(0, length);
if (!lazy && oldChannel) {
channel.set(oldChannel, offset);
}
channelOffset = 0;
}
else {
// We are decreasing the size of the buffer
if (!unslicedChannel) {
// Unsliced channel does not exist
// We can assume that we always have at least one unsliced
// channel, so we can copy its length
var unslicedLength = unslicedChannels[0].length;
unslicedChannel = new Float32Array(unslicedLength);
}
// Begin subarray-of-subarray fix
offset = channelOffset;
channel = unslicedChannel.subarray(offset, offset + length);
// End subarray-of-subarray fix
// Uncomment the following lines when subarray-of-subarray is
// fixed.
// TODO: Write version where subarray-of-subarray is used
}
channels[i] = channel;
unslicedChannels[i] = unslicedChannel;
}
this.channels = channels.slice(0, numberOfChannels);
this.unslicedChannels = unslicedChannels.slice(0, numberOfChannels);
this.length = length;
this.numberOfChannels = numberOfChannels;
this.channelOffset = channelOffset;
};
/**
* Append the data from a second buffer to the end of the buffer
*
* @param {AudioletBuffer} buffer The buffer to append to this buffer.
*/
AudioletBuffer.prototype.push = function(buffer) {
var bufferLength = buffer.length;
this.resize(this.numberOfChannels, this.length + bufferLength);
this.setSection(buffer, bufferLength, 0, this.length - bufferLength);
};
/**
* Remove data from the end of the buffer, placing it in a second buffer.
*
* @param {AudioletBuffer} buffer The buffer to move data into.
*/
AudioletBuffer.prototype.pop = function(buffer) {
var bufferLength = buffer.length;
var offset = this.length - bufferLength;
buffer.setSection(this, bufferLength, offset, 0);
this.resize(this.numberOfChannels, offset);
};
/**
* Prepend data from a second buffer to the beginning of the buffer.
*
* @param {AudioletBuffer} buffer The buffer to prepend to this buffer.
*/
AudioletBuffer.prototype.unshift = function(buffer) {
var bufferLength = buffer.length;
this.resize(this.numberOfChannels, this.length + bufferLength, false,
bufferLength);
this.setSection(buffer, bufferLength, 0, 0);
};
/**
* Remove data from the beginning of the buffer, placing it in a second buffer.
*
* @param {AudioletBuffer} buffer The buffer to move data into.
*/
AudioletBuffer.prototype.shift = function(buffer) {
var bufferLength = buffer.length;
buffer.setSection(this, bufferLength, 0, 0);
this.resize(this.numberOfChannels, this.length - bufferLength,
false, bufferLength);
};
/**
* Make all values in the buffer 0
*/
AudioletBuffer.prototype.zero = function() {
var numberOfChannels = this.numberOfChannels;
for (var i = 0; i < numberOfChannels; i++) {
var channel = this.getChannelData(i);
var length = this.length;
for (var j = 0; j < length; j++) {
channel[j] = 0;
}
}
};
/**
* Copy the buffer into a single Float32Array, with each channel appended to
* the end of the previous one.
*
* @return {Float32Array} The combined array of data.
*/
AudioletBuffer.prototype.combined = function() {
var channels = this.channels;
var numberOfChannels = this.numberOfChannels;
var length = this.length;
var combined = new Float32Array(numberOfChannels * length);
for (var i = 0; i < numberOfChannels; i++) {
combined.set(channels[i], i * length);
}
return combined;
};
/**
* Copy the buffer into a single Float32Array, with the channels interleaved.
*
* @return {Float32Array} The interleaved array of data.
*/
AudioletBuffer.prototype.interleaved = function() {
var channels = this.channels;
var numberOfChannels = this.numberOfChannels;
var length = this.length;
var interleaved = new Float32Array(numberOfChannels * length);
for (var i = 0; i < length; i++) {
for (var j = 0; j < numberOfChannels; j++) {
interleaved[numberOfChannels * i + j] = channels[j][i];
}
}
return interleaved;
};
/**
* Return a new copy of the buffer.
*
* @return {AudioletBuffer} The copy of the buffer.
*/
AudioletBuffer.prototype.copy = function() {
var buffer = new AudioletBuffer(this.numberOfChannels, this.length);
buffer.set(this);
return buffer;
};
/**
* Load a .wav or .aiff file into the buffer using audiofile.js
*
* @param {String} path The path to the file.
* @param {Boolean} [async=true] Whether to load the file asynchronously.
* @param {Function} [callback] Function called if the file loaded sucessfully.
*/
AudioletBuffer.prototype.load = function(path, async, callback) {
var request = new AudioFileRequest(path, async);
request.onSuccess = function(decoded) {
this.length = decoded.length;
this.numberOfChannels = decoded.channels.length;
this.unslicedChannels = decoded.channels;
this.channels = decoded.channels;
this.channelOffset = 0;
if (callback) {
callback();
}
}.bind(this);
request.onFailure = function() {
console.error('Could not load', path);
}.bind(this);
request.send();
};
/**
* A container for collections of connected AudioletNodes. Groups make it
* possible to create multiple copies of predefined networks of nodes,
* without having to manually create and connect up each individual node.
*
* From the outside groups look and behave exactly the same as nodes.
* Internally you can connect nodes directly to the group's inputs and
* outputs, allowing connection to nodes outside of the group.
*
* @constructor
* @param {Audiolet} audiolet The audiolet object.
* @param {Number} numberOfInputs The number of inputs.
* @param {Number} numberOfOutputs The number of outputs.
*/
var AudioletGroup = function(audiolet, numberOfInputs, numberOfOutputs) {
this.audiolet = audiolet;
this.inputs = [];
for (var i = 0; i < numberOfInputs; i++) {
this.inputs.push(new PassThroughNode(this.audiolet, 1, 1));
}
this.outputs = [];
for (var i = 0; i < numberOfOutputs; i++) {
this.outputs.push(new PassThroughNode(this.audiolet, 1, 1));
}
};
/**
* Connect the group to another node or group
*
* @param {AudioletNode|AudioletGroup} node The node to connect to.
* @param {Number} [output=0] The index of the output to connect from.
* @param {Number} [input=0] The index of the input to connect to.
*/
AudioletGroup.prototype.connect = function(node, output, input) {
this.outputs[output || 0].connect(node, 0, input);
};
/**
* Disconnect the group from another node or group
*
* @param {AudioletNode|AudioletGroup} node The node to disconnect from.
* @param {Number} [output=0] The index of the output to disconnect.
* @param {Number} [input=0] The index of the input to disconnect.
*/
AudioletGroup.prototype.disconnect = function(node, output, input) {
this.outputs[output || 0].disconnect(node, 0, input);
};
/**
* Remove the group completely from the processing graph, disconnecting all
* of its inputs and outputs
*/
AudioletGroup.prototype.remove = function() {
var numberOfInputs = this.inputs.length;
for (var i = 0; i < numberOfInputs; i++) {
this.inputs[i].remove();
}
var numberOfOutputs = this.outputs.length;
for (var i = 0; i < numberOfOutputs; i++) {
this.outputs[i].remove();
}
};
/*!
* @depends AudioletGroup.js
*/
/**
* Group containing all of the components for the Audiolet output chain. The
* chain consists of:
*
* Input => Scheduler => UpMixer => Output
*
* **Inputs**
*
* - Audio
*
* @constructor
* @extends AudioletGroup
* @param {Audiolet} audiolet The audiolet object.
* @param {Number} [sampleRate=44100] The sample rate to run at.
* @param {Number} [numberOfChannels=2] The number of output channels.
* @param {Number} [bufferSize=8192] A fixed buffer size to use.
*/
var AudioletDestination = function(audiolet, sampleRate, numberOfChannels,
bufferSize) {
AudioletGroup.call(this, audiolet, 1, 0);
this.device = new AudioletDevice(audiolet, sampleRate,
numberOfChannels, bufferSize);
audiolet.device = this.device; // Shortcut
this.scheduler = new Scheduler(audiolet);
audiolet.scheduler = this.scheduler; // Shortcut
this.upMixer = new UpMixer(audiolet, this.device.numberOfChannels);
this.inputs[0].connect(this.scheduler);
this.scheduler.connect(this.upMixer);
this.upMixer.connect(this.device);
};
extend(AudioletDestination, AudioletGroup);
/**
* toString
*
* @return {String} String representation.
*/
AudioletDestination.prototype.toString = function() {
return 'Destination';
};
/**
* The basic building block of Audiolet applications. Nodes are connected
* together to create a processing graph which governs the flow of audio data.
* AudioletNodes can contain any number of inputs and outputs which send and
* receive one or more channels of audio data. Audio data is created and
* processed using the generate function, which is called whenever new data is
* needed.
*
* @constructor
* @param {Audiolet} audiolet The audiolet object.
* @param {Number} numberOfInputs The number of inputs.
* @param {Number} numberOfOutputs The number of outputs.
* @param {Function} [generate] A replacement for the generate function.
*/
var AudioletNode = function(audiolet, numberOfInputs, numberOfOutputs,
generate) {
this.audiolet = audiolet;
this.inputs = [];
for (var i = 0; i < numberOfInputs; i++) {
this.inputs.push(new AudioletInput(this, i));
}
this.outputs = [];
for (var i = 0; i < numberOfOutputs; i++) {
this.outputs.push(new AudioletOutput(this, i));
}
if (generate) {
this.generate = generate;
}
};
/**
* Connect the node to another node or group.
*
* @param {AudioletNode|AudioletGroup} node The node to connect to.
* @param {Number} [output=0] The index of the output to connect from.
* @param {Number} [input=0] The index of the input to connect to.
*/
AudioletNode.prototype.connect = function(node, output, input) {
if (node instanceof AudioletGroup) {
// Connect to the pass-through node rather than the group
node = node.inputs[input || 0];
input = 0;
}
var outputPin = this.outputs[output || 0];
var inputPin = node.inputs[input || 0];
outputPin.connect(inputPin);
inputPin.connect(outputPin);
this.audiolet.device.needTraverse = true;
};
/**
* Disconnect the node from another node or group
*
* @param {AudioletNode|AudioletGroup} node The node to disconnect from.
* @param {Number} [output=0] The index of the output to disconnect.
* @param {Number} [input=0] The index of the input to disconnect.
*/
AudioletNode.prototype.disconnect = function(node, output, input) {
if (node instanceof AudioletGroup) {
node = node.inputs[input || 0];
input = 0;
}
var outputPin = this.outputs[output || 0];
var inputPin = node.inputs[input || 0];
inputPin.disconnect(outputPin);
outputPin.disconnect(inputPin);
this.audiolet.device.needTraverse = true;
};
/**
* Force an output to contain a fixed number of channels.
*
* @param {Number} output The index of the output.
* @param {Number} numberOfChannels The number of channels.
*/
AudioletNode.prototype.setNumberOfOutputChannels = function(output,
numberOfChannels) {
this.outputs[output].numberOfChannels = numberOfChannels;
};
/**
* Link an output to an input, forcing the output to always contain the
* same number of channels as the input.
*
* @param {Number} output The index of the output.
* @param {Number} input The index of the input.
*/
AudioletNode.prototype.linkNumberOfOutputChannels = function(output, input) {
this.outputs[output].linkNumberOfChannels(this.inputs[input]);
};
/**
* Process samples a from each channel. This function should not be called
* manually by users, who should instead rely on automatic ticking from
* connections to the AudioletDevice.
*/
AudioletNode.prototype.tick = function() {
this.createInputSamples();
this.createOutputSamples();
this.generate();
};
/**
* Traverse the audio graph, adding this and any parent nodes to the nodes
* array.
*
* @param {AudioletNode[]} nodes Array to add nodes to.
*/
AudioletNode.prototype.traverse = function(nodes) {
if (nodes.indexOf(this) == -1) {
nodes.push(this);
nodes = this.traverseParents(nodes);
}
return nodes;
};
/**
* Call the traverse function on nodes which are connected to the inputs.
*/
AudioletNode.prototype.traverseParents = function(nodes) {
var numberOfInputs = this.inputs.length;
for (var i = 0; i < numberOfInputs; i++) {
var input = this.inputs[i];
var numberOfStreams = input.connectedFrom.length;
for (var j = 0; j < numberOfStreams; j++) {
nodes = input.connectedFrom[j].node.traverse(nodes);
}
}
return nodes;
};
/**
* Process a sample for each channel, reading from the inputs and putting new
* values into the outputs. Override me!
*/
AudioletNode.prototype.generate = function() {
};
/**
* Create the input samples by grabbing data from the outputs of connected
* nodes and summing it. If no nodes are connected to an input, then
* give an empty array
*/
AudioletNode.prototype.createInputSamples = function() {
var numberOfInputs = this.inputs.length;
for (var i = 0; i < numberOfInputs; i++) {
var input = this.inputs[i];
var numberOfInputChannels = 0;
for (var j = 0; j < input.connectedFrom.length; j++) {
var output = input.connectedFrom[j];
for (var k = 0; k < output.samples.length; k++) {
var sample = output.samples[k];
if (k < numberOfInputChannels) {
input.samples[k] += sample;
}
else {
input.samples[k] = sample;
numberOfInputChannels += 1;
}
}
}
if (input.samples.length > numberOfInputChannels) {
input.samples = input.samples.slice(0, numberOfInputChannels);
}
}
};
/**
* Create output samples for each channel.
*/
AudioletNode.prototype.createOutputSamples = function() {
var numberOfOutputs = this.outputs.length;
for (var i = 0; i < numberOfOutputs; i++) {
var output = this.outputs[i];
var numberOfChannels = output.getNumberOfChannels();
if (output.samples.length == numberOfChannels) {
continue;
}
else if (output.samples.length > numberOfChannels) {
output.samples = output.samples.slice(0, numberOfChannels);
continue;
}
for (var j = output.samples.length; j < numberOfChannels; j++) {
output.samples[j] = 0;
}
}
};
/**
* Remove the node completely from the processing graph, disconnecting all
* of its inputs and outputs.
*/
AudioletNode.prototype.remove = function() {
// Disconnect inputs
var numberOfInputs = this.inputs.length;
for (var i = 0; i < numberOfInputs; i++) {
var input = this.inputs[i];
var numberOfStreams = input.connectedFrom.length;
for (var j = 0; j < numberOfStreams; j++) {
var outputPin = input.connectedFrom[j];
var output = outputPin.node;
output.disconnect(this, outputPin.index, i);
}
}
// Disconnect outputs
var numberOfOutputs = this.outputs.length;
for (var i = 0; i < numberOfOutputs; i++) {
var output = this.outputs[i];
var numberOfStreams = output.connectedTo.length;
for (var j = 0; j < numberOfStreams; j++) {
var inputPin = output.connectedTo[j];
var input = inputPin.node;
this.disconnect(input, i, inputPin.index);
}
}
};
/*!
* @depends AudioletNode.js
*/
/**