使用System.Speech.Synthesis在C#中实现文本到语音转换

目录

介绍

使用System.Speech.Synthesis在C#中实现文本到语音转换

先决条件

实现

代码大纲


介绍

此提示说明如何使用System.Speech.Synthesis命名空间在C#中实现文本到语音转换(TTS)应用程序。TTS技术有许多实际用例,例如辅助功能工具和支持语音的应用程序。按照提供的分步指南,读者将能够创建一个简单的控制台应用程序,该应用程序从文本合成语音。本文还提供了所用代码的说明,并提供了探索该SpeechSynthesizer类的更高级功能的建议。

使用System.Speech.SynthesisC#中实现文本到语音转换

文本到语音转换(TTS)技术已经存在了一段时间,并且已经发现了许多用例,例如语言学习、视障人士的辅助功能工具以及支持语音的应用程序。在本文中,我们将探讨如何使用C#System.Speech.Synthesis命名空间实现简单的TTS应用程序。

先决条件

在开始之前,您需要在计算机上安装以下内容:

  1. .NET Framework 4.6.1或更高版本
  2. Visual Studio 2017或更高版本

实现

我们将使用System.Speech.Synthesis命名空间,它提供了用于从文本合成语音的类。按照以下步骤在C#中创建控制台应用程序并实现TTS

  1. 打开Visual Studio并创建一个新的控制台应用程序项目。
  2. 添加对System.Speech程序集的引用。在解决方案资源管理器中右键单击该项目,选择“添加引用”,然后从程序集列表中进行选择System.Speech。
  3. Program.cs文件中,添加以下代码:
using System;
using System.IO;
using System.Speech.Synthesis;

class Program
{
    static void Main(string[] args)
    {
        // Basic TTS
        SpeechSynthesizer synth = new SpeechSynthesizer();
        synth.SetOutputToDefaultAudioDevice();
        synth.Speak("Hello, world!");

        // Changing the Voice
        synth.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult);
        synth.Speak("Hello, I am a female voice!");

        // Changing the Pitch and Rate
        synth.Rate = -2;
        synth.Volume = 100;
        synth.Speak("Hello, I am speaking slower and louder!");

        // Pausing and Resuming Speech
        synth.Speak("Hello, I will pause for 3 seconds now.");
        synth.Pause();
        System.Threading.Thread.Sleep(3000); // wait for 3 seconds
        synth.Resume();
        synth.Speak("I am back!");

        // Saving Speech to a WAV File
        synth.SetOutputToWaveFile("output.wav");
        synth.Speak("Hello, I am saving my speech to a WAV file!");

        // Setting the Speech Stream
        MemoryStream stream = new MemoryStream();
        synth.SetOutputToWaveStream(stream);
        synth.Speak("Hello, I am being streamed to a memory stream!");
        byte[] speechBytes = stream.GetBuffer();

        // Changing the Voice and Pronunciation
        PromptBuilder builder = new PromptBuilder();
        builder.StartVoice(VoiceGender.Female, VoiceAge.Adult, 1);
        builder.AppendText("Hello, my name is Emily.");
        builder.StartVoice(VoiceGender.Female, VoiceAge.Teen, 2);
        builder.AppendText("I am from New York City.");
        builder.StartStyle(new PromptStyle() { Emphasis = PromptEmphasis.Strong });
        builder.AppendText("I really love chocolate!");
        builder.EndStyle();
        builder.StartStyle(new PromptStyle() { Emphasis = PromptEmphasis.Reduced });
        builder.AppendText("But I'm allergic to it...");
        builder.EndStyle();
        synth.Speak(builder);

        Console.ReadLine();
    }
}

代码大纲

1、基本TTS

创建一个SpeechSynthesizer实例并使用默认音频设备合成文本Hello, world!

2、改变声音

选择成人女性声音并使用该声音合成文本Hello, I am a female voice!

3、改变音高和速率

将语速设置为-2(较慢),将音量设置为100(较响),并合成文本Hello, I am speaking slower and louder!

4、暂停和恢复语音

合成文本Hello, I will pause for 3 seconds now.,暂停语音3秒钟,然后恢复语音并合成文本I am back!

5、将语音保存到WAV文件

SpeechSynthesizer的输出设置为名为output.wavWAV文件,并合成文本Hello, I am saving my speech to a WAV file!

6、设置语音流

SpeechSynthesizer的输出设置为内存流,合成文本Hello, I am being streamed to a memory stream!,并从内存流中获取生成的语音字节。

7、改变声音和发音

使用该PromptBuilder类创建更复杂的提示,更改提示的某些部分的语音,并向提示的某些部分添加强调和减少强调。然后使用SpeechSynthesizer合成生成的提示符。

这些代码示例演示了SpeechSynthesizer类的一些基本和高级功能,包括更改语音和音调、暂停和恢复语音以及将合成语音保存到文件或内存流。

https://www.codeproject.com/Tips/5357005/Implementing-Text-to-Speech-in-Csharp-using-System

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在C#使用Speech.Synthesis播放音频,你可以按照以下步骤进行操作: 1. 首先,确保你已经添加了命名空间`System.Speech.Synthesis`。 2. 创建一个SpeechSynthesizer对象,并设置所需的输出。 3. 使用`SetOutputToWaveFile`方法将输出设置为音频文件,并指定保存路径。例如:`sy.SetOutputToWaveFile("D:\\record.wav");` 4. 使用`Speak`方法将文本转换语音并输出。例如:`sy.Speak("大家好");` 5. 如果你之后需要将输出设置回默认的音频设备,可以使用`SetOutputToDefaultAudioDevice`方法。例如:`sy.SetOutputToDefaultAudioDevice();` 以下是一个示例代码,展示了如何在C#使用Speech.Synthesis播放音频: ```csharp using System.Speech.Synthesis; public static void PlayAudio() { SpeechSynthesizer synth = new SpeechSynthesizer(); synth.SetOutputToWaveFile("D:\\record.wav"); synth.Speak("大家好"); synth.SetOutputToDefaultAudioDevice(); } ``` 你可以根据自己的需求修改保存路径和要转换文本。希望这可以帮助到你! [2<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [System.Speech.Synthesis 保存合成语音](https://blog.csdn.net/qq_36051316/article/details/84961786)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [C#朗读语音无法引用System.Speech解决方法](https://blog.csdn.net/huanghai231/article/details/117304191)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [RPiSpeechSynthesis:Raspberry Pi上的Windows 10 IoT核心语音合成](https://download.csdn.net/download/weixin_42139042/18760047)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值