c# - 如何显示AudioMeterInformation?

标签 c# windows audio

如何显示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,而不必从设备中取样。

最佳答案

尝试CoreAudioApi(CodeProject article)。

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;

关于c# - 如何显示AudioMeterInformation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11096625/

相关文章:

java - 在 Activity 中实现音乐

Windows:如何告诉打印机在打印过程中发出 FormFeed?

c# - 对相同类型的 Func 使用三元运算符需要进行强制转换

c# - 如果我在写入变量时锁定,如果读取是原子的,我是否也需要在读取时锁定?

c# - 给定一个 Uri 和一个 UriTemplate,如何获取模板参数值?

c++ - 以同步模式运行 Windows GUI 应用程序?

c++ - 在 C++ 中访问 UNC 驱动器/远程网络驱动器

matlab - 初始化函数时出现意外的MATLAB运算符错误

javascript - <音频> 和 javascript : can I force audio files to be redownloaded each time they're used?

c# - 使用 UseSetting 覆盖配置