Detailed description of the audio recording and playing process for ALSA Conversion

Source: Internet
Author: User
Tags preg

Preface:

In Linux, the data streams of recording and playing must be clearly analyzed in both OSS and ALSA systems. Analyze the Alsa driver layer and associate it with the Alsa library layer and application layer.

 

Link Analysis:

In the core/pcm_native.c file. MMAP = snd_pcm_mmap call snd_pcm_mmap_data (substream, file, area); Further call substream-> OPS-> MMAP (substream, area); according to soc/PXA/pxa3xx-pcm.c file. MMAP = pxa3xx_pcm_mmap. dma_mmap_writecombine (, runtime-> dma_addr,) is called. The pxa3xx_pcm_hw_params () function in the SOC/PXA/pxa3xx-pcm.c file is analyzed in the previous chapter.
= Runtime-> dma_addr; and dma_desc-> dsadr = dma_buff_phys; we can see that runtime-> dma_addr is the address of the DMA memory, which is passed in by the Alsa library layer. Also according to dma_desc-> dtadr = prtd-> Params-> dev_addr and SOC/PXA/pxa3xx-ac97.c files. dev_addr = _ preg (pcdr). We can see that the address of the DMA peripheral is the FIFO read/write register pcdr in the ac97 controller. So far, the first link has been established: FIFO interacts with memory through DMA. To be continued 1
.

The ac97 interface or I2S or PCM Interface can connect the CPU and CODEC (wm9714/alc5620/alc5621), and configure the format: the PCM Interface must be configured with the sampling rate, number of sampling digits, number of channels, and transmission format. The I2S interface must be configured with the sampling rate, number of sampling digits, number of channels, and alignment mode. The ac97 interface is flexible, it can be considered that the CPU does not need to be configured on the codec end. Of course, the power supply, clock, and I/O must be configured on any digital chip. At last, the concept of data interface and control interface cannot be obfuscated. I2S and PCM can only transmit audio data. The registers used to access codec must use I2C and other control interfaces, and the ac97 interface must transmit control and data in a time-based manner. The ADC and DAC in codec interact with the CPU's FIFO data through interfaces such as ac97. The second link is established. To be continued 2
.

PCM in alsa_lib source code. in the c file, snd_pcm_readi (, buffer, size) calls _ snd_pcm_readi (, buffer, size) in the pcm_local.h file; Further calls PCM-> fast_ops-> READI (PCM-> fast_op_arg, buffer, size); according to the file pcm_h1_h. READI = snd_pcm_hw_readi: IOCTL (FD, sndrv_pcm_ioctl_readi_frames,
& Xferi); called. In the kernel, according to the/soc/pcm_native.c file. unlocked_ioctl = snd_pcm_capture_ioctl. snd_pcm_capture_ioctl1 is called. According to the sndrv_pcm_ioctl_readi_frames parameter, snd_pcm_lib_read (substream, xferi. buf, xferi. frames); The snd_pcm_lib_read1 (, snd_pcm_lib_read_transfer) is called. According to the call of transfer, we can see that snd_pcm_lib_read_transfer is called, and then copy_to_user (BUF,
Hwbuf, frames_to_bytes (runtime, frames), we can see that the data in the memory of the DMA end is copied to the memory indicated by a pointer provided by alsa_lib, the ALSA library functions snd_pcm_readi and snd_pcm_writei implement memory-to-memory interaction, or are considered to be memory-to-audio file interaction. So far, the last link has been established. To be continued 3.

 

Perform analysis:

Recording: the mic is sent to codec and converted to a digital signal through the ADC. It is stored in the first-in-first-out (FIFO) of the CPU through the ac97 interface in pending 2, and transferred to the memory through the DMA Interface in pending 1, the snd_pcm_readi interface in alsa_lib to be continued 3 is passed to the recording software and encoded to form an audio file.

Sound Play: The playing software decodes the audio file and transmits it to the DMA-related memory through the snd_pcm_writei interface in pending 3. After the DMA in pending 2, the audio file is transmitted to the FIFO of the CPU, it is then passed to the DAC through the ac97 and Other interfaces, and finally to the speaker connected to the codec.

 

TIPS:

1. ac97 data transmission is complex and time-sharing. The relationship between the first-in-first-out CPU and the ADC/DAC on codec must be well matched. For example, the PCM left FIFO at the CPU end occupies slot3, And the ADC can pass the data to it only when it is configured as slot3. If it is configured as slot6, the data is transmitted to the CPU mic in FIFO. Generally, slot6 is used for recording single channels, and slot3 and slot4 are used for recording dual channels.

2.wav audio file size calculation: to test whether the recording is lost or not, the file size must be calculated. The general method is to use the formula: recording time (unit: s) based on the recording time) X sampling rate X (number of sampling digits/8) X number of channels. For example, if the recording time is 5 seconds, the sampling rate is 8 kHz, the number of digits is 16 bits, and the number of channels is 1, then 5x8000x (16/8) x1 = 80 K. The actual WAV file size is slightly larger than 80 K. There is also a method for calculating the file size: Generally, the audio system uses DMA or DMA interrupt. You can print the count in the DMA interrupt, and the number of xdma interrupt cycle bytes is enough.

