Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.3 Oscillate

In this lesson, I’ll explain how to create an oscillator and change its frequency.

Related Links

2.3 Oscillate

The most important thing in a traditional analog synthesizer is the oscillator. This is what generates it sound. I showed how to make an oscillator earlier in this course. But let's do it again in the context of our synthesizer. Remember, when working with the Web Audio API, we'll need an audio context. So let's start by creating one. In our JavaScript file, we write var context = new AudioContext. Then, we create our oscillator. Let's call it Osc, again, for short. Osc = context.createOscillator. And we also want to be able to control the volume. So let's create a gain note. volume = context.createGain. As half the volume of those later by setting the gain attributes of our volume node to 0.5. So that's volume.gain.value = 0.5;. Next, we have to chain all the notes together in order for the sound generated by our oscillator to flow through to our speakers. First, we connect the oscillator to our volume node. Osc.connect(volume);. Then, we connect our volume to our speakers. Like so. volume.connect(context.destination);. That all looks good. So all we need to do now is to tell us the oscillator to start generating its tone. And we do that by saying osc.start, and we want this to happen right now. So we use the Web Audio API's precise timing system. To get now as a precise moment in time. We do this by passing (context.currentTime). We don't want this oscillator to run forever, so let's make it stop after one second. We do that by saying osc.stop, and then (context.currentTime + 1). So what we're saying here, is stop the oscillator, playing now, plus one second. Let's head over to our browser to hear how that sounds. [SOUND] Great. Now, a synthesizer that only plays a single note isn't much fun. So let's have a look at frequencies. When you play a different note on a musical instrument, whether it be a piano, a guitar, or a tuba, the noise produced is of a certain frequency. Frequencies are measured in hertz, and basically refer to how quickly a wave is repeating. Or essentially, how fast your instrument, the air, and your eardrum is vibrating. The higher the frequency, the higher the pitch of the musical note. Let's try it out. First, comment out the call to the start method on your oscillator. Then, open the page in your browser. [SOUND] You'll hear a noise played before, but obviously, it's not stopping. Open up your browser's JavaScript counsel again. Now, type, osc, and hit return. You'll see because we created our oscillator and our global scope, I'll have access to it here. Next, type osc.frequency.value. This will show you the current frequency of our oscillator in hertz. Let's change this. Why don't we double it? Type osc.frequency.value = 880. [SOUND] In the pressure tone, you'll hear the pitch goes up an octave. [SOUND] A similar thing can be said when we half it. Let's try osc.frequency.value = 220. That's much lower. I have a player run with these values. [SOUND] And use osc.stop() when you're done. Wikipedia has a useful page that lists the frequencies that relate to individual musical notes. Now, you may notice that when you change a frequency, the note slides from one to the next. That's because we're using just one oscillator. Keep this in mind for the next lesson where we'll be adding a keyboard to control the frequency.

Back to the top