How do I develop C++Builder applications that speak and listen?
Last reviewed: 7/15/2011
HOW Article ID: H071134
The information in this article applies to:
- SpeechKit 7
Summary
You can develop C++Builder applications that speak and listen using your favorite version of C++Builder.
More Information
SpeechKit includes C++Builder header files comprised of the C++Builder classes that manage grammars and automatically load and unload the runtime DLL.
To access the SpeechKit C++Builder classes within your application, first add a project reference to the C++Builder header include files:
- Within your C++Builder project, select Project Options.
- Select the C++Builder compiler options
- Add Include path reference to the SpeechKit header files directory: C:\Program Files\Chant\SpeechKit 7\Win32\CBuilder\include
To access the SpeechKit C++Builder classes within your application, add a reference to the SpeechKit Header files in your C++Builder application header file:
#include "cchantaudio.h"
#include "cchantsr.h"
#include "cchanttts.h"
Add a reference in your C++Builder application source file to the SpeechKit code needed to dynamically load and unload the runtime DLL:
#include "CChantSpeechKit.cpp"
Object Instantiation
Declare a global variable for the ChantSR class, instantiate an instance, set the license and serial properties, and set the event handler message.
Add the following to your application header file:
protected:
void __fastcall OnHasEvent(TMessage& msg);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_APP+1, TMessage, OnHasAudioEvent);
MESSAGE_HANDLER(WM_APP+2, TMessage, OnHasSREvent);
MESSAGE_HANDLER(WM_APP+3, TMessage, OnHasTTSEvent);
END_MESSAGE_MAP(TForm)
private: // User declarations
CChantAudio* ChantAudio1;
CChantSR* ChantSR1;
CChantTTS* ChantTTS1;
Add the following to your application source file:
ChantAudio1 = new CChantAudio();
ChantSR1 = new CChantSR();
ChantTTS1 = new CChantTTS();
// Set license properties
ChantAudio1->SetStringProperty(CSPLicense,"LicenseRegistrationNumber");
ChantAudio1->SetStringProperty(CSPSerials,"LicenseLicenseSerialNumber");
ChantSR1->SetStringProperty(CSPLicense,"LicenseRegistrationNumber");
ChantSR1->SetStringProperty(CSPSerials,"LicenseSerialNumber");
ChantTTS1->SetStringProperty(CSPLicense,"LicenseRegistrationNumber");
ChantTTS1->SetStringProperty(CSPSerials,"LicenseLicenseSerialNumber");
// Define the HasEvent callback message
ChantAudio1->SetWindowMessage(Handle, WM_APP+1, 0, 0);
ChantSR1->SetWindowMessage(Handle, WM_APP+2, 0, 0);
ChantTTS1->SetWindowMessage(Handle, WM_APP+3, 0, 0);
Event Callbacks
Event callbacks are the mechanism in which the component library sends information back to the application such as a headset was plugged in, speech recognition occurred, audio playback finished, or there was an error.
void __fastcall TForm1::OnHasAudioEvent(TMessage& msg)
{
int numberOfEvents = ChantAudio1->GetResourceCount(CSREvent,0,0);
for (int i = 0; i < numberOfEvents; i++)
{
// Get the event from the event queue
CChantAudioEvent* pEvent = ChantAudio1->GetChantAudioEvent(0);
switch (pEvent->GetChantCallback())
{
default:
break;
case CCDDeviceArrival:
{
...
break;
}
}
...
// Remove the event from the event queue
ChantAudio1->RemoveResource(CSREvent,0,0,"");
delete pEvent;
}
}
void __fastcall TForm1::OnHasSREvent(TMessage& msg)
{
int numberOfEvents = ChantSR1->GetResourceCount(CSREvent,0,0);
for (int i = 0; i < numberOfEvents; i++)
{
// Get the event from the event queue
CChantSREvent* pEvent = ChantSR1->GetChantSREvent(0);
switch (pEvent->GetChantCallback())
{
default:
break;
case CCSRHasCommand:
{
...
break;
}
}
...
// Remove the event from the event queue
ChantSR1->RemoveResource(CSREvent,0,0,"");
delete pEvent;
}
}
void __fastcall TForm1::OnHasTTSEvent(TMessage& msg)
{
int numberOfEvents = ChantTTS1->GetResourceCount(CSREvent,0,0);
for (int i = 0; i < numberOfEvents; i++)
{
// Get the event from the event queue
CChantTTSEvent* pEvent = ChantTTS1->GetChantTTSEvent(0);
switch (pEvent->GetChantCallback())
{
default:
break;
case CCDAudioStop:
{
...
break;
}
}
...
// Remove the event from the event queue
ChantTTS1->RemoveResource(CSREvent,0,0,"");
delete pEvent;
}
}
Deployment Checklist
When you are ready to deploy your C++Builder application, you need to ensure you have a valid license, bundle the correct Chant component library, and configure your installation properly on the target system. Review the following checklist before deploying your applications:
- You may deploy your C++Builder application to any system with a valid license from the Chant.
- Copy CSpeechKit.dll to the target system and place in the same directory with your application.
Sample Projects
Embarcadero C++Builder sample projects are installed at the following location:
- My Documents\Chant SpeechKit 7\Win32\CBuilder\RS 2007,
- My Documents\Chant SpeechKit 7\Win32\CBuilder\RS 2009,
- My Documents\Chant SpeechKit 7\Win32\CBuilder\RS 2010,
- My Documents\Chant SpeechKit 7\Win32\CBuilder\RS XE, and
- My Documents\Chant SpeechKit 7\Win32\CBuilder\RS XE2.