OK. I only want to read on the input of soundcard. So I wrote an other program easier than the first, I write the data in file, but it seems that it doesn't work...
#include <string.h> #include <stdio.h> #include <stdlib.h> #include "alsa/asoundlib.h" #include <errno.h>
unsigned int rate = 44100; unsigned int nb_channels = 2;
int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *hw_params) { int err; int dir=0; if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) { fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err)); exit (1); }
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) { fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror (err)); exit (1); } printf("Initialisation des paramètres\n");
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err)); exit (1); } printf("Access card configuration\n");
if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) { fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err)); exit (1); }
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &rate, &dir)) < 0) { fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, nb_chann)) < 0) { fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err)); exit (1); }
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) { fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err)); exit (1); } printf("Parameters recorded\n");
//snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (handle)) < 0) { fprintf (stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror (err)); exit (1); } return 0; }
int main(int argc, char *argv[]){ int err; int j; printf("Beginning \n"); snd_pcm_hw_params_t *hw_params_capture, *hw_params_playback; short buf[256]; FILE *file;
fichier = fopen( "/root/Desktop/Test/Audio/Capture.001", "w"); if (fichier == NULL) printf("cannot create file : %i\n", errno); else printf("file created\n");
/*Open soundcard as capture*/ snd_pcm_t *capture_handle;
if ((err = snd_pcm_open (&capture_handle, "hw:0,0", SND_PCM_STREAM_CAPTURE, 0)) < 0) { fprintf (stderr, "cannot open audio device %s (%s)\n", argv[1], snd_strerror (err)); exit (1); }
set_hwparams(capture_handle, hw_params_capture);
for (j = 0 ; j < 256 ; j++) { if ((err = snd_pcm_readi (capture_handle, buf, 128)) < 0) printf("cherche pas\n"); putc(buf[j], file); //write in file LEFT Channel putc(buf[j+1], file); //write in file RIGHT Channel putc(0x0A, file); }
printf("Close File\n"); fclose(file); snd_pcm_close (capture_handle); return 0 ; }
What do you think about this program... ? I searched about an How-to or some help for capture on a soundcard with ALSA, but I didn't found
Thank you