本文主要是介绍K210机器听觉— 音频播放,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
K210机器听觉— 音频播放
- 使用设备
- ZTFR开发板
- 喇叭接口,注意喇叭正负极
- 音频播放
- 使用方法
- 构造函数
- 示例程序
使用设备
ZTFR开发板
喇叭接口,注意喇叭正负极
音频播放
使用方法
import audioplayer = audio.Audio(path = "/sd/6.wav")
构造函数
创建 I2S 对象(用于处理音频对象)
from Maix import I2S# init i2s(i2s0)
wav_dev = I2S(I2S.DEVICE_0)
# config i2s according to audio info
wav_dev.channel_config(wav_dev.CHANNEL_1, I2S.TRANSMITTER,resolution = I2S.RESOLUTION_16_BIT ,cycles = I2S.SCLK_CYCLES_32, align_mode = I2S.RIGHT_JUSTIFYING_MODE)
获取 audio 对象信息并关联 I2S 对象
# read audio info
wav_info = player.play_process(wav_dev)
print("wav file head information: ", wav_info)
据 audio 信息配置 I2S 对象
sample_rate = wav_info[1]
wav_dev.set_sample_rate(sample_rate)
使用已关联的 I2S 对象播放音频
# loop to play audio
while True:ret = player.play()if ret == None:print("format error")breakelif ret==0:print("end")break
结束播放
player.finish()
示例程序
播放wav格式音频:wav格式的音频频率不能超过16KHZ
from fpioa_manager import *
from Maix import I2S, GPIO
import audio# disable wifi
fm.register(8, fm.fpioa.GPIO0)
wifi_en=GPIO(GPIO.GPIO0,GPIO.OUT)
wifi_en.value(0)# register i2s(i2s0) pin
fm.register(34,fm.fpioa.I2S0_OUT_D1)
fm.register(35,fm.fpioa.I2S0_SCLK)
fm.register(33,fm.fpioa.I2S0_WS)# init i2s(i2s0)
wav_dev = I2S(I2S.DEVICE_0)# init audio
player = audio.Audio(path = "/sd/tishi16K.wav")
player.volume(40)# read audio info
wav_info = player.play_process(wav_dev)
print("wav file head information: ", wav_info)# config i2s according to audio info
wav_dev.channel_config(wav_dev.CHANNEL_1, I2S.TRANSMITTER,resolution = I2S.RESOLUTION_16_BIT ,cycles = I2S.SCLK_CYCLES_32, align_mode = I2S.RIGHT_JUSTIFYING_MODE)
wav_dev.set_sample_rate(wav_info[1])# loop to play audio
while True:ret = player.play()if ret == None:print("format error")breakelif ret==0:print("end")break
player.finish()
这篇关于K210机器听觉— 音频播放的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!