-
Notifications
You must be signed in to change notification settings - Fork 3
/
ola2.html
208 lines (165 loc) · 7.25 KB
/
ola2.html
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script type="text/javascript" src="OLA.js"></script>
<script type="text/javascript" src="../bpm-timeline.js/formulas.js"></script>
<script type="text/javascript" src="../bpm-timeline.js/bpm-timeline.js"></script>
<script type="text/javascript" src="SegmentProcessor.js"></script>
<script type="text/javascript" src="SegmentProcessorV2.js"></script>
<script type="text/javascript">
var context = new AudioContext();
var BUFFER_SIZE = 2048;
var FRAME_SIZE = 4096;
var node = context.createScriptProcessor(BUFFER_SIZE, 2);
var buffer;
var segProc;
var data;
function download_json(url, success, error) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var data = JSON.parse(request.responseText);
success(data);
} else {
error();
}
};
request.onerror = error;
request.send();
}
// params: {beatsSrc: URL, duration: Number | beats: Array, duration: Number | bpm: Number, firstBeat: Number, duration: Number}
function load_beats(params, callback) {
var P = params;
if (P.duration) {
if (P.beatsSrc)
download_json(P.beatsSrc, function(beats) {
callback(createBeatsArray1(beats, P.duration));
});
else if (P.beats)
callback(createBeatsArray1(P.beats, P.duration));
else if (P.bpm!=undefined && P.firstBeat!=undefined)
callback(createBeatsArray2(P.bpm, P.firstBeat, P.duration));
else
throw "Invalid parameters";
} else
throw "Invalid parameters";
function createBeatsArray1(beats, duration) {
var _beats = new Array(beats.length);
B = beats;
var star, end, beatPeriod, beatBPM;
for (var i=0; i < B.length-1 && B[i] < duration; i++) {
start = B[i];
end = B[i+1];
beatPeriod = end - start;
beatBPM = 60 / beatPeriod;
_beats[i] = [start, end, beatBPM];
}
start = B[B.length-1];
end = start + beatPeriod;
if (end <= duration)
_beats[B.length-1] = [start, end, beatBPM];
return _beats;
}
function createBeatsArray2(bpm, firstBeat, duration) {
var beatPeriod = 60/bpm;
var beats = [];
for (var i=firstBeat || 0; i>0; i-=beatPeriod) {}
for (var t0=i; t0<duration; t0+=beatPeriod)
var t1 = t0 + beatPeriod;
if (t1<=duration)
beat[beats.length] = [t0, t1, bpm];
return beats;
}
}
function parse_beats_json(obj) {
var arr = obj.beat[0].data;
var beats = new Array(arr.length);
for (var i=0; i < beats.length; i++) {
beats[i] = arr[i].time.value;
}
return beats;
}
loadSample = function(url, id) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = function() {
context.decodeAudioData(request.response, function(decodedData) {
download_json(beatsSrc, function(beatsJSON) {
var beats = parse_beats_json(beatsJSON);
buffer = decodedData;
load_beats({
beats: beats,
duration: buffer.duration
}, function(_data){
data = _data;
// segProc = new SegmentProcessor(buffer, FRAME_SIZE);
// segProc.set_reference_bpm(130);
segProc = new SegmentProcessorV2({frameSize: FRAME_SIZE, initialBPM: 110});
segProc.add_audio_data({id: "a1", audioBuffer: buffer});
var rd = Math.round;
// segProc.add_interval(0, buffer.length-1, 130);
// segProc.add_interval({ start: Math.round(data[0][0]*44100), end: Math.round(data[3][1]*44100), bpm: data[0][2] });
// segProc.add_interval({ start: Math.round(data[0][0]*44100), end: Math.round(data[3][1]*44100), bpm: data[0][2] });
// segProc.add_interval({ start: Math.round(data[15][0]*44100), end: Math.round(data[22][1]*44100), bpm: data[15][2] });
// segProc.add_interval({ start: Math.round(data[15][0]*44100), end: Math.round(data[22][1]*44100), bpm: data[15][2] });
segProc.add_interval({
start: rd(data[31][0]*44100),
end: rd(data[38][1]*44100),
bpm: data[31][2],
audioId: "a1"
});
segProc.add_interval({
start: rd(data[31+16][0]*44100),
end: rd(data[38+16][1]*44100),
bpm: data[31+16][2],
audioId: "a1"
});
segProc.add_interval({
start: rd(data[31+32][0]*44100),
end: rd(data[38+32][1]*44100),
bpm: data[31+32][2],
audioId: "a1"
});
segProc.add_interval({
start: rd(data[31+64][0]*44100),
end: rd(data[38+64][1]*44100),
bpm: data[31+64][2],
audioId: "a1"
});
segProc.add_interval({
start: rd(data[31+64*4][0]*44100),
end: rd(data[38+64*4][1]*44100),
bpm: data[31+64*4][2],
audioId: "a1"
});
segProc.add_interval({
start: rd(data[31+64*4][0]*44100),
end: rd(data[38+64*4][1]*44100),
bpm: data[31+64*4][2],
audioId: "a1"
});
node.onaudioprocess = function(e) {
segProc.process(e.outputBuffer);
}
node.connect(context.destination);
console.log("decoded");
});
});
});
};
request.send();
};
var beatsSrc = "http://localhost/annotator/Daft%20Punk%20-%20Discography%20-%201994-2013/01%20-%20Albums%20(CD%20Original)/beats/14.%20Too%20Long.json";
var audioSrc = "http://localhost/annotator/Daft%20Punk%20-%20Discography%20-%201994-2013/01%20-%20Albums%20(CD%20Original)/2001%20-%20Discovery%20-%20(320%20kbps)/14.%20Too%20Long.mp3";
// loadSample('http://localhost/soundtouchjs/1.mp3');
loadSample(audioSrc);
</script>
</body>
</html>