3. data Switching size problem: DMA transmission in pending 1 must match the FIFO feature: If the FIFO bit width is 16 bits, the depth is 16, if a request (handshake) is sent to the DMA instance when it is half full, the linked list type DMA must be configured with a Transmission Bit Width of 16 bits, and a burst of 16 bits can ensure that the number of BITs and the number of data are not lost. In the second to be continued, the number of FIFO digits on the CPU end must match the number of ADC/DAC sampling digits on the codec end. The I2S/PCM Interface can be configured with the same value, such as 16 bits. The ac97 interface is a little more complex, the CPU end does not need to be configured. What is the number of sampling digits? If the first-in-first-out channel width is 16 bits, the first-in-first-out channel ADC/DAC bits are 18 bits, and the second-Out channel is 20 bits, the overall sampling position is 16 bits, and the performance of the ADC/DAC is not fully utilized. In Part 3, the third parameter of the snd_pcm_readi and snd_pcm_writei functions indicates the size of the read/write data. The unit is bytes, not bytes. The size of the dual-channel 16-bit format is 4 bytes.

Preface:

In Linux, the data streams of recording and playing must be clearly analyzed in both OSS and ALSA systems. Analyze the Alsa driver layer and associate it with the Alsa library layer and application layer.

 

Link Analysis:

In the core/pcm_native.c file. MMAP = snd_pcm_mmap call snd_pcm_mmap_data (substream, file, area); Further call substream-> OPS-> MMAP (substream, area); according to soc/PXA/pxa3xx-pcm.c file. MMAP = pxa3xx_pcm_mmap. dma_mmap_writecombine (, runtime-> dma_addr,) is called. The pxa3xx_pcm_hw_params () function in the SOC/PXA/pxa3xx-pcm.c file is analyzed in the previous chapter.
= Runtime-> dma_addr; and dma_desc-> dsadr = dma_buff_phys; we can see that runtime-> dma_addr is the address of the DMA memory, which is passed in by the Alsa library layer. Also according to dma_desc-> dtadr = prtd-> Params-> dev_addr and SOC/PXA/pxa3xx-ac97.c files. dev_addr = _ preg (pcdr). We can see that the address of the DMA peripheral is the FIFO read/write register pcdr in the ac97 controller. So far, the first link has been established: FIFO interacts with memory through DMA. To be continued 1
.

The ac97 interface or I2S or PCM Interface can connect the CPU and CODEC (wm9714/alc5620/alc5621), and configure the format: the PCM Interface must be configured with the sampling rate, number of sampling digits, number of channels, and transmission format. The I2S interface must be configured with the sampling rate, number of sampling digits, number of channels, and alignment mode. The ac97 interface is flexible, it can be considered that the CPU does not need to be configured on the codec end. Of course, the power supply, clock, and I/O must be configured on any digital chip. At last, the concept of data interface and control interface cannot be obfuscated. I2S and PCM can only transmit audio data. The registers used to access codec must use I2C and other control interfaces, and the ac97 interface must transmit control and data in a time-based manner. The ADC and DAC in codec interact with the CPU's FIFO data through interfaces such as ac97. The second link is established. To be continued 2
.

