The operation of the audio device is primarily to initialize the audio device and send PCM (Pulse Code modulation) data to the audio device. For convenience, this article uses the libraries and drivers provided by ALSA (Advanced Linux Sound architecture). When compiling and running the MP3 streaming Media player in this article, you must first install the ALSA-related files.
In this paper, the functions of PCM equipment operation are divided into PCM equipment initialization functions and PCM interface of some operation functions.
PCM Hardware Device parameter settings and initialization functions are:
int Snd_pcm_hw_params_malloc (snd_pcm_hw_params_t **ptr) int Snd_pcm_hw_params_any (snd_pcm_t *pcm, Snd_pcm_h w_params_t *params) void Snd_pcm_hw_params_free (snd_pcm_hw_params_t *obj) int snd_pcm_hw_params_set_access (s
nd_pcm_t *PCM, snd_pcm_hw_params_t *params,
snd_pcm_access_t _access) int Snd_pcm_hw_params_set_format (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val) int SND_PCM_HW
_params_set_channels (snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
unsigned int val) int snd_pcm_hw_params_set_rate_near (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
PCM interface functions are:
int snd_pcm_hw_params (snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
int snd_pcm_prepare (snd_pcm_t *pcm)
int snd_pcm_open (snd_pcm_t **pcm, const char *name,
snd_pcm_stream_t stream, int mode)
int Snd_ Pcm_close (snd_pcm_t *pcm)
snd_pcm_sframes_t Snd_pcm_writei (snd_pcm_t *pcm,
const void *buffer, Snd_ pcm_uframes_t size)
These functions use the snd_pcm_hw_params_t structure, which contains the hardware information configuration used to play the PCM data stream. Before writing audio data to an audio device (sound card), you must set the access type, sample format, sampling rate, number of channels, and so on.
First use Snd_pcm_open () to open PCM equipment, in ALSA, PCM devices have names corresponding to them. For example, we can define the PCM device name as char *pcm_name = "plughw:0,0". The most important PCM device interfaces are "PLUGHW" and "HW" interfaces. With the "PLUGHW" interface, programmers do not have to care too much about hardware, and ALSA automatically converts data if the configuration parameters set are inconsistent with the actual hardware-supported parameters. If you use the "HW" interface, we must detect whether the hardware supports the set parameters. The two digits following the PLUGHW indicate the device number and the secondary device (subdevice) number respectively.
Snd_pcm_hw_params_malloc () Allocates space for the snd_pcm_hw_params_t structure in the stack, and then initializes the assigned Snd_ with the Snd_pcm_hw_params_any () function using the full configuration space parameter of the sound card pcm_hw_params_t structure. Snd_pcm_hw_params_set_access () Sets the access type, and the macro definitions for the common access types are:
Snd_pcm_access_rw_interleaved
Staggered access. Each PCM frame in the buffer contains sequential sampling data for all set channels. Like a sound card. The PCM stereo data of the sampling length is 16-bit, which indicates that there is 16-bit left channel data in each PCM frame, then the 16-bit right channel data.
Snd_pcm_access_rw_noninterleaved
Non-interleaved access. Each PCM frame is just the data needed for a channel, and if multiple channels are used, the first frame is the first channel data, the second is the second channel, and so on.
The function Snd_pcm_hw_params_set_format () sets the data format, which mainly controls the type of audio data entered, unsigned or signed, Little-endian or Bit-endian. For example, the sampling data for 16-bit length can be set to:
Snd_pcm_format_s16_le signed bit Little endian
Snd_pcm_format_s16_be signed bit big endian
snd_pcm_ Format_u16_le unsigned bit Little endian
snd_pcm_format_u16_be unsigned bit big endian for
example for 32-bit The length of the sample data can be set to:
Snd_pcm_format_s32_le signed bit Little endian
Snd_pcm_format_s32_be signed bit big Endian
snd_pcm_format_u32_le unsigned bit Little endian
snd_pcm_format_u32_be unsigned bit big endian