ALSA捕获示例中的分段错误

Segmentation fault in ALSA capturing example

我正在学习使用 ALSA 从麦克风读取数据。我找到了可以执行此操作的示例程序,但在使用 运行 时遇到了问题。程序编译正确,但是当我想 运行 它时,出现分段错误。这是我的代码:

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

这是输出:

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

看来下一行有问题:

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

有人可以帮我解决这个问题吗?我是初学者,我在编码时从未遇到过此类错误。

编辑: 按照建议,我在编译期间启用了警告,它们是:

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

所以看起来 snd_pcm_hw_params_set_rate_near 的第三个参数应该是一个指针,编译器将整数转换为指针。但是我还是不知道该怎么办。

snd_pcm_hw_params_set_rate_near的第三个参数是in-out。您将其设置为所需的值,然后 return 它具有实际设置的值。所以,

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