将您的文本或.txt文件转换为.wav格式的语音或音频文件
一个转换器,用于将用户文本或任何.txt文件转换为语音或.wav音频文件

引言
这是一个将用户的文本或.txt格式的文本文件转换为.wav格式音频文件的应用程序。该应用程序识别文本文件中的单词和标点符号,并根据相关的字母和音素进行分解。用户将获得他们在富文本框中输入的任何文本,或者他们通过相关文本框引入的任何文本文件的音频输出。
背景
我正在尝试开发一个“朋友”(一个应用程序),它能理解你的话,记住它们,并且还能像朋友一样说话。这个文本转语音转换器是该应用程序的另一个模块,通过它,该应用程序理解用户的语言并相应地重述它们(目标)。
Using the Code
/*********************************************************/
//This is a converter to convert your .txt file or your Text
//to voice or .wav file.
/*********************************************************/
<code>
using System; //Adding System namespace through keyword using.
using System.Collections.Generic; //Adding namespace Collections.Generic of System
using System.ComponentModel; //Adding namespace ComponentModel of System
using System.Data; //Adding namespace Data
using System.Drawing; //Adding namespace Drawing
using System.Text; //Adding namespace Text
using System.Windows.Forms; //Adding namespace Windows Forms
using SpeechLib; //Adding namespace/dll file SpeechLib
在这里,在上面的代码中,您会注意到的最重要区别是SpeechLib
命名空间。这是SAPI5.1的动态链接库,即Speech Application programming Interphase5.1。
namespace WindowsApplication1 //Namespace WindowApplication1 Started
{
public partial class Form1 : Form //Class Form1 is extended to Class Form (system)
{
public Form1() //Constructor for Form1
{
InitializeComponent(); //Method to initialize the Form1 Component.
}
SpFileStream spFileStream = new SpFileStream(); //declaring and Initializing
//fileStream obj
SpeechStreamFileMode spFileMode =
SpeechStreamFileMode.SSFMCreateForWrite; //declaring fileStreamMode as to
//Create or Write
SpObjectTokenClass my_SpObjToken = new SpObjectTokenClass(); //declaring and
//initializing SpObject Token Class
SpeechVoiceSpeakFlags my_Spflag =
SpeechVoiceSpeakFlags.SVSFlagsAsync; // declaring and initializing
//Speech Voice Flags
SpVoice my_Voice = new SpVoice(); //declaring and initializing SpVoice Class
int sp_Rate=0,sp_Volume=70; //integral variables declaration initialization
private void Form1_Load(object sender, EventArgs e)
{
TextArea_User.Hide();
FilePath_Lbl.Hide();
FileBrowse_TxtBox.Hide();
Browse_Btn.Hide();
}
在上面的代码中,我定义了几个对象,例如SpFileStream
类的"spFileStream"
,SpeechStreamFileMode
类的"spFilemode"
,SpObjectTokenClass
类的"my_SpObjToken"
,SpVoice
类的"my_Voice"
。我将在后面的章节中讨论每个对象。有一些整型变量,它们是sp_Rate
初始化为"0"
和sp_Volume
初始化为"70"
。这些sp_Rate
和sp_Volume
会将它们的值传递给不同的部分。
在Form1_Load
事件中,我隐藏了一些将在后续按需使用的对象。
//Speak Button to Convert Text to Audio
private void Speak_Btn_Click(object sender, EventArgs e)
{
if (TxtArea_Chk.Checked == true)
{
my_Voice.Speak(TextArea_User.Text, my_Spflag);
my_Voice.Volume = sp_Volume;
my_Voice.Rate = sp_Rate;
}
else if (TxtFilePath_Chk.Checked == true)
{
my_Voice.Speak(my_fDlg.FileName,
SpeechVoiceSpeakFlags.SVSFIsFilename); //to update with Browse_Btn
my_Voice.Volume = sp_Volume;
my_Voice.Rate = sp_Rate;
}
}
在Speak_Btn_Click
事件中,我正在检查TxtArea_Chk
(checkBox
)是否被选中。如果被选中,则TextArea_User
(richTextBox
)的文本将通过相关对象my_Voice
和my_Spflag
变量(定义为"SVSFlagsAsync
"(即异步发音))传递给SpVoice
类的Speak
方法。如果用户选择了TxtFilePath_Chk
(checkBox
),则从openDialogBox
获取的文本文件通过相关对象"my_Voice
"和enum SpeechVoiceSpeakFlags
的第二个参数"SVSFIsFilename
"传递给SpVoice
类的Speak
方法。在这两种情况下,SpVoice
类的音量和速率都通过同一个对象my_Voice
用变量"sp_Volume
"和"sp_Rate
"进行初始化。
//Decrement Rate Button to define Rate value
private void DecRate_Btn_Click(object sender, EventArgs e)
{
if (sp_Rate > 0)
sp_Rate--;
else
MessageBox.Show("It is minimum Level.");
}
在DecRate_Btn_Click
事件中,如果sp_Rate
的值大于0
,则将其减一,否则会显示最小限制的消息。
//Increment Rate Button to define Rate value
private void IncRate_Btn_Click(object sender, EventArgs e)
{
if (sp_Rate <= 20)
sp_Rate++;
else
MessageBox.Show("It is maximum Level.");
}
在IncRate_Btn_Click
事件中,如果sp_Rate
的值小于21
,则将其加一,否则会显示最大限制的消息。
//Decrement Volume Button to define Volume value
private void DecVolume_Btn_Click(object sender, EventArgs e)
{
if (sp_Volume > 60)
sp_Volume-=2;
else
MessageBox.Show("It is minimum Limit");
}
在DecVolume_Btn_Click
事件中,如果sp_Volume
的值大于60,则将其减二,否则会显示最小限制的消息。
//Increment Volume Button to define Volume value
private void IncVolume_Btn_Click(object sender, EventArgs e)
{
if (sp_Volume <= 100)
sp_Volume+=2;
else
MessageBox.Show("It is Maximum Limit.");
}
在IncVolume_Btn_Click
事件中,如果sp_Volume
的值小于100
,则将其加二,否则会显示最大限制的消息。
//Button to Exit from Application
private void Exit_Btn_Click(object sender, EventArgs e)
{
this.Dispose();
}
在Exit_Btn_Click
事件中,应用程序将被销毁。
//Save Button To preferred user's location
private void SaveFile_Btn_Click(object sender, EventArgs e)
{
SaveFileDialog my_Sfd = new SaveFileDialog();
my_Sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
my_Sfd.Title = "Save to a wav file.";
my_Sfd.FilterIndex = 2;
my_Sfd.RestoreDirectory = true;
if ((my_Sfd.ShowDialog() == DialogResult.OK)&&(TxtArea_Chk.Checked == true))
{
spFileStream.Open(my_Sfd.FileName, spFileMode, false);
my_Voice.AudioOutputStream = spFileStream;
my_Voice.Speak(TextArea_User.Text, my_Spflag);
my_Voice.WaitUntilDone(-1);
spFileStream.Close();
}
else if((my_Sfd.ShowDialog() ==
DialogResult.OK)&&(TxtFilePath_Chk.Checked == true))
{
spFileStream.Open(my_Sfd.FileName, spFileMode, false);
my_Voice.AudioOutputStream = spFileStream;
my_Voice.Speak(my_fDlg.FileName, SpeechVoiceSpeakFlags.SVSFIsFilename);
my_Voice.WaitUntilDone(-1);
spFileStream.Close();
}
}
在SaveFile_Btn_Click
事件中,我打开了saveDialogBox
,其过滤器为".wav"
,并在检查TxtArea_Chk
或TxtFilePath_Chk
的选中状态后,将文本转换为音频文件,并按照用户首选的名称和位置保存。
private void TxtArea_Chk_CheckedChanged(object sender, EventArgs e)
{
TxtFilePath_Chk.Enabled = false;
UserChoice_GrpBox.Text = "User Text Area";
TextArea_User.Show();
FilePath_Lbl.Hide();
FileBrowse_TxtBox.Hide();
Browse_Btn.Hide();
}
在TxtArea_Chk
的选中更改事件中,我禁用了TxtFilePath_Chk
,并更改了GroupBox
的文本,同时调用TextArea_User
的show
方法,以及FilePath_Lbl
、FileBrowse_TxtBox
和Browse_Btn
的Hide
方法。
private void TxtFilePath_Chk_CheckedChanged(object sender, EventArgs e)
{
TxtArea_Chk.Enabled = false;
UserChoice_GrpBox.Text = "User Text File";
TextArea_User.Hide();
FilePath_Lbl.Show();
FileBrowse_TxtBox.Show();
Browse_Btn.Show();
}
在TxtFilePath_Chk
的选中更改事件中,我禁用了TxtArea_Chk
,并更改了GroupBox
的文本,同时调用TextArea_User
的hide
方法,以及FilePath_Lbl
、FileBrowse_TxtBox
和Browse_Btn
的show
方法。
int igCounter = 0;
private void Refresh_Btn_Click(object sender, EventArgs e)
{
UserChoice_GrpBox.Text = "User Choice Area";
TxtArea_Chk.Enabled = true;
TxtFilePath_Chk.Enabled = true;
TxtArea_Chk.Checked = false;
TxtFilePath_Chk.Checked = false;
TextArea_User.Hide();
FilePath_Lbl.Hide();
FileBrowse_TxtBox.Hide();
Browse_Btn.Hide();
if (igCounter == 0)
{
MessageBox.Show("Single Click twice to Complete refresh.");
igCounter++;
}
}
在Refresh_Btn
的Click事件中,我所做的只是将布尔值true
赋给两个checkbox
的enabled
属性,并隐藏groupBox
的所有其他成员。
OpenFileDialog my_fDlg = new OpenFileDialog();
String user_WaveFile;
private void Browse_Btn_Click(object sender, EventArgs e)
{
my_fDlg.Title = "Browse your .wav file.";
my_fDlg.Filter = "Wav Files (*.txt)|*.txt";
my_fDlg.RestoreDirectory = true;
if (my_fDlg.ShowDialog() == DialogResult.OK)
{
user_WaveFile = @my_fDlg.FileName;
FileBrowse_TxtBox.Text = @my_fDlg.FileName;
}
}
在Browse_Btn
的Click事件中,我打开了一个过滤器为".txt"的openDialogBox
,并将textBox
的文本初始化为用户首选的文件名。
代码的作用是什么?
这段代码属于主窗体"Form1.cs"。在用户选择组部分,用户有两个选择复选框——一个用于richTextbox
,另一个用于从系统中浏览文本文件。通过这些复选框,用户可以选择所需的文本到语音/音频文件的转换方式,或者用户可以浏览他们的文本文件将其转换为音频文件。当用户点击"speak"按钮时,富文本框中的文本或.txt文件中的文本会通过包含在Speechlib.dll文件中的spVoice
类的speak
方法传递。通过SaveFile
按钮,过程与speak方法相同,将文本转换为.wav的音频格式,并打开一个saveDialogbox
供用户选择保存位置。用户还可以通过"Inc"和"Dec"按钮设置音量和语速。这里的音量和语速是整型变量,用于初始化Speechlib.dll文件中SpVoice
类的Volume和Rate属性。
如何使用?
勾选用户需要的checkbox
。如果用户使用richtextbox
,则用户必须在richtextbox
中输入文本并点击speak按钮;或者,如果用户勾选了TextFilePath
复选框,则他们必须通过浏览器按钮浏览textfile
,然后点击speak按钮。要保存音频文件,用户将通过saveFile
按钮获得一个saveDialogbox
,并且可以以新名称在任何位置保存生成的音频文件。
为什么有人会想使用你的版本?
可以找到类似的应用程序,但其独特性在于将富文本框或文本文件中的文本集成转换成音频文件的模式。
代码的环境限制是什么?
此代码是用C#2.0、Visual Studio 2005和.NET 3.0 Framework编写的。
关注点
从用户简单的文本或其简单的文本文件中获取音频文件是此应用程序最重要的功能。我在这里使用的核心概念是(SAPI5.1)Spvoice
类中的speak方法。
历史
- 2009 年 6 月 18 日:初始发布