1 /** 2 * Sound manager 3 * @param {Function} callback 4 * @constructor 5 */ 6 function BB_Sound(callback) { 7 var self = this; 8 this.index = -1; 9 this.disabled = true; 10 this.sounds = BB_Sound.notes; 11 try { 12 this.callback = callback; 13 this.service = new Worker('worker.js'); 14 this.service.addEventListener('message', function(e) { 15 self.finish(e.data); 16 }); 17 this.create(); 18 } catch (e) { 19 callback.call(this); 20 } 21 } 22 23 /** 24 * Disable/Enable sounds 25 * @returns {Boolean} 26 */ 27 BB_Sound.prototype.disable = function() { 28 this.disabled = !this.disabled; 29 if (this.disabled) { 30 for (var i=0; i<this.sounds.length; i++) { 31 this.stop(i); 32 } 33 } 34 return this.disabled; 35 }; 36 37 /** 38 * Generate sound with the worker process 39 */ 40 BB_Sound.prototype.create = function() { 41 if (++this.index < this.sounds.length) { 42 this.service.postMessage(this.sounds[this.index]); 43 } else { 44 this.service.terminate(); 45 } 46 }; 47 48 /** 49 * Create audio tag 50 * @param {String} data Base64 encoded WAV data 51 */ 52 BB_Sound.prototype.finish = function(data) { 53 var sound = new Audio(); 54 sound.preload = true; 55 sound.src = data; 56 sound.load(); 57 this.sounds[this.index] = sound; 58 this.callback.call(this); 59 this.create(); 60 }; 61 62 /** 63 * Play sound 64 * @param {Number} index 65 * @param {Number} volume 66 * @param {Boolean} loop 67 */ 68 BB_Sound.prototype.play = function(index, volume, loop) { 69 var sound = this.sounds[index]; 70 if (!this.disabled && typeof sound == 'object') { 71 sound.volume = volume || 1; 72 sound.loop = loop || false; 73 if (sound.currentTime) { 74 sound.pause(); 75 sound.currentTime = 0; 76 } 77 sound.play(); 78 } 79 }; 80 81 /** 82 * Stop playing 83 * @param {Number} index 84 */ 85 BB_Sound.prototype.stop = function(index) { 86 var sound = this.sounds[index]; 87 if (typeof sound == 'object') { 88 sound.pause(); 89 } 90 }; 91 92 /** 93 * Sound notes 94 * chanells separated by pipe 95 * notes separated by comas 96 * note format: 97 * - the first number is the length 1/n 98 * - character and nmber pairs represents music note and octave 99 */ 100 BB_Sound.notes = [ 101 //bounce 102 '||||16e3', 103 '||||16e3', 104 //coin 105 '||||32e5,32b5', 106 '||||32e5,32b5', 107 //sax 108 '1,4,' + 109 '1,4,' + 110 '1,4,' + 111 '2,4,8b4,8e5,8g5,8a5,' + 112 '8bb5,8b5,8bb5,8a5,4e5,4b4,4d5,' + 113 '2e5,4,16gb5,16g5,16gb5,16e5,4d5,' + 114 '2e5,4,16d5,16e5,16d5,16b4,4a4,' + 115 '2b4,4,8b4,8e5,8g5,8a5,' + 116 '8bb5,8b5,8bb5,8a5,4e5,4b4,4d5,' + 117 '2e5,4,16d5,16e5,16d5,16b4,4a4,' + 118 '2b4,4,16gb5,16g5,16gb5,16e5,4d5,' + 119 '1e5,4,' + 120 '8e6,4g6,8e6,4c6,8a5,8b5,8c6,8db6,' + 121 '8d6,4gb6,8d6,4b5,8g5,8a5,8bb5,8b5,' + 122 '8c6,4e6,8c6,4a5,8gb5,8g5,8a5,8bb5,' + 123 '8b5,8bb5,8b5,8c6,4d6,8d6,8db6,8d6,8eb6,' + 124 '8e6,4g6,8e6,4c6,8a5,8b5,8c6,8db6,' + 125 '8d6,4gb6,8d6,4b5,8g5,8a5,8bb5,8b5,' + 126 '8c6,4e6,8c6,4a5,8gb5,8a5,8d6,8c6,' + 127 '2b5,4,8b4,8e5,8g5,8a5,' + 128 '8bb5,8b5,8bb5,8a5,4e5,4b4,4d5,' + 129 '2e5,4,16gb5,16g5,16gb5,16e5,4d5,' + 130 '2e5,4,16d5,16e5,16d5,16b4,4a4,' + 131 '2b4,4,8b4,8e5,8g5,8a5,' + 132 '8bb5,8b5,8bb5,8a5,4e5,4b4,4d5,' + 133 '2e5,4,16d5,16e5,16d5,16b4,4a4,' + 134 '2b4,4,16gb5,16g5,16gb5,16e5,4d5,' + 135 '1e5,4|' + 136 //piano 137 '8e2,4g3b3e4,8e2,4g3b3e4,4b3,4ab3a3d4,' + 138 '8e2,4g3b3e4,8e2,4g3b3e4,4b3,4ab3a3d4,' + 139 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2,4g3b3e4,4b3,4ab3a3d4,' + 164 '8e2,4g3b3e4,8e2,4g3b3e4,4b3,4ab3a3d4|' + 165 //bass 166 '2e2,4e2,4b2,4b1,' + 167 '2e2,4e2,4b2,4b1,' + 168 '2e2,4e2,4b2,4b1,' + 169 '2e2,4e2,4b2,4b1,' + 170 '2e2,4e2,4b2,4b1,' + 171 '2e2,4e2,4b2,4b1,' + 172 '2e2,4e2,4b2,4b1,' + 173 '2e2,4e2,4b2,4b1,' + 174 '2e2,4e2,4b2,4b1,' + 175 '2e2,4e2,4b2,4b1,' + 176 '2e2,4e2,4b2,4b1,' + 177 '2e2,4e2,4b2,4b1,' + 178 '4c2,1,'+ 179 '4b1,1,'+ 180 '4a1,1,'+ 181 '4b1,2,4g1,4b1,'+ 182 '4c2,1,'+ 183 '4b1,1,'+ 184 '4a1,1,'+ 185 '4gb1,2,4b1,4,'+ 186 '2e2,4e2,4b2,4b1,' + 187 '2e2,4e2,4b2,4b1,' + 188 '2e2,4e2,4b2,4b1,' + 189 '2e2,4e2,4b2,4b1,' + 190 '2e2,4e2,4b2,4b1,' + 191 '2e2,4e2,4b2,4b1,' + 192 '2e2,4e2,4b2,4b1,' + 193 '2e2,4e2,4b2,4b1|' + 194 //drum 195 '4e6,8,8b8,4,8b8,8,4,' + 196 '4e6,8,8b8,4,8b8,8,4,' + 197 '4e6,8,8b8,4,8b8,8,4,' + 198 '4e6,8,8b8,4,8b8,8,4,' + 199 '4e6,8,8b8,4,8b8,8,4,' + 200 '4e6,8,8b8,4,8b8,8,4,' + 201 '4e6,8,8b8,4,8b8,8,4,' + 202 '4e6,8,8b8,4,8b8,8,4,' + 203 '4e6,8,8b8,4,8b8,8,4,' + 204 '4e6,8,8b8,4,8b8,8,4,' + 205 '4e6,8,8b8,4,8b8,8,4,' + 206 '4e6,8,8b8,4,8b8,8,4,' + 207 '4e6,8,8b8,4,8b8,8,4,' + 208 '4e6,8,8b8,4,8b8,8,4,' + 209 '4e6,8,8b8,4,8b8,8,4,' + 210 '4e6,8,8b8,4,8b8,8,4,' + 211 '4e6,8,8b8,4,8b8,8,4,' + 212 '4e6,8,8b8,4,8b8,8,4,' + 213 '4e6,8,8b8,4,8b8,8,4,' + 214 '4e6,8,8b8,4,8b8,8,4,' + 215 '4e6,8,8b8,4,8b8,8,4,' + 216 '4e6,8,8b8,4,8b8,8,4,' + 217 '4e6,8,8b8,4,8b8,8,4,' + 218 '4e6,8,8b8,4,8b8,8,4,' + 219 '4e6,8,8b8,4,8b8,8,4,' + 220 '4e6,8,8b8,4,8b8,8,4,' + 221 '4e6,8,8b8,4,8b8,8,4,' + 222 '4e6,8,8b8,4,8b8,8,4']; 223