我尝试制作一个简单的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;
}
}
}
}