private void startRecordingButton_Click(object sender, EventArgs e)
 {
     if (wavRecorder == null)
     {
         wavRecorder                 = new WAVRecorder();
         wavRecorder.SampleRate      = 16000;
         wavRecorder.StorageDuration = 10;
     }
     if (!wavRecorder.IsRecording)
     {
         recognizeButton.Enabled = false;
         recordingButton.Text    = "Stop recording";
         wavRecorder.Start();
     }
     else
     {
         wavRecorder.Stop();
         byte[] recordedBytes = wavRecorder.GetAllRecordedBytes();
         if (recordedBytes != null)
         {
             if (recordedBytes.Length > 0)
             {
                 WAVSound sound = new WAVSound("", wavRecorder.SampleRate, wavRecorder.NumberOfChannels, wavRecorder.NumberOfBitsPerSample);
                 sound.AppendSamplesAsBytes(recordedBytes);
                 sound.SetMaximumNonClippingVolume();
                 soundVisualizer.SetSound(sound);
             }
         }
         recordingButton.Text    = "Start recording";
         recognizeButton.Enabled = true;
     }
 }
 private void recordToolStripButton_Click(object sender, EventArgs e)
 {
     if (recordToolStripButton.Text.Contains("Start"))  // A bit ugly, but OK
     {
         recordToolStripButton.Text = "Stop recording";
         wavRecorder          = new WAVRecorder();
         wavRecorder.DeviceId = 0;
         wavRecorder.Start();
     }
     else
     {
         wavRecorder.Stop();
         byte[] recordedBytes = wavRecorder.GetAllRecordedBytes();
         if (recordedBytes != null)
         {
             if (recordedBytes.Length > 0)
             {
                 WAVSound sound = new WAVSound("", wavRecorder.SampleRate, wavRecorder.NumberOfChannels, wavRecorder.NumberOfBitsPerSample);
                 sound.AppendSamplesAsBytes(recordedBytes);
                 soundVisualizer.SetSound(sound);
             }
         }
         recordToolStripButton.Text         = "Start recording";
         playSoundButton.Enabled            = true;
         saveSoundToolStripMenuItem.Enabled = true;
     }
 }
 private void recordingButton_Click(object sender, EventArgs e)
 {
     if (!recording)
     {
         recording = true;
         recorder.Start();
         recordingButton.Text = "Stop recording";
     }
     else
     {
         recorder.Stop();
         byte[] recordedBytes = recorder.GetAllRecordedBytes();
         if (recordedBytes != null)
         {
             if (recordedBytes.Length > 0)
             {
                 WAVSound sound = new WAVSound("", recorder.SampleRate, recorder.NumberOfChannels, recorder.NumberOfBitsPerSample);
                 sound.AppendSamplesAsBytes(recordedBytes);
                 soundVisualizer.SetSound(sound);
             }
         }
         recordingButton.Text = "Start recording";
         saveSoundToolStripMenuItem.Enabled = true;
         raiseVolumeButton.Enabled          = true;
         reduceVolumeButton.Enabled         = true;
         playButton.Enabled      = true;
         playButton.Enabled      = true;
         filterToolStrip.Enabled = true;
         recording = false;
     }
 }
        private void RunLoop()
        {
            Thread.Sleep(1);
            DateTime utteranceStartDateTime         = DateTime.Now;      // Just needed for initialization.
            DateTime utteranceEndDateTime           = DateTime.MinValue; // Just needed for initialization.
            DateTime previousUtteranceStartDateTime = DateTime.MinValue;
            DateTime previousUtteranceEndDateTime   = DateTime.MinValue;
            DateTime recordingStartDateTime;
            DateTime recordingEndDateTime;
            double   utteranceStartTime = 0; // In seconds, measured from the start of the current recording.  (=0 just for initialization).
            double   utteranceEndTime;

            while (running)
            {
                Thread.Sleep(millisecondRecordingInterval);
                byte[] soundData = wavRecorder.GetAllRecordedBytes(out recordingStartDateTime, out recordingEndDateTime);
                if (soundData != null)
                {
                    if (soundData.Length > 0)
                    {
                        WAVSound sound = new WAVSound("", wavRecorder.SampleRate, wavRecorder.NumberOfChannels, wavRecorder.NumberOfBitsPerSample);
                        sound.AppendSamplesAsBytes(soundData);
                        if (showSoundStream)
                        {
                            if (!displayBusy)
                            {
                                WAVSound soundToDisplay = sound.Copy(); // 20171207: Make a new copy here, since the code below may process the sound before visualization is completed.
                                if (InvokeRequired)
                                {
                                    this.BeginInvoke(new MethodInvoker(() => ShowSound(soundToDisplay)));
                                }
                                else
                                {
                                    ShowSound(soundToDisplay);
                                }
                            }
                        }

                        // Next, remove all parts of the sound that have already been recognized, if any:
                        if (previousUtteranceEndDateTime > recordingStartDateTime)
                        {
                            double extractionStartTime = (previousUtteranceEndDateTime - recordingStartDateTime).TotalSeconds;
                            double extractionEndTime   = sound.GetDuration();
                            sound = sound.Extract(extractionStartTime, extractionEndTime);

                            // Debug code, remove

                            /*   if (sound == null)  // Should not happen, unless the recognition thread is stopped using a breakpoint.
                             *  {
                             *
                             *  }  */
                        }

                        if (!inUtterance)
                        {
                            utteranceStartTime = sound.GetFirstTimeAboveThreshold(0, movingAverageLength, detectionThreshold);
                            if (utteranceStartTime > 0)
                            {
                                double duration   = sound.GetDuration();
                                double timeToEnd  = duration - utteranceStartTime;
                                long   ticksToEnd = TICKS_PER_SECOND * (long)(timeToEnd);
                                utteranceStartDateTime = recordingEndDateTime.Subtract(new TimeSpan(ticksToEnd));
                                if (utteranceStartDateTime > previousUtteranceEndDateTime) // True (by construction) the FIRST time.
                                {
                                    inUtterance = true;
                                    long utteranceStartTimeAsTicks = (long)(TICKS_PER_SECOND * utteranceStartTime);  // Corrected 20170907 (1000000 -> 10000000)
                                    utteranceStartDateTime = recordingStartDateTime.Add(new TimeSpan(utteranceStartTimeAsTicks));
                                }
                            }
                        }
                        else
                        {
                            double   duration              = sound.GetDuration();
                            WAVSound endOfSound            = sound.Extract(duration - detectionSilenceInterval, duration);
                            double   startTimeInEndOfSound = endOfSound.GetFirstTimeAboveThreshold(0, movingAverageLength, detectionThreshold);
                            if (startTimeInEndOfSound < 0)  //  <=> silence at the end of the sound
                            {
                                inUtterance                    = false;
                                utteranceEndDateTime           = recordingEndDateTime; // recordingStartDateTime.Add(new TimeSpan(utteranceEndTimeAsTicks));
                                previousUtteranceStartDateTime = utteranceStartDateTime;
                                previousUtteranceEndDateTime   = utteranceEndDateTime;
                                //    Monitor.Enter(recognitionLockObject);
                                if (!recognizerBusy)
                                {
                                    recognizerBusy = true;
                                    WAVSound soundToRecognize = sound.Extract(utteranceStartTime - extractionMargin, duration).Copy();
                                    //    Monitor.Exit(recognitionLockObject);
                                    RunRecognizer(soundToRecognize);
                                }
                            }
                        }
                    }
                }
            }
        }