示例#1
0
        private void speakNative(Model.Wrapper wrapper)
        {
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
            SpVoice speechSynthesizer = new SpVoice();

            SpObjectToken voice = getSapiVoice(wrapper.Voice);

            if (voice != null)
            {
                speechSynthesizer.Voice = voice;
            }

            speechSynthesizer.Volume = calculateVolume(wrapper.Volume);
            speechSynthesizer.Rate   = calculateRate(wrapper.Rate);

            System.Threading.Thread worker = new System.Threading.Thread(() => speechSynthesizer.Speak(prepareText(wrapper), wrapper.ForceSSML && !Speaker.isAutoClearTags ? SpeechVoiceSpeakFlags.SVSFIsXML : SpeechVoiceSpeakFlags.SVSFIsNotXML));
            worker.Start();

            do
            {
                System.Threading.Thread.Sleep(50);
            } while (worker.IsAlive && !silence);

            if (silence)
            {
                speechSynthesizer.Skip("Sentence", int.MaxValue);
            }

            //speechSynthesizer.WaitUntilDone(int.MaxValue);
#endif
        }
示例#2
0
        private void speakToFile(Model.Wrapper wrapper, string outputFile)
        {
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
            SpVoice speechSynthesizer = new SpVoice();

            SpFileStream spFile = new SpFileStream();

            spFile.Format.Type = SpeechAudioFormatType.SAFT48kHz16BitStereo;
            spFile.Open(outputFile, SpeechStreamFileMode.SSFMCreateForWrite);

            SpObjectToken voice = getSapiVoice(wrapper.Voice);

            if (voice != null)
            {
                speechSynthesizer.Voice = voice;
            }

            speechSynthesizer.AudioOutputStream = spFile;
            speechSynthesizer.Volume            = calculateVolume(wrapper.Volume);
            speechSynthesizer.Rate = calculateRate(wrapper.Rate);
            speechSynthesizer.Speak(prepareText(wrapper), wrapper.ForceSSML && !Speaker.isAutoClearTags ? SpeechVoiceSpeakFlags.SVSFIsXML : SpeechVoiceSpeakFlags.SVSFIsNotXML);

            speechSynthesizer.WaitUntilDone(int.MaxValue);

            spFile.Close();
#endif
        }
示例#3
0
        private void Initialize()
        {
            try
            {
                voice            = new SpVoiceClass();
                voice.EndStream += Voice_EndStream;

                ISpeechObjectTokens objTokens = voice.GetVoices("", "");
                const string        useVoice  = "ScanSoft Mei-Ling_Full_22kHz";
                int useIndex = -1;
                for (int i = 0; i < objTokens.Count; i++)
                {
                    SpObjectToken sot = objTokens.Item(i);
                    if (sot.GetDescription(0) == useVoice)
                    {
                        useIndex = i;
                        break;
                    }
                }
                if (useIndex == -1)
                {
                    useIndex = 0;
                }
                voice.Voice = objTokens.Item(useIndex);
            }
            catch (Exception e)
            {
                MessageBox.Show("Error:" + e.Message);
            }
        }
示例#4
0
        public void Save(string filePath, string text)
        {
            try
            {
                SpObjectToken backupSapi = null;
                SpFileStream  ss         = new SpFileStream();
                ss.Open(filePath, SpeechStreamFileMode.SSFMCreateForWrite);
                sapi.AudioOutputStream = ss;

                Thread t = new Thread(() => {
                    backupSapi  = sapi.Voice;
                    sapi.Voice  = SpeakerList[AvatorIdx];
                    sapi.Rate   = Speed;
                    sapi.Volume = Volume;
                    sapi.Speak(text);
                    sapi.Voice = backupSapi;
                });
                t.SetApartmentState(ApartmentState.STA);
                t.Start();
                t.Join();
                ss.Close();
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("保存処理で落ちたっす。{0}", e.Message));
            }
        }
示例#5
0
        public void Talk(string text, bool asyncFlag = false)
        {
            try
            {
                SpObjectToken backupSapi = null;

                Thread t = new Thread(() =>
                {
                    backupSapi       = sapi.Voice;
                    sapi.AudioOutput = OutputDeviceList[DevIdx];
                    sapi.Voice       = SpeakerList[AvatorIdx];
                    sapi.Rate        = Speed;
                    sapi.Volume      = Volume;
                    sapi.Speak(text);
                    sapi.Voice = backupSapi;
                });
                t.SetApartmentState(ApartmentState.STA);
                t.Start();
                if (!asyncFlag)
                {
                    t.Join();
                }
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("発声処理で落ちたっす。{0}", e.Message));
            }
        }
示例#6
0
 /// <summary>Creates a speaker with the specified voice</summary>
 /// <param name="Name"></param>
 /// <param name="Color"></param>
 /// <param name="Voice"></param>
 public Actor(string Name, ConsoleColor Color, SpObjectToken Voice)
 {
     SAPI = new SpVoice {
         Voice = Voice
     };
     this.Name  = Name;
     this.Color = Color;
 }
