“缓冲区已满”例外

时间:2016-01-26 12:09:29

标签: c# .net multithreading sockets naudio

我正在尝试创建发送和接收音频的应用程序。我有2个 BufferedWaveProvider的。

我做的第一个

private void AsioOut_AudioAvailable(object sender, AsioAudioAvailableEventArgs e)
{
    float[] sourceAudio = new float[e.SamplesPerBuffer*e.InputBuffers.Length];
    e.GetAsInterleavedSamples(sourceAudio);
    float[] proccesedAudio = new float[settings.BufferSize];
    byte[] result = new byte[settings.BufferSize*4];
    byte[] sendingAudio = new byte[e.SamplesPerBuffer*4];

    for (int j = 1, q = 0; j < sourceAudio.Length; j += 2, q++)
        proccesedAudio[q] = sourceAudio[j];

    //...audio is beeing proccesed by VSTHost...

    result.CopyTo(sendingAudio,0);

    if (connection.IsConnected)
        Task.Run(() => connection.Send(sendingAudio)); //Proccesed audio sends by socket
    inputAudioBufferedWaveProvider.AddSamples(result, 0, result.Length);
}

对于我做的第二个

private void AudioReceiving()
{
    while (isAudioReceiving)
    {
        incomingBytes = connection.AudioReceiving(); //Socket receives bytes
        incomingAudioBufferedWaveProvider.AddSamples(incomingBytes, 0, incomingBytes.Length);
    }
}

这两个BufferedWiveProvider正在由MixingSampleProvider混合。

mixingProvider = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(Convert.ToInt32(settings.SampleRate), 2));

mixingProvider.AddMixerInput(inputAudioPanningProvider);
mixingProvider.AddMixerInput(incomingAudioPanningProvider);
mixingProvider.ReadFully = true;

asioOut.InitRecordAndPlayback(new SampleToWaveProvider(mixingProvider),2,Convert.ToInt32(settings.SampleRate));

开始申请后3-4分钟后,我得到异常(缓冲区已满)。我认为缓冲区的问题是从网络接收的字节填充。

我做错了什么?

0 个答案:

没有答案