附录 - 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:
NOTE[octave][:duration]
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”(两只老虎):
import music
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"]
music.play(tune)
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:
importmusic
tune=["C4:4","D","E","C","C","D","E","C","E","F","G:8","E:4","F","G:8"]
music.play(tune)
Notice:How the octave and duration values only change when they have to. It’s a lot less typing and simpler to read.
教学(自学)器材
以下器材和套件比较适合入门Python编程学习的老师和同学,可以配合扩展板(带电池)使用MicroBit完成前期编程任务的学习,后续也可以进行扩展应用学习,所有学习教程都可以在订阅号中找到,并可以得到微信/QQ在线的技术支持!
领取专属 10元无门槛券
私享最新 技术干货