An abstract audio object that can be passed in as a parameter or use its methods to play audio directly

1. Module functions

1.1. Constructor

Constructing an Audio object

audio.Audio (array = None, path = None, points = 1024)


This interface can pass a parameter, each parameter will determine a different audio type

  • array: data of typebytearray, which can be converted to audio objects, default None

  • path: The path of the opened audio file, currently only supports thewav format. The default is None, Note The keywordspath, audio.Audio (" / sd / 1.wav " )This is wrong! !! audio.Audio (path =" /sd/1.wav ") is correct

  • points: open audio buffer with points sample points, one sample point size is 32bit. If it is 0, the buffer will not be opened. The default is 1024.

return value

Returns an Audio object

1.2. to_bytes: bytes conversion function

Converts audio data in an audio object to an object of type bytearray

audio_data = test_audio.to_bytes ()



return value

The returned audio data bytearray object

1.3. play_process: Play preprocessing function

It is used to preprocess audio objects, and audio files need to be parsed before playback, so preprocessing is required. Here you need to pass in an I2S device for playback

wav_info = test_audio.play_process (i2s_dev)


  • i2s_dev: i2s device for playback

return value

Header information of the wav file, list type, which arenumchannels, samplerate,byterate, blockalign,bitspersample, datasize

1.4. play: Play function

Read audio files and parse them. Generally used in conjunction with loops.



return value

  • None: Format does not support playback
  • 0: End of playback
  • 1: Now playing

1.5. finish: Audio post-processing functions

Complete the audio playback. This function must be called after the playback is completed, and the underlying allocated resources are recycled.



return value


2. Examples

Play wav audio

from fpioa_manager import *
from Maix import I2S, GPIO
import audio

# disable wifi
fm.register (8, fm.fpioa.GPIO0)
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/6.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 = ()
    if ret == None:
        print ("format error")
    elif ret == 0:
        print ("end")
player.finish ()
powered by GitbookFile Modify: 2021-02-03 10:26:57

results matching ""

    No results matching ""