如何通过asoundlib监控ALSA上的播放?

时间:2014-03-28 06:38:19

标签: linux audio alsa sampling

我正在构建一个允许ALSA配置的应用程序,并且在GUI中有一个窥视表,允许客户端实时查看回放级别。我很难确定要连接的设备,因为我不知道ALSA是否有默认的“环回”以及它的名称。我也无法将读取数据转换为样本,然后找到所述样本的幅度。这是我到目前为止所建立的: 抓取设备并设置硬件参数

if (0 == snd_pcm_open(&pPcm, "default", SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK))
{
    if (0 == snd_pcm_set_params(pPcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 96000, 1, 1)) // This last argument confuses me because I'm not given a unit of measurement (second, millisecond, mircosecond, etc.)
    {
        return snd_pcm_start(pPcm);
    }
}

pPcm = nullptr;
return -1;

从设备读取并返回音频信号的视频

int iRtn = -1;
if (nullptr == pPcm)
{
    if (-1 == SetupListener())
    {
        return iRtn;
    }
}

// Check to make the state is sane for reading.
if (SND_PCM_STATE_PREPARED == snd_pcm_state(pPcm) ||
        SND_PCM_STATE_RUNNING == snd_pcm_state(pPcm))
{
    snd_pcm_resume(pPcm); // This might be superfluous.

    // The state is sane, read from the stream.
    signed short iBuffer = 0;
    int iNumRead = snd_pcm_readi(pPcm, &iBuffer, 1);

    if (0 < iNumRead)
    {
        // This calculates an approximation.
        // We have some audio data, acquire it's peek in dB. (decibels)
        float nSample = static_cast<float>(iBuffer);
        float nAmplitude = nSample / MAX_AMPLITUDE_S16; // MAX_AMPLITUDE_S16 is defined as "32767"
        float nDecibels = (0 < nAmplitude) ? 20 * log10(nAmplitude) : 0;
        iRtn = static_cast<int>(nDecibels); // Cast to integer for GUI element.
    }
}

return iRtn;

ALSA文档似乎非常贫瘠,所以如果我滥用API,我会道歉。

0 个答案:

没有答案