Code:
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
int main(int argc, char **argv) //argv[1] = "hw:0,0"
int i;
int err;
long buf[128];
int exact_rate = 44100;
int time_s = 3;
static unsigned int channels = 2;
snd_pcm_t *playback_handle;
snd_pcm_hw_params_t *hw_params;
char *pcm_name = strdup("plughw:0,0");
snd_pcm_t *capture_handle;
snd_pcm_open (&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0);
snd_pcm_hw_params_malloc (&hw_params);
snd_pcm_hw_params_any (capture_handle, hw_params);
snd_pcm_hw_params_set_access(capture_handle,hw_params,SND_PCM_ACCESS_RW_INTERLEAVED));
nd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &exact_rate, 0);
snd_pcm_hw_params_set_channels (capture_handle, hw_params, channels);
snd_pcm_hw_params (capture_handle, hw_params);
snd_pcm_hw_params_free (hw_params);
snd_pcm_prepare (capture_handle);
int fd = open("file_rec", O_WRONLY), b;
if(!fd)
printf("file didn't open\n");
for (i = 0; i < time_s*44100/128; i++)
{
b = snd_pcm_readi(capture_handle, buf, 128);
write(fd,buf,128);
}
close(fd);
snd_pcm_close (capture_handle);
snd_pcm_open(&playback_handle,pcm_name,SND_PCM_STREAM_PLAYBACK, 0);
snd_pcm_hw_params_malloc (&hw_params);
snd_pcm_hw_params_any (playback_handle, hw_params);
snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_rate_near(playback_handle, hw_params, &exact_rate,0);
snd_pcm_hw_params_set_channels (playback_handle, hw_params, 2);
snd_pcm_hw_params (playback_handle, hw_params);
snd_pcm_hw_params_free (hw_params);
snd_pcm_prepare (playback_handle);
fd = open("file_rec", O_RDONLY);
while((b=read(fd, buf, 128)) > 0)
snd_pcm_writei (playback_handle, buf, b);
snd_pcm_close (playback_handle);
close(fd);
exit (0);
}
i'm recording a stream in standard audiorecording programm(from mic) and ones in my programm, and get in my programm sound much dirty with a lot of noises. can you help, what can i do??