in Web Audio API

TSW Web Audio Primer – Part 5

Well, playing one note at a time is fun for a little while, but it would be nice to play more than one note at a time. In using the Web Audio API, the easiest way to do this is to use a separate oscillator for each note. Let us define some new functions:

playChord

Define notes in chord

For each note in the chord, call PlayNote

playNote

Create an oscillator object, start the note, and push it into an array of oscillators

stopChord

For each entry in the array of oscillators, use the osc.stop function in TSW

First, let’s work on the playChord function. We can use the use the tsw.chord function to get a list of the notes in the chord. Here is a first cut:

function playChord() {

var testChord;

testChord = tsw.chord('A4', 'major');

console.log(testChord);

};

This function will write the contents of the list to the Javascript console. To call this function, we will add a line of HTML to create a new button that will call this function. The button logic in index.html should be changed as follows:

FROM:

<p>Press Play to hear how the pitch sounds. <button onclick="turn_on();">Play</button></p>
<p>Press Stop to turn it off. <button onclick="turn_off();">Stop</button></p>

TO:

<p>Press Play to hear how the pitch sounds. <button onclick="turn_on();">Play</button></p>
<p>Press Stop to turn it off. <button onclick="turn_off();">Stop</button></p>
<p>Test function <button onclick="playChord();">Start Chord</button></p>

In Google Chrome, you can see the Javascript console by clicking on the menu button in the upper right corner of the window, then selecting the Tools menu option, then the Javascript Console sub-menu option.

By clicking on the Test button, you should see the following in the console:

[“A”, “C#”, “E”, “A”]

Now, add more logic to test working with the elements of the array:

function playChord() {
var testChord;
testChord = tsw.chord('A4', 'major');
console.log(testChord);
for (i = 0; i &lt; testChord.length; i++) {
console.log(testChord[i]);
}
};

Our result is as follows:

[“A”, “C#”, “E”, “A”]

A

C#

E

A

Now, we can use the tsw.frequency function to get the frequency for the notes in the array.

function playChord() {
var testChord;
testChord = tsw.chord('A4', 'major');
console.log(testChord);
for (i = 0; i &lt; testChord.length; i++) {
console.log(testChord[i], tsw.frequency(testChord[i]));
}
};

<span style="font-size: 14px; line-height: 1.5em;">

We then see:

[“A”, “C#”, “E”, “A”]

A 440

C# 277.1826309768721

E 329.6275569128699

A 440

Now, create the playNote function and pass in the frequency of each note. This will look like the turn_on function created earlier, except that the oscillator object will be created in the function rather than be a global variable outside the function.

function playNote(frequency) {
var osc;
var oscType = 'sine';
osc = tsw.oscillator (frequency, oscType);
tsw.connect(osc, volume, tsw.speakers);
osc.start();
}:

In addition, we will now add some logic in playChord to call playNote for the notes in the chord, but will skip the note in the first element in the array, since it is the same as the first note. Update the function as follows:

function playChord() {
var testChord;
testChord = tsw.chord('A4', 'major');
console.log(testChord);
for (i = 0; i &lt; testChord.length; i++) {
console.log(testChord[i], tsw.frequency(testChord[i]));
}
for (i = 1; i &lt; testChord.length; i++) {
playNote(tsw.frequency(testChord[i]));
}
};

After these updates, you should be able to hear the notes in the chord play when you click the Test button. Now, the last part is to be able to turn the notes off.

Since we are creating a new oscillator object for each note, we need to have a way to turn those objects off. We will add logic to push those objects into an array, and then loop through the array to turn them off.

Change the variable declarations as follows:

FROM:

var testChord;

TO:

var testChord;
var chordOscillators = [];

Update the function playNote to be as follows:

function playNote(frequency) {
var osc;
var oscType = 'sine';
osc = tsw.oscillator (frequency, oscType);
tsw.connect(osc, volume, tsw.speakers);
osc.start();
chordOscillators.push(osc);
};

Now add the following function:

function stopChord() {
for (i = 0; i &lt; chordOscillators.length; i++) {
chordOscillators[i].stop();
}
};

In index.html, we will add a new button to call the stopChord function:

FROM:

<span style="color: #222222;"> <span style="font-family: 'Times New Roman', serif;"><span> <p>Test function <button onclick="playChord();">Start chord</button></p>
</span></span></span>

TO:

<span style="color: #222222;"> <span style="font-family: 'Times New Roman', serif;"><span> <p>Start Chord <button onclick="playChord();">Start Chord</button></p>
<p>Stop Chord <button onclick="stopChord();">Stop Chord</button></p>
</span></span></span>

When you press the Start Chord button, you should hear the chord play. When you press the Stop Chord button, the chord will stop!

You can also go to the demo of this application and see if you can play the chord.

Download the source code for all of the examples in this series from Github.

Write a Comment

Comment