Ejemplo n.º 1
0
            // prepare batch for processing
            private float[] PrepareInputs(int count)
            {
                // define overlap
                if (m_position >= count)
                {
                    m_position -= (int)(float)(count * 0.5);
                }

                // Set Label
                if (m_wavReader != null && m_wavReader.HasTranscription)
                {
                    char c     = m_wavReader.GetTranscription((int)m_position, (int)m_position + count);
                    int  index = StringToDigitIndexes(c);

                    Array.Clear(Owner.Label.Host, 0, Owner.Label.Count);

                    // if unknown character, continue without setting any connection
                    Owner.Label.Host[index] = 1.00f;
                    Owner.Label.SafeCopyToDevice();
                }

                // if input is corpus, cycle files in the set
                float[] result = new float[count];
                if (Owner.InputType == InputTypeEnum.UserDefined && Owner.m_InputPathCorpus != "")
                {
                    bool eof = (m_position + count < m_InputData.Length)?false: true;
                    for (int i = 0; i < count; i++)
                    {
                        result[i]  = (float)m_InputData[m_position];
                        m_position = ++m_position % m_InputData.Length;
                    }

                    if (eof)
                    {
                        m_position  = 0;
                        m_wavReader = new WavPlayer(audio[m_currentCorpusFile]);
                        AttachTranscriptFileIfExists(audio[m_currentCorpusFile]);
                        if (m_currentCorpusFile + 1 < audio.Length)
                        {
                            m_currentCorpusFile++;
                        }
                        else
                        {
                            m_currentCorpusFile = 0;
                        }
                        m_InputData = m_wavReader.ReadShort(m_wavReader.m_length);
                    }
                }
                else // if input is single audio, cycle the file itself
                {
                    for (int i = 0; i < count; i++)
                    {
                        result[i]  = (float)m_InputData[m_position];
                        m_position = ++m_position % m_InputData.Length;
                    }
                }

                return(result);
            }
Ejemplo n.º 2
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();
                }
            }