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

Linux 如何通过asoundlib监控ALSA上的播放?,linux,audio,alsa,sampling,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_s

我正在构建一个允许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;
intirtn=-1;
如果(nullptr==pPcm)
{
如果(-1==SetupListener())
{
返回iRtn;
}
}
//检查以确保读取状态正常。
如果(SND\U PCM\U状态准备==SND\U PCM\U状态(pPcm)||
SND_PCM_状态_运行==SND_PCM_状态(pPcm))
{
snd_pcm_resume(pPcm);//这可能是多余的。
//状态是正常的,从流中读取。
符号短iBuffer=0;
int iNumRead=snd_pcm_readi(pPcm和iBuffer,1);
如果(0

ALSA文档似乎非常贫乏,因此,如果我误用了API,我深表歉意。

在查看PCM接口文档时,我看到了一个名为“Scope Plugin”的东西,这是怎么回事?这就是我想要用来完成我想做的事情吗?你能控制应用程序播放声音吗?我不知道你的意思。我正在尝试从“主”频道播放并监视它。我不认为我需要控制播放应用程序,因为我正在尝试让显示器播放所有声音。如果你的硬件不支持监控,你必须在播放路径中插入监控插件。我如何检查它是否支持?如果没有,我将如何插入/删除该插件?