一、RawSample
import board
from audiocore import RawSample
import time
import array
import math
tone_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 // frequency
sine_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 board
from audiocore import WaveFile
wave_file = open('sound/jump.wav', "rb")
wave = WaveFile(wave_file)
board.BUZZ.play(wave)
三、mp3
import board
from audiomp3 import MP3Decoder
mp3 = open('sound/jump.mp3', "rb")
decoder = MP3Decoder(mp3)
board.BUZZ.play(decoder)
注意:
正常的情况下,audio对象应该是这样产生的(假设输出针脚为A0):
try:
from audioio import AudioOut
except ImportError:
try:
from audiopwmio import PWMAudioOut as AudioOut
except ImportError:
pass # not always supported by every board!
audio = AudioOut(board.A0)
而在meowbit中,board.BUZZ 直接就是一个 AudioOut
因此,在meowbit中,直接用board.BUZZ.play方法来播放。