# 用Microbit 学Python-附录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.

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20180619G0EWJ100?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 yunjia_community@tencent.com 删除。

2018-06-22

2020-12-03

2020-12-03

2020-12-03

2020-12-03

2018-06-06

2018-06-25

2018-06-25

2018-06-20

2020-12-03

2020-12-03

2020-12-03