-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSound.js
210 lines (202 loc) · 6.38 KB
/
Sound.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
class Sound {
sounds = {};
/**
* Constructor for Sound.
*/
constructor() {
this.add('music', 1, Sound.SONG);
this.add('hit', 1, [3,,0.1283,0.6002,0.4009,0.06,,,,,,,,,,,-0.0393,-0.2507,1,,,,,0.5]);
this.add('pickup', 2, [2,0.0109,0.2089,0.2983,0.3261,0.2563,,-0.0176,0.4618,0.0029,-0.6415,-0.2028,0.3867,0.9195,0.6075,0.3908,0.1457,-0.0004,0.967,,0.2442,,-0.5556,0.29]);
this.add('end', 5, [2,0.0738,0.9417,0.1338,0.1844,0.504,,-0.0519,0.1093,0.1416,0.696,0.4653,0.0134,0.2708,-0.2926,-0.1538,,-0.0891,0.997,0.0084,0.7638,0.0076,0.6493,0.5]);
this.add('push', 1, [3,0.25,0.1706,,0.45,0.85,,0.04,0.36,,,-0.02,,,,,0.28,,1,,,0.1,,0.23]);
this.add('door', 2, [3,0.295,0.49,0.7,0.43,0.73,0.32,0.24,-0.62,0.46,0.104,-0.54,,0.19,-0.4712,0.21,-0.3,-0.18,0.36,-0.6,0.08,0.57,0.28,0.5]);
}
/**
* Generates a sound using the given data and adds it to the stored sounds under the
* given name. It will generate the sound multiple times if count is greater than one.
* This method handles both jsfxr sounds and SoundBox compositions.
*
* @param {Object} name The name of the sound to create. This is the key in the stored sounds.
* @param {Object} count The number of times to generate the sound.
* @param {Object} data The data containing the parameters of the sound to be generated.
*/
add(name, count, data) {
this.sounds[name] = {tick: 0, count: count, pool: []};
for (let i = 0; i < count; i++) {
let audio = new Audio();
if (data instanceof Array) {
// If it is an Array, it must be jsfxr data.
audio.src = jsfxr(data);
audio.volume = 1.0;
} else {
// Otherwise it is SoundBox data.
let player = new CPlayer();
player.init(Sound.SONG);
// Using only 4 instruments. This saves a bit of space.
player.generate();
player.generate();
player.generate();
audio.src = URL.createObjectURL(new Blob([player.createWave()], {type: "audio/wav"}));
// This is background music, so we set it to loop and turn the volume down a bit.
audio.loop = true;
audio.volume = 1.0;
audio.playbackRate = 0.8;
}
this.sounds[name].pool.push(audio);
}
}
/**
* Plays the sound of the given name. All sounds are stored as pre-generated Audio
* objects. So it is simply a matter of telling it to play. Some sounds have multiple
* copies, particularly if the sound can be played in quick succession, potentially
* overlapping. In such a case, it can't use the same Audio, so iterates over a pool
* of Audios containing the same sound.
*
* @param {string} name The name of the sound to play.
*/
play(name) {
let sound = this.sounds[name];
sound.pool[sound.tick].play();
sound.tick < sound.count - 1 ? sound.tick++ : sound.tick = 0;
}
/**
* Starts playing the game music.
*/
playSong() {
this.play('music');
}
/**
* This is background music, composed on and generated by the SoundBox.
*/
static SONG = {
songData: [
{ // Instrument 2
i: [
0, // OSC1_WAVEFORM
0, // OSC1_VOL
140, // OSC1_SEMI
0, // OSC1_XENV
0, // OSC2_WAVEFORM
0, // OSC2_VOL
140, // OSC2_SEMI
0, // OSC2_DETUNE
0, // OSC2_XENV
255, // NOISE_VOL
158, // ENV_ATTACK
158, // ENV_SUSTAIN
158, // ENV_RELEASE
0, // ARP_CHORD
0, // ARP_SPEED
0, // LFO_WAVEFORM
51, // LFO_AMT
2, // LFO_FREQ
1, // LFO_FX_FREQ
2, // FX_FILTER
58, // FX_FREQ
239, // FX_RESONANCE
0, // FX_DIST
32, // FX_DRIVE
88, // FX_PAN_AMT
1, // FX_PAN_FREQ
157, // FX_DELAY_AMT
2 // FX_DELAY_TIME
],
// Patterns
p: [,,,,1,,2,,1,,2,,1,,2,,1,,2],
// Columns
c: [
{n: [132],
f: []},
{n: [128],
f: []}
]
},
{ // Instrument 3
i: [
0, // OSC1_WAVEFORM
109, // OSC1_VOL
116, // OSC1_SEMI
1, // OSC1_XENV
0, // OSC2_WAVEFORM
107, // OSC2_VOL
116, // OSC2_SEMI
0, // OSC2_DETUNE
1, // OSC2_XENV
0, // NOISE_VOL
4, // ENV_ATTACK
6, // ENV_SUSTAIN
35, // ENV_RELEASE
0, // ARP_CHORD
0, // ARP_SPEED
0, // LFO_WAVEFORM
0, // LFO_AMT
0, // LFO_FREQ
0, // LFO_FX_FREQ
2, // FX_FILTER
14, // FX_FREQ
0, // FX_RESONANCE
0, // FX_DIST
32, // FX_DRIVE
0, // FX_PAN_AMT
0, // FX_PAN_FREQ
0, // FX_DELAY_AMT
0 // FX_DELAY_TIME
],
// Patterns
p: [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
// Columns
c: [
{n: [147,,,,,,147,,,,,,,,,,147,,,,,,147],
f: []}
]
},
{ // Instrument 4
i: [
0, // OSC1_WAVEFORM
0, // OSC1_VOL
140, // OSC1_SEMI
0, // OSC1_XENV
0, // OSC2_WAVEFORM
0, // OSC2_VOL
140, // OSC2_SEMI
0, // OSC2_DETUNE
0, // OSC2_XENV
60, // NOISE_VOL
4, // ENV_ATTACK
10, // ENV_SUSTAIN
34, // ENV_RELEASE
0, // ARP_CHORD
0, // ARP_SPEED
0, // LFO_WAVEFORM
187, // LFO_AMT
5, // LFO_FREQ
0, // LFO_FX_FREQ
1, // FX_FILTER
239, // FX_FREQ
135, // FX_RESONANCE
0, // FX_DIST
32, // FX_DRIVE
108, // FX_PAN_AMT
5, // FX_PAN_FREQ
16, // FX_DELAY_AMT
4 // FX_DELAY_TIME
],
// Patterns
p: [,,,3,1,1,1,2,1,1,1,2,1,1,1,2,1,1,1,2],
// Columns
c: [
{n: [,,,,,,,,147,,,,,,,,,,,,,,,,147],
f: []},
{n: [,,,,,,,,147,,,,,,,,,,,,,,,,147,,,,,,147],
f: []},
{n: [,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,147],
f: []}
]
}
],
rowLen: 4134, // In sample lengths
patternLen: 32, // Rows per pattern
endPattern: 23, // End pattern
numChannels: 5 // Number of channels
};
}