我正在从Voicemeeter本地接收数据,后者使用VBAN协议发送字节流。
我正在尝试使用NAudio解码字节并使用WaveOutEvent播放音频。我的实现与NAudioDemo中的NetworkChatDemo几乎相同,请查看此link以了解有关NAudio的更多信息,它是一个很棒的音频库,非常感谢Mark Heath。
我的实现:
private readonly ILogger<AudioController> _logger;
private NetworkAudioPlayer player;
private readonly INetworkChatCodec _codec;
public AudioController(ILogger<AudioController> logger)
{
_logger = logger;
_codec = new ALawChatCodec();
}
[HttpGet]
[Route("start")]
public IActionResult Start()
{
var receiver = new UdpAudioReceiver(6980);
player = new NetworkAudioPlayer(_codec, receiver);
return Ok("Hello");
}
网络音频播放器:
private readonly INetworkChatCodec codec;
private readonly IAudioReceiver receiver;
private readonly IWavePlayer waveOut;
private readonly BufferedWaveProvider waveProvider;
public NetworkAudioPlayer(INetworkChatCodec codec, IAudioReceiver receiver)
{
this.codec = codec;
this.receiver = receiver;
receiver.OnReceived(OnDataReceived);
waveOut = new WaveOutEvent();
waveProvider = new BufferedWaveProvider(codec.RecordFormat);
waveProvider.BufferDuration = TimeSpan.FromSeconds(300);
waveOut.Init(waveProvider);
waveOut.Play();
}
void OnDataReceived(byte[] compressed)
{
byte[] decoded = codec.Decode(compressed, 0, compressed.Length);
waveProvider.AddSamples(decoded, 0, decoded.Length);
}
public void Dispose()
{
receiver?.Dispose();
waveOut?.Dispose();
}
我在此实现中遇到两个问题:
如何将BufferDuration设置为无穷大?如何正确播放音频?
更新:通过将bufferPorivider的DiscardOnBufferOverflow的值设置为true,我能够解决第一个问题。
关于噪声问题,我已经从question到达解决方案,Mark Heath提到了
而且,您确定接收到的网络数据包中没有周围的元数据吗?如果是这样,则需要将其剥离,否则会导致噪音。
所以,我将样本添加到BufferProvider时将offset的值更改为等于28,这会导致异常:
void OnDataReceived(byte[] compressed)
{
var l = compressed.Length;
waveProvider.AddSamples(compressed, 28, compressed.Length);
}
堆栈跟踪:
>
> System.ArgumentException
HResult=0x80070057
Message=Source array was not long enough. Check the source index, length, and the array's lower bounds. (Parameter 'sourceArray')
Source=System.Private.CoreLib
StackTrace:
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
at NAudio.Utils.CircularBuffer.Write(Byte[] data, Int32 offset, Int32 count)
at NAudio.Wave.BufferedWaveProvider.AddSamples(Byte[] buffer, Int32 offset, Int32 count)
at POC.AudioStreaming.AudioManagers.NetworkAudioPlayer.OnDataReceived(Byte[] compressed) in C:\dev\POC.AudioStreaming\POC.AudioStreaming\AudioManagers\NetworkAudioPlayer.cs:line 41
at POC.AudioStreaming.AudioManagers.UdpAudioReceiver.ListenerThread(Object state) in C:\dev\POC.AudioStreaming\POC.AudioStreaming\AudioManagers\UdpAudioReceiver.cs:line 38
at System.Threading.QueueUserWorkItemCallback.<>c.<.cctor>b__6_0(QueueUserWorkItemCallback quwi)
at System.Threading.ExecutionContext.RunForThreadPoolUnsafe[TState](ExecutionContext executionContext, Action`1 callback, TState& state)
at System.Threading.QueueUserWorkItemCallback.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
我该如何解决此异常?
我已经使用跳过功能解决了从接收到的数据包中删除前28个字节的异常:
void OnDataReceived(byte[] compressed)
{
byte[] buffer = compressed.Skip(28).ToArray();
waveProvider.AddSamples(buffer, 0, buffer.Length);
}
通过解决异常,我能够很好地听到音频。另外,我已经回答了我的问题。