0

我们正在使用用 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 字节数组(这意味着 600 毫秒而不是 100 毫秒的缓冲区)。2. 线程将要休眠,然后获得 2400 个样本(4800 字节),而不是预期的 1600 字节帧

知道那里发生了什么吗?

4

1 回答 1

2

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

于 2013-02-15T07:49:48.303 回答