-
Notifications
You must be signed in to change notification settings - Fork 1
/
toneGenerator.js
63 lines (48 loc) · 2.04 KB
/
toneGenerator.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
/*jslint devel: true, browser: true, nomen: true, maxerr: 50, indent: 4 */
(function (global) {
"use strict";
var ToneGenerator = function () {
};
ToneGenerator.prototype.init = function () {
var oscillator,
amp;
if (global.context) {
oscillator = global.context.createOscillator();
global.fixOscillator(oscillator);
oscillator.frequency.value = 440;
amp = global.context.createGain ? global.context.createGain() : global.context.createGainNode();
amp.gain.value = 0;
// Connect ooscillator to amp and amp to the mixer of the context.
// This is like connecting cables between jacks on a modular synth.
oscillator.connect(amp);
amp.connect(global.context.destination);
oscillator.start(0);
this._context = global.context;
this._amp = amp;
this._oscillator = oscillator;
}
};
ToneGenerator.prototype.startTone = function (frequency) {
var context = this._context,
amp = this._amp,
oscillator = this._oscillator,
now = context.currentTime;
oscillator.frequency.setValueAtTime(frequency, now);
// Ramp up the gain so we can hear the sound.
// We can ramp smoothly to the desired value.
// First we should cancel any previous scheduled events that might interfere.
amp.gain.cancelScheduledValues(now);
// Anchor beginning of ramp at current value.
amp.gain.setValueAtTime(amp.gain.value, now);
amp.gain.linearRampToValueAtTime(0.5, context.currentTime + 0.1);
};
ToneGenerator.prototype.stopTone = function () {
var context = this._context,
amp = this._amp,
now = context.currentTime;
amp.gain.cancelScheduledValues(now);
amp.gain.setValueAtTime(amp.gain.value, now);
amp.gain.linearRampToValueAtTime(0.0, context.currentTime + 1.0);
};
global.ToneGenerator = ToneGenerator;
}(this));