我尝试制作一个简单的wpf程序,该程序使用 System.Speech.Synthesis 中的 SpeechSynthesizer 到语音,实现文本转语音,语音转文本。目前只是一个初级的用法。
主要代码如下:

前台代码

<Window x:Class="SoundToText.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="795.4" Loaded="Window_Loaded_1" WindowStyle="ToolWindow" WindowStartupLocation="CenterScreen"><Grid Margin="0,0,3.2,-0.2"><Button x:Name="ReadBT" Content="朗读" HorizontalAlignment="Left" Margin="670,24,0,0" VerticalAlignment="Top" Width="82" Click="ReadBT_Click" Height="46"/><Button x:Name="ConvertBT" Content="保存" HorizontalAlignment="Left" Margin="670,77,0,0" VerticalAlignment="Top" Width="82" Click="ConvertBT_Click" Height="46"/><Button x:Name="WavTTBT" Content="语音转文字" HorizontalAlignment="Left" Margin="677,184,0,0" VerticalAlignment="Top" Width="75" Click="WavTTBT_Click" Height="116"/><TextBox x:Name="InText" HorizontalAlignment="Left" Height="63" Margin="48,24,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="617"/><GroupBox Header="文字转语音" HorizontalAlignment="Right" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Width="752" Height="137" Margin="0,0,10,0"></GroupBox><Label Content="输
入
文
字" HorizontalAlignment="Left" Height="85" VerticalAlignment="Top" Width="24" Margin="19,19,0,0"/><Label Content="保存路径" HorizontalAlignment="Left" Margin="19,97,0,0" VerticalAlignment="Top"/><TextBox x:Name="savepathTXT" HorizontalAlignment="Left" Height="23" Margin="82,97,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="495"/><Button x:Name="SelectPathBT" Content="选择" HorizontalAlignment="Left" Margin="590,95,0,0" VerticalAlignment="Top" Width="75" Height="24" Click="SelectPathBT_Click"/><TextBox x:Name="SelectSoundTXT" HorizontalAlignment="Left" Height="23" Margin="82,184,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="495"/><Button x:Name="SelectSoundBT" Content="选择" HorizontalAlignment="Left" Margin="590,184,0,0" VerticalAlignment="Top" Width="75" Height="24" Click="SelectSoundBT_Click"/><GroupBox Header="语音转文字" HorizontalAlignment="Right" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Width="752" Height="157" Margin="0,154,9.6,0"/><Label Content="选择语音" HorizontalAlignment="Left" Margin="24,181,0,0" VerticalAlignment="Top"/><TextBox x:Name="OutTXT" HorizontalAlignment="Left" Height="85" Margin="53,215,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="612"/><Label Content="输
出
文
字" HorizontalAlignment="Left" Height="85" VerticalAlignment="Top" Width="24" Margin="24,215,0,0"/><Rectangle Fill="#B2090909" HorizontalAlignment="Left" Height="3" Margin="0,142,-9.4,0" Stroke="#00000000" VerticalAlignment="Top" Width="795"/></Grid>
</Window>

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace SoundToText
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void ConvertBT_Click(object sender, RoutedEventArgs e){if (InText.Text.Trim()==""){System.Windows.Forms.MessageBox.Show("请输入要转换的文字");return;}ConvertBT.IsEnabled = false;ConvertBT.Content = "正在转换";            SpeechSynthesizer voice = new SpeechSynthesizer();   //创建语音实例voice.Rate = -1; //设置语速,[-10,10]voice.Volume = 100; //设置音量,[0,100]
//            var z = voice.GetInstalledVoices();voice.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Child);voice.SetOutputToDefaultAudioDevice();string savepath = @savepathTXT.Text +"\\"+ DateTime.Now.ToString("yyyyMMddHHddss") + ".wav";voice.SetOutputToWaveFile(savepath);// voice.SelectVoice = "";//voice.SpeakAsync("Hellow Word");  //播放指定的字符串,这是异步朗读string content = InText.Text.Trim();//下面的代码为一些SpeechSynthesizer的属性,看实际情况是否需要使用voice.SpeakAsyncCancelAll();  //取消朗读voice.SpeakAsync(content);  //同步朗读//voice.StateChanged += schanged;voice.SpeakCompleted += speech_SpeakCompleted;voice.Dispose();  //释放所有语音资源}private void ReadBT_Click(object sender, RoutedEventArgs e){if (InText.Text.Trim() == ""){System.Windows.Forms.MessageBox.Show("请输入要转换的文字");return;}SpeechSynthesizer voice = new SpeechSynthesizer();   //创建语音实例voice.Rate = -1; //设置语速,[-10,10]voice.Volume = 100; //设置音量,[0,100]//            var z = voice.GetInstalledVoices();voice.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Child);voice.SetOutputToDefaultAudioDevice();// voice.SelectVoice = "";//voice.SpeakAsync("Hellow Word");  //播放指定的字符串,这是异步朗读string content = InText.Text.Trim();//下面的代码为一些SpeechSynthesizer的属性,看实际情况是否需要使用// voice.SpeakAsyncCancelAll();  //取消朗读voice.SpeakAsync(content);  //同步朗读//voice.StateChanged += schanged;//voice.SpeakCompleted += speech_SpeakCompleted;voice.Pause();  //暂停朗读voice.Resume(); //继续朗读voice.Dispose();  //释放所有语音资源}private void schanged(object sender, StateChangedEventArgs e){// throw new NotImplementedException();}private void speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e){System.Windows.Forms.MessageBox.Show("转换完成");ConvertBT.Content = "保存";ConvertBT.IsEnabled = true;}private void WavTTBT_Click(object sender, RoutedEventArgs e){if (SelectSoundTXT.Text.Trim() == ""){System.Windows.Forms.MessageBox.Show("请选择要转换的声音文件");return;}try{WavTTBT.Content = "正在转换";System.Speech.Recognition.SpeechRecognitionEngine sre = new System.Speech.Recognition.SpeechRecognitionEngine();sre.LoadGrammar(new System.Speech.Recognition.DictationGrammar());sre.SetInputToWaveFile(SelectSoundTXT.Text.Trim());string res = null;StringBuilder sb = new StringBuilder();do{try{res = sre.Recognize().Text;}catch (Exception){res = null;}sb.Append(res);} while (res != null);sre.Dispose();OutTXT.Text = sb.ToString();WavTTBT.Content = "语音转文字";}catch (Exception ex){//return (e.Message);}}private void Window_Loaded_1(object sender, RoutedEventArgs e){string outPathOld = Properties.Settings.Default.FolderSelectPath;if (outPathOld == ""){savepathTXT.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);}else{savepathTXT.Text = outPathOld;}}private void SelectPathBT_Click(object sender, RoutedEventArgs e){FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();            if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK){string outpath = folderBrowserDialog.SelectedPath;if (outpath.Substring(outpath.Length-1,1)==@"\"){savepathTXT.Text =  folderBrowserDialog.SelectedPath;}else{savepathTXT.Text = folderBrowserDialog.SelectedPath + @"\";}Properties.Settings.Default["FolderSelectPath"] = InText.Text.ToString();Properties.Settings.Default.Save();Properties.Settings.Default.Reload();}}private void SelectSoundBT_Click(object sender, RoutedEventArgs e){OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.Title = "选择文件";openFileDialog.Filter = "音频文件(*.wav)|*.wav";           if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK){SelectSoundTXT.Text = openFileDialog.FileName;}}}
}

