0

我正在使用 alsa 库读取声音值并返回一些像 40239717 这样的值。但我不明白意味着什么。如何转换这个值的范式。

我的阅读代码是这样的:

if ((err = snd_pcm_open (&capture_handle, "default", SND_PCM_STREAM_CAPTURE, 0)) < 0) {
            qDebug("cannot open audio device default\n");
            exit (1);
    }

    if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
            qDebug ("cannot allocate hardware parameter structure\n");
            exit (1);
    }

    if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
            qDebug("cannot initialize hardware parameter structure\n");
            exit (1);
    }

    if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
            qDebug ("cannot set access type \n");
            exit (1);
    }

    if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
            qDebug ("cannot set sample format\n");
            exit (1);
    }

    if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &sample_rate, 0)) < 0) {
            qDebug ("cannot set sample rate\n");
            exit (1);
    }

    if ((err = snd_pcm_hw_params_set_period_size_near (capture_handle, hw_params, &frame, 0)) < 0) {
            qDebug ("cannot set sample rate\n");
            exit (1);
    }
    if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) {
            qDebug ( "cannot set channel count\n");
            exit (1);
    }

    if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
            qDebug ("cannot set parameters\n");
            exit (1);
    }
    if ((err = snd_pcm_prepare (capture_handle)) < 0) {
            qDebug ("cannot prepare audio interface for use\n");
            exit (1);
    }
    if ((err = snd_pcm_readi(capture_handle,buffer,frame)) != frame) {
                      qDebug("read from audio interface failed\n");
          }
4

1 回答 1

0

您正在为 16 位样本配置设备 ( SND_PCM_FORMAT_S16_LE)。

该值40239717不适合 16 位,因此您显然使用了错误的数据类型来读取buffer. 确保buffer具有int16_t或类型signed short int

于 2013-05-02T19:36:52.177 回答