MicroPython Cookbook
上QQ阅读APP看书,第一时间看更新

How it works...

The first line of code in this recipe imports the Circuit Playground Express library. Then, three global constants are defined and are named after their associated musical notes. The scientific pitch notation (SPN) is used in this recipe. This notation works by combining the name of a musical note with the number specifying the pitch's octave. In the case of E5, the note will be E and the octave will be 5. Here, each note maps to a specific frequency of sound.

In the first block of code, the E5 note is played simply by referring to the E5 global constant when calling the play_tone method. Setting the duration to 0.15 allows each note play for 150 milliseconds, which creates a comfortable pace to the music. Reducing or increasing this value can increase or reduce the pace that the musical tone will play. The second block of code plays the remaining two notes defined at the same speed.

The frequencies used in this chapter follow the standard piano key frequencies. This is equivalent to the standard concert pitch and the 12-tone equal temperament.