【发布时间】: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