AUDIO Devices
Audio devices are a very important part of embedded systems, responsible for sampling and outputting audio data. Audio devices usually consist of a data bus interface, a control bus interface, an audio codec (Codec), a speaker, and a microphone, as shown in the following figure:

The RT-Thread Audio device driver framework is the underlying part of the Audio framework, which is mainly responsible for the acquisition and output of native audio data, audio stream control, audio device management, volume adjustment, and abstraction of different hardware and Codecs.
Interface: standard device interface (open/close/read/control).
Synchronous mode access.
Supports playback and recording.
Support audio parameter management.
Support volume adjustment.
The application obtains the device handle according to the Audio device name, and then can operate the Audio device. The device search function is as follows:
parameter
describe
name
Audio Device Name
return
——
Device handle
If the corresponding device is found, the corresponding device handle will be returned.
RT_NULL
No corresponding device object was found
The usage examples are as follows:
Through the device handle, the application can open and close the device, and open the device through the following function:
parameter
describe
dev
Device handle
oflags
Device mode flags
return
——
RT_EOK
Device opened successfully
- RT_EBUSY
If the device registration parameter includes the RT_DEVICE_FLAG_STANDALONE parameter, the device will not be allowed to be opened repeatedly.
-RT_EINVAL
Unsupported open parameters
Other error codes
Device open failed
The oflags parameter supports the following parameters:
Audio devices are divided into two types: playback and recording. The playback device outputs audio data to the Codec, while the recording device reads the data. When in use, the playback device is identified by a write-only flag, and the recording device is identified by a read-only flag.
The following is an example of opening the Audio playback device:
The following is an example of opening the Audio recording device:
Through the command control word, the application can configure the Audio device through the following functions:
parameter
describe
dev
Device handle
cmd
Command control word, see below for details
arg
Control parameters, detailed description see below
return
——
RT_EOK
Function execution successful
-RT_ENOSYS
Execution failed, dev is empty
Other error codes
Execution failed
The cmd currently supports the following command control characters:
The definition of the device function attribute structure is as follows
Set the audio parameters for playback
Set the playback sampling rate, sampling channel, and sampling bit number.
Set the master volume for playback
Sets the master volume for playback.
Set the audio parameter information of the recording
Set the recording sampling rate, sampling channel, and sampling bit number.
Set the master volume for recording
Set the master volume for recording.
Writing data to the audio playback device can be done through the following function:
parameter
describe
dev
Device handle
pos
Write data offset. This parameter is not used by audio devices.
buffer
Memory buffer pointer where the data to be written is placed
size
The size of the data to be written
return
——
The actual size of the data written
In bytes;
Calling this function will write the data in the buffer buffer to the device dev. The size of the written data is size. This function is a synchronous interface. The driver framework will first save the data to the buffer of the audio device. When the buffer is full, the function is blocked.
The following function can be called to read the data received by the audio recording device:
parameter
describe
dev
Device handle
pos
Read data offset. This parameter is not used by the serial port device.
buffer
Buffer pointer. The read data will be saved in the buffer.
size
The size of the data to be read
return
——
The actual size of the data read
If it is a character device, the returned size is in bytes
0
You need to read the errno of the current thread to determine the error status
Call this function to read data of size from the audio recording device into the buffer. This function is a synchronous interface. When the data in the Pipe cache inside the driver framework is less than size, the function is blocked.
When the application completes the serial port operation, it can close the audio device, which is done through the following function:
parameter
describe
dev
Device handle
return
——
RT_EOK
Shut down the device successfully
-RT_ERROR
The device has been completely shut down. You cannot shut down the device again.
Other error codes
Failed to shut down the device
Closing the device interface and opening the device interface must be used in pairs. Each time you open the device, you must close it once. Only in this way can the device be completely closed. Otherwise, the device will still be in an open state.
Audio devices are used for playback and recording, usually with the encoding and decoding of audio files. The following is an example of playing and recording wav files. The complete code can be obtained through the RT-Thread wavplayer software package .
The main steps to play a piece of audio data are as follows:
First, find the Audio device and get the device handle.
Open the Audio device in write-only mode.
Set audio parameter information (sampling rate, channel, etc.).
Decode the audio file's data.
Write audio file data.
When playback is complete, turn off the device.
The main steps for recording a piece of audio data are as follows:
First, find the Audio device and get the device handle.
Open the Audio device in read-only mode.
Set audio parameter information (sampling rate, channel, etc.).
Read data from the audio device.
Process the read data, etc.
When the recording is complete, turn off the device.
Last updated
Was this helpful?