ALSA捕获示例中的分段错误

时间:2018-02-02 18:07:15

标签: c audio alsa recording fault

我正在学习使用ALSA从麦克风中读取数据。我找到了执行它的示例程序,并且在运行它时遇到了问题。程序编译正确,但是当我想运行它时,我得到Segmentation fault错误。这是我的代码:

#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:在函数'main'中:

     

main.c:48:3:警告:传递'snd_pcm_hw_params_set_rate_near'的参数3

     

从整数生成指针而不使用强制转换[默认启用]

     

if((err = snd_pcm_hw_params_set_rate_near(capture_handle,hw_params,&gt; 44100,0))&lt; 0){

     

在/usr/include/alsa/asoundlib.h:54:0中包含的文件中,                   来自main.c:3:   /usr/include/alsa/pcm.h:747:5:注意:预期'unsigned int *'但参数&gt;的类型为'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的第三个参数应该是指针,编译器将整数转换为指针。但我仍然不确定该怎么做。

1 个答案:

答案 0 :(得分:0)

function getProduct(ID_product) { //AJAX request to get a product data from the server $.ajax({ type: "POST", url: "product.php", dataType: "json", data: { id_product: ID_product // the id of the single product }, success: function(data){ // data is a json object console.log(data);//don't convert it (it's already an object) } }); 的第三个参数是snd_pcm_hw_params_set_rate_near。您使用所需的值设置它,并在返回时具有实际设置的值。所以,

in-out