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

附录 - 4

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.

教学(自学)器材

以下器材和套件比较适合入门Python编程学习的老师和同学,可以配合扩展板(带电池)使用MicroBit完成前期编程任务的学习,后续也可以进行扩展应用学习,所有学习教程都可以在订阅号中找到,并可以得到微信/QQ在线的技术支持!

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180620G03HD200?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。

扫码关注云+社区

领取腾讯云代金券