kkalwa
kkalwa

Reputation: 95

Segmentation fault in ALSA capturing example

I am learning to use ALSA to read data from microphone. I found example program to do it and have problems with running it. Program compiles correctly, but when I want to run it, I get Segmentation fault error. Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>

static char *urzadzenie = "default";

main (int argc, char *argv[])
{
    int i;
    int err;
    short buf[128];
    snd_pcm_t *capture_handle;
    snd_pcm_hw_params_t *hw_params;
    fprintf(stderr, "Poczatek programu\n");
    if ((err = snd_pcm_open (&capture_handle, urzadzenie, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
        fprintf (stderr, "cannot open audio device %s (%s)\n", 
             urzadzenie,
             snd_strerror (err));
        exit (1);
    }
       fprintf(stderr, "Otwarto");
    if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
        fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    fprintf(stderr, "Zrobiono jakis malloc\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);
    }
        fprintf(stderr, "Inicjalizowano parametry\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);
    }
    fprintf(stderr, "Ustalono typ\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);
    }
    fprintf(stderr, "Ustalono probkowanie - format\n");
    if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, 44100, 0)) < 0) {
        fprintf (stderr, "cannot set sample rate (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    fprintf(stderr, "Ustalono probkowanie - czas\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);
    }
    fprintf(stderr, "Ustalono liczbe kanalow\n");
    if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
        fprintf (stderr, "cannot set parameters (%s)\n",
             snd_strerror (err));
        exit (1);
    }
    fprintf(stderr, "Ustalono parametry\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);
    }
    fprintf(stderr, "Przygotowano interfejs audio\n");
    for (i = 0; i < 10; ++i) {
        if ((err = snd_pcm_readi (capture_handle, buf, 128)) != 128) {
            fprintf (stderr, "read from audio interface failed (%s)\n",
                 snd_strerror (err));
            exit (1);
        }
    fprintf(stderr, buf);
    }

    snd_pcm_close (capture_handle);
    exit (0);
}

And here is the output:

osboxes@osboxes:~/z$ ./wynik
Poczatek programu
OtwartoZrobiono jakis malloc
Inicjalizowano parametry
Ustalono typ
Ustalono probkowanie - format
Segmentation fault (core dumped)

It looks like the following line is a problem:

if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, 44100, 0)) < 0)

Can someone please help me with this problem? I am a beginner and I never had these kind of errors while coding.

EDIT: Following advice I enabled warnings during compile and here they are:

main.c: In function ‘main’:

main.c:48:3: warning: passing argument 3 of ‘snd_pcm_hw_params_set_rate_near’

makes pointer from integer without a cast [enabled by default]

if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, >44100, 0)) < 0) {

In file included from /usr/include/alsa/asoundlib.h:54:0, from main.c:3: /usr/include/alsa/pcm.h:747:5: note: expected ‘unsigned int *’ but argument >is of type ‘int’

int snd_pcm_hw_params_set_rate_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);

So it looks like third argument of snd_pcm_hw_params_set_rate_near should be a pointer and compiler converts integer to a pointer. But I am still not sure what to do.

Upvotes: 2

Views: 1642

Answers (1)

user58697
user58697

Reputation: 7923

The third parameter of snd_pcm_hw_params_set_rate_near is in-out. You set it with the desired value, and upon return it has the value actually set. So,

    unsigned int rate = 41000;
    if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, 0)) < 0) {
        handle_error;
    } else {
        printf("The rate set is %u\n", rate);
    }

Upvotes: 2

Related Questions