This project has moved and is read-only. For the latest updates, please go here.

Effect not working and behaving differently on different sound cards

Mar 8, 2015 at 8:46 PM
I'm using the WasapiLoopbackCapture in combination with WaveOut in order to create an echo effect that is global, i.e. affects the current audio output.

However, my implementation, which seems pretty poor to me, doesn't work well at all. On every machine it sounds different, sometimes it even crashes.

This is the code I came up with. It records the audio output and every 75ms it plays the recorded stream using WaveOut.

What am I doing wrong here, how can this be improved?
private static void CreateReverb()
{
    MemoryStream loopbackWave = new MemoryStream();
    DateTime lastEcho = DateTime.Now;

    WasapiLoopbackCapture waveIn = new WasapiLoopbackCapture();
    waveIn.StartRecording();
    waveIn.DataAvailable += delegate(object sender, WaveInEventArgs e)
    {
        loopbackWave.Write(e.Buffer, 0, e.BytesRecorded);
        if ((DateTime.Now - lastEcho).TotalMilliseconds > 75)
        {
            lastEcho = DateTime.Now;
            byte[] loopbackBytes = loopbackWave.ToArray();
            loopbackWave.Dispose();
            loopbackWave = new MemoryStream();

            using (MemoryStream waveStream = new MemoryStream())
            {
                using (WaveFileWriter waveFileWriter = new WaveFileWriter(waveStream, waveIn.WaveFormat))
                {
                    waveFileWriter.Write(loopbackBytes, 0, loopbackBytes.Length);
                }
                loopbackBytes = waveStream.ToArray();
            }

            for (int i = 0; i < 3; i++)
            {
                ThreadPool.QueueUserWorkItem(delegate
                {
                    try
                    {
                        WaveOut waveOut = new WaveOut();
                        waveOut.Init(new WaveFileReader(new MemoryStream(loopbackBytes)));
                        waveOut.Volume = ReverbIntensity * .5f;
                        waveOut.Play();
                        Thread.Sleep(200);
                        waveOut.Dispose();
                    }
                    catch { }
                });
            }
        }
    };
}