下载源代码

【c#】文本转语音,语音转文本相关推荐

  1. c# 实现文本读取,语音报警功能

    c# 实现文本读取,语音报警功能 在测试软件功能过程中,由于需要操作硬件进行变位,但是操作完成后在去看监控软件有时间延迟,除非2个人配合,就想实现告警进行语音播报功能 实现方法1 [DllImport ...

  2. .NET 将文本转换成语音 (转)

    用过金山词霸的都知道金山词霸里有个功能能够将单词按其读音读出来.也许会有人认为它是事先将所有的单词的读音文件存储在数据库里,然后事需要的时候再调用.其实事实上并不是这样,我们有更好的解决办法,MS就专 ...

  3. 长语音识别_长文本语音识别_语音 识别 - 云+社区 - 腾讯云

    广告关闭 腾讯云双11爆品提前享,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高满返5000元! 录音文件识别请求,数据结构,android sdk,ios sdk,自学习模型,使 ...

  4. google sdk speech-to-text(谷歌语音转文本、谷歌语音转字幕)

    google sdk speech-to-text 同步识别(REST 和 gRPC)将音频数据发送到 Speech-to-Text API,对该数据执行识别,并在所有音频处理完毕后返回结果.同步识别 ...

  5. 如何把文本文字转换为语音

    现在很多的人在到达较高的年纪后,眼镜就开始有点老花了.每天长时间阅读报纸.书籍就会导致眼镜十分的疲惫,甚至连手机上的文字看起来都变得模糊.另外还有很多视力不好的小伙伴,在长期用眼后也会感觉到眩晕.疲劳 ...

  6. 基于音频和文本的多模态语音情感识别(一篇极好的论文,值得一看哦!)

    基于音频和文本的多模态语音情感识别 语音情感识别是一项具有挑战性的任务,在构建性能良好的分类器时,广泛依赖于使用音频功能的模型.本文提出了一种新的深度双循环编码器模型,该模型同时利用文本数据和音频信号 ...

  7. 【VC++技术杂谈004】使用微软TTS语音引擎实现文本朗读

    本文主要介绍如何使用微软TTS语音引擎实现文本朗读,以及生成wav格式的声音文件. 1.语音引擎及语音库的安装 TTS(Text-To-Speech)是指文本语音的简称,即通过TTS引擎把文本转化为语 ...

  8. 在微信的视频通话中将语音转成文字并显示在通话界面中,可以使用语音识别技术,将语音转换成文本,再通过编程技巧将文本显示在通话界面中。实现方法...

    在微信的视频通话中将语音转成文字并显示在通话界面中,可以使用以下步骤进行实现: 使用语音识别技术将语音转换成文本.这可以使用现有的语音识别API,如百度语音识别API等. 通过编程技巧将文本显示在通话 ...

  9. 在微信的视频通话中将语音转成文字并显示在通话界面中,可以使用语音识别技术,将语音转换成文本,再通过编程技巧将文本显示在通话界面中。在手机上实现方法代码...

    在微信的视频通话中将语音转成文字并显示在通话界面中可以通过以下步骤实现: 使用微信的语音识别 API 识别语音并将其转换为文本 使用编程技巧将文本显示在通话界面中 在手机上使用相应的编程语言(如Jav ...

  10. python输出文本对齐_speech-aligner,是一个从“人声语音”及其“语言文本”,产生音素级别时间对齐标注的工具...

    speech-aligner Chinese readme: speech-aligner,是一个从"人声语音"及其"语言文本",产生音素级别时间对齐标注的工具 ...

最新文章

  1. Openfiler的配置
  2. python与excel表格-Python操作 Excel表格
  3. combobox的联动练习
  4. Java中什么时候throws_何时在Java方法声明中使用throws?
  5. mysql高级操作_MySQL数据库的高级操作
  6. ubuntu10.04 NFS服务
  7. python函数手册(1)
  8. 第六届全国大学生GIS应用技能大赛开发题答案(非官方)
  9. OpenCore黑苹果引导配置说明-基于OpenCore-0.7.0-06-08正式版
  10. 计算机二级报考哪个科目比较好?
  11. 慕有轶:看不清的方向,你还在盲目操作,犹如飞蛾扑火!
  12. PingTunnel隧道搭建
  13. 区块链智能合约及形式化验证平台 VaaS 讲解
  14. 8瓶药水3只小白鼠问题
  15. cortex系列处理器排行_谁有ARM Cortex-A系列(A53、A57、A73等)处理器性能分类与对比??...
  16. mysql 设置 utc_关于时间:MySQL应该将其时区设置为UTC吗?
  17. 香港中文大学计算机专业学费,香港中文大学研究生专业学费是多少?
  18. Python打印九九乘法口诀表
  19. VSCode调试C/C++项目
  20. 222222222222

热门文章

  1. 零基础入门网络安全最直线距离的学习路线
  2. WPS/WORD论文格式调整方法
  3. FPGA数字信号处理(1)- AM调制的FPGA实现
  4. [ECCV2018]Generating 3D faces using Convolutional Mesh Autoencoders
  5. Mutual Information Neural Estimation梳理
  6. 法正 (21) :端午
  7. 绘画入门经典教程——如果你想, 一切皆有可能!
  8. UBUNTU环境下编译的openwrt
  9. 南京邮电大学操作系统实验二:线程的互斥与同步
  10. Python之ARP协议探测MAC地址