maix.audio

maix.audio module

You can use maix.audio to access this module with MaixPy
This module is generated from MaixPy and MaixCDK

1. Module#

No module

2. Enum#

2.1. Format#

Audio type

item describe
values FMT_NONE: format invalid
FMT_S8: unsigned 8 bits
FMT_S16_LE: signed 16 bits, little endian
FMT_S32_LE: signed 32 bits, little endian
FMT_S16_BE: signed 16 bits, big endian
FMT_S32_BE: signed 32 bits, big endian
FMT_U8: unsigned 8 bits
FMT_U16_LE: unsigned 16 bits, little endian
FMT_U32_LE: unsigned 32 bits, little endian
FMT_U16_BE: unsigned 16 bits, big endian
FMT_U32_BE: unsigned 32 bits, big endian

C++ defination code:

enum Format
    {
        FMT_NONE = 0,       // format invalid
        FMT_S8,             // unsigned 8 bits
        FMT_S16_LE,         // signed 16 bits, little endian
        FMT_S32_LE,         // signed 32 bits, little endian
        FMT_S16_BE,         // signed 16 bits, big endian
        FMT_S32_BE,         // signed 32 bits, big endian
        FMT_U8,             // unsigned 8 bits
        FMT_U16_LE,         // unsigned 16 bits, little endian
        FMT_U32_LE,         // unsigned 32 bits, little endian
        FMT_U16_BE,         // unsigned 16 bits, big endian
        FMT_U32_BE,         // unsigned 32 bits, big endian
    }

3. Variable#

3.1. fmt_bits#

Map the audio format to the number of bits

item description
param format: audio format
return number of bits
value {
0, 8, 16, 32, 16, 32, 8, 16, 32, 16, 32
}
readonly True

C++ defination code:

const std::vector<int> fmt_bits = {
        0, 8, 16, 32, 16, 32, 8, 16, 32, 16, 32
    }

4. Function#

5. Class#

5.1. File#

Audio file reader

C++ defination code:

class File: public AudioFile<float>

5.1.1. __init__#

def __init__(self, sample_rate: int = 16000, channels: int = 1, bits_per_sample: int = 16) -> None

Construct a new File object.

item description
type func
param path: wav or pcm file path
sample_rate: sample rate, need to be filled in when parsing .pcm files
channels: channels, need to be filled in when parsing .pcm files
bits_per_sample: bits per sample, need to be filled in when parsing .pcm files
static False

C++ defination code:

File(int sample_rate = 16000, int channels = 1, int bits_per_sample = 16)

5.1.2. load#

def load(self, path: str, sample_rate: int = 16000, channels: int = 1, bits_per_sample: int = 16) -> maix.err.Err

Loads an audio file from a given file path.

item description
type func
param path: The file path to load the audio file from.
sample_rate: The sample rate of the audio file. Only required for PCM files
channels: The number of channels in the audio file. Only required for PCM files
bits_per_sample: The number of bits per sample in the audio file. Only required for PCM files
return An error code indicating whether the operation was successful or not.
static False

C++ defination code:

err::Err load(std::string path, int sample_rate = 16000, int channels = 1, int bits_per_sample = 16)

5.1.3. save#

def save(self, path: str) -> maix.err.Err

Saves an audio file to a given file path.

item description
type func
param path: The path to the file where the audio file will be saved.
return An error code indicating whether the operation was successful or not.
static False

C++ defination code:

err::Err save(std::string path)

5.1.4. get_pcm#

def get_pcm(*args, **kwargs)

Get pcm data

item description
type func
return pcm data. datatype @see Bytes
static False

C++ defination code:

Bytes *get_pcm(bool copy = true)

5.1.5. set_pcm#

def set_pcm(self, new_pcm: maix.Bytes(bytes), copy: bool = True) -> None

Set pcm data

item description
type func
param new_pcm: pcm data. datatype @see Bytes
static False

C++ defination code:

void set_pcm(Bytes *new_pcm, bool copy = true)

5.1.6. sample_bits#

def sample_bits(self, new_sample_bits: int = -1) -> int

Get sample bit

item description
type func
param new_sample_bit: if new_sample_bit > 0, set sample bit
return current sample bit
static False

C++ defination code:

int sample_bits(int new_sample_bits = -1)

5.1.7. channels#

def channels(self, new_channels: int = -1) -> int

Get channels

item description
type func
param new_channels: if new_channels > 0, change channels
return current channels
static False

C++ defination code:

int channels(int new_channels = -1)

