Example #1
0
            public override void Execute()
            {
                if (SimulationStep == 0)
                {
                    #region First step init
                    m_position = 0;
                    Owner.Features.Fill(0);
                    Owner.Label.Fill(0);

                    try
                    {   // load input data on simulation start
                        switch (Owner.m_UserInput)
                        {
                        case InputTypeEnum.SampleSound:
                            m_wavReader = new WavPlayer(GoodAI.SoundWorld.Properties.Resources.Digits_en_wav);
                            m_wavReader.m_SamplesPerSec = 32000;
                            m_wavReader.AttachTranscription(GoodAI.SoundWorld.Properties.Resources.Digits_en_txt);

                            m_InputData = m_wavReader.ReadShort(m_wavReader.m_length);
                            break;

                        case InputTypeEnum.UserDefined:
                            // reading corpus files
                            if (Owner.m_InputPathCorpus != "")
                            {
                                audio = Directory.GetFiles(Owner.m_InputPathCorpus, "*.wav");

                                m_wavReader = new WavPlayer(audio[m_currentCorpusFile]);
                                AttachTranscriptFileIfExists(audio[m_currentCorpusFile]);
                                m_currentCorpusFile = 1;
                                m_InputData         = m_wavReader.ReadShort(m_wavReader.m_length);
                            }
                            else
                            {
                                m_wavReader = new WavPlayer(Owner.m_InputPathAudio);
                                AttachTranscriptFileIfExists(Owner.m_InputPathAudio);
                                m_InputData = m_wavReader.ReadShort(m_wavReader.m_length);
                            }

                            break;
                        }
                    }
                    catch (Exception)
                    {
                        MyLog.ERROR.WriteLine("Not a valid sound device!");
                    }
                    #endregion
                }

                if (SimulationStep % ExpositionTime == 0)
                {
                    #region Every time step
                    if (m_InputData == null)
                    {
                        return;
                    }

                    int     size   = 0;
                    float[] result = new float[Owner.FeaturesCount];

                    // process data according to chosen feature type
                    switch (Owner.FeaturesType)
                    {
                    case FeatureType.Samples:
                        result = PrepareInputs(Owner.FeaturesCount);
                        break;

                    case FeatureType.FFT:
                        // input size must be power of 2 and double sized due to the mirror nature of FFT
                        size   = NextPowerOf2(Owner.FeaturesCount * 2);
                        result = PerformFFT(PrepareInputs(size));
                        //result = PerformFFT(GenerateSine(size));  // generate a test sine signal
                        break;

                    case FeatureType.MFCC:
                        result = WindowFunction.Hanning(PrepareInputs(256));
                        result = PerformFFT(result);
                        result = MFCC.Compute(result, player.m_SamplesPerSec, Owner.FeaturesCount);
                        break;

                    case FeatureType.LPC:
                        result = WindowFunction.Hanning(PrepareInputs(256));
                        result = LPC.Compute(result, Owner.FeaturesCount);
                        break;
                    }
                    #endregion

                    // flush processed features into GPU
                    Array.Clear(Owner.Features.Host, 0, Owner.Features.Count);
                    for (int i = 0; i < Owner.FeaturesCount; i++)
                    {
                        Owner.Features.Host[i] = result[i];
                    }
                    Owner.Features.SafeCopyToDevice();
                }
            }