Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating a tone using pure javascript with Chromium WebAudio API

How can I generate a tone (pure sine wave, for instance) using only javascript and Chromium's WebAudio API?

I would like to accomplish something like the Firefox equivalent.

The Chromium WebAudio demos here appear to all use prerecorded <audio> elements.

Thanks!

like image 823
Alex Churchill Avatar asked Jul 28 '11 23:07

Alex Churchill


2 Answers

The Web Audio API has what's known as the Oscillator Interface to generate the tones you're talking about. They're pretty straight forward to get going...

var context = new webkitAudioContext(),
    //Call function on context
    oscillator = context.createOscillator(); // Oscillator defaults to sine wave

oscillator.connect(context.destination);
oscillator.start();

You can change the type of wave by doing:

oscillator.type = 1; // Change to square wave.

or alternatively:

oscillator.type = oscillator.SQUARE;

I've written an article about this very topic in more detail, so that might be of some use to you!

like image 72
Stuart Memo Avatar answered Oct 22 '22 21:10

Stuart Memo


Probably not these best way, but I used dsp.js to generate different types of sinusoids, then passed them off to the Web Audio API in this demo: http://www.htmlfivewow.com/demos/waveform-generator/index.html

like image 35
ebidel Avatar answered Oct 22 '22 22:10

ebidel