jldugger
jldugger

Reputation: 2499

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.

Upvotes: 11

Views: 17386

Answers (8)

Jerome
Jerome

Reputation: 396

Actually this is possible, probably due to new functions implemented in Java in the meantime. Here is a short example:

var context = new AudioContext();
var o = context.createOscillator();
o.type = "sine";
o.connect(context.destination);
o.start();
setTimeout(function(){ 
        o.stop();
}, 100);

To get more, please visit this site https://marcgg.com/blog/2016/11/01/javascript-audio/ where I found the solution.

Upvotes: 0

Matas Vaitkevicius
Matas Vaitkevicius

Reputation: 61509

Solution by Alex gave me Uncaught TypeError: osc.noteOn is not a function

This did it though:

Play = (function() {
  
  var ctx = new(AudioContext || webkitAudioContext);
  
  return function(duration, freq, finishedCallback) {
    duration = +duration;
    if (typeof finishedCallback != "function") {
      finishedCallback = function() {};
    }
    var osc = ctx.createOscillator();
    osc.type = 0;
    osc.connect(ctx.destination);
    osc.frequency.value = freq;
    
    if (osc.start) osc.start();
    else osc.noteOn(0);
    
    setTimeout(
      function() {
        if (osc.stop) osc.stop(0);
        else osc.noteOff(0);
        finishedCallback();
      }, duration
    );
  };
})();
Play(42, 666)

Took it from here

Hope this saves you some time

Upvotes: 1

Ahosan Karim Asik
Ahosan Karim Asik

Reputation: 3299

Try following way: It may easy for you....

function play_beep() {
  var snd = new Audio("http://www.externalharddrive.com/waves/computer/hello.wav");
  snd.play();
  return false;
}
<input type="submit" value="Play Beep" onclick="return play_beep();" />

Upvotes: 3

alex
alex

Reputation: 490557

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.

Upvotes: 5

Tim Joseph
Tim Joseph

Reputation: 199

I honestly haven't tested this, but it would be worth a look,

Real Java's how to emit a beep but it does depend on you being able to ensure your client has an appropriate version of the JDK installed on every machine you are targeting.

Upvotes: 0

LiraNuna
LiraNuna

Reputation: 67302

Using JavaScript, it's impossible - JavaScript has no access to the client computer except cookies and the new HTML5 local storage.

What you can do, however, is use a Java applet that will be controllable via JavaScript - hidden or not.

You can find an example here.

This requires Java runtime to be installed on the client computer.

Upvotes: 9

ChristopheD
ChristopheD

Reputation: 116287

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

Upvotes: 2

Gabe Moothart
Gabe Moothart

Reputation: 32112

This is not possible with native Javascript. You could possibly write an ActiveX control to do it, though.

Upvotes: 1

Related Questions