5.1.8. sample_rate#

def sample_rate(self, new_sample_rate: int = -1) -> int

Get sample rate

item description
type func
param new_sample_rate: if new_sample_rate > 0, change sample rate
return current sample rate
static False

C++ defination code:

int sample_rate(int new_sample_rate = -1)

5.2. Recorder#

Recorder class

C++ defination code:

class Recorder

5.2.1. __init__#

def __init__(self, path: str = '', sample_rate: int = 48000, format: Format = ..., channel: int = 1, block: bool = True) -> None

Construct a new Recorder object. currectly only pcm and wav formats supported.

item description
type func
param path: record path. the path determines the location where you save the file, if path is none, the audio module will not save file.
sample_rate: record sample rate, default is 48000(48KHz), means 48000 samples per second.
format: record sample format, default is audio::Format::FMT_S16_LE, means sampling 16 bits at a time and save as signed 16 bits, little endian. see @audio::Format
channel: record sample channel, default is 1, means 1 channel sampling at the same time
block: block record, default is true, means block record, if false, record will not block
static False

C++ defination code:

Recorder(std::string path = std::string(), int sample_rate = 48000, audio::Format format = audio::Format::FMT_S16_LE, int channel = 1, bool block = true)

5.2.2. volume#

def volume(self, value: int = -1) -> int

Set/Get record volume

item description
type func
param value: volume value, If you use this parameter, audio will set the value to volume,
if you don't, it will return the current volume. range is [0, 100].
return the current volume
static False

C++ defination code:

int volume(int value = -1)

5.2.3. mute#

def mute(self, data: int = -1) -> bool

Mute

item description
type func
note MaixCAM2 dose not support this api.
param data: mute data, If you set this parameter to true, audio will set the value to mute,
if you don't, it will return the current mute status.
return Returns whether mute is currently enabled.
static False

C++ defination code:

bool mute(int data = -1)

5.2.4. reset#

def reset(self, start: bool = True) -> None

Reset record status

item description
type func
param start: start prepare audio data, default is True
static False

C++ defination code:

void reset(bool start = true)

5.2.5. record#

def record(*args, **kwargs)

Record, Read all cached data in buffer and return. If there is no audio data in the buffer, may return empty data.

item description
type func
note 1. Do not set the time too low, for example: 1ms, as the buffer may not be ready with audio data, which could corrupt the internal state.
2. In non-blocking mode, you need to actively execute reset() before you can start capturing audio.
Additionally, in non-blocking mode, if the buffer does not have enough data, only the currently prepared audio data will be returned.
As a result, the length of the actual output audio data may not match the length of the captured audio data.
param record_ms: Block and record audio data lasting record_ms milliseconds and save it to a file, the return value does not return audio data. Only valid if the initialisation path is set.
return pcm data. datatype @see Bytes. If you pass in record_ms parameter, the return value is an empty Bytes object.
static False

C++ defination code:

maix::Bytes *record(int record_ms = -1)

5.2.6. finish#

def finish(self) -> maix.err.Err

Finish the record, if you have passed in the path, this api will save the audio data to file.

item description
type func
return error code, err::ERR_NONE means success, others means failed
static False

C++ defination code:

err::Err finish()

5.2.7. frame_size#

def frame_size(self, frame_count: int = 1) -> int

Returns the number of bytes for frame_count frames.

item description
type func
param frame_count: frame count
return frame bytes
static False

C++ defination code:

int frame_size(int frame_count = 1)

5.2.8. get_remaining_frames#

def get_remaining_frames(self) -> int

Return the number of frames available for reading during recording, unit is frame.

item description
type func
note 1. MaixCAM2 dose not support this api.
2. The number of bytes per frame can be calculated using frame_size().
return remaining frames
static False

C++ defination code:

int get_remaining_frames()

5.2.9. period_size#

def period_size(self, period_size: int = -1) -> int

Set/Get the audio buffer size, unit: frame.

item description
type func
note 1. Generally, the buffer size needs to be modified during non-blocking operations.
2. The number of bytes per frame can be calculated using frame_size().
param period_size: When period_size is less than 0, the current value of period_size will be returned;
when period_size is greater than 0, period_size will be updated, and the size of period_size after setting will be returned.
return the current period size
static False

C++ defination code:

int period_size(int period_size = -1)

5.2.10. period_count#

def period_count(self, period_count: int = -1) -> int

Set/Get the audio buffer count, unit: frame.

