URL: https://linuxfr.org/forums/programmation-c--2/posts/incompr%C3%A9hensible-utilisation-dalsa Title: incompréhensible - utilisation d'ALSA Authors: berti Date: 2006年03月27日T23:54:16+02:00 Tags: Score: 0 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](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[]){ £spaces£ £/spaces£printf("debut\n"); £spaces£ £/spaces£snd_pcm_hw_params_t *hw_params; £spaces£ £/spaces£int i; £spaces£ £/spaces£int err; £spaces£ £/spaces£int dir=0; £spaces£ £/spaces£int rate=44100; £spaces£ £/spaces£short buf[128]; £spaces£ £/spaces£/*ON LIT*/ £spaces£ £/spaces£snd_pcm_t *capture_handle; £spaces£ £/spaces£ £spaces£ £/spaces£if ((err = snd_pcm_open (&capture_handle, "hw:0,0", SND_PCM_STREAM_CAPTURE, 0)) < 0) { £spaces£ £/spaces£fprintf (stderr, "cannot open audio device %s (%s)\n", £spaces£ £/spaces£argv[1], £spaces£ £/spaces£snd_strerror (err)); £spaces£ £/spaces£exit (1); £spaces£ £/spaces£} £spaces£ £/spaces£printf("1\n"); £spaces£ £/spaces£ £spaces£ £/spaces£if ((err = snd_pcm_hw_params_malloc (&hw_params)) \< 0) { £spaces£ £/spaces£fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", £spaces£ £/spaces£snd_strerror (err)); £spaces£ £/spaces£exit (1); £spaces£ £/spaces£} £spaces£ £/spaces£printf("2\n"); £spaces£ £/spaces£ £spaces£ £/spaces£if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) { £spaces£ £/spaces£fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", £spaces£ £/spaces£snd_strerror (err)); £spaces£ £/spaces£exit (1); £spaces£ £/spaces£} £spaces£ £/spaces£printf("3\n"); £spaces£ £/spaces£ £spaces£ £/spaces£if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { £spaces£ £/spaces£fprintf (stderr, "cannot set access type (%s)\n", £spaces£ £/spaces£snd_strerror (err)); £spaces£ £/spaces£exit (1); £spaces£ £/spaces£} £spaces£ £/spaces£printf("4\n"); £spaces£ £/spaces£if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) { £spaces£ £/spaces£fprintf (stderr, "cannot set sample format (%s)\n", £spaces£ £/spaces£snd_strerror (err)); £spaces£ £/spaces£exit (1); £spaces£ £/spaces£} £spaces£ £/spaces£printf("5\n"); £spaces£ £/spaces£ £spaces£ £/spaces£if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, &dir)) < 0) { £spaces£ £/spaces£fprintf (stderr, "cannot set sample rate (%s)\n", £spaces£ £/spaces£snd_strerror (err)); £spaces£ £/spaces£exit (1); £spaces£ £/spaces£} £spaces£ £/spaces£printf("6\n"); £spaces£ £/spaces£ £spaces£ £/spaces£if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) { £spaces£ £/spaces£fprintf (stderr, "cannot set channel count (%s)\n", £spaces£ £/spaces£snd_strerror (err)); £spaces£ £/spaces£exit (1); £spaces£ £/spaces£} £spaces£ £/spaces£printf("7\n"); £spaces£ £/spaces£ £spaces£ £/spaces£if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) { £spaces£ £/spaces£fprintf (stderr, "cannot set parameters (%s)\n", £spaces£ £/spaces£snd_strerror (err)); £spaces£ £/spaces£exit (1); £spaces£ £/spaces£} £spaces£ £/spaces£printf("8\n"); £spaces£ £/spaces£ £spaces£ £/spaces£snd_pcm_hw_params_free (hw_params); £spaces£ £/spaces£ £spaces£ £/spaces£if ((err = snd_pcm_prepare (capture_handle)) < 0) { £spaces£ £/spaces£fprintf (stderr, "cannot prepare audio interface for use (%s)\n", £spaces£ £/spaces£snd_strerror (err)); £spaces£ £/spaces£exit (1); £spaces£ £/spaces£} £spaces£ £/spaces£printf("9\n"); £spaces£ £/spaces£printf("10\n"); £spaces£ £/spaces£int j; £spaces£ £/spaces£for (j = 0; j < 10; ++j) { £spaces£ £/spaces£printf("j=%i\n",j); £spaces£ £/spaces£printf("j=%i\n",j); £spaces£ £/spaces£snd_pcm_readi (capture_handle, buf, 128); £spaces£ £/spaces£printf("j=%i\n",j); £spaces£ £/spaces£} £spaces£ £/spaces£ £spaces£ £/spaces£snd_pcm_close (capture_handle); £spaces£ £/spaces£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 ?

AltStyle によって変換されたページ (->オリジナル) /