0

如何显示AudioMeterInformation?

我需要显示默认捕获设备和输出设备的计量,类似于在 Windows 7 中打开 Windows 播放和录制设备对话框时。

有一个可用的开源 API,它是名为 naudio 的 Windows 核心音频的包装器。提供的示例是使用 WaveIn 设备收集声音样本,然后生成计量信息

WaveIn = new WaveIn { DeviceNumber = 0 };
WaveIn.DataAvailable += OnDataAvailable;
WaveIn.RecordingStopped += OnRecordingStopped;
WaveIn.WaveFormat = new WaveFormat(44100, 1);

public virtual void OnDataAvailable(object sender, WaveInEventArgs e)
{
    byte[] buffer = e.Buffer;
    for (int index = 0; index < e.BytesRecorded; index += 2)
    {
        short sample = (short)((buffer[index + 1] << 8) | buffer[index + 0]);
        float sample32 = sample / 32768f;
        SampleAggregator.Add(sample32);
    }
}

public class SampleAggregator
{
    // volume
    private int Count { get; set; }

    public float MaxValue { get; set; }
    public float MinValue { get; set; }
    public int NotificationCount { get; set; }
    public event EventHandler<MaxSampleEventArgs> MaximumCalculated;
    public event EventHandler Restart = delegate { };

    public void RaiseRestart()
    {
        Restart(this, EventArgs.Empty);
    }

    private void Reset()
    {
        Count = 0;
        MaxValue = MinValue = 0;
    }

    public void Add(float value)
    {
        MaxValue = Math.Max(MaxValue, value);
        MinValue = Math.Min(MinValue, value);

        Count++;

        if (Count >= NotificationCount && NotificationCount > 0)
        {
            if (MaximumCalculated != null)
            {
                MaximumCalculated(this, new MaxSampleEventArgs(MinValue, MaxValue));
            }

            Reset();
        }
    }
}

在 AudioMeterInformation 不可用的 Windows XP 中进行采样是有意义的。有人可以帮助显示 AudioMeterInformation 而无需从设备中取样。

4

1 回答 1

0

试试CoreAudioApiCodeProject 文章)。

MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
MMDevice defaultDevice = 
  devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);

// Call this line as often as you need.
int level = defaultDevice.AudioMeterInformation.MasterPeakValue;
于 2012-07-26T17:58:49.157 回答