This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.js
94 lines (73 loc) · 1.99 KB
/
audio.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
var extension = ".ogg";
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf('safari')!=-1){
if(ua.indexOf('chrome') > -1){
//its chrome
}else{
//its safari
extension = ".mp3";
}
}
var audio = [];
function loadAudio() {
audio[0] = new Audio();
audio[0].addEventListener('canplaythrough', audioLoaded, false);
audio[0].loop = true;
audio[0].src = 'audio/02_un_-_the_tempest'+extension;
audio[1] = new Audio();
audio[1].addEventListener('canplaythrough', audioLoaded, false);
audio[1].loop = true;
audio[1].src = 'audio/06_seetyca_-_verlosche_mich'+extension;
}
var activeaudio = null;
var loadedaudio = 0;
var timeout;
// good idea to preload, except it takes too long to start playing
function audioLoaded() {
//loadedaudio++;
//if (loadedaudio == audio.length) init(); //doneLoadingAudio();
}
function playAudio(id) {
var prev = activeaudio;
activeaudio = id;
audio[activeaudio].volume = 0.0;
audio[activeaudio].play();
if (timeout) {
clearTimeout(timeout);
}
function doFade() {
timeout = setTimeout( function() {
var ready = 0;
var target = 1.0;
if (audio[activeaudio]._volume) target = audio[activeaudio]._volume;
if ( audio[activeaudio].volume < target ) {
var vol = audio[activeaudio].volume + 0.05;
if (vol >= target ) audio[activeaudio].volume = target;
else audio[activeaudio].volume = vol;
} else {
ready++;
}
for (i=0; i< audio.length; i++) {
if (i !== activeaudio) {
if ( audio[i].volume > 0.0 ) {
vol = audio[i].volume - 0.05;
if (vol < 0.0 ) audio[i].volume = 0.0;
else audio[i].volume = vol;
} else {
//console.log(i + " ready");
ready++;
audio[i].pause();
}
}
}
//console.log(activeaudio + " " + audio.length + " " + ready + " " + audio[activeaudio].volume + " " + target);
if (ready == audio.length) {
//if (prev) audio[prev].pause();
//console.log("finished crossfade");
} else {
doFade();
}
}, 180 );
}
doFade();
}