C#TTS使用SpeechLib語音合成NAudio選擇播放輸出設備

(1)沒在Unity3d測試,但是應該可用。

(2)之前在Unity遇到過調用如下代碼會崩潰的情況。不顯示指定,刪掉,默認就好了

voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);

(3)SpeechLib沒有語音,有可能是因爲盜版系統語音模塊被精簡掉了,需要安裝語音模塊和語音庫(Lily、Huihui等)

(4).Net自帶的System.Speech,實測Unity5.6不能直接使用,找不到關聯的程序集或com

標題:

1

2

3

using System;
using NAudio.Wave;
using SpeechLib;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            for (int n = 0; n < WaveOut.DeviceCount; n++)
            {
                Console.WriteLine(n + " : " + WaveOut.GetCapabilities(n).ProductName);
            }
            while (true)
            {
                int i = 0;
                do
                {
                    Console.WriteLine("選擇設備:");
                } while (!int.TryParse(Console.ReadLine(), out i) || i >= WaveOut.DeviceCount || i < 0);

                d = i;
                Console.WriteLine(WaveOut.GetCapabilities(i).ProductName);
                Console.WriteLine("輸入文字:");
                new Thread(new ParameterizedThreadStart(Play)).Start(Console.ReadLine());
            }
        }
        static int d = 0;
        static void Play(object o)
        {
            string s = o as string;
            SpVoiceClass sp = new SpVoiceClass();
            using (var waveOut = new WaveOut())
            {
                waveOut.DeviceNumber = d;
                SpMemoryStreamClass spms = new SpMemoryStreamClass();
                sp.AudioOutputStream = spms;
                sp.Speak(s, SpeechVoiceSpeakFlags.SVSFlagsAsync);
                sp.WaitUntilDone(3000);
                var data = spms.GetData() as byte[];
                SpWaveFormatEx f = spms.Format.GetWaveFormatEx();
                using (RawSourceWaveStream rsws = new RawSourceWaveStream(data, 0, data.Length, new WaveFormat(f.SamplesPerSec, f.BitsPerSample, f.Channels)))
                {
                    waveOut.Init(rsws);
                    waveOut.Play();
                }
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章