Faire un don ! | | style | statistiques | contactez-nous | plan | lettre d'information

Programmation.c : incompréhensible - utilisation d'ALSA

Posté par berti () le 27 mars 2006
Bonjour,



je tente d'écrire un soft devant lire/écrire à partir de la carte son en utilisant ALSA. Je pars du tutoriel http://www.equalarea.com/paul/alsa-audio.html



Mon code qui pose problème est :



#include "string.h";

#include "stdio.h";

#include "stdlib.h";

#include "alsa/asoundlib.h";



int main(int argc, char *argv[]){

printf("debut\n");

snd_pcm_hw_params_t *hw_params;

int i;

int err;

int dir=0;

int rate=44100;



short buf[128];





/*ON LIT*/

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);

}

printf("1\n");



if ((err = snd_pcm_hw_params_malloc (&hw_params)) \< 0) {

fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",

snd_strerror (err));

exit (1);

}

printf("2\n");



if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {

fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",

snd_strerror (err));

exit (1);

}

printf("3\n");



if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {

fprintf (stderr, "cannot set access type (%s)\n",

snd_strerror (err));

exit (1);

}

printf("4\n");



if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {

fprintf (stderr, "cannot set sample format (%s)\n",

snd_strerror (err));

exit (1);

}

printf("5\n");



if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, &dir)) < 0) {

fprintf (stderr, "cannot set sample rate (%s)\n",

snd_strerror (err));

exit (1);

}

printf("6\n");



if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) {

fprintf (stderr, "cannot set channel count (%s)\n",

snd_strerror (err));

exit (1);

}

printf("7\n");



if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {

fprintf (stderr, "cannot set parameters (%s)\n",

snd_strerror (err));

exit (1);

}

printf("8\n");



snd_pcm_hw_params_free (hw_params);



if ((err = snd_pcm_prepare (capture_handle)) < 0) {

fprintf (stderr, "cannot prepare audio interface for use (%s)\n",

snd_strerror (err));

exit (1);

}

printf("9\n");



printf("10\n");



int j;

for (j = 0; j < 10; ++j) {

printf("j=%i\n",j);

printf("j=%i\n",j);

snd_pcm_readi (capture_handle, buf, 128);

printf("j=%i\n",j);

}



snd_pcm_close (capture_handle);







return 0 ;

}









à compiler avec

gcc -lasound test.c



Cela marche chez moi sur ma machine i386, mais sur mon amd64 j'ai le résutat suivant :

berti@neptune:~/tmp/prob$ ./a.out

debut

1

2

3

4

5

6

7

8

9

10

j=0

j=0

j=23003252

Segmentation fault




Ce que je n'arrive pas du tout à comprendre c'est pourquoi la valeur de "j" change dès que je fais un appel à la fonction snd_pcm_readi (capture_handle, buf, 128);



De plus si j'enlève la boucle et je copie la fonction snd_pcm_readi 10 fois, là il n'y plus de problème de segfault...



Quelqu'un a une idée ?

> Lire le message (6 commentaires, moyenne: 2,2).  

Vous avez demandé le commentaire #696260.

format printf ?

Posté par Barnabé () le 28/03/2006 à 04:25. (lien). Évalué à 2.

essaie
printf("j=%n\n",j);
au lieu de
printf("j=%i\n",j);

  • [^]Re: format printf ?

    Posté par Barnabé () le 28/03/2006 à 05:35. (lien). Évalué à 1.

    C'est la dernière fois que je poste à 6h25.
    Oubliez le message précédent, j'ai certainement confondu un truc avec un autre, mais je ne sais même plus quoi....