Making Notes

Songs are made up of sequences of notes (or combinations of multiple notes). The Ready Set STEM API provides a Note() function that will create notes of different pitches.

The basic use of the Note() function is to create a note:

Note(pitch)

where pitch can be either a number (which represents the frequency of the pitch), or a letter (A though G, the name of the note).

For example, an 'A' note (above middle 'C') has the frequency of 440Hz (Hz is a unit of frequency — it is how many times something happens each second). So, to create an 'A' note you have two choices:

Both a_note and another_a_note are equivalent. But, we still haven't played anything yet — we've just created the note we are going to play.

If you've played an instrument, you know there are more notes than just A through G. For one, each note can be played a half-step up ("sharp") or down ("flat"). To specify sharps or flats with the Note() function, append a '#' to your note for a sharp, or 'b' for a flat.

For example, to create a B sharp and a B flat, we could do this:

And there's octaves, too! To specify the octave, append a single digit to the very end of your note. By default, the octave is the fourth octave. One octave higher would be the fifth octave, one octave lower would be the third.

For example, some more ways to specify an 'A':

Finally, you can combine sharps or flats with octaves, as in: 'Ab4', 'B#6', or 'Db7'

Making Music

To play notes, as in the previous project, you'll use the play function, which by default plays a note for one second. For example:

Try the above if you want, and you'll be disappointed to find your note doesn't play. The reason is that play() just starts playing a note, in the background. After starting playing, the program will continue to run — but in our program above, there's no more code after line 3, so the program ends. The program actually ends before we hear the note playing, so we don't hear anything.

This is why we typically use a wait() to force our program to wait for the note to finish. For example here we've added a wait:

Note that the wait() above essentially just waits for one second; if you preferred, you could have used a sleep(1) to accomplish nearly the same thing.

Playing your own song would involve a sequence of playing notes of different durations and waiting for them to finish playing. In addition, a time.sleep() can be be used as a silence between notes. To change the duration that a note plays, you use the duration parameter, to set the seconds to play, as we did in . But, you also can play a note forever by specifying the duration=None.

Finally, what if you want to stop a note early? When writing a game, you might want to start a note when something happens (say a button is pressed) and then stop it when the button is released. To stop it, you'd use the stop() function.

Here is an example:

There are 2 sleeps highlighted in the code above:

So, in the above code, the note plays for two seconds, there are two seconds of silence, and then the program ends.