什么是alsa库读取值的含义?

时间:2013-05-02 14:47:14

标签: c++ alsa

我正在使用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");
          }

1 个答案:

答案 0 :(得分:0)

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

40239717不适合16位,因此您显然使用了错误的数据类型来读取buffer。 确保buffer的类型为int16_tsigned short int