开发者

Generating a tone using pure javascript with Chromium WebAudio API

开发者 https://www.devze.com 2023-03-24 04:31 出处:网络
How can I generate a tone (pure s开发者_JAVA百科ine wave, for instance) using only javascript and Chromium\'s WebAudio API?

How can I generate a tone (pure s开发者_JAVA百科ine 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!


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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号