【问题标题】:Change sample rate of audiocard with WasapiOut Naudio使用 WasapiOut Naudio 更改声卡的采样率
【发布时间】:2019-09-21 23:55:07
【问题描述】:

我想使用 Naudio 在 c# 中以编程方式为我的声卡选择一个特定的采样率。 我的输出是独占模式下的 WasapiOut。

我已经尝试了很多东西,但没有任何效果,我到处搜索,我只找到了这个:How to Change Speaker Configuration in Windows in C#? 但他们并没有真正找到正确的解决方案。

这是我的 WasapiOut :

var enumerator = new MMDeviceEnumerator();
MMDevice device = enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active).FirstOrDefault(d => d.DeviceFriendlyName == name);
outputDevice = new WasapiOut(device, AudioClientShareMode.Exclusive, false,200);

我不明白的是这里: https://github.com/naudio/NAudio/blob/master/Docs/WasapiOut.md 它说: “如果您选择 AudioClientShareMode.Exclusive,那么您将请求对声卡的独占访问。这种方法的好处是您可以指定所需的确切采样率” 而且我在任何地方都没有找到如何指定采样率。

如果有人知道答案,那就太好了,谢谢!

编辑:

我想我通过这样做找到了一种方法:

var waveFormat5 = WaveFormat.CreateIeeeFloatWaveFormat(Int32.Parse(comboBox1.Text), 2);
            var test2 = new MixingSampleProvider(waveFormat5);
            var audioFile = new AudioFileReader("test.wav");
            var input = audioFile;
            test2.ReadFully = true;
            test2.AddMixerInput(new AutoDisposeFileReader(input,waveFormat5));
            outputDevice.Init(test2);

使用“outputDevice”作为我的 WasapiOut。 因此,我将 ouputDevice 采样率设置为使用 Mixing Sample Provider 选择的采样率,然后将音频文件发送到该混音器,这是正确的方法吗? 因为我的音频文件采样率是44100,我选择把我的outputDevice采样率也设置为44100,但是当我做outputDevice.Play()时,我听到的声音比原来的要快。

【问题讨论】:

    标签: c# audio device naudio sample-rate


    【解决方案1】:

    创建WasapiOut 的实例后,您调用Init 传递您要播放的音频。假设声卡支持它,它将尝试直接使用该音频的采样率(和WaveFormat)。用户

    【讨论】:

    • 嘿,马克!谢谢,但我可能没有问正确的问题。例如,我想将 wasapiOut 采样率设置为 48000,即使我想在 44100 播放音频文件。 outputDevice 采样率必须与我选择的相同,并且音频文件的采样率也必须与它是。我编辑了我的问题以添加新信息。
    【解决方案2】:

    我解决了我的问题,我使用了带有 MixingSampleProvider 的 AudioPlaybackEngine (https://markheath.net/post/fire-and-forget-audio-playback-with),并使用 try/catch 来处理“输入的采样率不同”的消息错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-10
      • 2013-10-25
      • 2015-07-13
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多