Professional Documents
Culture Documents
(C++ Builder VCL) Make Your Applications Talkative and Listening Applications by Using SAPI (Speech Synthesis & Speech Recognition)
(C++ Builder VCL) Make Your Applications Talkative and Listening Applications by Using SAPI (Speech Synthesis & Speech Recognition)
(C++ Builder VCL) Make Your Applications Talkative and Listening Applications by Using SAPI (Speech Synthesis & Speech Recognition)
COM/)
(https://www.embarcadero.com/)
C + + ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C A T E G O R Y / C P P / )
Author: Jmac61
Let’s make your C++ VCL applications talkative 😀 May be another wife 😀
This is my first blog post here so I start with stimulating an old Delphi article http://edn.embarcadero.com/article/29583 about Microsoft’s SAPI library for Speech
Synthesis & Speech Recognition which is edited by Brain Lang. So there is much more information about SAPI in this old article which is very helpful. If you want to do that on Delphi, please read Brain
Lang’s article.
If you read and get some info from the links above now lets start to code our simple C++ Builder VCL example which will work on XE versions.
Part I: Text To Speech in C++ Builder
1. Create a new C++ Builder VCL Project, add Edit and Button from component palette. Now we will add SAPI API (Type Library) as a component and its
wrappers. Select Component->Import Component.. from the menu of C++ Builder as shown below
3. Type SAPI ( or you can type any category to the Palette Page and make sure Generate Component Wrappers is checked as below
4. Select Microsoft Speech Library (be sure that it is sapi.dll) from Registered Type Libraries. My version is 5.4 on Windows 10, yours may be different
and go Next
First Create a “SpeakingVCL” folder on your desktop or on your code projects folder. We will save everything to this SpeakingVCL folder.
Select Unit1.cpp from the list by hitting F12 and save it as “SpeakingVCL_Unit1.cpp” to
Select “SpeechLib_OCX.cpp” and “SpeechLib_TLB.cpp” and save both to folder in their names.
Now we can save this new project by Save Project As from menu with those new changes
8. Now select Unit1 to add Voice component and see your form by hitting (or double hit) F11. When we installed SAPI type library it is also added components to the SAPI
category on your Component Palette (if you didn’t type anything it may be found on ActiveX category). Or you can find them by typing “TSp” to the search of Component Palette.
Drag TSpVoice to your form and double click to Button1 and add lines inside it to set Rate, Volume of SpVoice1 and let it say
This simple full project can be found here SpeakingVCL. It doesnt work if you didn’t add SAPI Type Lib as explained above.
1. Add Memo1 and another button (Button2) to your form to listen. Change caption of Button to “Listen”
Drag TSpSharedRecoContext1Recognition of SAPI from Components Palette to your form. You may also use TSpInprocRecognizer too.
2. Now lets obtain data when MS Speech Recognition is used. Select your SpSharedRecoContext1Recognition1 , go to events of this component, double click OnRecognition and
add this line inside
ISpeechRecoGrammar *SRGrammar;
3. Back To Form1 double click to “Listen” captioned Button2 add these lines;
SpSharedRecoContext1->EventInterests = SpeechRecoEvents::SREAllEvents;
SRGrammar=SpSharedRecoContext1->CreateGrammar(Variant(0));
SRGrammar->CmdSetRuleIdState(0, SpeechRuleState::SGDSActive);
SRGrammar->DictationSetState(SpeechRuleState::SGDSActive);
1 //---------------------------------------------------------------------------
2
3 #include <vcl.h>
4 #pragma hdrstop
5
6 #include "SpeakingVCL_Unit1.h"
7 //---------------------------------------------------------------------------
8 #pragma package(smart_init)
9 #pragma link "SpeechLib_OCX"
10 #pragma resource "*.dfm"
11 TForm1 *Form1;
12 //---------------------------------------------------------------------------
13 __fastcall TForm1::TForm1(TComponent* Owner)
14 : TForm(Owner)
15 {
16 }
17 //---------------------------------------------------------------------------
18 void __fastcall TForm1::Button1Click(TObject *Sender)
19 {
20 Form1->SpVoice1->Rate=2;
21 Form1->SpVoice1->Volume=100;
22 Form1->SpVoice1->Speak(Edit1->Text.w_str(), SpeechVoiceSpeakFlags::SVSFlagsAsync ); //SpeechVoiceSpeakFlags::SVSFDefault
23
24 }
25 //---------------------------------------------------------------------------
26 void __fastcall TForm1::SpSharedRecoContext1Recognition(TObject *Sender, long StreamNumber,
27 Variant StreamPosition, SpeechRecognitionType RecognitionType,
28 ISpeechRecoResult *Result)
29 {
30 Memo1->Lines->Add( Result->PhraseInfo->GetText(0, -1, True) );
31 }
5. Now you can test and run your application by hitting F9. That’s all !
To get better results you need to train MS Speech Recognition by your voice, also you might need more tweaks. You may use other classes of SAPI too.
In windows 10; there is Cortana speech recognition which works well with less number of languages supported, it is still under development. I can briefly
say it is much more successful than MS Speech Recognition. But its API is not shared yet. It works on windows and uses your apps as a tool. There is a
way to use Cortana by XAML or you can use cooperative running application with your VCL/FMX application. I am waiting new update on Windows 10.
After that may be we can discuss this again.
On Android Google’s Text To Speech & Recognition API is very successful. There is a wrapper example on Jim McKeeth’s github on Delphi and C++ but
it’s components can be compiled on RADs only because of Delphi base.
https://github.com/jimmckeethhere (https://github.com/jimmckeethhere)
On iOS i need time to check and test. May be someone adds here.
(https://register.gotowebinar.com/register/6110028331282630231?source=blogs_embt)
Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
0 (https://blogs.embarcadero.com/c-builder-vcl-make-your-
applications-talkative-and-listening-applications-by-using-sapi-speech- 3277
synthesis-speech-recognition/#respond)
(https://blog PREVIOUS
new-edition- A new edition of our free C++ compiler
of-our-free- (https://blogs.embarcadero.com/a-new-edition-of-our-free-c-compiler/)
(https://blog NEXT
cmake-with- Using CMake with the free Embarcadero C++ compiler
the-free- (https://blogs.embarcadero.com/using-cmake-with-the-free-embarcadero-c-compiler/)
Leave a Reply
SIGN UP
Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
(https://www.youtube.com/watch?v=xRTql7giLp8)
Search
SEARCH
Something Fresh
(https://blogs.e Three Professional C++ Posts That You Should Read (https://blogs.embarcadero.com/three-professional-c-posts-that-
you-should-read/)
professional-c-
December 22, 2023
posts-that-you-
(https://blogs.e How To Create A Real iOS App (Even If You Do Not Have A Mac) (https://blogs.embarcadero.com/how-to-create-a-
real-ios-app-even-if-you-do-not-have-a-mac/)
to-create-a-
December 20, 2023
real-ios-app-
(https://blogs.e Sqids: Integration with RAD Server and standalone library (https://blogs.embarcadero.com/sqids-integration-with-rad-
server-and-standalone-library/)
integration-
December 19, 2023
with-rad-server-
Popular Posts
p y
RAD Studio 12 (https://blogs.embarcad Studio 12 Athens (https
Questions
C++ and ero.com/3-x-12-rtl-
NEWS
(https://blogs.embarcad
NEWS
ero.co
NEWS
Answers!
R ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C A T E G O R ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C A T E G O R ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C A T E G O R (HTTPS:/
answers/)
NEWS
( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C A T E G O R
c (https://blogs.embarcadero.com/author/marcoc (https://blogs.embarcadero.com/author/marcoc (https://bl
PRODUCTIVITY
com/) com/) com/)
( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C A T E G O R
(https://blogs.embarcadero.com/author/davidm By Marco Cantu By Marco Cantu By Marco
RAD STUDIO
c By
( HDavid
T T P S : Millington
// B L O G S . E M B A R C A D E R O . C O M / C A T E G O R
(https://blogs.embarcadero.com/author/marcoc (https://blogs.embarcadero.com/author/marcoc (https://bl
(https://blogs.embarcadero.com/author/davidm
STUDIO/) com/) - com/) - com/) -
December
W E B I N A R 11, 2023 December 4, 2023 November 7, 2023 Septembe
( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C A T E G O R
Categories
B U S I N E S S D E V E L O P M E N T ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / N E W S / B U S I N E S S - D E V E L O P M E N T / ) 3
C + + ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / C P P / ) 907
C A S E S T U D Y ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / C A S E - S T U D Y / ) 22
C O D E ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / C O D E / ) 580
C O N T E S T ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / N E W S / C O N T E S T / ) 2
D AT A B A S E ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / D AT A B A S E / ) 135
D E L P H I ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / D E L P H I / ) 1571
D E V E L O P E R I N T E R V I E W S ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / D E V E L O P E R - I N T E R V I E W S / ) 21
E V E N T ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / N E W S / E V E N T / ) 6
F I R E M O N K E Y ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / F I R E M O N K E Y / ) 11
H O W - T O ' S ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / N E W S / H O W - T O S / ) 14
I D E ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / I D E / ) 251
I N T E R B A S E ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / I N T E R B A S E / ) 41
I N T E R V I E W ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / I N T E R V I E W / ) 51
M O D E R N I Z AT I O N ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / M O D E R N I Z AT I O N / ) 6
N E W S ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / N E W S / ) 943
P R O D U C T I V I T Y ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / N E W S / P R O D U C T I V I T Y / ) 23
P Y T H O N ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / P Y T H O N / ) 14
R A D S E R V E R ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / R A D - S E R V E R / ) 5
R A D S T U D I O ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / R A D - S T U D I O / ) 680
R E S E A R C H ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / N E W S / R E S E A R C H / ) 11
S H O W C A S E ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / S H O W C A S E / ) 291
T E C H PA R T N E R ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / T E C H - PA R T N E R / ) 185
W E B I N A R ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / N E W S / W E B I N A R / ) 24
W H I T E PA P E R ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / N E W S / W H I T E PA P E R / ) 9
W I N D O W S ( H T T P S : // B L O G S . E M B A R C A D E R O . C O M / C AT E G O R Y / W I N D O W S / ) 20
News (https://blogs.embarcadero.com/category/news/)
RAD Studio
Delphi (https://blogs.embarcadero.com/category/delphi/)
C++ (https://blogs.embarcadero.com/category/cpp/)
InterBase (https://blogs.embarcadero.com/category/interbase/)
Community (https://blogs.embarcadero.com/community/)
USEFUL LINKS
Home (/)
News
Showcase
Community
🌎
FOLLOW US
Embarcadero’s users understand the scalability and stability of C++ and Delphi programming, and depend on the decades of
innovation those languages bring to development. Ninety of the Fortune 100 and an active community of more than three million
users worldwide have relied on Embarcadero’s award-winning products over the past 30 years. Icons by Icons8.com
(https://icons8.com/).