【问题标题】:NAudio stream Read method latencyNAudio 流读取方法延迟
【发布时间】:2020-10-18 15:07:34
【问题描述】:

我制作了自己的哔声代码作为练习。然而它是滞后的。

2-3 次哔声滞后,然后第 3 次或第 4 次来得更快。

有人能解释一下原因吗?以及如何消除延迟?

我使用了 Mark Heath 博客和 NAudio github 代码中的位。

这是主控制台代码:

var waveSine = new BeepStream(waveWhite.WaveFormat);
var sineChannel = new WaveChannel32(waveSine) { PadWithZeroes = false };

List<WaveChannel32> inputs = new List<WaveChannel32>();
inputs.Add(sineChannel);

var mixer = new MixingWaveProvider32(inputs);

var output = new WaveOut();
output.Init(mixer);
output.Play();
output.Volume = 0.5f;

while (true)
{
    Thread.Sleep(1000);
    waveSine.Beep(1000, 150);
}

这是哔声代码(它基本上输出 0,但是当 Beep 被调用时,它会推送缓存的正弦样本): (注 1:不是生产代码,所以请忽略明显的不一致) (注 1:实际上至少有 2 个频道 - 一个一直在播放,这就是为什么我必须制作第二个 - 哔声频道 - 也一直在播放。我很乐意写 SamplesProviders 但找不到好的例子)

public class BeepStream : WaveStream
...
public override int Read(byte[] buffer, int offset, int count)
{
    int totalBytesRead = 0;
    int beepBytesRead = 0;

    while (totalBytesRead < count)
    {
        if(playingNow == null)
        {
            // silence
            buffer[totalBytesRead] = 0;
            totalBytesRead += 1;// bytesRead;
        }
        else
        {
            // beep
            buffer[totalBytesRead] = playingNow[beepBytesRead++];
            totalBytesRead += 1;// bytesRead;

            if (beepBytesRead >= playingNow.Length)
                playingNow = null;
        }
    }

    return totalBytesRead;
}

观察 - 读取数始终为 52920。这个神奇的数字是什么?我可以减少它吗?如果有帮助,波形格式为 float 44100..

【问题讨论】:

    标签: c# .net naudio


    【解决方案1】:

    通过简单的尝试找到解决方案。

    使用独占 WASAPI 输出:

    var output = new WasapiOut(NAudio.CoreAudioApi.AudioClientShareMode.Exclusive, 3);
    output.Init(mixer);
    output.Play();
    
    

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 2018-01-04
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多