一、RawSample

    1. import board
    2. from audiocore import RawSample
    3. import time
    4. import array
    5. import math
    6. tone_volume = 0.1 # Increase this to increase the volume of the tone.
    7. frequency = 440 # Set this to the Hz of the tone you want to generate.
    8. length = 8000 // frequency
    9. sine_wave = array.array("H", [0] * length)
    10. for i in range(length):
    11. sine_wave[i] = int((1 + math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
    12. sine_wave_sample = RawSample(sine_wave)
    13. board.BUZZ.play(sine_wave_sample, loop=True)
    14. time.sleep(1)
    15. board.BUZZ.stop()

    二、wav

    1. import board
    2. from audiocore import WaveFile
    3. wave_file = open('sound/jump.wav', "rb")
    4. wave = WaveFile(wave_file)
    5. board.BUZZ.play(wave)

    三、mp3

    1. import board
    2. from audiomp3 import MP3Decoder
    3. mp3 = open('sound/jump.mp3', "rb")
    4. decoder = MP3Decoder(mp3)
    5. board.BUZZ.play(decoder)

    注意:
    正常的情况下,audio对象应该是这样产生的(假设输出针脚为A0):

    1. try:
    2. from audioio import AudioOut
    3. except ImportError:
    4. try:
    5. from audiopwmio import PWMAudioOut as AudioOut
    6. except ImportError:
    7. pass # not always supported by every board!
    8. audio = AudioOut(board.A0)

    而在meowbit中,board.BUZZ 直接就是一个 AudioOut 对象:
    image.png
    因此,在meowbit中,直接用board.BUZZ.play方法来播放。