-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
56 lines (49 loc) · 1.25 KB
/
player.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
// This program is licensed under the MIT License.
// Copyright 2016, aike (@aike1000)
var Player = function(ctx, file) {
this.playing = false;
this.ctx = ctx;
var self = this;
this.loadwav(file, function(buf) { self.sample = buf; });
}
Player.prototype.loadwav = function(file, callback) {
var xhr = new XMLHttpRequest();
xhr.open("GET", file, true);
xhr.responseType = "arraybuffer";
var self = this;
xhr.onload = function() {
self.ctx.decodeAudioData(xhr.response,function(buf){
callback(buf);
}, function(){});
};
xhr.send();
}
Player.prototype.connect = function(next) {
this.next = next;
}
Player.prototype.playsample = function(buf, tim) {
this.src = this.ctx.createBufferSource();
this.src.buffer = buf;
this.src.connect(this.next);
this.src.loop = true;
this.src.start(tim);
}
Player.prototype.toggle = function() {
if (this.playing)
this.stop();
else
this.play();
}
Player.prototype.stop = function() {
if (!this.playing)
return;
this.playing = false;
this.src.stop(0);
}
Player.prototype.play = function() {
if (this.playing)
return;
this.playing = true;
this.playsample(this.sample, 0);
var self = this;
}