一、RawSample
import boardfrom audiocore import RawSampleimport timeimport arrayimport mathtone_volume = 0.1 # Increase this to increase the volume of the tone.frequency = 440 # Set this to the Hz of the tone you want to generate.length = 8000 // frequencysine_wave = array.array("H", [0] * length)for i in range(length):sine_wave[i] = int((1 + math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))sine_wave_sample = RawSample(sine_wave)board.BUZZ.play(sine_wave_sample, loop=True)time.sleep(1)board.BUZZ.stop()
二、wav
import boardfrom audiocore import WaveFilewave_file = open('sound/jump.wav', "rb")wave = WaveFile(wave_file)board.BUZZ.play(wave)
三、mp3
import boardfrom audiomp3 import MP3Decodermp3 = open('sound/jump.mp3', "rb")decoder = MP3Decoder(mp3)board.BUZZ.play(decoder)
注意:
正常的情况下,audio对象应该是这样产生的(假设输出针脚为A0):
try:from audioio import AudioOutexcept ImportError:try:from audiopwmio import PWMAudioOut as AudioOutexcept ImportError:pass # not always supported by every board!audio = AudioOut(board.A0)
而在meowbit中,board.BUZZ 直接就是一个 AudioOut
因此,在meowbit中,直接用board.BUZZ.play方法来播放。
