public void Dispose()
        {
            PSE.Enable                 = false;
            PSE.VolumeRequest         -= PSE_VolumeRequest;
            PSE.ExtractedDegreeOfRisk -= PSE_ExtractedDegreeOfRisk;
            PSEFadeTimer.Stop();
            PSEFadeTimer.Dispose();
            PSE.Dispose();
            if (Mixer != null)
            {
                Mixer.RemoveAllMixerInputs();
            }
            if (WasapiCapture != null)
            {
                WasapiCapture.Dispose();
            }
            if (BufferedWaveProvider != null)
            {
                BufferedWaveProvider.ClearBuffer();
            }

            Mixer                = null;
            WasapiCapture        = null;
            BufferedWaveProvider = null;
        }
        /*
         * public static void StartRecording()
         * {
         *  //AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit);
         *
         *  micAud = new NAudio.Wave.WaveInEvent();
         *  micAud.WaveFormat = new NAudio.Wave.WaveFormat(44100, 2);
         *  micAud.DataAvailable += MicAud_DataAvailable;
         *  micAud.RecordingStopped += MicAud_RecordingStopped;
         *  // micAud.DataAvailable += (s, capData) => wfw.Write(capData.Buffer, 0, capData.BytesRecorded);
         *  wfw = new WaveFileWriter(_micLoc, micAud.WaveFormat);
         *  micAud.StartRecording();
         *
         *  using (spkAud = new CSCore.SoundIn.WasapiLoopbackCapture())
         *  {
         *      spkAud.Initialize();
         *      using (var w = new WaveWriter(_spkLoc, spkAud.WaveFormat))
         *      {
         *          spkAud.DataAvailable += (s, capData) => w.Write(capData.Data, capData.Offset, capData.ByteCount);
         *          spkAud.Start();
         *
         *          Debug.WriteLine("Sleeping thread");
         *          Thread.Sleep(3000);
         *
         *          //spkAud.Dispose();
         *          spkAud.Stop();
         *          micAud.StopRecording();
         *      }
         *  }
         * }
         *
         * /*
         * static void OnProcessExit(object sender, EventArgs e)
         * {
         *  Debug.WriteLine("I'm out of here");
         *
         *  Debug.WriteLine("Stopping the mic recording");
         *  MergeWav(_spkLoc, _micLoc, _finLoc);
         * }
         * //
         *
         * private static void MicAud_RecordingStopped(object sender, NAudio.Wave.StoppedEventArgs e)
         * {
         *  if (micAud != null)
         *  {
         *      micAud.Dispose();
         *      micAud = null;
         *  }
         *
         *  if (wfw != null)
         *  {
         *      wfw.Dispose();
         *      wfw = null;
         *  }
         *
         * }
         *
         * private static void MicAud_DataAvailable(object sender, WaveInEventArgs e)
         * {
         *  wfw.Write(e.Buffer, 0, e.BytesRecorded);
         *  wfw.Flush();
         * }
         */

        public void MergeWav(string inp1, string inp2, string output, string lamLoc)
        {
            if (!File.Exists(inp1))
            {
                return;
            }
            Console.WriteLine(inp1);
            Console.WriteLine(inp2);
            var paths = new[] {
                inp1,
                inp2
            };

            // open all the input files
            var readers = paths.Select(f => new NAudio.Wave.WaveFileReader(f)).ToArray();

            // choose the sample rate we will mix at
            var maxSampleRate = readers.Max(r => r.WaveFormat.SampleRate);

            // create the mixer inputs, resampling if necessary
            var mixerInputs = readers.Select(r => r.WaveFormat.SampleRate == maxSampleRate ?
                                             r.ToSampleProvider() :
                                             new MediaFoundationResampler(r, NAudio.Wave.WaveFormat.CreateIeeeFloatWaveFormat(maxSampleRate, r.WaveFormat.Channels)).ToSampleProvider());

            // create the mixer
            var mixer = new NAudio.Wave.SampleProviders.MixingSampleProvider(mixerInputs);

            // write the mixed audio to a 16 bit WAV file
            WaveFileWriter.CreateWaveFile16(output, mixer);

            // clean up the readers
            foreach (var reader in readers)
            {
                reader.Dispose();
            }


            string mp3output = Path.Combine(Path.GetDirectoryName(output), Path.GetFileNameWithoutExtension(output)) + ".mp3";

            //Console.WriteLine("output loc: " + mp3output);
            LameWavToMp3(output, mp3output, lamLoc);

            //????????????????????????????Uncomment when taking the final build
            //File.Delete(inp1);
            //File.Delete(inp2);

            //Console.WriteLine("Done converting!");

            //Console.ReadLine();
        }
        public void Initialize(ISampleProvider BaseProvider)
        {
            baseProv = BaseProvider;

            PSE.VolumeRequest         += PSE_VolumeRequest;
            PSE.ExtractedDegreeOfRisk += PSE_ExtractedDegreeOfRisk;
            PSE.Enable = true;

            PSEFadeTimer.Interval = 3000;
            PSEFadeTimer.Tick    += PSEFadeTimer_Tick;

            BufferedWaveProvider = new BufferedWaveProvider(WaveFormat);

            List <ISampleProvider> sources = new List <ISampleProvider>();

            sources.Add(baseProv);
            sources.Add(WaveExtensionMethods.ToSampleProvider(BufferedWaveProvider));

            Mixer = new NAudio.Wave.SampleProviders.MixingSampleProvider(sources);
        }
        public static void MergeWav()
        {
            string inp1, inp2, output;

            inp1 = inp2 = output = "NA";

            if (inp1 == "NA")
            {
                inp1   = _spkLoc;
                inp2   = _micLoc;
                output = _finLoc;
            }
            var paths = new[] {
                inp1,
                inp2
            };

            // open all the input files
            var readers = paths.Select(f => new NAudio.Wave.WaveFileReader(f)).ToArray();

            // choose the sample rate we will mix at
            var maxSampleRate = readers.Max(r => r.WaveFormat.SampleRate);

            // create the mixer inputs, resampling if necessary
            var mixerInputs = readers.Select(r => r.WaveFormat.SampleRate == maxSampleRate ?
                                             r.ToSampleProvider() :
                                             new MediaFoundationResampler(r, NAudio.Wave.WaveFormat.CreateIeeeFloatWaveFormat(maxSampleRate, r.WaveFormat.Channels)).ToSampleProvider());

            // create the mixer
            var mixer = new NAudio.Wave.SampleProviders.MixingSampleProvider(mixerInputs);

            // write the mixed audio to a 16 bit WAV file
            WaveFileWriter.CreateWaveFile16(output, mixer);

            // clean up the readers
            foreach (var reader in readers)
            {
                reader.Dispose();
            }
        }