Merge branch 'master' of ssh://repo.or.cz/srv/git/bcl
[bcl.git] / speech_sapi.cpp
blob44691858bf15a1d42d7420bebb44072e74e5d7f8
1 #include <vole/vole.hpp>
3 #include <comstl/util/initialisers.hpp>
5 #include <iostream>
6 #include "bcl.h"
7 using namespace stlsoft;
8 using namespace std;
10 string voicestr;
12 vole::object getSpeaker() {
13 static vole::object ret =vole::object::create("SAPI.SpVoice");
14 return ret;
16 vole::collection getVoices() {
17 static vole::collection ret =getSpeaker().invoke_method<vole::collection>(L"GetVoices");
18 return ret;
20 DECLSPEC void initSpeech() {
21 try
23 comstl::com_initialiser coinit;
24 vole::object voice =getSpeaker().get_property<vole::object>(L"voice");
25 voicestr =voice.invoke_method<string>(L"GetDescription");
27 catch(std::bad_alloc&)
29 std::cerr << "out of memory" << std::endl;
31 catch(vole::vole_exception& x)
33 std::cerr << "operation failed: " << x.what() << ": " << winstl::error_desc_a(x.hr()) << std::endl;
35 catch(std::exception& x)
37 std::cerr << "operation failed: " << x.what() << std::endl;
39 catch(...)
41 std::cerr << "unexpected condition" << std::endl;
45 DECLSPEC void terminateSpeech() {
46 return;
48 DECLSPEC void speak(char * text, int flags)
50 int sapiFlags =1;
51 if (flags &SPEAK_BLOCKING) sapiFlags =0;
52 if (flags &SPEAK_INTERRUPTING) sapiFlags =2;
53 getSpeaker().invoke_method<void>(L"Speak", "<voice required=\"name="+voicestr+"\">"+text+"</voice>", sapiFlags);
55 DECLSPEC void setSpeechRate(int rate)
57 getSpeaker().put_property<int>(L"Rate",rate);
60 DECLSPEC int getSpeechRate() {
61 int ret = getSpeaker().get_property<int>(L"rate");
62 return ret;
64 DECLSPEC void setSpeechVoice(int v)
66 int count = getVoices().get_property<int>(L"count");
67 if (v >=count) return;
68 vole::object voice = getVoices().invoke_method<vole::object>(L"Item",v);
69 voicestr =voice.invoke_method<string>(L"GetDescription");
71 DECLSPEC int getSpeechVoice()
73 int count = getVoices().get_property<int>(L"count");
74 for (int i =0;i<count;i++)
76 vole::object voice =getVoices().invoke_method<vole::object>(L"item", i);
77 if (voicestr ==voice.invoke_method<string>(L"GetDescription")) return i;
79 return 0;
82 DECLSPEC void stopSpeech()
84 getSpeaker().invoke_method<void>(L"Speak", "", 2);