Home >Web Front-end >HTML Tutorial >Real-time sound generation using JavaScript/HTML5

Real-time sound generation using JavaScript/HTML5

PHPz
PHPzforward
2023-09-15 09:29:061439browse

Real-time sound generation using JavaScript/HTML5

The Web Audio API is used to control audio, allowing you to select the audio source. You can also add effects; create audio visualizations, panning, and more.

Example

You can try running the following code snippet to generate the sound−

// use one context per document. Here we are creating one context for one document. You can create for other documents also
var context = new (window.AudioContext || window.webkitAudioContext)();

// oscillator
var os = context.createOscillator();  
os.type = 'sine'; // sine is the default. So you can also use square, saw tooth, triangle
os.frequency.value = 500; // setting the frequency Hz
os.connect(context.destination); // connecting  to the destination

// starting the oscillator
os.start();  
os.stop(context.currentTime + 5); // stop 5 seconds after the current time

The above is the detailed content of Real-time sound generation using JavaScript/HTML5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete