Skip to content

Commit a617c23

Browse files
committed
Web Audio API
1 parent cba06c4 commit a617c23

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

index.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,17 @@ <h2>Demo:</h2>
6868
pcm=[];
6969
value=kjmp2_decode_frame(mp2,frame,pcm,l,r);
7070
if (value) {
71+
if (!!(window.AudioContext || window.webkitAudioContext)) {
72+
for(var i=0;i<l.length;i++){
73+
lchannel.push(l[i]);
74+
rchannel.push(r[i]);
75+
};
76+
} else {
7177
for(var i=0;i<l.length;i++){
7278
dec.push(l[i]);
7379
dec.push(r[i]);
7480
};
81+
}
7582
frame=frame.subarray(value);
7683
bytes_left -= value;
7784
t.innerHTML="Bytes left to decode: " +bytes_left;
@@ -106,6 +113,8 @@ <h2>Demo:</h2>
106113
get_mp2(function(){
107114
s.parentNode.removeChild(s);
108115
dec=[];
116+
lchannel = [];
117+
rchannel = [];
109118
r=[];
110119
l=[];
111120
buffer = xhr.response ? xhr.response : xhr.mozResponseArrayBuffer;
@@ -179,6 +188,22 @@ <h2>Demo:</h2>
179188
};
180189
}
181190

191+
if (!!(window.AudioContext || window.webkitAudioContext)) {
192+
var context = new (window.AudioContext || window.webkitAudioContext)();
193+
var sndbuffer = context.createBuffer(2, lchannel.length + rchannel.length, sampleRate);
194+
sndbuffer.copyToChannel(new Float32Array(lchannel), 0, 0);
195+
sndbuffer.copyToChannel(new Float32Array(rchannel), 1, 0);
196+
var source = context.createBufferSource();
197+
source.buffer = sndbuffer;
198+
source.connect(context.destination);
199+
var start=function() {
200+
source.start(0);
201+
}
202+
203+
var stop=function() {
204+
source.stop(0);
205+
};
206+
} else {
182207
var audioDestination = new AudioDataDestination(sampleRate, requestSoundData);
183208
var start=function() {
184209
currentSoundSample = 0;
@@ -189,6 +214,7 @@ <h2>Demo:</h2>
189214
var stop=function() {
190215
frequency = 0;
191216
};
217+
}
192218
var start_button=document.createElement("button");
193219
start_button.innerHTML="Start playback";
194220
start_button.onclick=start;

0 commit comments

Comments
 (0)