the use of audio

Detailed API reference: audio API

Instructions

MaixAmigo, MaixCube needs Initialize ES8374 audio decoder chip before using audio

  • Create audio object
import audio

player = audio.Audio(path = "/sd/6.wav")
  • Create I2S objects (used to process audio objects)
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)
  • Get audio object information and associate I2S object
# read audio info
wav_info = player.play_process(wav_dev)
print("wav file head information: ", wav_info)
  • Configure I2S objects according to audio information
sample_rate = wav_info[1]
wav_dev.set_sample_rate(sample_rate)
  • Use the associated I2S object to play audio
# loop to play audio
while True:
    ret = player.play()
    if ret == None:
        print("format error")
        break
    elif ret==0:
        print("end")
        break
  • End playback
player.finish()

Routine

Test audio address: 6.wav