PCM in alsa_lib source code. in the c file, snd_pcm_readi (, buffer, size) calls _ snd_pcm_readi (, buffer, size) in the pcm_local.h file; Further calls PCM-> fast_ops-> READI (PCM-> fast_op_arg, buffer, size); according to the file pcm_h1_h. READI = snd_pcm_hw_readi: IOCTL (FD, sndrv_pcm_ioctl_readi_frames,
& Xferi); called. In the kernel, according to the/soc/pcm_native.c file. unlocked_ioctl = snd_pcm_capture_ioctl. snd_pcm_capture_ioctl1 is called. According to the sndrv_pcm_ioctl_readi_frames parameter, snd_pcm_lib_read (substream, xferi. buf, xferi. frames); The snd_pcm_lib_read1 (, snd_pcm_lib_read_transfer) is called. According to the call of transfer, we can see that snd_pcm_lib_read_transfer is called, and then copy_to_user (BUF,
Hwbuf, frames_to_bytes (runtime, frames), we can see that the data in the memory of the DMA end is copied to the memory indicated by a pointer provided by alsa_lib, the ALSA library functions snd_pcm_readi and snd_pcm_writei implement memory-to-memory interaction, or are considered to be memory-to-audio file interaction. So far, the last link has been established. To be continued 3.

 

Perform analysis:

Recording: the mic is sent to codec and converted to a digital signal through the ADC. It is stored in the first-in-first-out (FIFO) of the CPU through the ac97 interface in pending 2, and transferred to the memory through the DMA Interface in pending 1, the snd_pcm_readi interface in alsa_lib to be continued 3 is passed to the recording software and encoded to form an audio file.

Sound Play: The playing software decodes the audio file and transmits it to the DMA-related memory through the snd_pcm_writei interface in pending 3. After the DMA in pending 2, the audio file is transmitted to the FIFO of the CPU, it is then passed to the DAC through the ac97 and Other interfaces, and finally to the speaker connected to the codec.

 

TIPS:

1. ac97 data transmission is complex and time-sharing. The relationship between the first-in-first-out CPU and the ADC/DAC on codec must be well matched. For example, the PCM left FIFO at the CPU end occupies slot3, And the ADC can pass the data to it only when it is configured as slot3. If it is configured as slot6, the data is transmitted to the CPU mic in FIFO. Generally, slot6 is used for recording single channels, and slot3 and slot4 are used for recording dual channels.

2.wav audio file size calculation: to test whether the recording is lost or not, the file size must be calculated. The general method is to use the formula: recording time (unit: s) based on the recording time) X sampling rate X (number of sampling digits/8) X number of channels. For example, if the recording time is 5 seconds, the sampling rate is 8 kHz, the number of digits is 16 bits, and the number of channels is 1, then 5x8000x (16/8) x1 = 80 K. The actual WAV file size is slightly larger than 80 K. There is also a method for calculating the file size: Generally, the audio system uses DMA or DMA interrupt. You can print the count in the DMA interrupt, and the number of xdma interrupt cycle bytes is enough.

3. data Switching size problem: DMA transmission in pending 1 must match the FIFO feature: If the FIFO bit width is 16 bits, the depth is 16, if a request (handshake) is sent to the DMA instance when it is half full, the linked list type DMA must be configured with a Transmission Bit Width of 16 bits, and a burst of 16 bits can ensure that the number of BITs and the number of data are not lost. In the second to be continued, the number of FIFO digits on the CPU end must match the number of ADC/DAC sampling digits on the codec end. The I2S/PCM Interface can be configured with the same value, such as 16 bits. The ac97 interface is a little more complex, the CPU end does not need to be configured. What is the number of sampling digits? If the first-in-first-out channel width is 16 bits, the first-in-first-out channel ADC/DAC bits are 18 bits, and the second-Out channel is 20 bits, the overall sampling position is 16 bits, and the performance of the ADC/DAC is not fully utilized. In Part 3, the third parameter of the snd_pcm_readi and snd_pcm_writei functions indicates the size of the read/write data. The unit is bytes, not bytes. The size of the dual-channel 16-bit format is 4 bytes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.