使用 NAudio 在运行时更改波形格式
我已经使用以下方法初始化了设备:
static IWavePlayer waveOut;
static WaveFormat waveFormat;
static BufferedWaveProvider waveProvider;
private static int AudioDeviceInit()
{
waveOut = new DirectSoundOut();
waveFormat = new WaveFormat(44100, 2);
waveProvider = new BufferedWaveProvider(waveFormat);
waveOut.Init(waveProvider);
waveOut.Play();
return 0;
}
我使用以下方法向其添加 pcm 流:
waveProvider.AddSamples(samples, 0, size);
只要流数据具有相同的配置,上述内容就可以正常工作。
我有另一个接收采样率和通道数的函数,我想重新配置waveprovider以使用新提供的配置。这是我正在使用的代码:
private static void AudioConfigCallback(int rate, int channel)
{
waveFormat = new WaveFormat(rate, channel);
waveProvider = new BufferedWaveProvider(waveFormat);
waveOut.Init(waveProvider);
return;
}
这不起作用,我相信这也不是正确的方法。知道如何重新配置设备以使用新的 Sample_rate 和 num_channels
谢谢。
I have initialized the device using:
static IWavePlayer waveOut;
static WaveFormat waveFormat;
static BufferedWaveProvider waveProvider;
private static int AudioDeviceInit()
{
waveOut = new DirectSoundOut();
waveFormat = new WaveFormat(44100, 2);
waveProvider = new BufferedWaveProvider(waveFormat);
waveOut.Init(waveProvider);
waveOut.Play();
return 0;
}
I am adding pcm stream to it using:
waveProvider.AddSamples(samples, 0, size);
The above is working fine as long as the stream data is of the same configuration.
I have another function that receives sample rate and number of channels and I want to reconfigure the waveprovider to use the newly provided configuration. Here is the code that I am using:
private static void AudioConfigCallback(int rate, int channel)
{
waveFormat = new WaveFormat(rate, channel);
waveProvider = new BufferedWaveProvider(waveFormat);
waveOut.Init(waveProvider);
return;
}
This is not working and I believe that this is not the correct way of doing it as well. Any idea how I can reconfigure the device to use new sample_rate and num_channels
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是不可能的。当您打开输出设备时,无论是 WaveOut、Direct Sound、WASAPI 还是 ASIO,此时您必须指定您将使用的格式。您必须关闭输出设备并使用新的 WaveFormat 重新打开它。
另一种方法是始终转换为固定的 WaveFormat,并在传入格式发生变化时使用 WaveFormatConversionStream 转换为正确的格式。这将使您避免打开和关闭输出设备。
This is not possible. When you open an output device, whether WaveOut, Direct Sound, WASAPI or ASIO, at that point you must specify the format at which you will work. You must close the output device and re-open it with the new WaveFormat.
An alternative approach would be to always convert to a fixed WaveFormat, and use WaveFormatConversionStream to convert to the correct format whenever the incoming format changes. This would allow you to avoid opening and closing the output device.