March 12, 2011

Playback Sound

This program is based on Windows Form Application using C#.Net Framework 4.0. The conclusion of this program is to built a such type of application who save your voice at runtime and playback your voice after stop saving that voice also user has an option of saving the recoding at the time of play-back.

For this purpose you have to include Microsoft SAPI dll file add in Refernce of your project. For add reference right click on reference in solution explorer then click on reference and on Tab named Browse go to this path

C:\Program Files\Common Files\\Microsoft shared\Speech\Sapi.dll

To enable us to reference the required namespaces directly from code.

using System.Diagnostics;
using System.Threading;
using SpeechLib;

For Creating User Interface you need following controls

3 Buttons named as;

Start -----> btnStart
Stop -----> btnStop
Replay-----> btnReplay

1 checkbox named as

Checkbox -----> chkSaveVoice

1 Textbox named as txtText (Multiline = true)

The GUI of form is as follow;

Playback-Sound-Image

Add these members in form class

private SpeechLib.SpSharedRecoContext objRecoContext;
private SpeechLib.ISpeechRecoGrammar grammar;
private string strData="No recording yet";

Here's the code

public void RecoContext_Recognition(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
{
strData = Result.PhraseInfo.GetText(0, -1, true);
Debug.WriteLine("Recognition: " + strData + ", " + StreamNumber + ", " + StreamPosition);
txt.Text = strData;
}

private void btnStartDictation_Click(object sender, EventArgs e)
{
try
{
if (objRecoContext == null)
{
objRecoContext = new SpeechLib.SpSharedRecoContext();
objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoContext_Recognition);
grammar = objRecoContext.CreateGrammar(1);
grammar.DictationLoad("", SpeechLoadOption.SLOStatic);
}

grammar.DictationSetState(SpeechRuleState.SGDSActive);
}

catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show("Error occurred: " + ex.ToString(),"Error");
}
}

private void btnStopDictation_Click(object sender, EventArgs e)
{
grammar.DictationSetState(SpeechRuleState.SGDSInactive);
}

private void btnReplay_Click(object sender, EventArgs e)
{
try
{
SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice Voice = new SpVoice();
if (chkSaveReplay.Checked)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
sfd.Title = "Save to a wave file";
sfd.FilterIndex = 2;
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
SpFileStream SpFileStream = new SpFileStream();
SpFileStream.Open(sfd.FileName, SpFileMode, false);
Voice.AudioOutputStream = SpFileStream;
Voice.Speak("You recorded the following message " + strData, SpFlags);
Voice.WaitUntilDone(Timeout.Infinite);
SpFileStream.Close();
}
}
else
{
Voice.Speak("You recorded the following message" + strData, SpFlags);
}
}
catch
{
MessageBox.Show("Speak error", "SimpleTTS", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Important Notes

Every time the Start Dictation button is clicked, we set the DictationState of the grammar object to active.

Every time the Stop Dictation button is clicked, we just sets the DictationSetState of the grammar object to Inactive.

This program saves the recognized words and append the user voice input to the textbox and save it to our string member variable.

If user selected to save the playback recording using checkbox by checked then we display a Save File Dialog and save the user’s playback to the file.