Exemple #1
0
        /// <summary>
        /// Process 16 bit sample
        /// </summary>
        /// <param name="wave"></param>
        public void Process(ref byte[] wave)
        {
            IsEventActive = false;
            _waveLeft = new double[wave.Length / 4];
            _waveRight = new double[wave.Length / 4];

            if (_isTest == false)
            {
                // Split out channels from sample
                int h = 0;
                for (int i = 0; i < wave.Length; i += 4)
                {
                    _waveLeft[h] = (double)BitConverter.ToInt16(wave, i);
                    if (IsDetectingEvents == true)
                        if (_waveLeft[h] > AmplitudeThreshold || _waveLeft[h] < -AmplitudeThreshold)
                            IsEventActive = true;
                    _waveRight[h] = (double)BitConverter.ToInt16(wave, i + 2);
                    if (IsDetectingEvents == true)
                        if (_waveLeft[h] > AmplitudeThreshold || _waveLeft[h] < -AmplitudeThreshold)
                            IsEventActive = true;
                    h++;
                }
            }
            else
            {
                // Generate artificial sample for testing
                _signalGenerator = new SignalGenerator();
                _signalGenerator.SetWaveform("Sine");
                _signalGenerator.SetSamplingRate(44100);
                _signalGenerator.SetSamples(8192);
                _signalGenerator.SetFrequency(4096);
                _signalGenerator.SetAmplitude(32768);
                _waveLeft = _signalGenerator.GenerateSignal();
                _waveRight = _signalGenerator.GenerateSignal();
            }

            // Generate frequency domain data in decibels
            _fftLeft = FourierTransform.FFT(ref _waveLeft);
            _fftLeftSpect.Add(_fftLeft);
            if (_fftLeftSpect.Count > _maxHeightLeftSpect)
                _fftLeftSpect.RemoveAt(0);
            _fftRight = FourierTransform.FFT(ref _waveRight);
            _fftRightSpect.Add(_fftRight);
            if (_fftRightSpect.Count > _maxHeightRightSpect)
                _fftRightSpect.RemoveAt(0);
        }
        /// <summary>
        /// Process 16 bit sample
        /// </summary>
        /// <param name="wave"></param>
        public void Process(ref byte[] wave)
        {
            _waveLeft = new double[wave.Length / 4];
            _waveRight = new double[wave.Length / 4];

            if (_isTest == false)
            {
                // Split out channels from sample
                int h = 0;
                for (int i = 0; i < wave.Length; i += 4)
                {
                    _waveLeft[h] = (double)BitConverter.ToInt16(wave, i);
                    if (IsDetectingEvents == true)
                        if (_waveLeft[h] > AmplitudeThreshold || _waveLeft[h] < -AmplitudeThreshold)
                            IsEventActive = true;
                    _waveRight[h] = (double)BitConverter.ToInt16(wave, i + 2);
                    if (IsDetectingEvents == true)
                        if (_waveLeft[h] > AmplitudeThreshold || _waveLeft[h] < -AmplitudeThreshold)
                            IsEventActive = true;
                    h++;
                }
            }
            else
            {
                // Generate artificial sample for testing
                _signalGenerator = new SignalGenerator();
                _signalGenerator.SetWaveform("Sine");
                _signalGenerator.SetSamplingRate(44100);
                _signalGenerator.SetSamples(8192);
                _signalGenerator.SetFrequency(4096);
                _signalGenerator.SetAmplitude(32768);
                _waveLeft = _signalGenerator.GenerateSignal();
                _waveRight = _signalGenerator.GenerateSignal();
            }

            if (takeSample)
            {

                DateTime currentTime = DateTime.Now;
                int difference = (currentTime - initialTime).Seconds;

                if (difference < sampleRecordDuration)
                {
                    try
                    {
                        double[] soundData = new double[_waveLeft.Length];
                        Array.Copy(_waveLeft, soundData, _waveLeft.Length);
                        Array.Sort(soundData);
                        double min = soundData[0];
                        double max = soundData[soundData.Length - 1];

                        if (min < minPerSecond)
                        {
                            minPerSecond = min;
                        }

                        if (max > maxPerSecond)
                        {
                            maxPerSecond = max;
                        }

                        minSumPerSecond += min;
                        maxSumPerSecond += max;

                        string data = Environment.NewLine + counter + "). " + "min = " +min + " max = " + max;

                        Byte[] text = new UTF8Encoding(true).GetBytes(data);
                        output.Write(text, 0, text.Length);
                        counter++;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }

                else
                {
                    minAveragePerSecond = minSumPerSecond / counter;
                    maxAveragePerSecond = maxSumPerSecond / counter;

                    string data = Environment.NewLine + "min Per second = " + minPerSecond + Environment.NewLine + "max per second = " + maxPerSecond + Environment.NewLine + "min average = " + minAveragePerSecond + Environment.NewLine + "max average = " + maxAveragePerSecond;

                    Byte[] text = new UTF8Encoding(true).GetBytes(data);
                    output.Write(text, 0, text.Length);

                    counter = 0;

                    output.Flush();
                    output.Close();
                    takeSample = false;
                    sampleMessageTextLabel.Text = "Finished recording";

                    // reset variables
                    minPerSecond = 0;
                    maxPerSecond = 0;

                    minSumPerSecond = 0;
                    maxSumPerSecond = 0;

                    minAveragePerSecond= 0;
                    maxAveragePerSecond= 0;
                }

            }

            //--------Record Interaction-------------
            if (recordInteraction)
            {
                DateTime interactCurrentTime = DateTime.Now;
                int difference = (interactCurrentTime - interactInitialTime).Seconds;

                if (difference < interactRecordDuration)
                {
                    try
                    {
                        double[] soundData = new double[_waveLeft.Length];
                        Array.Copy(_waveLeft, soundData, _waveLeft.Length);
                        Array.Sort(soundData);
                        double min = soundData[0];
                        double max = soundData[soundData.Length - 1];

                        if (min < interactMinPerSecond)
                        {
                            interactMinPerSecond = min;
                        }

                        if (max > interactMaxPerSecond)
                        {
                            interactMaxPerSecond = max;
                        }

                        interactMinSumPerSecond += min;
                        interactMaxSumPerSecond += max;

                        interactCounter++;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }

                else
                {
                    interactMinAveragePerSecond = interactMinSumPerSecond / interactCounter;
                    interactMaxAveragePerSecond = interactMaxSumPerSecond / interactCounter;

                    interactCounter = 0;
                    recordInteraction = false;
                    interactLabel.Text = "Interaction finished ";

                    double [] input = {interactMinPerSecond, interactMaxPerSecond, interactMinAveragePerSecond, interactMaxAveragePerSecond  };

                    String voiceTone = neuralNetwork.computeOutput(input);
                    neuralOutputLabel.Text = "Classification is: " + voiceTone;
                    Console.WriteLine("Min per second = " + interactMinPerSecond + " Max per second = " + interactMaxPerSecond + " Min average = " + interactMinAveragePerSecond + " Max average = " + interactMaxAveragePerSecond );

                    if (voiceTone != "Unrecognised")
                    {
                        robot.listen(voiceTone);
                    }

                    else
                    {
                        unrecognisedVoiceTone = true;
                    }

                    // reset variables
                    interactMinPerSecond = 0;
                    interactMaxPerSecond = 0;

                    interactMinSumPerSecond = 0;
                    interactMaxSumPerSecond = 0;

                    interactMinAveragePerSecond = 0;
                    interactMaxAveragePerSecond = 0;
                }

            }
        }