C#TTS使用SpeechLib语音合成NAudio选择播放输出设备

(1)没在Unity3d测试,但是应该可用。

(2)之前在Unity遇到过调用如下代码会崩溃的情况。不显示指定,删掉,默认就好了

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

(3)SpeechLib没有语音,有可能是因为盗版系统语音模块被精简掉了,需要安装语音模块和语音库(Lily、Huihui等)

C#TTS使用SpeechLib语音合成NAudio选择播放输出设备_第1张图片

(4).Net自带的System.Speech,实测Unity5.6不能直接使用,找不到关联的程序集或com

标题:

1

C#TTS使用SpeechLib语音合成NAudio选择播放输出设备_第2张图片

2

C#TTS使用SpeechLib语音合成NAudio选择播放输出设备_第3张图片

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();
                }
            }
        }
    }
}

4

Unity中SpMemoryStreamClass.GetData()返回都是null,不明原因,换了一种方式

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

class SpeechDevice
{
    public void AysncPlay(string text, int device = -1)
    {
        d = device;
        new Thread(new ParameterizedThreadStart(Play)).Start(text);
        //Play2(inputField.text);
        //Play(inputField.text);
    }
    int d = 0;
    bool playing = false;
    void Play(object o)
    {
        if (playing)
        {
            return;
        }
        playing = true;
        string s = o as string;
        SpVoiceClass sp = new SpVoiceClass();
        using (var waveOut = new WaveOut())
        {
            if (d < WaveOut.DeviceCount && d >= 0)
            {
                waveOut.DeviceNumber = d;
            }
            SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;

            SpFileStream SpFileStream = new SpFileStream();
            if (!Directory.Exists("Temp"))
            {
                Directory.CreateDirectory("Temp");
            }
            string name = "Temp\\call" + DateTime.Now.Ticks + ".wav";
            SpFileStream.Open(name, SpFileMode, false);

            sp.AudioOutputStream = SpFileStream;
            sp.Speak(s, SpeechVoiceSpeakFlags.SVSFlagsAsync);
            sp.WaitUntilDone(5000);
            SpFileStream.Close();
            using (AudioFileReader audioFileReader = new AudioFileReader(name))
            {
                waveOut.Init(audioFileReader);
                waveOut.Play();

                while (waveOut.PlaybackState == PlaybackState.Playing)
                {
                    Thread.Sleep(30);
                }
            }
            if (File.Exists(name))
            {
                File.Delete(name);
            }
        }
        playing = false;
    }
}

 

你可能感兴趣的:(Unity,C#,TTS)