blob: 2c7e35c69b21ac44d392b018f9e423adb7a75b28 [file] [log] [blame]
window.org_eclipse_osbp_utils_vaadin_beeper_Beeper = function() {
var audioCtx = new (window.AudioContext || window.webkitAudioContext || window.audioContext);
var soundBuffer = null;
var bufferLoader;
this.beepSound = function(duration, frequency) {
var oscillator = audioCtx.createOscillator();
var gainNode = audioCtx.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
if (frequency){
oscillator.frequency.value = frequency;
}
setTimeout(function(){
oscillator.stop();
oscillator.disconnect();
gainNode.disconnect();
}, (duration ? duration : 1500));
oscillator.start();
};
function loadSound(url) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
// Decode asynchronously
request.onload = function() {
audioCtx.decodeAudioData(request.response, function(buffer) {
var source = audioCtx.createBufferSource(); // creates a sound source
source.buffer = buffer; // tell the source which sound to play
source.connect(audioCtx.destination); // connect the source to the context's destination (the speakers)
source.start(0); // play the source now
},
function(e){"Error with decoding audio data" + e.err}
);
}
request.send();
}
this.playSound = function playSound(url) {
loadSound(url);
// soundInit();
}
function soundInit() {
bufferLoader = new BufferLoader(audioCtx, [
'sounds/baby-babble-1.mp3',
'sounds/people-cheering.mp3',
'sounds/y1.mp3',
'sounds/applause-4.mp3', ],
finishedLoading);
bufferLoader.load();
}
function finishedLoading(bufferList) {
// Create two sources and play them both together.
var source1 = audioCtx.createBufferSource();
var source2 = audioCtx.createBufferSource();
source1.buffer = bufferList[0];
source2.buffer = bufferList[1];
source1.connect(audioCtx.destination);
source2.connect(audioCtx.destination);
source1.start(0);
source2.start(0);
}
};