ALSA microphone for audio collection

Source: Internet
Author: User

ALSA is a Linux advanced audio interface. ALSA provides a set of standard access mechanisms for Linux audio developers to easily develop audio devices. Believe it? Next we will use it to compile a simple recording/broadcasting program, but this requires you to have a certain degree of computer language basics.

A typical audio program should have the following structure:

Enable the audio device

Set read/write parameters for devices

Read/write audio data to an audio device

Disable the device

The ALSA Library provides a variety of interfaces for us to implement these operations.

First, let's encapsulate a function to enable the audio device:

1.Snd_pcm_t* Pcm_handle;

2.

3.Bool Device_open (Int Mode ){

4.If(Snd_pcm_open(& Pcm_handle,"Default",Mode,0)<0)

5.ReturnFalse;

6.ReturnTrue;

7.}

Snd_pcm_open is the function provided by the Alsa library to enable the device. Here we specify to enable the default audio device and set the device to the recording or playback status based on the mode parameter. If the device is successfully enabled, pcm_handle points to the device handle, which is saved as a global variable for future use.

The second step is to set parameters. Improper parameter settings will cause the audio device to fail to work properly. Before setting parameters, we need to understand the meaning of each parameter and some basic concepts.

Sample: a sample is the most basic unit for recording audio data. It is usually 8-bit or 16-bit.

Channel Number (Channel): this parameter is 1, indicating a single channel, and 2 is a stereo.

Attention (FRAME): Attention records a sound unit. Its length is the product of the sample length and the number of channels.

Sampling Rate: the number of samples per second.

Period (period): the number of records required for one processing of an audio device. The unit for data access and storage of audio data is this.

Interleaved is a recording method of audio data. In this mode, data is stored in the continuous seek mode, that is, first record the Left and Right audio samples of limit 1 (assumed in stereo format), and then start record of limit 2. In non-staggered mode, the first record is the left-channel samples of all the channels in a cycle, and then the right-channel samples. The data is stored in a continuous channel. However, in most cases, we only need to use the staggered mode.

After understanding the meaning and relationship of each parameter, we can set the parameters:

1.Int Bit_per_sample;// Sample Length (BIT)

2.Int Period_size;// Cycle length (number of workers)

3.Int Chunk_byte;// Cycle length (bytes)

4.Snd_pcm_hw_params_t* Params;// Define parameter variables

5.

6.Bool Device_setparams ()

7.{

8.Snd_pcm_hw_params_t* Hw_params;

9.Snd_pcm_hw_params_malloc(& Hw_params );// Allocate space for parameter variables

10. snd_pcm_hw_params_malloc(& Params );

11. snd_pcm_hw_params_any(Pcm_handle,Hw_params);// Parameter initialization

12. snd_pcm_hw_params_set_access(Pcm_handle,Hw_params,

13.Snd_pcm_access_rw_interleaved );// Set to staggered Mode

14. snd_pcm_hw_params_set_format (Pcm_handle,Hw_params,

15.Snd_format_s16_le );// Use a 16-digit sample

16. snd_pcm_hw_params_set_rate_near (Pcm_handle,Hw_params,

17.44100,0 );// Set the sampling rate to 44.1 kHz.

18. snd_pcm_hw_params_set_channels (Pcm_handle,Hw_params,2 );// Set to stereo

19. snd_pcm_hw_params_get_period_size (Hw_params,& Period_size );// Obtain the cycle length

20. bit_per_sample=Snd_pcm_hw_format_physical_width (Hw_params.format);

21.// Obtain the Sample Length

22. chunk_byte=Period_size*Bit_per_sample*Hw_params.channels/8;

23. // length of the computing cycle (bytes)=Number of workers per cycle*Sample Length (BIT)*Number of channels/8)

24. snd_pcm_hw_params (Pcm_handle,Hw_params );// Set parameters

25. Params=Hw_params;// Save the parameters for future use

26. snd_pcm_hw_params_free (Hw_params );// Release the parameter variable space

27. ReturnTrue;

28.

29 .}

Here we first use a series of snd_pcm_hw_params_set _ functions provided by ALSA to assign values to parameter variables. Finally, the parameters are passed to the device through snd_pcm_hw_params. It must be noted that the parameter settings must be processed in the formal development process, which is only used as an example program and not considered.

After setting the parameters, you can start recording. The recording process is actually to read and save data from the audio device.

1.Char * Wave_buf;

2.Int Wave_buf_len;

3.Bool Device_capture (Int Dtime){

4.Wave_buf_len=Dtime*Params. Rate*Bit_per_sample*Params. Channels/8;

5.// Calculate the length of audio data (in seconds)*Sampling Rate*Bytes length)

6.  Char * Data=Wave_buf=(Char*) Malloc (Wave_buf_len);// Allocate space

7.

8.Int R=0;

9.While(Data? CWave_buf<=Wave_buf_len? CChunk_size){

10. r=Snd_pcm_readi (Pcm_handle,Data,Chunk_size );

11. If(R> 0)Data+ =R*Chunk_byte;

12. Else

13. ReturnFalse

14 .}

15. ReturnTrue;

16 .}

The dtime parameter is used to determine the recording time, allocate data space according to the recording time, and then call snd_pcm_readi to read audio data from the audio device and store it in wave_buf.

In the same way, we add another playing function to write data to the audio device:

1.Bool Device_play (){

2.Char * Data=Wave_buf;

3.Int R=0;

4.While(Data? CWave_buf<=Wave_buf_len? CChunk_size){

5.R=Snd_pcm_writei (Pcm_handle,Data,Chunk_size );

6.If(R> 0)Data+ =R*Chunk_byte;

7.Else

8.ReturnFalse

9.}

10. ReturnTrue;

11 .}

Finally, we add the main function to this example program.

1.# Include<ALSA/asoundlib. h>

2.

3.Bool Device_open (Int Mode );

4.Bool Device_setparams ();

5.Bool Device_capture (Int Dtime);

6.Bool Device_play ();

7.Char * Wave_buf;

8.Int Wave_buf_len;

9.Int Bit_per_sample;

10.Int Period_size;

11.Int Chunk_byte;

12.Int Chunk_size;

13. snd_pcm_hw_params_t* Params;

14.

15.Int Main (Int ,Char**){

16. // recording

17. If(! Device_open (snd_pcm_stream_capture)Return1;

18. If(! Device_setparams ())Return2;

19. If(! Device_capture (3))Return3;// Recording for 3 seconds

20. snd_pcm_close (Pcm_handle);

21.

22. // play

23. If(! Device_open (Snd_pcm_stream_playback)Return4;

24. If(! Device_setparams ())Return5;

25. If(! Device_play ())Return6;

26. snd_pcm_close (Pcm_handle);

27.

28. Return0;

29 .}

In this way, we have completed an audio program with the recording and broadcasting functions, because the Alsa library is used. If you are using the GCC compiler, remember to include the parameter "lasound" at the end of the link.

Limited by space, the Alsa interface provides more powerful functions than this. If you are interested, you can refer to ALSA howto, and you will be able to discover the power of ALSA.

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.