item description
type func
note Generally, the buffer size needs to be modified during non-blocking operations.
param period_count: When period_count is less than 0, the current value of period_count will be returned;
when period_count is greater than 0, period_count will be updated, and the size of period_count after setting will be returned.
return the current period size
static False

C++ defination code:

int period_count(int period_count = -1)

5.2.11. sample_rate#

def sample_rate(self) -> int

Get sample rate

item description
type func
return returns sample rate
static False

C++ defination code:

int sample_rate()

5.2.12. format#

def format(self) -> Format

Get sample format

item description
type func
return returns sample format
static False

C++ defination code:

audio::Format format()

5.2.13. channel#

def channel(self) -> int

Get sample channel

item description
type func
return returns sample channel
static False

C++ defination code:

int channel()

5.3. Player#

Player class

C++ defination code:

class Player

5.3.1. __init__#

def __init__(self, path: str = '', sample_rate: int = 48000, format: Format = ..., channel: int = 1, block: bool = True) -> None

Construct a new Player object

item description
type func
param path: player path. the path determines the location where you save the file, if path is none, the audio module will not save file.
sample_rate: player sample rate, default is 48000(48KHz), means 48000 samples per second.
format: player sample format, default is audio::Format::FMT_S16_LE, means sampling 16 bits at a time and save as signed 16 bits, little endian. see @audio::Format
channel: player sample channel, default is 1, means 1 channel sampling at the same time
block: block record, default is true, means block record, if false, record will not block
static False

C++ defination code:

Player(std::string path = std::string(), int sample_rate = 48000, audio::Format format = audio::Format::FMT_S16_LE, int channel = 1, bool block = true)

5.3.2. volume#

def volume(self, value: int = -1) -> int

Set/Get player volume

item description
type func
param value: volume value, If you use this parameter, audio will set the value to volume,
if you don't, it will return the current volume. range is [0, 100].
return the current volume
static False

C++ defination code:

int volume(int value = -1)

5.3.3. play#

def play(self, data: maix.Bytes(bytes) = b'') -> maix.err.Err

Play

item description
type func
param data: audio data, must be raw data
return error code, err::ERR_NONE means success, others means failed
static False

C++ defination code:

err::Err play(maix::Bytes *data = maix::audio::Player::NoneBytes)

5.3.4. frame_size#

def frame_size(self, frame_count: int = 1) -> int

Returns the number of bytes for frame_count frames.

item description
type func
param frame_count: frame count
return frame bytes
static False

C++ defination code:

int frame_size(int frame_count = 1)

5.3.5. get_remaining_frames#

def get_remaining_frames(self) -> int

Return the number of idle frames available for writing during playback, unit: frame. if there are no idle frames, it will cause blocking.

item description
type func
note 1. MaixCAM2 dose not support this api.
2. The number of bytes per frame can be calculated using frame_size().
return remaining frames
static False

C++ defination code:

int get_remaining_frames()

5.3.6. period_size#

def period_size(self, period_size: int = -1) -> int

Set/Get the audio buffer size, unit: frame.

item description
type func
note 1. Generally, the buffer size needs to be modified during non-blocking operations.
2. The number of bytes per frame can be calculated using frame_size().
param period_size: When period_size is less than 0, the current value of period_size will be returned;
when period_size is greater than 0, period_size will be updated, and the size of period_size after setting will be returned.
return the current period size
static False

C++ defination code:

int period_size(int period_size = -1)

5.3.7. period_count#

def period_count(self, period_count: int = -1) -> int

Set/Get the audio buffer count, unit: frame.

item description
type func
note Generally, the buffer size needs to be modified during non-blocking operations.
param period_count: When period_count is less than 0, the current value of period_count will be returned;
when period_count is greater than 0, period_count will be updated, and the size of period_count after setting will be returned.
return the current period size
static False

C++ defination code:

int period_count(int period_count = -1)

5.3.8. reset#

def reset(self, start: bool = False) -> None

Reset player status

item description
type func
param start: start play audio data, default is False
static False

C++ defination code:

void reset(bool start = false)

5.3.9. sample_rate#

def sample_rate(self) -> int

Get sample rate

item description
type func
return returns sample rate
static False

C++ defination code:

int sample_rate()

5.3.10. format#

def format(self) -> Format

Get sample format

item description
type func
return returns sample format
static False

C++ defination code:

audio::Format format()

5.3.11. channel#

def channel(self) -> int

Get sample channel

item description
type func
return returns sample channel
static False

C++ defination code:

int channel()