TVP

# 用Microbit 学Python-附录4-Sound Effects的生成

Sound Effects(特殊音效)

MicroPython lets you make tones that are not musical notes. For example, here’s how to create a Police siren effect:

importmusic

whileTrue:forfreqinrange(880,1760,8):music.pitch(freq,3)forfreqinrange(1760,880,-8):music.pitch(freq,3)

Notice：The music.pitch method is used in this instance. It expects a frequency. For example, the frequency of 440 is the same as a concert A used to tune a symphony orchestra.

In the example above the range function is used to generate ranges of numeric values. These numbers are used to define the pitch of the tone. The three arguments for the range function are the start value, end value and step size. Therefore, the first use of range is saying, in English, “create a range of numbers between 880 and 1760 in steps of 8”. The second use of range is saying, “create a range of values between 1760 and 880 in steps of -8”. This is how we get a range of frequencies that go up and down in pitch like a siren. For each frequency in the specified range of frequencies, the pitch of that frequency is played for 3 milliseconds.

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

2018-06-22

2018-06-22

2023-03-29

2023-03-29

2023-03-29

2023-03-29

2023-03-29

2018-06-22

2018-06-22

2023-03-29

2021-01-08

10元无门槛代金券