示例#7
0
        //マイクから読み取るため、マイク用のデバイスを指定する
        private SpObjectToken CreateMicrofon()
        {
            SpeechLib.SpObjectTokenCategory objAudioTokenCategory = new SpeechLib.SpObjectTokenCategory();
            objAudioTokenCategory.SetId(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech Server\v11.0\AudioInput", false);
            SpObjectToken objAudioToken = new SpObjectToken();

            objAudioToken.SetId(objAudioTokenCategory.Default, @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech Server\v11.0\AudioInput", false);
            //return null;
            return(objAudioToken);
        }
示例#8
0
        private SpObjectToken CreateMicrofon()
        {
            SpObjectTokenCategory objAudioTokenCategory = new SpObjectTokenCategory();

            objAudioTokenCategory.SetId(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\AudioInput");
            SpObjectToken objAudioToken = new SpObjectToken();

            objAudioToken.SetId(objAudioTokenCategory.Default, @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\AudioInput");
            return(objAudioToken);
        }
示例#9
0
        private void Voice_VoiceChange(int StreamNumber, object StreamPosition, SpObjectToken VoiceObjectToken)
        {
            int index = VoiceCombo.FindStringExact(VoiceObjectToken.GetDescription(0));

            if (index != -1)
            {
                VoiceCombo.SelectedIndex = index;
            }

            AddEventMessage("VoiceChange: Voice = " + VoiceObjectToken.GetDescription(0));
        }
示例#10
0
        public void initialize_audio()
        {
            SpObjectTokenCategory category = new SpObjectTokenCategory();

            category.SetId(SpeechStringConstants.SpeechCategoryAudioIn, false);

            SpObjectToken token = new SpObjectToken();

            token.SetId(category.EnumerateTokens().Item(0).Id, "", false);
            //token.SetId(category.Default, "", false);
            context.Recognizer.AudioInput = token;
        }
示例#11
0
        public override int runCommand(XdmNode node)
        {
            Speech sp = Speech.instance;
            //SpeechSynthesizer sp = new SpeechSynthesizer();

            /*
             * Store values
             */
            SpObjectToken prevVoice  = sp.speech.Voice;
            int           prevVolume = sp.speech.Volume;
            int           prevRate   = sp.speech.Rate;


            foreach (string key in getProperties().Keys)
            {
                string value = getProperties()[key];
                switch (key)
                {
                case "voice":
                    ISpeechObjectToken voice = sp.getVoice(value);
                    if (voice != null)
                    {
                        sp.speech.Voice = (SpObjectToken)voice;
                    }
                    break;

                case "volume":
                    int volume = Int32.Parse(value);
                    if (volume > -1 && volume < 101)
                    {
                        sp.speech.Volume = volume;
                    }
                    break;

                case "rate":
                    int rate = Int32.Parse(value);
                    if (rate > -11 && rate < 11)
                    {
                        sp.speech.Rate = rate;
                    }
                    break;
                }
            }
            runChilds(node);
            if (commands.Count > 0)
            {
                sp.speech.Voice  = prevVoice;
                sp.speech.Volume = prevVolume;
                sp.speech.Rate   = prevRate;
            }
            return(0);
        }
示例#12
0
        static void Main(string[] args)
        {
            try
            {
                SpVoice sapi = new SpVoice();
                SpObjectTokenCategory sapiCat = new SpObjectTokenCategory();
                Dictionary <string, SpObjectToken> TokerPool = new Dictionary <string, SpObjectToken>();
                SpFileStream Sfs = null;

                // See https://qiita.com/7shi/items/7781516d6746e29c03b4
                sapiCat.SetId(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech_OneCore\Voices", false);

                // Narrator voices
                foreach (SpObjectToken token in sapiCat.EnumerateTokens())
                {
                    if (!TokerPool.ContainsKey(token.GetAttribute("name")))
                    {
                        TokerPool.Add(token.GetAttribute("name"), token);
                    }
                }

                // SAPI voices
                foreach (SpObjectToken token in sapi.GetVoices("", ""))
                {
                    if (!TokerPool.ContainsKey(token.GetAttribute("name")))
                    {
                        TokerPool.Add(token.GetAttribute("name"), token);
                    }
                }

                SpeakerList = TokerPool.Select((val, idx) => new { Key = idx, Value = val.Value }).ToDictionary(s => s.Key, s => s.Value);

                if (!Opt(args))
                {
                    return;
                }

                if (file != "")
                {
                    Sfs = new SpFileStream();
                    Sfs.Open(file, SpeechStreamFileMode.SSFMCreateForWrite);
                    sapi.AudioOutputStream = Sfs;
                }

                Thread t = new Thread(() => {
                    SpObjectToken backupSapi = sapi.Voice;
                    sapi.Voice  = SpeakerList[AvatorIdx];
                    sapi.Rate   = Speed;
                    sapi.Volume = Volume;
                    sapi.Speak(text);
                    sapi.Voice = backupSapi;
                });
                t.SetApartmentState(ApartmentState.STA);
                t.Start();
                t.Join();

                if (file != "")
                {
                    Sfs.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("err:{0},{1}", e.Message, e.InnerException == null ? "" : e.InnerException.Message);
            }
        }
示例#13
0
        //button5生成读音
        private void button5_Click(object sender, RoutedEventArgs e)
        {
            //数据库全部读出
            WordLogic[] wordList = WordLogic.FindAll();

            SpFileStream          SpFileStream = new SpFileStream();
            String                fileName;
            SpeechVoiceSpeakFlags SpFlags    = SpeechVoiceSpeakFlags.SVSFlagsAsync;
            SpeechStreamFileMode  SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
            SpVoice               voice      = new SpVoice();

            // prepare voice
            SpObjectTokenCategory aotc = new SpObjectTokenCategory();

            aotc.SetId(SpeechLib.SpeechStringConstants.SpeechCategoryVoices);

            foreach (ISpeechObjectToken token in aotc.EnumerateTokens())
            {
                if (token.GetDescription() == "VW Julie")
                {
                    englishToken = (SpObjectToken)token;
                }
                else if (token.GetDescription() == "VW Hui")
                {
                    chineseToken = (SpObjectToken)token;
                }
            }

            voice.Voice = englishToken;
            voice.Rate  = -4;

            String outFolderPath = Directory.GetParent("../../TestCases/") + @"\单词音频\";

            if (!Directory.Exists(outFolderPath))
            {
                Directory.CreateDirectory(outFolderPath);
            }

            for (int i = 0; i < wordList.Length; i++)
            {
                String word = wordList[i].word;

                if (word != null)
                {
                    word = word.Trim();
                }

                if (String.IsNullOrEmpty(word))
                {
                    // 遇到无效内容,退出
                    continue;
                }
                word = convert(word);

                fileName = outFolderPath + word + ".wav";
                SpFileStream.Open(fileName, SpFileMode, false);
                voice.AudioOutputStream = SpFileStream;
                voice.Speak(word, SpFlags);
                voice.WaitUntilDone(Timeout.Infinite);
                SpFileStream.Close();
            }


            MessageBox.Show("音频生成完毕!");
        }
示例#14
0
        public SpeechService(ServiceCreationInfo info)
            : base("speech", info)
        {
            mVoice = new SpVoice();

            // Select voice
            string voiceName = null;

            try
            {
                voiceName = info.Configuration.Voice;
            }
            catch (RuntimeBinderException) {}

            if (!string.IsNullOrEmpty(voiceName))
            {
                SpObjectToken voiceToken = null;

                CultureInfo culture = new CultureInfo("en-US");
                foreach (var voice in mVoice.GetVoices())
                {
                    var token = voice as SpObjectToken;
                    if (token == null)
                    {
                        continue;
                    }

                    if (culture.CompareInfo.IndexOf(token.Id, voiceName, CompareOptions.IgnoreCase) < 0)
                    {
                        continue;
                    }

                    voiceToken = token;
                }

                if (voiceToken != null)
                {
                    mVoice.Voice = voiceToken;
                }
            }

            // Select output. Why isn't this default any longer?
            var      enumerator = new MMDeviceEnumerator();
            MMDevice endpoint   = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);

            if (endpoint != null)
            {
                foreach (var output in mVoice.GetAudioOutputs())
                {
                    var token = output as SpObjectToken;
                    if (token == null)
                    {
                        continue;
                    }

                    if (token.Id.IndexOf(endpoint.ID) < 0)
                    {
                        continue;
                    }

                    mVoice.AudioOutput = token;
                    break;
                }
            }

            mVoiceCommands = new Dictionary <string, DeviceBase.VoiceCommand>();

            mInput = new AudioInput();

            mRecognizer = new SpeechRecognitionEngine();
            mRecognizer.SpeechRecognized        += new EventHandler <SpeechRecognizedEventArgs>(OnSpeechRecognized);
            mRecognizer.RecognizerUpdateReached += new EventHandler <RecognizerUpdateReachedEventArgs>(OnUpdateRecognizer);
            mRecognizer.RecognizeCompleted      += new EventHandler <RecognizeCompletedEventArgs>(OnRecognizeCompleted);

            var grammar = new Grammar(new GrammarBuilder(new Choices(new string[] { "computer" })));

            mRecognizer.LoadGrammar(grammar);

            var speechFormat = new SpeechAudioFormatInfo(44100, AudioBitsPerSample.Sixteen, AudioChannel.Mono);

            mRecognizer.SetInputToAudioStream(mInput.mStream, speechFormat);

            mRecognizer.RecognizeAsync(RecognizeMode.Multiple);
        }