//String lastCompleteResult = "";
        void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            float  confidence = e.Result.Confidence;
            string textResult = e.Result.Text;
            //string semantics = e.Result.Semantics.Value + "";
            string grammarName   = e.Result.Grammar.Name;
            string ruleName      = e.Result.Grammar.RuleName;
            double audioDuration = -1;

            if (e.Result.Audio != null)
            {
                audioDuration = e.Result.Audio.Duration.TotalMilliseconds;
            }
            //string phonetic = "";
            //foreach (RecognizedWordUnit unit in e.Result.Words)
            //{
            //    phonetic += unit.Pronunciation + " ";
            //}
            //Debug.WriteLine(textResult + "[" + phonetic.TrimEnd() + "]");

            Debug.WriteLine("audio duration=" + audioDuration);
            KeyValuePair <string, SemanticValue>[] kvp = e.Result.Semantics.ToArray();


            string fileP   = null;
            string relPath = null;

            //only write audio file when given path is not null
            if (saveAudioPath != null)
            {
                string indexerStr = waveFileNameIndexer + "";
                while (indexerStr.Length < 4)
                {
                    indexerStr = "0" + indexerStr;
                }

                fileP = saveAudioPath + "\\" + indexerStr + ".wav";

                relPath = EBookUtil.convertAudioToRelativePath(@fileP);
            }



            //AbstractEBookEvent.raise(new RecognitionResultEvent(confidence, textResult, false,
            //    kvp,grammarName,ruleName,audioDuration,@fileP));
            ActivityExecutor.add(new InternalSpeechRecognitionResultActivity(confidence, textResult, false,
                                                                             kvp, grammarName, ruleName, audioDuration, relPath));

            //only write audio file when given path is not null
            if (fileP != null)
            {
                //write audio to file
                FileStream stream = new FileStream(fileP, FileMode.Create);

                e.Result.Audio.WriteToWaveStream(stream);
                stream.Flush();
                stream.Close();
                unconfirmSaveAudioList.Add(fileP);
                Trace.WriteLine("write to file " + fileP);
                waveFileNameIndexer++;
            }
            String timeStamp = GetTimestamp(DateTime.Now);

            string text = "\n" + e.Result.Confidence + "\t" + e.Result.Text + "(complete)\t\t" +
                          e.Result.Semantics.Value + "\t" + e.Result.Grammar.Name + "\t" + timeStamp;

            Trace.WriteLine(text);
        }
예제 #2
0
        public void generateSynthesisData(Story story)
        {
            saveData = true;
            foreach (InstalledVoice iv in synthesizer.GetInstalledVoices())
            {
                string voiceName = iv.VoiceInfo.Name;
                Debug.WriteLine("installed voice :" + voiceName);
                synthesizer.SelectVoice(voiceName);
                string path = story.getFullPath() + "\\" + EBookInteractiveSystem.voice_dir + "\\" + voiceName.Replace(" ", "_");
                Directory.CreateDirectory(path);
                int  index = 0;
                Page p     = story.GetFirstPage();

                logResultFile = path + "\\" + StoryLoggingDevice.logFileName;

                if (File.Exists(logResultFile))
                {
                    Debug.WriteLine("skip gnerating synthesis data, data found in " + path);
                    return;
                }
                startTime = (long)LogPlayer.GetUnixTimestamp();

                while (p != null)
                {
                    //reset startIndex when start a new page
                    startIndex = 0;

                    List <string[]> textArray = p.GetListOfTextArray();
                    string          whole     = "";
                    foreach (string[] text in textArray)
                    {
                        if (text != null)
                        {
                            foreach (string ea in text)
                            {
                                whole += ea + " ";
                            }
                        }
                    }
                    string[]      separator = { "!", "?", "." };
                    string[]      sp        = whole.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
                    List <string> sentences = new List <string>();
                    string        tmpStr    = "";
                    foreach (string ea in sp)
                    {
                        tmpStr += ea + " ";
                        foreach (string punc in separator)
                        {
                            if (ea.Contains(punc))
                            {
                                sentences.Add(tmpStr.TrimEnd());
                                tmpStr = "";
                                break;
                            }
                        }
                    }
                    foreach (string ea in sentences)
                    {
                        Debug.WriteLine("generating tts for:" + ea);
                        string audioPath = path + "\\" + getPrefix(index) + ".wav";
                        synthesizer.SetOutputToWaveFile(@audioPath);

                        PromptBuilder pb = new PromptBuilder();
                        synthesizer.Rate = synthesisSpeakRate;
                        prompt           = "";
                        writeToFile("start::" + startTime + "\n");
                        startTime += 10;
                        synthesizer.Speak(ea);
                        index++;
                        startTime += (totalSentenceDuration);

                        string data = "time::" + startTime + "|confidence::0.99|textResult::" + prompt.Trim() +
                                      "|isHypothesis::False|key::startIndex|value::" + startIndex + "|grammarName::|ruleName::index_" +
                                      startIndex + "|duration::" + totalSentenceDuration + "|wavePath::" + EBookUtil.convertAudioToRelativePath(@audioPath) + "\n";
                        writeToFile(data);

                        string[] tmp = ea.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
                        startIndex += tmp.Length;

                        startTime += 1000;//one second pause
                    }
                    writeToFile("finishPage\n");
                    p = story.GetNextPage();
                }
            }
            saveData = false;
        }