-
Notifications
You must be signed in to change notification settings - Fork 3
/
Pizzicato.js
1062 lines (816 loc) · 25.3 KB
/
Pizzicato.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(root) {
'use strict';
var Pizzicato = root.Pz = root.Pizzicato = {};
var Context = window.AudioContext || window.webkitAudioContext;
Pizzicato.context = new Context();
Pizzicato.Events = {
/**
* Adds an event handler that will be treated upon
* the triggering of that event.
*/
on: function(name, callback, context) {
if (!name || !callback)
return;
this._events = this._events || {};
var _event = this._events[name] || (this._events[name] = []);
_event.push({
callback: callback,
context: context || this,
handler: this
});
},
/**
* Triggers a particular event. If a handler
* is linked to that event, the handler will be
* executed.
*/
trigger: function(name) {
if (!name)
return;
var _event, length, args, i;
this._events = this._events || {};
_event = this._events[name] || (this._events[name] = []);
if (!_event)
return;
length = Math.max(0, arguments.length - 1);
args = [];
for (i = 0; i < length; i++) args[i] = arguments[i + 1];
for (i = 0; i < _event.length; i++)
_event[i].callback.apply(_event[i].context, args);
},
/**
* Removes an event handler. If no name is provided,
* all handlers for this object will be removed.
*/
off: function(name) {
if (name)
this._events[name] = undefined;
else
this._events = {};
}
};
Pizzicato.Util = {
isString: function(arg) {
return toString.call(arg) === '[object String]';
},
isObject: function(arg) {
return toString.call(arg) === '[object Object]';
},
isFunction: function(arg) {
return toString.call(arg) === '[object Function]';
},
isNumber: function(arg) {
return toString.call(arg) === '[object Number]' && arg === +arg;
},
isInRange: function(arg, min, max) {
if (!Pz.Util.isNumber(arg) || !Pz.Util.isNumber(min) || !Pz.Util.isNumber(max))
return false;
return arg >= min && arg <= max;
},
isOscillator: function(audioNode) {
return (audioNode && audioNode.toString() === "[object OscillatorNode]");
},
// Takes a number from 0 to 1 and normalizes it
// to fit within range floor to ceiling
normalize: function(num, floor, ceil) {
if (!Pz.Util.isNumber(num) || !Pz.Util.isNumber(floor) || !Pz.Util.isNumber(ceil))
return;
return ((ceil - floor) * num) / 1 + floor;
},
getDryLevel: function(mix) {
if (!Pz.Util.isNumber(mix) || mix > 1 || mix < 0)
return 0;
if (mix <= 0.5)
return 1;
return 1 - ((mix - 0.5) * 2);
},
getWetLevel: function(mix) {
if (!Pz.Util.isNumber(mix) || mix > 1 || mix < 0)
return 0;
if (mix >= 0.5)
return 1;
return 1 - ((0.5 - mix) * 2);
}
};
Pizzicato.Sound = function(description, callback) {
var self = this;
var util = Pizzicato.Util;
var descriptionError = getDescriptionError(description);
var hasOptions = util.isObject(description) && util.isObject(description.options);
var defaultAttack = 0.04;
var defaultSustain = 0.04;
if (descriptionError) {
console.error(descriptionError);
throw new Error('Error initializing Pizzicato Sound: ' + descriptionError);
}
this.masterVolume = this.getMasterVolume();
this.lastTimePlayed = 0;
this.effects = [];
this.playing = this.paused = false;
this.loop = hasOptions && description.options.loop;
this.attack = hasOptions && util.isNumber(description.options.attack) ? description.options.attack : defaultAttack;
this.sustain = hasOptions && util.isNumber(description.options.sustain) ? description.options.sustain : defaultSustain;
this.volume = hasOptions && util.isNumber(description.options.volume) ? description.options.volume : 1;
if (!description)
(initializeWithWave.bind(this))({}, callback);
else if (util.isString(description))
(initializeWithUrl.bind(this))(description, callback);
else if (util.isFunction(description))
(initializeWithFunction.bind(this))(description, callback);
else if (description.source === 'file')
(initializeWithUrl.bind(this))(description.options.path, callback);
else if (description.source === 'wave')
(initializeWithWave.bind(this))(description.options, callback);
else if (description.source === 'input')
(initializeWithInput.bind(this))(description, callback);
else if (description.source === 'script')
(initializeWithFunction.bind(this))(description.options, callback);
function getDescriptionError (description) {
var supportedSources = ['wave', 'file', 'input', 'script'];
if (description && (!util.isFunction(description) && !util.isString(description) && !util.isObject(description)))
return 'Description type not supported. Initialize a sound using an object, a function or a string.';
if (util.isObject(description)) {
if (!util.isString(description.source) || supportedSources.indexOf(description.source) === -1)
return 'Specified source not supported. Sources can be wave, file, input or script';
if (description.source === 'file' && (!description.options || !description.options.path))
return 'A path is needed for sounds with a file source';
if (description.source === 'script' && (!description.options || !description.options.audioFunction))
return 'An audio function is needed for sounds with a script source';
}
}
function initializeWithWave (waveOptions, callback) {
waveOptions = waveOptions || {};
this.getRawSourceNode = function() {
var frequency = this.sourceNode ? this.sourceNode.frequency.value : waveOptions.frequency;
var node = Pizzicato.context.createOscillator();
node.type = waveOptions.type || 'sine';
node.frequency.value = (frequency || 440);
return node;
};
this.sourceNode = this.getRawSourceNode();
if (util.isFunction(callback))
callback();
}
function initializeWithUrl (url, callback) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = function(progressEvent) {
var response = progressEvent.target.response;
Pizzicato.context.decodeAudioData(response, (function(buffer) {
self.getRawSourceNode = function() {
var node = Pizzicato.context.createBufferSource();
node.loop = this.loop;
node.buffer = buffer;
return node;
};
if (util.isFunction(callback))
callback();
}).bind(self), (function(error) {
error = error || new Error('Error decoding audio file ' + url);
if (util.isFunction(callback))
callback(error);
}).bind(self));
};
request.onreadystatechange = function(event) {
if (request.readyState === 4 && request.status !== 200)
console.error('Error while fetching ' + url + '. ' + request.statusText);
};
request.send();
}
function initializeWithInput(options, callback) {
navigator.getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
if (!navigator.getUserMedia) return;
navigator.getUserMedia({ audio: true }, (function(stream) {
self.getRawSourceNode = function() {
return Pizzicato.context.createMediaStreamSource(stream);
};
if (util.isFunction(callback))
callback();
}).bind(self), function(error) {
if (util.isFunction(callback))
callback(error);
});
}
function initializeWithFunction(options, callback) {
var audioFunction = util.isFunction(options) ? options : options.audioFunction;
var bufferSize = util.isObject(options) && options.bufferSize ? options.bufferSize : null;
if (!bufferSize) {
try { // Webkit does not automatically chose the buffer size
var test = Pizzicato.context.createScriptProcessor();
} catch (e) {
bufferSize = 2048;
}
}
this.getRawSourceNode = function() {
var node = Pizzicato.context.createScriptProcessor(bufferSize, 1, 1);
node.onaudioprocess = audioFunction;
return node;
};
}
};
Pizzicato.Sound.prototype = Object.create(Pizzicato.Events, {
play: {
enumerable: true,
value: function() {
if (this.playing)
return;
this.playing = true;
this.paused = false;
this.sourceNode = this.getSourceNode();
if (this.attack)
this.applyAttack();
if (Pz.Util.isFunction(this.sourceNode.start)) {
this.lastTimePlayed = Pizzicato.context.currentTime;
this.sourceNode.start(0, this.startTime || 0);
}
this.trigger('play');
}
},
stop: {
enumerable: true,
value: function() {
if (!this.paused && !this.playing)
return;
this.paused = this.playing = false;
var stopSound = function(node) {
return Pz.Util.isFunction(node.stop) ? node.stop(0) : node.disconnect();
};
if (this.sustain)
this.applySustain(stopSound);
else
stopSound(this.sourceNode);
this.startTime = 0;
this.trigger('stop');
}
},
pause: {
enumerable: true,
value: function() {
if (this.paused || !this.playing)
return;
this.paused = true;
this.playing = false;
var stopSound = function(node) {
return Pz.Util.isFunction(node.stop) ? node.stop(0) : node.disconnect();
};
if (this.sustain)
this.applySustain(stopSound);
else
stopSound(this.sourceNode);
this.startTime = Pz.context.currentTime - this.lastTimePlayed;
this.trigger('pause');
}
},
onEnded: {
enumerable: true,
value: function() {
if (!this.paused)
this.trigger('end');
}
},
addEffect: {
enumerable: true,
value: function(effect) {
this.effects.push(effect);
this.connectEffects();
if (!!this.sourceNode) {
this.sourceNode.fadeNode.disconnect();
this.sourceNode.fadeNode.connect(this.getInputNode());
}
}
},
removeEffect: {
enumerable: true,
value: function(effect) {
var index = this.effects.indexOf(effect);
if (index === -1) return;
this.effects.splice(index, 1);
this.connectEffects();
}
},
connectEffects: {
enumerable: true,
value: function() {
for (var i = 0; i < this.effects.length; i++) {
var destinationNode = i < this.effects.length - 1 ? this.effects[i + 1].inputNode : this.masterVolume;
this.effects[i].outputNode.disconnect();
this.effects[i].outputNode.connect(destinationNode);
}
}
},
volume: {
enumerable: true,
get: function() {
if (this.masterVolume)
return this.masterVolume.gain.value;
},
set: function(volume) {
if (Pz.Util.isInRange(volume, 0, 1) && this.masterVolume)
this.masterVolume.gain.value = volume;
}
},
frequency: {
enumerable: true,
get: function() {
if (this.sourceNode && Pz.Util.isOscillator(this.sourceNode)) {
return this.sourceNode.frequency.value;
}
return null;
},
set: function(frequency) {
if (this.sourceNode && Pz.Util.isOscillator(this.sourceNode)) {
this.sourceNode.frequency.value = frequency;
}
}
},
/**
* Returns the node that produces the sound. For example, an oscillator
* if the Sound object was initialized with a wave option.
*
* Each source node has its own attack/sustain gain node. so coming
* out of here are • source -> gain • This fade node can be accessed as
* the fadeNode property of the sourceNode.
*/
getSourceNode: {
enumerable: true,
value: function() {
var sourceNode = this.getRawSourceNode();
var fadeNode = Pizzicato.context.createGain();
sourceNode.connect(fadeNode);
sourceNode.onended = this.onEnded.bind(this);
sourceNode.fadeNode = fadeNode;
fadeNode.connect(this.getInputNode());
return sourceNode;
}
},
/**
* Returns the first node in the graph. When there are effects,
* the first node is the input node of the first effect.
*/
getInputNode: {
enumerable: true,
value: function() {
if (this.effects.length > 0)
return this.effects[0].inputNode;
return this.masterVolume;
}
},
/**
* Returns the node used for the master volume.
*/
getMasterVolume: {
enumerable: true,
value: function() {
if (this.masterVolume)
return this.masterVolume;
var masterVolume = Pizzicato.context.createGain();
masterVolume.connect(Pizzicato.context.destination);
return masterVolume;
}
},
/**
* Will take the current source node and work up the volume
* gradually in as much time as specified in the attack property
* of the sound.
*/
applyAttack: {
enumerable: false,
value: function() {
if (!this.attack)
return;
var fadeNode = this.sourceNode.fadeNode;
fadeNode.gain.setValueAtTime(0.00001, Pizzicato.context.currentTime);
fadeNode.gain.linearRampToValueAtTime(1, Pizzicato.context.currentTime + this.attack);
}
},
/**
* Will take the current source node and work down the volume
* gradually in as much time as specified in the sustain property
* of the sound. After the sustain, an optional callback is called with
* the affected node as parameter (even if in the meanwhile the source
* node has changed).
*/
applySustain: {
enumerable: false,
value: function(callback) {
if (!this.sustain)
return;
var node = this.sourceNode;
var fadeNode = node.fadeNode;
fadeNode.gain.setValueAtTime(fadeNode.gain.value, Pizzicato.context.currentTime);
fadeNode.gain.linearRampToValueAtTime(0.00001, Pizzicato.context.currentTime + this.sustain);
if (callback)
window.setTimeout(function() { callback(node); }, this.sustain * 1000);
}
}
});
Pizzicato.Effects = {};
Pizzicato.Effects.Delay = function(options) {
this.options = {};
options = options || this.options;
var defaults = {
feedback: 0.5,
time: 0.3,
mix: 0.5
};
this.inputNode = Pizzicato.context.createGain();
this.outputNode = Pizzicato.context.createGain();
this.dryGainNode = Pizzicato.context.createGain();
this.wetGainNode = Pizzicato.context.createGain();
this.feedbackGainNode = Pizzicato.context.createGain();
this.delayNode = Pizzicato.context.createDelay();
this.inputNode.connect(this.dryGainNode);
this.inputNode.connect(this.delayNode);
this.delayNode.connect(this.feedbackGainNode);
this.delayNode.connect(this.wetGainNode);
this.feedbackGainNode.connect(this.delayNode);
this.dryGainNode.connect(this.outputNode);
this.wetGainNode.connect(this.outputNode);
for (var key in defaults) {
this[key] = options[key];
this[key] = (this[key] === undefined || this[key] === null) ? defaults[key] : this[key];
}
};
Pizzicato.Effects.Delay.prototype = Object.create(null, {
/**
* Gets and sets the dry/wet mix.
*/
mix: {
enumerable: true,
get: function() {
return this.options.mix ;
},
set: function(mix) {
if (!Pz.Util.isInRange(mix, 0, 1))
return;
this.options.mix = mix;
this.dryGainNode.gain.value = Pizzicato.Util.getDryLevel(this.mix);
this.wetGainNode.gain.value = Pizzicato.Util.getWetLevel(this.mix);
}
},
/**
* Time between each delayed sound
*/
time: {
enumerable: true,
get: function() {
return this.options.time;
},
set: function(time) {
if (!Pz.Util.isInRange(time, 0, 180))
return;
this.options.time = time;
this.delayNode.delayTime.value = time;
}
},
/**
* Strength of each of the echoed delayed sounds.
*/
feedback: {
enumerable: true,
get: function() {
return this.options.feedback;
},
set: function(feedback) {
if (!Pz.Util.isInRange(feedback, 0, 1))
return;
this.options.feedback = parseFloat(feedback, 10);
this.feedbackGainNode.gain.value = this.feedback;
}
}
});
Pizzicato.Effects.Compressor = function(options) {
this.options = {};
options = options || this.options;
var defaults = {
threshold: -24,
knee: 30,
attack: 0.003,
release: 0.250,
ratio: 12
};
this.inputNode = this.compressorNode = Pizzicato.context.createDynamicsCompressor();
this.outputNode = Pizzicato.context.createGain();
this.compressorNode.connect(this.outputNode);
for (var key in defaults) {
this[key] = options[key];
this[key] = (this[key] === undefined || this[key] === null) ? defaults[key] : this[key];
}
};
Pizzicato.Effects.Compressor.prototype = Object.create(null, {
/**
* The level above which compression is applied to the audio.
* MIN: -100
* MAX: 0
*/
threshold: {
enumerable: true,
get: function() {
return this.compressorNode.threshold.value;
},
set: function(value) {
if (Pizzicato.Util.isInRange(value, -100, 0))
this.compressorNode.threshold.value = value;
}
},
/**
* A value representing the range above the threshold where
* the curve smoothly transitions to the "ratio" portion. More info:
* http://www.homestudiocorner.com/what-is-knee-on-a-compressor/
* MIN 0
* MAX 40
*/
knee: {
enumerable: true,
get: function() {
return this.compressorNode.knee.value;
},
set: function(value) {
if (Pizzicato.Util.isInRange(value, 0, 40))
this.compressorNode.knee.value = value;
}
},
/**
* How soon the compressor starts to compress the dynamics after
* the threshold is exceeded. If volume changes are slow, you can
* push this to a high value. Short attack times will result in a
* fast response to sudden, loud sounds, but will make the changes
* in volume much more obvious to listeners.
* MIN 0
* MAX 1
*/
attack: {
enumerable: true,
get: function() {
return this.compressorNode.attack.value;
},
set: function(value) {
if (Pizzicato.Util.isInRange(value, 0, 1))
this.compressorNode.attack.value = value;
}
},
/**
* How soon the compressor starts to release the volume level
* back to normal after the level drops below the threshold.
* A long time value will tend to lose quiet sounds that come
* after loud ones, but will avoid the volume being raised too
* much during short quiet sections like pauses in speech.
* MIN 0
* MAX 1
*/
release: {
enumerable: true,
get: function() {
return this.compressorNode.release.value;
},
set: function(value) {
if (Pizzicato.Util.isInRange(value, 0, 1))
this.compressorNode.release.value = value;
}
},
/**
* The amount of compression applied to the audio once it
* passes the threshold level. The higher the Ratio the more
* the loud parts of the audio will be compressed.
* MIN 1
* MAX 20
*/
ratio: {
enumerable: true,
get: function() {
return this.compressorNode.ratio.value;
},
set: function(value) {
if (Pizzicato.Util.isInRange(value, 1, 20))
this.compressorNode.ratio.value = value;
}
},
getCurrentGainReduction: function() {
return this.compressorNode.reduction;
}
});
/**
* Frequencies below the cutoff frequency pass
* through; frequencies above it are attenuated.
*/
Pizzicato.Effects.LowPassFilter = function(options) {
Filter.call(this, options, 'lowpass');
};
/**
* Frequencies below the cutoff frequency are
* attenuated; frequencies above it pass through.
*/
Pizzicato.Effects.HighPassFilter = function(options) {
Filter.call(this, options, 'highpass');
};
/**
* Filters used by Pizzicato stem from the biquad filter node. This
* function acts as a common constructor. The only thing that changes
* between filters is the 'type' of the biquad filter node.
*/
function Filter(options, type) {
this.options = {};
options = options || this.options;
var defaults = {
frequency: 350,
peak: 1
};
this.inputNode = this.filterNode = Pz.context.createBiquadFilter();
this.filterNode.type = type;
this.outputNode = Pizzicato.context.createGain();
this.filterNode.connect(this.outputNode);
for (var key in defaults) {
this[key] = options[key];
this[key] = (this[key] === undefined || this[key] === null) ? defaults[key] : this[key];
}
}
var filterPrototype = Object.create(null, {
/**
* The cutoff frequency of the filter.
* MIN: 10
* MAX: 22050 (half the sampling rate of the current context)
*/
frequency: {
enumerable: true,
get: function() {
return this.filterNode.frequency.value;
},
set: function(value) {
if (Pizzicato.Util.isInRange(value, 10, 22050))
this.filterNode.frequency.value = value;
}
},
/**
* Indicates how peaked the frequency is around
* the cutoff. The greater the value is, the
* greater is the peak.
* MIN: 0.0001
* MAX: 1000
*/
peak: {
enumerable: true,
get: function() {
return this.filterNode.Q.value;
},
set: function(value) {
if (Pizzicato.Util.isInRange(value, 0.0001, 1000))
this.filterNode.Q.value = value;
}
}
});
Pizzicato.Effects.LowPassFilter.prototype = filterPrototype;
Pizzicato.Effects.HighPassFilter.prototype = filterPrototype;
Pizzicato.Effects.Distortion = function(options) {
this.options = {};
options = options || this.options;
var defaults = {
gain: 0.5
};
this.waveShaperNode = Pizzicato.context.createWaveShaper();
this.inputNode = this.outputNode = this.waveShaperNode;
for (var key in defaults) {
this[key] = options[key];
this[key] = (this[key] === undefined || this[key] === null) ? defaults[key] : this[key];
}
};
Pizzicato.Effects.Distortion.prototype = Object.create(null, {
/**
* Gets and sets the gain (amount of distortion).
*/
gain: {
enumerable: true,
get: function() {
return this.options.gain;
},
set: function(gain) {
if (!Pz.Util.isInRange(gain, 0, 1))
return;
this.options.gain = gain;
this.adjustGain();
}
},
/**
* Sets the wave curve with the correct gain. Taken from
* http://stackoverflow.com/questions/22312841/waveshaper-node-in-webaudio-how-to-emulate-distortion
*/
adjustGain: {
writable: false,
configurable: false,
enumerable: false,
value: function() {
var gain = Pz.Util.isNumber(this.options.gain) ? parseInt(this.options.gain * 100, 10) : 50;
var n_samples = 44100;
var curve = new Float32Array(n_samples);
var deg = Math.PI / 180;
var x;
for (var i = 0; i < n_samples; ++i ) {
x = i * 2 / n_samples - 1;
curve[i] = (3 + gain) * x * 20 * deg / (Math.PI + gain * Math.abs(x));
}
this.waveShaperNode.curve = curve;
}
}
});
Pizzicato.Effects.Flanger = function(options) {
this.options = {};
options = options || this.options;
var defaults = {
time: 0.45,
speed: 0.2,
depth: 0.1,
feedback: 0.1,
mix: 0.5
};
this.inputNode = Pizzicato.context.createGain();
this.outputNode = Pizzicato.context.createGain();
this.inputFeedbackNode = Pizzicato.context.createGain();
this.wetGainNode = Pizzicato.context.createGain();
this.dryGainNode = Pizzicato.context.createGain();
this.delayNode = Pizzicato.context.createDelay();
this.oscillatorNode = Pizzicato.context.createOscillator();
this.gainNode = Pizzicato.context.createGain();
this.feedbackNode = Pizzicato.context.createGain();
this.oscillatorNode.type = 'sine';
this.inputNode.connect(this.inputFeedbackNode);
this.inputNode.connect(this.dryGainNode);
this.inputFeedbackNode.connect(this.delayNode);
this.inputFeedbackNode.connect(this.wetGainNode);
this.delayNode.connect(this.wetGainNode);
this.delayNode.connect(this.feedbackNode);
this.feedbackNode.connect(this.inputFeedbackNode);
this.oscillatorNode.connect(this.gainNode);
this.gainNode.connect(this.delayNode.delayTime);
this.dryGainNode.connect(this.outputNode);
this.wetGainNode.connect(this.outputNode);
this.oscillatorNode.start(0);
for (var key in defaults) {
this[key] = options[key];
this[key] = (this[key] === undefined || this[key] === null) ? defaults[key] : this[key];
}
};
Pizzicato.Effects.Flanger.prototype = Object.create(null, {
time: {
enumberable: true,
get: function() {
return this.options.time;
},
set: function(time) {
if (!Pz.Util.isInRange(time, 0, 1))
return;
this.options.time = time;
this.delayNode.delayTime.value = Pz.Util.normalize(time, 0.001, 0.02);
}
},
speed: {
enumberable: true,
get: function() {
return this.options.speed;
},
set: function(speed) {
if (!Pz.Util.isInRange(speed, 0, 1))
return;