-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioManagerJS.js
51 lines (45 loc) · 1.16 KB
/
AudioManagerJS.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
AudioManagerJS = function()
{
this.webAudioContext = new webkitAudioContext();
};
AudioManagerJS.prototype.loadNotes = function(selectedSoundBank, fLoadingComplete)
{
noteBuffers = {};
var noteBuffersArray = [];
for(note in selectedSoundBank)
{
this.webAudioContext.decodeAudioData(Base64Binary.decodeArrayBuffer(selectedSoundBank[note].split(",")[1]), function(buffer)
{
noteBuffersArray.push(buffer);
}, function()
{
throw "AudioManagerJS: Sound file failed to load.";
});
}
var loadingInterval = setInterval(function()
{
if(noteBuffersArray.length === Object.keys(selectedSoundBank).length)
{
clearInterval(loadingInterval);
var count = 0;
for(note in selectedSoundBank)
{
noteBuffers[note] = noteBuffersArray[count];
count++;
}
delete noteBuffersArray;
fLoadingComplete();
}
}, 10);
};
AudioManagerJS.prototype._playBuffer = function(buffer)
{
var source = this.webAudioContext.createBufferSource();
source.buffer = buffer;
source.connect(this.webAudioContext.destination);
source.noteOn(0);
};
AudioManagerJS.prototype.playNote = function(note)
{
this._playBuffer(noteBuffers[note]);
};