Background:
Microsoft's Speach Server provides
the foundation technology to convert text file to
WAV file format. WAV is clear, but not as compact as
MP3. Our goal is to convert text file to MP3 by using Microsoft
Speach Server SAPI 5.1
and Open Source Lame Project.
Microsoft's SAPI5.1 + LAME
The following code C# method convert text txt to wave file speechFile:
private void Txt2Wav(string txt, string speechFile, string region)
{
SpFileStream SpFileStream = null;
string TranslatedText = "";
try
{
SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice Voice = new SpVoice();
if (File.Exists(speechFile))
File.Delete(speechFile);
SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
SpFileStream = new SpFileStream();
SpFileStream.Open(speechFile, SpFileMode, false);
Voice.Rate = -2; // -10 to 10, 0 is normal speed
Voice.AudioOutputStream = SpFileStream;
Voice.Speak(txt, SpFlags);
Voice.WaitUntilDone(Timeout.Infinite);
}
catch(Exception error)
{
Console.Write(error.Message);
}
SpFileStream.Close();
}
The following code convert MCI WaveFile in folder pworkingDir to MP3 by executing LAME.EXE:
public void mciConvertWavMP3(string pworkingDir, string fileName, bool waitFlag)
{
string lameDir = @"c:\lame\";
string outfile= "--resample 22 -c -m m -h -k --comp 27 -q 0 \""+pworkingDir+fileName + "\" \""
+ pworkingDir+fileName.Replace(".wav",".mp3")+"\"";
System.Diagnostics.ProcessStartInfo psi=new System.Diagnostics.ProcessStartInfo();
psi.FileName="\""+lameDir+"lame.exe"+"\"";
psi.Arguments=outfile;
psi.WindowStyle=System.Diagnostics.ProcessWindowStyle.Minimized;
System.Diagnostics.Process p=System.Diagnostics.Process.Start(psi);
if (waitFlag)
{
p.WaitForExit(); // wait for exit of called application
}
}
|