Linux audio programming (a) ALSA Introduction

Linux audio programming under there OSS and ALSA , this article will ALSA associated description. ALSA offers a range of set-based command-line tools, such as mixer (Mixer) , an audio file player (aplay) , as well as tool-specific properties of specific sound control.

A, ALSA 's  API is divided into the following interfaces:
( 1) Control Interface: provides a flexible way to manage the registration card and query the existing sound card.
( 2) the PCM Interface: manage digital audio playback (Playback) and recording (Capture) interface .

( Pulse Code Modulation (PCM). This is a term describing a method of using an analog signal in digital form. This method is used by almost all computer sound card, which in the "audio" referred to the API by the ALSA . )
( 3) of the original MIDI interface: supports MIDI (musical Instrument Digital Interface), a standard electronic music instruction set. The API provides access to MIDI bus on the card. The original excuse to work directly on The MIDI event, the programmer only needs to manage protocol and time.
( 4) Time Recording Interface : Provides access to the timer on the sound card supports synchronous event sounds.
( 5) sequencer interfaces: one than the original MIDI interface advanced MIDI programming and sound with high-level interface step. It can handle a lot of MIDI protocol and timer.
( 6) mixer interface: transmits a control signal on the control device and the size of the sound card.

two,

1, pcm alsa used to describe the digital audio stream. Alsa audio playback / recording can by achieved pcm .

2, the kernel device driver layer, the ALSA provided alsa-driver, while the application layer, the ALSA provided alsa-lib, the application as long as the call alsa-lib API provided by us, i.e., complete control of the underlying audio hardware.

3, alsa driver actually divided into two layers, such s3c24xx-iis.c s3c24xx-ac97.c or the like, the main audio hardware initialization bus (iis / ac97, GPIO, dma, etc.), another is wm8987.c driving chip or the like, mainly to provide the register read-write interface (I2C, etc.), create a mixer apparatus or the like.

Three, PCM Introduction

(A) brief

1, according to ALSA PCM to write a simple application, we first need to open a handle (Handle) for the PCM device, and then specify the direction of the PCM stream is the Playback .

2, pcm playback and below a capture stream, playback and capture under each have a substream.

( . 1) Playback how the user space application sent, PCM data, transforming the human ear can distinguish analog audio .

( 2) Capture the mic pickup to get an analog signal, sampled, quantized, converted into a PCM signal back to the user space application .

(B) procedures

( 1) This part of the program can refer to:

Refer to the link: https://blog.csdn.net/orz415678659/article/details/8995163 .

 

among them:

snd_card shows an example of a sound card, a sound card comprising a plurality of devices;

snd_device represents a sound equipment components:

snd_pcm represents one kind of a PCM device, sound device for playback and recording;

Control snd_control means a device, sound device, for controlling the sound card; snd_pcm_str represents PCM stream is divided into playback and Capture;

snd_pcm_substream PCM sub-stream, an audio playback or recording;

snd_pcm_ops PCM stream operation set.

Specific reference links:

https://www.cnblogs.com/hzl6255/p/9979377.html

( 2) For the following links pcm.c procedure:

https://github.com/tinyalsa/tinyalsa/blob/master/src/pcm.c

1, reasonable pcm_config can do better low latency and power consumption .

struct pcm_config {

unsigned int channels;

unsigned int rate;

unsigned int period_size;

unsigned int period_count;

enum pcm_format format;

unsigned int start_threshold;

unsigned int stop_threshold;

unsigned int silence_threshold;

int avail_min;

};

( 1) unit structures each argument is Frame (Frame = 1 * channel sample bit depth):

period_size. Each time the length of the transmission data. The smaller the value, the smaller the delay, the higher the cpu usage.

( 2) period_count. Buffer number of the buffer zone period. The larger the buffer, the less the chance of XRUN.

( . 3) the format. Data format is defined, such as sample bit depth, the size of the terminal.

