gpt4 book ai didi

c# - WasapiCapture NAudio

转载 作者:行者123 更新时间:2023-12-03 00:19:12 28 4
gpt4 key购买 nike

我们正在使用用 c# 编写的 NAudio 堆栈,并尝试以 PCM 8kHZ 和每个样本 16 位的独占模式捕获音频。

在以下函数中:

private void InitializeCaptureDevice()
{
if (initialized)
return;

long requestedDuration = REFTIMES_PER_MILLISEC * 100;

if (!audioClient.IsFormatSupported(AudioClientShareMode.Shared, WaveFormat) &&
(!audioClient.IsFormatSupported(AudioClientShareMode.Exclusive, WaveFormat)))
{
throw new ArgumentException("Unsupported Wave Format");
}

var streamFlags = GetAudioClientStreamFlags();

audioClient.Initialize(AudioClientShareMode.Shared,
streamFlags,
requestedDuration,
requestedDuration,
this.waveFormat,
Guid.Empty);

int bufferFrameCount = audioClient.BufferSize;
this.bytesPerFrame = this.waveFormat.Channels * this.waveFormat.BitsPerSample / 8;
this.recordBuffer = new byte[bufferFrameCount * bytesPerFrame];
Debug.WriteLine(string.Format("record buffer size = {0}", this.recordBuffer.Length));

initialized = true;
}

我们在调用此函数之前将 WaveFormat 配置为 (8000,1) 以及 100 ms 的周期。
我们预计系统会根据要求为缓冲区分配 1600 字节,间隔为 100 毫秒。

但我们注意到发生了以下情况:
1.系统分配audioClient.BufferSize为4800,“this.recordBuffer”是一个9600字节的数组(意思是600ms而不是100ms的缓冲区)。
2. 线程将要休眠,然后获得 2400 个样本(4800 字节),而不是预期的 1600 字节帧

知道那里发生了什么吗?

最佳答案

您说您正在以独占模式捕获音频,但在示例代码中您调用 Initialize使用 AudioClientMode.Shared 的方法.我觉得共享模式不太可能让你在 8kHz 下工作。与 wave... API 不同,WASAPI 不会为您重新采样播放或捕获,因此声卡本身必须以您指定的采样率运行。

关于c# - WasapiCapture NAudio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14861468/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com