-
Notifications
You must be signed in to change notification settings - Fork 45
/
minim.js
75 lines (70 loc) · 1.69 KB
/
minim.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
/**
* Minim-emulation code by Daniel Hodgin
*/
// wrap the P5 Minim sound library classes
function Minim() {
this.loadFile = function (str) {
return new AudioPlayer(str);
}
}
// Browser Audio API
function AudioPlayer(str) {
var loaded = false;
var looping = false;
if (!!document.createElement('audio').canPlayType) {
var audio = document.createElement('audio');
audio.addEventListener('ended', function () {
if (looping) {
this.currentTime = 0;
this.play();
}
}, false);
audio.preload = 'auto';
audio.autobuffer = true;
if (canPlayOgg()) {
audio.src = str.split(".")[0] + ".ogg";
} else if (canPlayMp3()) {
audio.src = str;
}
loaded = true;
}
this.play = function () {
if (!loaded || audio.readyState==0) {
var local = this;
setTimeout(function() { local.play(); }, 50);
return;
}
audio.play();
};
this.loop = function () {
if (!loaded || audio.readyState==0) {
var local = this;
setTimeout(function() { local.loop(); }, 50);
return;
}
//audio.loop = 'loop';
looping = true;
audio.play();
};
this.pause = function () {
if (!loaded) {
return;
}
audio.pause();
};
this.rewind = function () {
if (!loaded || audio.readyState==0) {
return;
}
// rewind the sound to start
audio.currentTime = 0;
};
}
function canPlayOgg() {
var a = document.createElement('audio');
return !!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, ''));
}
function canPlayMp3() {
var a = document.createElement('audio');
return !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));
}