-
Notifications
You must be signed in to change notification settings - Fork 53
/
minim.js
executable file
·100 lines (95 loc) · 2.08 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
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
/**
* 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) {
var local = this;
setTimeout(function() { local.play(); }, 50);
return;
}
audio.play();
};
this.loop = function () {
if (!loaded) {
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) {
return;
}
// rewind the sound to start
if(audio.currentTime) {
audio.currentTime = 0;
}
};
this.position = function() {
if (!loaded) {
return -1;
}
if(audio.currentTime) {
return audio.currentTime * 1000;
}
return -1;
};
this.cue = function(position) {
if (!loaded) {
return;
}
if(audio.currentTime) {
audio.currentTime = position / 1000;
}
};
this.mute = function() {
audio.volume = 0.0;
};
this.unmute = function() {
audio.volume = 1.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/, ''));
}