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:
Embedded audio system components
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:
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:
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:
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:
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.
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.).
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 .