Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PC Speaker beep via javascript?

I'm revisiting an ID scanner station program we built ages ago and I have a request from users to make a system beep. We're considering moving the system to a web browser, but is it possible to invoke a speaker beep via javascript or something? It doesn't need to be cross-browser compatible, but it probably needs to work on Windows or Linux. The stations in question are not equipped with a soundcard or external speakers, hence the request for a PC speaker access.

I know someone's going to say it, so I'll address this up front: I don't care what you think about applications making noise, this isn't for you. Users request it, it makes sense, and the hardware scanner already makes noise anyways. Yes, we give visual feedback, with distinguishable text and color, but we find that people accept the existing beep as positive feedback and adding more audio context would help.

like image 632
jldugger Avatar asked Oct 28 '09 22:10

jldugger


2 Answers

It's possible with JavaScript today.

Here's a quick & dirty function I wrote...

var beep = function(duration, type, finishedCallback) {

    if (!(window.audioContext || window.webkitAudioContext)) {
        throw Error("Your browser does not support Audio Context.");
    }

    duration = +duration;

    // Only 0-4 are valid types.
    type = (type % 5) || 0;

    if (typeof finishedCallback != "function") {
        finishedCallback = function() {};   
    }

    var ctx = new (window.audioContext || window.webkitAudioContext);
    var osc = ctx.createOscillator();

    osc.type = type;

    osc.connect(ctx.destination);
    osc.noteOn(0);

    setTimeout(function() {
        osc.noteOff(0);
        finishedCallback();
    }, duration);

};

jsFiddle.

like image 106
alex Avatar answered Oct 12 '22 16:10

alex


I think your best bet would be a java applet doing the job...

like image 22
ChristopheD Avatar answered Oct 12 '22 17:10

ChristopheD