/
soundfx.js
149 lines (135 loc) · 3.96 KB
/
soundfx.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
/**
* Utils for producing sound effects positioned in 3D.
* @param {=AudioContext} opt_audioContext
* @constructor
*/
function SoundFx(opt_audioContext) {
this.ctx = opt_audioContext || SoundFx.getAudioContext();
if (this.ctx) {
if (!(this.ctx.createGain || this.ctx.createGainNode) || !this.ctx.createOscillator) {
this.ctx = null;
}
}
if (this.ctx) {
this.masterGain = this.createGain();
this.masterGain.connect(this.ctx.destination);
}
}
SoundFx.audioContext = null;
SoundFx.getAudioContext = function() {
if (SoundFx.audioContext != null) {
return SoundFx.audioContext;
} else if (typeof AudioContext !== 'undefined') {
SoundFx.audioContext = new AudioContext();
} else if (typeof webkitAudioContext !== 'undefined') {
SoundFx.audioContext = new webkitAudioContext();
}
return SoundFx.audioContext;
};
SoundFx.prototype.createGain = function() {
if (this.ctx.createGain) {
return this.ctx.createGain();
}
if (this.ctx.createGainNode) {
return this.ctx.createGainNode();
}
return null;
};
SoundFx.prototype.setListenerXYZ = function(x, y, z) {
if (!this.ctx) return;
this.ctx.listener.setPosition(x, y, z);
};
SoundFx.prototype.getMasterGain = function() {
return this.masterGain;
};
/**
* Make a simple one-shot sound.
* @param {number} x
* @param {number} y
* @param {number} z
* @param {number} vol
* @param {number} attack
* @param {number} sustain
* @param {number} decay
* @param {number} freq1
* @param {number} freq2
* @param {String} type Wave type string (square, sine, etc)
* @param {!number} opt_delay optional delay value, before attack
* @return freshly created oscillator node
*/
SoundFx.prototype.sound = function(x, y, z, vol, attack, sustain, decay, freq1, freq2, type, opt_delay) {
if (!this.ctx) return;
if (this.ctx.state != 'running') {
this.ctx.resume();
}
let delay = opt_delay || 0;
let c = this.ctx;
let t0 = c.currentTime + delay;
let t1 = t0 + attack + sustain + decay;
let gain = this.createGain();
if (attack) {
gain.gain.setValueAtTime(0.001, t0);
gain.gain.exponentialRampToValueAtTime(vol, t0 + attack);
}
gain.gain.setValueAtTime(vol, t0 + attack);
if (sustain) {
gain.gain.setValueAtTime(vol, t0 + attack + sustain);
}
if (decay) {
gain.gain.exponentialRampToValueAtTime(0.01, t0 + attack + sustain + decay);
}
let osc = this.createOscillator();
osc.frequency.setValueAtTime(freq1, t0);
osc.frequency.exponentialRampToValueAtTime(freq2, t0 + attack + sustain + decay);
osc.type = type;
osc.start(t0);
osc.stop(t1);
let panner = c.createPanner();
panner.setPosition(x, y, z);
osc.connect(gain);
gain.connect(panner);
panner.connect(this.masterGain);
return osc;
};
SoundFx.prototype.createOscillator = function() {
let osc = this.ctx.createOscillator();
if (!osc.start) osc.start = osc.noteOn;
if (!osc.stop) osc.start = osc.noteOff;
return osc;
};
/**
* Returns gain and panner nodes, connected to each other and to the master gain like
* gain -> panner -> master.
* @returns {*} an object with "gain" and "panner" fields, or null
*/
SoundFx.prototype.createGainAndPanner = function() {
if (!this.ctx) return null;
let c = this.ctx;
let r = {};
r.gain = this.createGain();
r.panner = c.createPanner();
r.gain.connect(r.panner);
r.panner.connect(this.masterGain);
return r;
};
/**
* Makes a sound with no attack, decay, or frequency changes.
* @param {number} x
* @param {number} y
* @param {number} z
* @param {number} vol
* @param {number} sustain
* @param {number} freq
* @param {String} opt_type Wave type string (square, sine, etc). Default is sine
*/
SoundFx.prototype.note = function(x, y, z, vol, sustain, freq, opt_type) {
let type = opt_type || 'sine';
this.sound(x, y, z, vol, 0, sustain, 0, freq, freq, type);
};
SoundFx.prototype.disconnect = function() {
if (this.masterGain) {
this.masterGain.gain = 0;
this.masterGain.disconnect();
this.masterGain = null;
}
};