Problems with sending audio from a line in to a speaker

Feb 24, 2015 at 1:43 PM
Hi,

I'm having issues with sending audio from a line in to a speaker device.
Sometimes the route works and sometimes it doesn't. It can start to play for a few seconds then suddenly stop. Only way to reactivate it is to start a new set of objects.
It seems to have issues with starting up because if the audio route is running for more than 10 seconds without issues then it works.

I suspect it somehow crashes the internal capture thread in the WasapiCapture class but I have not been able to confirm it.

Here is the code I'm using:
            _ProcessingThread = new Thread(() =>
            {
                try
                {
                    _CancelThread.Reset();

                    using (var capture = new WasapiCapture())
                    {
                        capture.Device = _SourceDevice;
                        capture.Initialize();

                        using (var source = new SoundInSource(capture))
                        {
                            using (var soundOut = new WasapiOut())
                            {
                                capture.Start();

                                soundOut.Device = _TargetDevice;
                                soundOut.Initialize(source);
                                soundOut.Play();

                                _CancelThread.WaitOne();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    DebugMsg("Exception in AudioDevice Thread", ex);
                }
            });
            _ProcessingThread.Name = "AudioRoute_Processing";
            _ProcessingThread.IsBackground = true;
            _ProcessingThread.SetApartmentState(ApartmentState.STA);
            _ProcessingThread.Start();
_SourceDevice and _TargetDevice is a MMDevice object

Any help and guidance is appreciated here.

Thanks in advance
Borte
Feb 24, 2015 at 8:21 PM
Edited Feb 24, 2015 at 8:22 PM
I was able to fix this problem. I'm not sure if this is a proper fix or not but it did solve my problem.

I tracked my problem down to the WasapiOut.PlaybackProc method where it calls the FeedBuffer method, for some reason my input device is slow to fill it's buffers at startup so it failed in the FeedBuffer method returning false as read or count was zero (or below). I changed the code to give it a second chance in this case and so far this has fixed my problem.

Here is my updated code for the WasapiOut.PlaybackProc method where it calls the FeedBuffer method:
if (!FeedBuffer(_renderClient, buffer, framesReadyToFill, frameSize))
{
    // Give client a chance to fill buffers
    Thread.Sleep(_latency / 8);
    if (!FeedBuffer(_renderClient, buffer, framesReadyToFill, frameSize))
    {
        // If it still fails, cancel rendering
        _playbackState = PlaybackState.Stopped; //TODO: Fire Stopped-event here?
    }
}