非常擬真的文字轉聲音服務(TTS)

Azure Speech服務除了可以將語音轉為文字,當然也可以從文字輸出語音。

我們看執行的結果:(有聲音)

上面這個影片當中,你會同時看到文字轉語音和語音轉文字,一開始的時候,我們讓電腦說出:

請透過語音下達指令…, 直到說 ‘我要離開’

執行的是底下這段程式碼:

await Speak(speechConfig, "請透過語音下達指令..., 直到說 '我要離開'");

而Speak方法的內容如下:

        async static Task Speak(SpeechConfig speechConfig, string text)
        {
            // Configure speech synthesis
            speechConfig.SpeechSynthesisLanguage = "zh-TW";
            speechConfig.SpeechSynthesisVoiceName = "zh-TW-HsiaoChenNeural"; //女生 
            speechConfig.SpeechSynthesisVoiceName = "zh-TW-YunJheNeural"; //男生 
            using SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer(speechConfig);

            // Synthesize spoken output
            SpeechSynthesisResult speak = await speechSynthesizer.SpeakTextAsync(text);
            if (speak.Reason != ResultReason.SynthesizingAudioCompleted)
            {
                Console.WriteLine(speak.Reason);
            }
            // Print the response
            Console.WriteLine("\n說了:" + text);
        }

關鍵是 SpeechSynthesizer 物件,我們可以透過該物件的SpeakTextAsync()方法,把要說的文字傳入,即可直接從標準的喇叭設備,來進行語音輸出。

要採用哪一種語音,可以透過 speechConfig的SpeechSynthesisVoiceName屬性來設定:

speechConfig.SpeechSynthesisVoiceName = "zh-TW-YunJheNeural"

完整的語音清單可以參考底下文件:
https://github.com/MicrosoftDocs/azure-docs.zh-tw/blob/master/articles/cognitive-services/Speech-Service/language-support.md

你會發現,透過C# SDK,要輸出類似真人的語音,也是非常簡單的。

完整的程式碼在GitHub, 可以透過底下方式取得:

git clone https://github.com/isdaviddong/STT_example.git

留言

這個網誌中的熱門文章

在POC或迷你專案中使用 LiteDB

使用 Airtable 在小型需求上取代傳統資料庫

專業的價值...

使用Qdrant向量資料庫實作語意相似度比對

精彩(且驚人)的Semantic Kernel入門範例