forked from MaxLaumeister/bitlisten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.js
227 lines (195 loc) · 5.54 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
var globalVolume = 50;
var globalScalePitch;
var globalBank;
//init volume at 50%
Howler.volume (globalVolume*.01);
function Sound() {
}
var soundBank = [];
// the second number is the number of sound files available
soundBank[0] = ["celesta", 22];
soundBank[1] = ["planet", 33];
soundBank[2] = ["wikki", 13];
function zeroPad(num, places) {
var zero = places - num.toString().length + 1;
return Array(+(zero > 0 && zero)).join("0") + num;
}
Sound.loadup = function(){
var newSound;
var istring;
// sound0 = celesta
sound0 = new Array();
// sound1 = planet
sound1 = new Array();
// sound2 = celestaB
sound2 = new Array();
// String swells0, for blocks
swells0 = new Array();
// String swells1, for blocks
swells1 = new Array();
// String swells2, for blocks
swells2 = new Array();
// These variables are populated (and their sounds loaded) by the Sound.change function below.
}
Sound.init = function() {
// Initialize sound toggle button
$("#volumeControl").click(function() {
if (!globalMute) {
globalMute = true;
Howler.mute();
$("#volumeControl").css("background-position", "0 0");
} else {
globalMute = false;
Howler.unmute();
$("#volumeControl").css("background-position", "0 -46px");
}
});
// Initialize sound slider
$("#volumeSlider").noUiSlider({
range : [0, 100],
start : 50,
handles : 1,
step : 1,
orientation : "vertical",
slide : function() {
globalVolume = 100 - $(this).val();
Howler.volume (globalVolume*.01);
}
});
globalScalePitch = $("#scalePitchCheckBox").attr("checked");
Sound.change(0);
}
Sound.change = function(instrument_number) {
var musicianString = "Donate to instrument creator: ";
// INSTRUMENT 0
if (instrument_number == 0 ) {
// Load sound and swells if not already loaded
if (sound0.length == 0) {
for (var i = 1; i <= 22; i++) {
istring = zeroPad(i, 3);
newSound = new Howl({
urls: ["sounds/celesta/" + "celesta" + istring + ".ogg",
"sounds/celesta/" + "celesta" + istring + ".mp3"],
autoplay: false
});
sound0.push(newSound);
}
}
if (swells0.length == 0) {
for (var i = 1; i <= 3; i++) {
newSound = new Howl({
urls: ["sounds/swells0/swell" + i +".ogg",
"sounds/swells0/swell" + i +".mp3"],
autoplay: false
});
swells0.push(newSound);
}
}
currentSound = sound0;
currentSwells = swells0;
$('#musicianDonation').text("");
}
// INSTRUMENT 1
else if (instrument_number == 1) {
// Load sound and swells if not already loaded
if (sound1.length == 0) {
for (var i = 1; i <= 33; i++) {
istring = zeroPad(i, 3);
newSound = new Howl({
urls: ["sounds/planet/" + "planet" + istring + ".ogg",
"sounds/planet/" + "planet" + istring + ".mp3"],
autoplay: false
});
sound1.push(newSound);
}
}
if (swells1.length == 0) {
for (var i = 1; i <= 3; i++) {
newSound = new Howl({
urls: ["sounds/swells1/planetswell" + i +".ogg",
"sounds/swells1/planetswell" + i +".mp3"],
autoplay: false
});
swells1.push(newSound);
}
}
currentSound = sound1;
currentSwells = swells1;
SOUND_DONATION_ADDRESS = "144b31mmaWQVDQFiUPo6HEzxc2Dm83WXrW";
$('#musicianDonation').html(musicianString + "<span>" + SOUND_DONATION_ADDRESS + "</span>");
}
// INSTRUMENT 2
else if (instrument_number == 2) {
// Load sound and swells if not already loaded
if (sound2.length == 0) {
for (var i = 1; i <= 13; i++) {
istring = zeroPad(i, 3);
newSound = new Howl({
urls: ["sounds/wikki/" + "wikki" + istring + ".ogg",
"sounds/wikki/" + "wikki" + istring + ".mp3"],
autoplay: false
});
sound2.push(newSound);
}
}
if (swells2.length == 0) {
for (var i = 1; i <= 3; i++) {
newSound = new Howl({
urls: ["sounds/swells2/wikkiswell" + i +".ogg",
"sounds/swells2/wikkiswell" + i +".mp3"],
autoplay: false
});
swells2.push(newSound);
}
}
currentSound = sound2;
currentSwells = swells2;
SOUND_DONATION_ADDRESS = "1JFaYRGkDmhpSTbFKwqDWKr2ncvvrgYEAV";
$('#musicianDonation').html(musicianString + "<span>" + SOUND_DONATION_ADDRESS + "</span>");
}
}
var currentNotes = 0;
var noteTimeout = 500;
Sound.playRandomAtVolume = function(volume) {
if (globalMute)
return;
var randomPitch = Math.floor(Math.random() * 100);
this.playPitchAtVolume(volume, randomPitch);
}
Sound.playPitchAtVolume = function(volume, pitch) {
if (globalMute)
return;
// Find the index corresponding to the requested pitch
var index = Math.floor(pitch / 100.0 * currentSound.length);
//console.log("Pitch: " + pitch);
// Here we fuzz the index a bit to prevent the same sound
// from being heard over and over again, which gets annoying
var fuzz = Math.floor(Math.random() * 4) - 2;
index += fuzz
index = Math.min(currentSound.length - 1, index);
index = Math.max(0, index);
//console.log("Fuzz: " + fuzz);
//console.log("Index: " + index);
//var readyState = currentSound[index].get("readyState");
if (currentNotes < 5) {
currentSound[index].volume(volume);
currentSound[index].play();
currentNotes++;
setTimeout(function() {
currentNotes--;
}, noteTimeout);
}
}
var lastBlockSound = -1;
Sound.playRandomSwell = function() {
if (globalMute)
return;
var randomIndex;
do {
randomIndex = Math.floor(Math.random() * currentSwells.length);
} while (randomIndex == lastBlockSound);
lastBlockSound = randomIndex;
//var readyState = this.swells[randomIndex].get("readyState");
//if (readyState >= 2)
currentSwells[randomIndex].play();
}