Repository navigation
Expand file tree
/
Copy pathbps.js
More file actions
97 lines (81 loc) · 2.2 KB
/
Copy pathbps.js
File metadata and controls
97 lines (81 loc) · 2.2 KB
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
var myAudioContext,
myBuffer,
mySource,
myNodes = {},
myBps = 2,
isPlaying = false;
function initBPS() {
if('webkitAudioContext' in window) {
myAudioContext = new webkitAudioContext();
} else {
myAudioContext = new AudioContext();
}
var request = new XMLHttpRequest();
request.open('GET', '/Pop.mp3', true);
request.responseType = 'arraybuffer';
request.addEventListener('load', bufferCallback, false);
request.send();
}
function bufferCallback(event) {
var request = event.target;
myAudioContext.decodeAudioData(request.response,
function(buffer) { myBuffer = buffer; }
);
}
function routeSound(source) {
myNodes.panner = myAudioContext.createPanner();
myNodes.volume = myAudioContext.createGain();
// set node values to current slider values
var panX = document.querySelector('#pan').value;
var volume = document.querySelector('#volume').value;
myNodes.panner.setPosition(panX, 0, 0);
myNodes.volume.gain.value = volume;
// pass source through series of nodes
source.connect(myNodes.panner);
myNodes.panner.connect(myNodes.volume);
myNodes.volume.connect(myAudioContext.destination);
return source;
}
function playSound() {
// create a new AudioBufferSourceNode
var source = myAudioContext.createBufferSource();
source.buffer = myBuffer;
source.loop = true;
source.loopStart = 0;
source.loopEnd = 1.0 / myBps;
source = routeSound(source);
source.start(0);
mySource = source;
}
function pauseSound() {
mySource.stop(0);
}
function toggleSound(button) {
if (isPlaying) {
pauseSound();
button.value = "Start";
isPlaying = false;
} else {
playSound();
button.value = "Stop";
isPlaying = true;
}
}
function volumeChange(slider) {
if (myNodes.volume) {
var volume = slider.value;
myNodes.volume.gain.value = volume;
}
}
function panChange(slider) {
if (myNodes.panner) {
var panX = slider.value;
myNodes.panner.setPosition(panX, 0, 0);
}
}
function bpsChange(radio) {
myBps = radio.value;
if (isPlaying) {
mySource.loopEnd = 1.0 / myBps;
}
}