How do I get an instrumental sound in JS?

In JavaScript, you can generate an instrumental sound using the Web Audio API, which provides an interface for controlling audio on a web page.

Here's a basic example of how you can generate a simple sine wave tone using the Web Audio API:

var context = new (window.AudioContext || window.webkitAudioContext)();
var oscillator = context.createOscillator();
oscillator.type = "sine";
oscillator.frequency.value = 440;  // A4 note

In this example, an instance of the AudioContext interface is created and stored in the context variable. A sine wave oscillator is created using the createOscillator method, and its frequency is set to 440 Hz, which is the frequency of the A4 note. Finally, the oscillator is connected to the audio output of the web page using the connect method, and the start method is called to start the sound.

You can change the type of the waveform, frequency, and volume of the sound by modifying the properties of the oscillator object. For example, you can change the waveform to a square wave by setting oscillator.type = "square", or change the frequency to a different note by setting a different value for oscillator.frequency.value.

Note that the Web Audio API provides many other features and effects that can be used to generate and manipulate sound in a web page, such as filtering, distortion, and spatialization.

Download the API form here


Added  Feb 02, 03:07 pm

Article tags

SignUp with Email