2 * Copyright (c) 2013 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
9 function loadAudioAndAddToPeerConnection(url
, peerconnection
) {
11 gContext
= new AudioContext();
13 var inputSink
= gContext
.createMediaStreamDestination();
14 peerconnection
.addStream(inputSink
.stream
);
16 loadAudioBuffer_(url
, function(voiceSoundBuffer
) {
17 if (peerconnection
.webAudioBufferSource
)
18 throw failTest('Cannot load more than one sound per peerconnection.');
20 peerconnection
.webAudioBufferSource
= gContext
.createBufferSource();
21 peerconnection
.webAudioBufferSource
.buffer
= voiceSoundBuffer
;
22 peerconnection
.webAudioBufferSource
.connect(inputSink
);
23 returnToTest('ok-added');
27 function playPreviouslyLoadedAudioFile(peerconnection
) {
28 if (!peerconnection
.webAudioBufferSource
)
29 throw failTest('Must call loadAudioAndAddToPeerConnection before this.');
30 peerconnection
.webAudioBufferSource
.start(gContext
.currentTime
);
34 function loadAudioBuffer_(url
, callback
) {
35 var request
= new XMLHttpRequest();
36 request
.open('GET', url
, true);
37 request
.responseType
= 'arraybuffer';
39 request
.onload = function() {
40 gContext
.decodeAudioData(request
.response
, function (decodedAudio
) {
41 voiceSoundBuffer
= decodedAudio
;
42 callback(voiceSoundBuffer
);