附录 - 3
Creating your own tunes
Each note has a name (like or ), an octave (telling MicroPython how high or low the note should be played) and a duration (how long it lasts through time). Octaves are indicated by a number ~ 0 is the lowest octave, 4 contains middle C and 8 is about as high as you’ll ever need unless you’re making music for dogs. Durations are also expressed as numbers. The higher the value of the duration the longer it will last. Such values are related to each other - for instance, a duration of will last twice as long as a duration (and so on). If you use the note name then MicroPython will play a rest (i.e. silence) for the specified duration.
Each note is expressed as a string of characters like this:
For example, refers to the note named in octave number to be played for a duration of .
Make a list of notes to create a melody (it’s equivalent to creating an animation with a list of images). For example, here’s how to make MicroPython play opening of “Frere Jaques”（两只老虎）：
tune = ["C4:4", "D4:4", "E4:4", "C4:4", "C4:4", "D4:4", "E4:4", "C4:4",
"E4:4", "F4:4", "G4:8", "E4:4", "F4:4", "G4:8"]
Note:MicroPython can help you to simplify such melodies. It’ll remember the octave and duration values until you next change them. As a result, the example above can be re-written as:
Notice：How the octave and duration values only change when they have to. It’s a lot less typing and simpler to read.