代码功能:采集441帧然后播放出来
详细功能参考代码:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <alsa/asoundlib.h>
4
5
6 int main(void){
7 /*Name of the PCM device ,like "default"*/
8 char *dev_name;
9 int rate = 44100;/*Sample rate*/
10 int exact_rate;/*Sample rate returned by*/
11 int dir;/*(1)exact_rate == rate --> dir=0,(2)exact_rate < rate --> dir=-1,(3)exact_rate > rate*/
12 int periods = 3;/*Number of periods*/
13 int err;
14 int size;
15 int pcmreturn;
16 // snd_pcm_uframes_t periodsize = 8192;
17 snd_pcm_uframes_t periodsize = 441;
18 snd_pcm_t *play_handle;
19 snd_pcm_t *capture_handle;
20 snd_pcm_stream_t play_stream = SND_PCM_STREAM_PLAYBACK;
21 snd_pcm_stream_t capture_stream = SND_PCM_STREAM_CAPTURE;
22 /*This structure contains information about */
23 /*the hardware and can be used to specify the */
24 /*configuration to be used for the PCM stream*/
25 snd_pcm_hw_params_t *hwparams;
26 snd_pcm_hw_params_t *c_hwparams;
27 /*Init dev_name, Of course, later you will make this configure*/
28 dev_name = strdup("default");
29 /*Allocate the snd_pcm_hw_params_t structure on the stack*/
30 snd_pcm_hw_params_alloca(&hwparams);
31 snd_pcm_hw_params_alloca(&c_hwparams);
32 /* Open PCM. The last parameter of this function is the mode. */
33 /* If this is set to 0, the standard mode is used. Possible */
34 /* other values are SND_PCM_NONBLOCK and SND_PCM_ASYNC. */
35 /* If SND_PCM_NONBLOCK is used, read / write access to the */
36 /* PCM device will return immediately. If SND_PCM_ASYNC is */
37 /* specified, SIGIO will be emitted whenever a period has */
38 /* been completely processed by the soundcard. */