( . 4) START_THRESHOLD. When this value exceeds the buffer data, hardware data transfer started. If too much, play from the beginning to sound out the delay is too long, too short even lead to broadcasting sound at all out; if too small, they may easily lead to XRUN.

( . 5) stop_threshold. When the buffer zone is greater than the idle value, it stops transmission hardware. By default, this number is the total size of the buffer that the entire buffer is empty, stop transmission. But occasionally causes of buffer space, such as CPU busy, increase the value, historical data buffer to continue playing, without closing the restart transmission hardware (usually at this time a clear voice Caton), you can achieve better experience.

( 6) silence_threshold. This value is in line with stop_threshold originally used to fill the silence data buffer, so as not to replay the historical data. But if not set silence_size,

( When 7) avail_min. Buffer spare area larger than this value, pcm_mmap_write () before writing data to the buffer. The higher the value, the fewer the number of times data is written to the buffer, the greater the chance of facing XRUN. Android samsung tuna apparatus increase the value to reduce power consumption when screen_off, reduce the value at screen_on to reduce the chance XRUN.

In different scenarios, parameter is between reasonable performance, latency, power consumption can be well balanced.

8)struct pcm pcm_open(unsigned int card, unsigned int device, unsinged int flags, struct pcm_config config)

2, from you can see pcm_open this interface, it gets a handle by several parameters, after all operations are done by the handle. The order of the parameters inside, card represents the first few sound cards, device is the device index mentioned above, it is configured with the driver DAI link related, flags parameter will indicate this type of device is the capture or playback type. By three parameters, can be found the corresponding PCM device file .

3, a pcm device include play / record two streams, each have several substream. A substream only be occupied by one process. Which snd_pcm_substream can achieve audio playback or recording. as follows: 

struct snd_pcm_substream { 
struct snd_pcm *pcm; 
struct snd_pcm_str *pstr; 
void *private_data; /* copied from pcm->private_data */ 
int number; 
char name[32]; /* substream name */ 
int stream; /* stream (direction) */ /* 录制/播放 */ 
struct pm_qos_request latency_pm_qos_req; /* pm_qos request */ 
size_t buffer_bytes_max; /* limit ring buffer size */ 
struct snd_dma_buffer dma_buffer; 
unsigned int dma_buf_id; 
size_t dma_max; 
/* -- hardware operations -- */ 
const struct snd_pcm_ops *ops; 
/* -- runtime information -- */ 
struct snd_pcm_runtime *runtime; 
/* -- timer section -- */ 
struct snd_timer *timer; /* timer */ 
timer_running unsigned:. 1; / * Time running IS * / 
/ * - Next substream is - * / 
struct * Next snd_pcm_substream; 
/ * - linked substreams - * / 
struct the list_head link_list; / * List Member linked * / 
struct snd_pcm_group self_group; / * Fake Group for non substream is linked (with substream is Inside Lock) * / 
struct * Group snd_pcm_group; / * pointer to Current Group * / 
/ * - Assigned Files - * / 
void * File; / * point pcm_file * / 
int ref_count; / * reference count is useful when opening the O_APPEND * / 
atomic_t mmap_count; / * the mmap reference count * / 
unsigned int f_flags; / * PCM open file mark * / 
void (* pcm_release) (struct snd_pcm_substream *); 
struct pid * pid; when / * where the process pid, which is used to select multiple substream * / using 
/ * misc flags * / 
unsigned int hw_opened: 1; / * if open, when released substream call the Close () * / 
}; 

 

Fourth, added:

alsa -driven device files can Linux system, / dev / snd  in view.

Wherein: c0d0 represents the sound 0 devices 0 , pcmC0D0c last c representative of Capture , pcmC0D0p last p Representative Playback .

controlC0 : for sound control, such as channel selection, mixing, microphone control ;

midiC0D0 : used to play midi audio ;

pcmC0D0c :  for recording pcm equipment ;

pcmC0D0p : for playback pcm equipment ;

SEQ  : Sequencer ;

the Timer : Timer ;

 

Guess you like

Origin www.cnblogs.com/L-102/p/11480240.html