Speech.pm

filter: Statementstmt 17.9% 28 / 156 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 42.8% 9 / 21 Podpod 0.0% 0 / 7 totaltotal 15.6% 37 / 236 CC 25 SCAR 29.2
CC
25
Cov
14%
describe_dictation_mode
56.0
recognize_line
30.0
test_speech_recognition
20.0
CRAP 18.6
« ZIO_Termcap.pm ↑ Summary ZIO_dumb.pm »
1 package Games::Rezrov::Speech;
2 # ZIO functions for speech synthesis (output) and recognition (input)
3 # mne feb. 2004
4 #
5 # HACK: this is just a subset of ZIO functions rather than a complete
6 # module. I did it this way just to isolate the speech clutter from
7 # ZIO_Generic.
8  
9 # will probably have to change radically as new speech APIs added
10  
11 # ADD:
12 # - error message method explaining problem if enable calls fail
13 # - event-based recognition model if ZIO can handle it: allow
14 # user also to type commands if they want (Tk interface could handle this,
15 # but a ZIO using say '$line = <STDIN>' couldn't due to hang)
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use Games::Rezrov::ZIO_Tools;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Games::Rezrov::MethodMaker qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
22 speaking
23 listening
24  
25 voicetext
26 voice_dictation
27 recognized_phrases
28
29 recog_dictionary_setup
30  
31 speech_synthesis_error
32 speech_recognition_error
33 1 );
34  
35 # constants copied from Microsoft Speech SDK\Include\speech.h
36 # ...are they accessible somewhere "officially" in Perl API? Dunno.
37 1 ▶use constant VSRMODE_OFF => 0x00000002;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use constant VSRMODE_DISABLED => 0x00000001;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use constant VSRMODE_CMDPAUSED => 0x00000004;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use constant VSRMODE_CMDONLY => 0x00000010;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use constant VSRMODE_DCTONLY => 0x00000020;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use constant VSRMODE_CMDANDDCT => 0x00000040;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 0 ▶sub init_speech_synthesis {
Subroutine init_speech_synthesis: not called CC=2 SCAR=17.9
Pod undocumented
45 #
46 # try to enable speech output, return success or failure
47 #
48 0 my ($self) = @_;
49 0 my $result = 0;
50 0 ▶ if (find_module('Win32::SAPI4')) {
Branch: 1 branch
BranchTrueFalseTotal
if (find_module('Win32::SAPI4')) { } 0 0 0
51 0 require Win32::SAPI4;
52 0 require Win32::OLE;
53 0 import Win32::SAPI4;
54 0 import Win32::OLE;
55 0 my $vt = Win32::SAPI4::VoiceText->new();
56 0 $self->voicetext($vt);
57 0 $result = 1;
58 } else {
59 0 $self->speech_synthesis_error("Can't enable speech synthesis because the required support module Win32::SAPI4 is not installed.");
60 }
61 0 return $self->speaking($result);
62 }
63  
64 0 ▶sub test_speech_recognition {
Subroutine test_speech_recognition: not called CC=4 SCAR=30.0
Pod undocumented
65 #
66 # interactively debug recognition engine
67 #
68 0 my ($self) = @_;
69 0 ▶ if (find_module('Win32::SAPI4')) {
Branch: 1 branch
BranchTrueFalseTotal
if (find_module("Win32::SAPI4")) 0 0 0
70 0 require Win32::SAPI4;
71 0 require Win32::OLE;
72 0 import Win32::SAPI4;
73 0 import Win32::OLE;
74  
75 0 my $done = 0;
76 0 ▶ $SIG{INT} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 0 print "[caught interrupt]\n";
78 0 $done = 1;
79 0 };
80  
81 0 my %stop_words = map {$_, 1} qw(
82 stop
83 quit
84 exit
85 finish
86 done
87 );
88  
89 0 my $comment_tag = '==>';
90 0 ▶ my $event_monitor = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
91 0 my ($obj, $event, @args) = @_;
92 0 printf "%s\n", join ",", $event, @args;
93 0 ▶ $done = 1 if $event eq "PhraseFinish" and exists $stop_words{$args[1]};
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "PhraseFinish" and exists $stop_words{$args[1]} 0 0 0
Condition0%0/3
$event eq "PhraseFinish" and exists $stop_words{$args[1]}
!ll&&!rl&&r
000
MC/DC0%0/2
$event eq "PhraseFinish" and exists $stop_words{$args[1]}
$event eq "PhraseFinish" exists $stop_words{$args[1]}
Truth table0%0/3
A and B
A= $event eq "PhraseFinish"B= exists $stop_words{$args[1]}
ABresult
0X0
100
111
94 0 };
95
96 0 my $vd = Win32::SAPI4::VoiceDictation->new();
97 0 Win32::OLE->WithEvents($vd, $event_monitor);
98 0 my $mode = $vd->Mode;
99 0 printf "Speech recognition mode is %d (%s)\n", $mode, describe_dictation_mode($mode);
100  
101 0 ▶ if (is_dictation_enabled($vd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_dictation_enabled($vd)) { } 0 0 0
102 0 printf "%s Say \"stop\", \"quit\", \"exit\" or hit ctrl-c to finish.\n\n", $comment_tag;
103  
104 0 $vd->Deactivate();
105 0 $vd->Activate();
106  
107 0 ▶ while (!$done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 0 0 0
108 0 Win32::OLE->SpinMessageLoop();
109 0 Win32::Sleep(20);
110 }
111  
112 0 $vd->Deactivate();
113 } else {
114 0 printf "PROBLEM: speech recognition won't work because dictation is not enabled!\n";
115 0 print "Run the Microsoft Dictation application and enable it.\n";
116 }
117 }
118 }
119  
120 0 ▶sub describe_dictation_mode {
Subroutine describe_dictation_mode: not called CC=7 SCAR=40.3
Pod undocumented
121 # static
122 0 my ($mode) = @_;
123 0 my $desc;
124 0 ▶ if ($mode == VSRMODE_DISABLED) {
Branch: 6 branches
BranchTrueFalseTotal
if ($mode == 1) { } 0 0 0
elsif ($mode == 2) { } 0 0 0
elsif ($mode == 4) { } 0 0 0
elsif ($mode == 16) { } 0 0 0
elsif ($mode == 32) { } 0 0 0
elsif ($mode == 64) { } 0 0 0
125 0 $desc = "disabled";
126 } elsif ($mode == VSRMODE_OFF) {
127 0 $desc = "off";
128 } elsif ($mode == VSRMODE_CMDPAUSED) {
129 0 $desc = "paused";
130 } elsif ($mode == VSRMODE_CMDONLY) {
131 0 $desc = "voice commands only";
132 } elsif ($mode == VSRMODE_DCTONLY) {
133 0 $desc = "dictation only";
134 } elsif ($mode == VSRMODE_CMDANDDCT) {
135 0 $desc = "voice commands and dictation";
136 } else {
137 0 $desc = "UNKNOWN (??)";
138 }
139 0 return $desc;
140 }
141  
142 0 ▶sub speak {
Subroutine speak: not called CC=4 SCAR=30.0
Pod undocumented
143 0 my ($self, $string, %options) = @_;
144
145 0 $string =~ s/([A-Z]\w+ )(I+)/$1 . length($2)/e;
146 # convert Roman numerals
147 # "Zork I" should be pronounced "Zork 1" rather than "Zork Eye"
148  
149 0 $string =~ s/(\w+ \d+ \/ Serial number )(\d\d)(\d\d)(\d\d)/$1 $2 $3 $4/i;
150 # pronounce serial numbers for what they mean: YY MM DD
151 # "86 09 04" rather than "860,904"
152  
153 0 $string =~ s/Infocom/InfoCom/;
154 # phonetic hack to aid SAPI pronunciation; "info-cum" => "info-com"
155
156 0 $string =~ s/^Copyright \(c\)/Copyright/;
157 # just "copyright", not "copyright (c)"
158  
159 0 $string =~ s/^\[(.*)\]$/$1/;
160 # don't pronounce the brackets around asides, e.g.
161 # "[I don't know the work "bogus".]"
162  
163 0 $string =~ s/\.{3,}/\. /;
164 # sometimes pronounces this as "point", e.g.
165 # "If you insist.... Poof, you're dead!"
166  
167 0 $string =~ s/\"$//;
168 # prevent pronouncing literal "quote" at end, e.g. Zork 1 reading leaflet
169  
170 0 ▶ return if $string eq ">";
Branch: 1 branch
BranchTrueFalseTotal
if $string eq ">" 0 0 0
171 # don't speak game prompt, HACK
172  
173 0 my $vt = $self->voicetext();
174  
175 0 ▶ my $gender = $options{"-gender"} || 1;
Condition0%0/2
$options{'-gender'} || 1
l!l
00
MC/DC0%0/1
$options{'-gender'} || 1
$options{'-gender'}
Truth table0%0/2
A
A= $options{'-gender'}
Aresult
11
00
176 # my $i = $vt->find("Mfg=Microsoft;Gender=2;ModeName=Sam");
177 0 my $i = $vt->find("Gender=$gender");
178 0 $vt->Select($i);
179
180 # $vt->Speed(200);
181 # why doesn't this method work?
182  
183 0 $self->update();
184  
185 0 my $speech_done = 0;
186 0 ▶ my $finish_callback = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
187 0 my ($obj, $event, @args) = @_;
188 0 ▶ $speech_done = 1 if $event eq "SpeakingDone";
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "SpeakingDone" 0 0 0
189 # wait for event signalling speech output is complete
190 0 };
191
192 0 Win32::OLE->WithEvents($vt, $finish_callback);
193 0 $vt->Speak($string);
194  
195 0 ▶ while (!$speech_done) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $speech_done) 0 0 0
196 # wait for speech output to finish
197 0 Win32::OLE->SpinMessageLoop();
198 0 Win32::Sleep(20);
199 }
200 }
201  
202 0 ▶sub init_speech_recognition {
Subroutine init_speech_recognition: not called CC=3 SCAR=24.8
Pod undocumented
203 #
204 # try to enable speech input, return success or failure
205 #
206 0 my ($self) = @_;
207 0 my $result = 0;
208 0 ▶ if (find_module('Win32::SAPI4')) {
Branch: 1 branch
BranchTrueFalseTotal
if (find_module('Win32::SAPI4')) { } 0 0 0
209 0 require Win32::SAPI4;
210 0 require Win32::OLE;
211 0 import Win32::SAPI4;
212 0 import Win32::OLE;
213  
214 0 my $vd = Win32::SAPI4::VoiceDictation->new();
215 0 $vd->Deactivate();
216  
217 0 ▶ $SIG{INT} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
218 0 $vd->Deactivate();
219 0 };
220  
221 0 $self->recognized_phrases([]);
222
223 0 ▶ my $event_monitor = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
224 0 my ($obj, $event, @args) = @_;
225 # printf "%s\n", join ",", $event, @args;
226 0 ▶ if ($event eq "PhraseFinish") {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "PhraseFinish") 0 0 0
227 0 push @{$self->recognized_phrases}, $args[1];
228 }
229 0 };
230
231 0 Win32::OLE->WithEvents($vd, $event_monitor);
232  
233 0 my $mode = $vd->Mode;
234 0 ▶ if (is_dictation_enabled($vd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_dictation_enabled($vd)) { } 0 0 0
235 # OK to proceed
236 0 $self->voice_dictation($vd);
237 0 $vd->Activate();
238 0 $result = 1;
239 } else {
240 0 $self->speech_recognition_error("Can't enable speech recognition because dictation is not enabled. Run Microsoft Dictation and enable it, then try again.");
241 }
242 } else {
243 0 $self->speech_recognition_error("Can't enable speech recognition because the required support module Win32::SAPI4 is not installed.");
244 }
245 0 return $self->listening($result);
246 }
247  
248 0 ▶sub recognize_line {
Subroutine recognize_line: not called CC=5 SCAR=34.0
Pod undocumented
249 0 my ($self) = @_;
250 0 ▶ unless ($self->recog_dictionary_setup()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->recog_dictionary_setup) 0 0 0
251 0 ▶ if (my $zd = Games::Rezrov::StoryFile::get_zdict()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $zd = Games::Rezrov::StoryFile::get_zdict()) 0 0 0
252 0 my @all_words = keys %{$zd->decoded_by_word};
253 0 $self->voice_dictation->Words(join " ", @all_words);
254 # feed game dictionary into recognition engine as hints.
255 # Pros: allows engine to recognize odds words like "grue".
256 # Cons: can reveal internally-truncated words ("mailbo" instead of "mailbox").
257 0 $self->recog_dictionary_setup(1);
258 }
259 }
260  
261 0 $self->recognized_phrases([]);
262 0 while (1) {
263 0 ▶ if (is_dictation_enabled($_[0]->voice_dictation)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_dictation_enabled($_[0]->voice_dictation)) { } 0 0 0
264 # dictation still enabled, wait for user to say something
265 0 my $phrases = $self->recognized_phrases();
266 0 ▶ if (@{$phrases}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$phrases;}) { } 0 0 0
267 0 $self->recognized_phrases([]);
268 0 return $phrases->[$#$phrases];
269 } else {
270 0 Win32::OLE->SpinMessageLoop();
271 0 Win32::Sleep(20);
272 }
273 } else {
274 # dictation no longer enabled, so break and return control to keyboard
275 0 $self->write_string("[dictation stopped]");
276 0 $self->listening(0);
277 0 return "";
278 }
279 }
280 }
281  
282 0 ▶sub is_dictation_enabled {
Subroutine is_dictation_enabled: not called CC=2 SCAR=17.9
Pod undocumented
283 # STATIC
284 0 my ($vd) = @_;
285 0 ▶ die unless $vd;
Branch: 1 branch
BranchTrueFalseTotal
unless $vd 0 0 0
286 0 my $mode = $vd->Mode;
287 0 return $mode == VSRMODE_DCTONLY or $mode == VSRMODE_CMDANDDCT;
288 }
289  
290 1 1;
« ZIO_Termcap.pm ↑ Summary ZIO_dumb.pm »