|
1922
|
1 /***********************************************************
|
|
2091
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
3 The Netherlands.
|
|
1922
|
4
|
|
|
5 All Rights Reserved
|
|
|
6
|
|
|
7 Permission to use, copy, modify, and distribute this software and its
|
|
|
8 documentation for any purpose and without fee is hereby granted,
|
|
|
9 provided that the above copyright notice appear in all copies and that
|
|
|
10 both that copyright notice and this permission notice appear in
|
|
|
11 supporting documentation, and that the names of Stichting Mathematisch
|
|
|
12 Centrum or CWI not be used in advertising or publicity pertaining to
|
|
|
13 distribution of the software without specific, written prior permission.
|
|
|
14
|
|
|
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
|
|
|
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
|
|
|
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
22
|
|
|
23 ******************************************************************/
|
|
|
24
|
|
|
25 /* xx module */
|
|
|
26
|
|
|
27 #include "allobjects.h"
|
|
|
28 #include "modsupport.h"
|
|
|
29
|
|
|
30 #include <GestaltEqu.h>
|
|
2027
|
31 #include "Speech.h"
|
|
|
32
|
|
|
33 #ifdef __MWERKS__
|
|
|
34 #include <Strings.h>
|
|
|
35 #define c2pstr C2PStr
|
|
|
36 #define p2cstr P2CStr
|
|
|
37 #else
|
|
1922
|
38 #include "pascal.h"
|
|
2027
|
39 #endif /* __MWERKS__ */
|
|
|
40
|
|
|
41 #ifdef __powerc
|
|
|
42 #include <FragLoad.h>
|
|
|
43 int lib_available;
|
|
|
44 #endif /* __powerc */
|
|
1922
|
45
|
|
|
46 /* Somehow the Apple Fix2X and X2Fix don't do what I expect */
|
|
|
47 #define fixed2double(x) (((double)(x))/32768.0)
|
|
|
48 #define double2fixed(x) ((Fixed)((x)*32768.0))
|
|
|
49
|
|
|
50 char *CurrentSpeech;
|
|
|
51 object *ms_error_object;
|
|
2027
|
52 int speech_available;
|
|
1922
|
53
|
|
2027
|
54 static
|
|
|
55 init_available() {
|
|
|
56 OSErr err;
|
|
|
57 long result;
|
|
|
58
|
|
|
59 #ifdef __powerc
|
|
|
60 lib_available = ((ProcPtr)SpeakString != (ProcPtr)0);
|
|
|
61 #endif
|
|
|
62 err = Gestalt(gestaltSpeechAttr, &result);
|
|
|
63 if ( err == noErr && (result & (1<<gestaltSpeechMgrPresent)))
|
|
|
64 return 1;
|
|
|
65 return 0;
|
|
|
66 }
|
|
|
67
|
|
|
68 static
|
|
|
69 check_available() {
|
|
|
70 if ( !speech_available ) {
|
|
|
71 err_setstr(ms_error_object, "Speech Mgr not available");
|
|
|
72 return 0;
|
|
|
73 }
|
|
|
74 #ifdef __powerc
|
|
|
75 if ( !lib_available ) {
|
|
|
76 err_setstr(ms_error_object, "Speech Mgr available, but shared lib missing");
|
|
|
77 return 0;
|
|
|
78 }
|
|
|
79 #endif
|
|
|
80 return 1;
|
|
1922
|
81 }
|
|
|
82
|
|
|
83 /* -------------
|
|
|
84 **
|
|
|
85 ** Part one - the speech channel object
|
|
|
86 */
|
|
|
87 typedef struct {
|
|
|
88 OB_HEAD
|
|
|
89 SpeechChannel chan;
|
|
|
90 object *curtext; /* If non-NULL current text being spoken */
|
|
|
91 } scobject;
|
|
|
92
|
|
|
93 staticforward typeobject sctype;
|
|
|
94
|
|
|
95 #define is_scobject(v) ((v)->ob_type == &sctype)
|
|
|
96
|
|
|
97 static scobject *
|
|
|
98 newscobject(arg)
|
|
|
99 VoiceSpec *arg;
|
|
|
100 {
|
|
2027
|
101 scobject *self;
|
|
1922
|
102 OSErr err;
|
|
|
103
|
|
2027
|
104 self = NEWOBJ(scobject, &sctype);
|
|
|
105 if (self == NULL)
|
|
1922
|
106 return NULL;
|
|
2027
|
107 if ( (err=NewSpeechChannel(arg, &self->chan)) != 0) {
|
|
|
108 DECREF(self);
|
|
|
109 return (scobject *)PyErr_Mac(ms_error_object, err);
|
|
1922
|
110 }
|
|
2027
|
111 self->curtext = NULL;
|
|
|
112 return self;
|
|
1922
|
113 }
|
|
|
114
|
|
|
115 /* sc methods */
|
|
|
116
|
|
|
117 static void
|
|
2027
|
118 sc_dealloc(self)
|
|
|
119 scobject *self;
|
|
1922
|
120 {
|
|
2027
|
121 DisposeSpeechChannel(self->chan);
|
|
|
122 DEL(self);
|
|
1922
|
123 }
|
|
|
124
|
|
|
125 static object *
|
|
|
126 sc_Stop(self, args)
|
|
|
127 scobject *self;
|
|
|
128 object *args;
|
|
|
129 {
|
|
|
130 OSErr err;
|
|
|
131
|
|
|
132 if (!getnoarg(args))
|
|
|
133 return NULL;
|
|
2027
|
134 if ((err=StopSpeech(self->chan)) != 0) {
|
|
|
135 PyErr_Mac(ms_error_object, err);
|
|
|
136 return NULL;
|
|
|
137 }
|
|
1922
|
138 if ( self->curtext ) {
|
|
|
139 DECREF(self->curtext);
|
|
|
140 self->curtext = NULL;
|
|
|
141 }
|
|
|
142 INCREF(None);
|
|
|
143 return None;
|
|
|
144 }
|
|
|
145
|
|
|
146 static object *
|
|
|
147 sc_SpeakText(self, args)
|
|
|
148 scobject *self;
|
|
|
149 object *args;
|
|
|
150 {
|
|
|
151 OSErr err;
|
|
|
152 char *str;
|
|
|
153 int len;
|
|
|
154
|
|
|
155 if (!getargs(args, "s#", &str, &len))
|
|
|
156 return NULL;
|
|
|
157 if ( self->curtext ) {
|
|
|
158 StopSpeech(self->chan);
|
|
|
159 DECREF(self->curtext);
|
|
|
160 self->curtext = NULL;
|
|
|
161 }
|
|
2027
|
162 if ((err=SpeakText(self->chan, (Ptr)str, (long)len)) != 0) {
|
|
|
163 PyErr_Mac(ms_error_object, err);
|
|
|
164 return 0;
|
|
|
165 }
|
|
1922
|
166 (void)getargs(args, "O", &self->curtext); /* Or should I check this? */
|
|
|
167 INCREF(self->curtext);
|
|
|
168 INCREF(None);
|
|
|
169 return None;
|
|
|
170 }
|
|
|
171
|
|
|
172 static object *
|
|
|
173 sc_GetRate(self, args)
|
|
|
174 scobject *self;
|
|
|
175 object *args;
|
|
|
176 {
|
|
|
177 OSErr err;
|
|
|
178 Fixed farg;
|
|
|
179
|
|
|
180 if (!getnoarg(args))
|
|
|
181 return NULL;
|
|
2027
|
182 if ((err=GetSpeechRate(self->chan, &farg)) != 0) {
|
|
|
183 PyErr_Mac(ms_error_object, err);
|
|
|
184 return 0;
|
|
|
185 }
|
|
1922
|
186 return newfloatobject(fixed2double(farg));
|
|
|
187 }
|
|
|
188
|
|
|
189 static object *
|
|
|
190 sc_GetPitch(self, args)
|
|
|
191 scobject *self;
|
|
|
192 object *args;
|
|
|
193 {
|
|
|
194 OSErr err;
|
|
|
195 Fixed farg;
|
|
|
196
|
|
|
197 if (!getnoarg(args))
|
|
|
198 return NULL;
|
|
2027
|
199 if ((err=GetSpeechPitch(self->chan, &farg)) != 0) {
|
|
|
200 PyErr_Mac(ms_error_object, err);
|
|
|
201 return 0;
|
|
|
202 }
|
|
1922
|
203 return newfloatobject(fixed2double(farg));
|
|
|
204 }
|
|
|
205
|
|
|
206 static object *
|
|
|
207 sc_SetRate(self, args)
|
|
|
208 scobject *self;
|
|
|
209 object *args;
|
|
|
210 {
|
|
|
211 OSErr err;
|
|
|
212 double darg;
|
|
|
213
|
|
|
214 if (!getargs(args, "d", &darg))
|
|
|
215 return NULL;
|
|
2027
|
216 if ((err=SetSpeechRate(self->chan, double2fixed(darg))) != 0) {
|
|
|
217 PyErr_Mac(ms_error_object, err);
|
|
|
218 return 0;
|
|
|
219 }
|
|
1922
|
220 INCREF(None);
|
|
|
221 return None;
|
|
|
222 }
|
|
|
223
|
|
|
224 static object *
|
|
|
225 sc_SetPitch(self, args)
|
|
|
226 scobject *self;
|
|
|
227 object *args;
|
|
|
228 {
|
|
|
229 OSErr err;
|
|
|
230 double darg;
|
|
|
231
|
|
|
232 if (!getargs(args, "d", &darg))
|
|
|
233 return NULL;
|
|
2027
|
234 if ((err=SetSpeechPitch(self->chan, double2fixed(darg))) != 0) {
|
|
|
235 PyErr_Mac(ms_error_object, err);
|
|
|
236 return NULL;
|
|
|
237 }
|
|
1922
|
238 INCREF(None);
|
|
|
239 return None;
|
|
|
240 }
|
|
|
241
|
|
|
242 static struct methodlist sc_methods[] = {
|
|
|
243 {"Stop", (method)sc_Stop},
|
|
|
244 {"SetRate", (method)sc_SetRate},
|
|
|
245 {"GetRate", (method)sc_GetRate},
|
|
|
246 {"SetPitch", (method)sc_SetPitch},
|
|
|
247 {"GetPitch", (method)sc_GetPitch},
|
|
|
248 {"SpeakText", (method)sc_SpeakText},
|
|
|
249 {NULL, NULL} /* sentinel */
|
|
|
250 };
|
|
|
251
|
|
|
252 static object *
|
|
2027
|
253 sc_getattr(self, name)
|
|
|
254 scobject *self;
|
|
1922
|
255 char *name;
|
|
|
256 {
|
|
2027
|
257 return findmethod(sc_methods, (object *)self, name);
|
|
1922
|
258 }
|
|
|
259
|
|
|
260 static typeobject sctype = {
|
|
|
261 OB_HEAD_INIT(&Typetype)
|
|
|
262 0, /*ob_size*/
|
|
|
263 "MacSpeechChannel", /*tp_name*/
|
|
|
264 sizeof(scobject), /*tp_basicsize*/
|
|
|
265 0, /*tp_itemsize*/
|
|
|
266 /* methods */
|
|
|
267 (destructor)sc_dealloc, /*tp_dealloc*/
|
|
|
268 0, /*tp_print*/
|
|
|
269 (getattrfunc)sc_getattr, /*tp_getattr*/
|
|
2027
|
270 0, /*tp_setattr*/
|
|
1922
|
271 0, /*tp_compare*/
|
|
|
272 0, /*tp_repr*/
|
|
|
273 0, /*tp_as_number*/
|
|
|
274 0, /*tp_as_sequence*/
|
|
|
275 0, /*tp_as_mapping*/
|
|
|
276 0, /*tp_hash*/
|
|
|
277 };
|
|
|
278
|
|
|
279 /* -------------
|
|
|
280 **
|
|
|
281 ** Part two - the voice object
|
|
|
282 */
|
|
|
283 typedef struct {
|
|
|
284 OB_HEAD
|
|
|
285 int initialized;
|
|
|
286 VoiceSpec vs;
|
|
|
287 VoiceDescription vd;
|
|
|
288 } mvobject;
|
|
|
289
|
|
|
290 staticforward typeobject mvtype;
|
|
|
291
|
|
|
292 #define is_mvobject(v) ((v)->ob_type == &mvtype)
|
|
|
293
|
|
|
294 static mvobject *
|
|
|
295 newmvobject()
|
|
|
296 {
|
|
2027
|
297 mvobject *self;
|
|
|
298 self = NEWOBJ(mvobject, &mvtype);
|
|
|
299 if (self == NULL)
|
|
1922
|
300 return NULL;
|
|
2027
|
301 self->initialized = 0;
|
|
|
302 return self;
|
|
1922
|
303 }
|
|
|
304
|
|
|
305 static int
|
|
|
306 initmvobject(self, ind)
|
|
|
307 mvobject *self;
|
|
|
308 int ind;
|
|
|
309 {
|
|
|
310 OSErr err;
|
|
|
311
|
|
|
312 if ( (err=GetIndVoice((short)ind, &self->vs)) != 0 ) {
|
|
2027
|
313 PyErr_Mac(ms_error_object, err);
|
|
1922
|
314 return 0;
|
|
|
315 }
|
|
|
316 if ( (err=GetVoiceDescription(&self->vs, &self->vd, sizeof self->vd)) != 0) {
|
|
2027
|
317 PyErr_Mac(ms_error_object, err);
|
|
1922
|
318 return 0;
|
|
|
319 }
|
|
|
320 self->initialized = 1;
|
|
|
321 return 1;
|
|
|
322 }
|
|
|
323 /* mv methods */
|
|
|
324
|
|
|
325 static void
|
|
2027
|
326 mv_dealloc(self)
|
|
|
327 mvobject *self;
|
|
1922
|
328 {
|
|
2027
|
329 DEL(self);
|
|
1922
|
330 }
|
|
|
331
|
|
|
332 static object *
|
|
|
333 mv_getgender(self, args)
|
|
|
334 mvobject *self;
|
|
|
335 object *args;
|
|
|
336 {
|
|
|
337 object *rv;
|
|
|
338
|
|
|
339 if (!getnoarg(args))
|
|
|
340 return NULL;
|
|
|
341 if (!self->initialized) {
|
|
|
342 err_setstr(ms_error_object, "Uninitialized voice");
|
|
|
343 return NULL;
|
|
|
344 }
|
|
|
345 rv = newintobject(self->vd.gender);
|
|
|
346 return rv;
|
|
|
347 }
|
|
|
348
|
|
|
349 static object *
|
|
|
350 mv_newchannel(self, args)
|
|
|
351 mvobject *self;
|
|
|
352 object *args;
|
|
|
353 {
|
|
|
354 if (!getnoarg(args))
|
|
|
355 return NULL;
|
|
|
356 if (!self->initialized) {
|
|
|
357 err_setstr(ms_error_object, "Uninitialized voice");
|
|
|
358 return NULL;
|
|
|
359 }
|
|
|
360 return (object *)newscobject(&self->vs);
|
|
|
361 }
|
|
|
362
|
|
|
363 static struct methodlist mv_methods[] = {
|
|
|
364 {"GetGender", (method)mv_getgender},
|
|
|
365 {"NewChannel", (method)mv_newchannel},
|
|
|
366 {NULL, NULL} /* sentinel */
|
|
|
367 };
|
|
|
368
|
|
|
369 static object *
|
|
2027
|
370 mv_getattr(self, name)
|
|
|
371 mvobject *self;
|
|
1922
|
372 char *name;
|
|
|
373 {
|
|
2027
|
374 return findmethod(mv_methods, (object *)self, name);
|
|
1922
|
375 }
|
|
|
376
|
|
|
377 static typeobject mvtype = {
|
|
|
378 OB_HEAD_INIT(&Typetype)
|
|
|
379 0, /*ob_size*/
|
|
|
380 "MacVoice", /*tp_name*/
|
|
|
381 sizeof(mvobject), /*tp_basicsize*/
|
|
|
382 0, /*tp_itemsize*/
|
|
|
383 /* methods */
|
|
|
384 (destructor)mv_dealloc, /*tp_dealloc*/
|
|
|
385 0, /*tp_print*/
|
|
|
386 (getattrfunc)mv_getattr, /*tp_getattr*/
|
|
2027
|
387 0, /*tp_setattr*/
|
|
1922
|
388 0, /*tp_compare*/
|
|
|
389 0, /*tp_repr*/
|
|
|
390 0, /*tp_as_number*/
|
|
|
391 0, /*tp_as_sequence*/
|
|
|
392 0, /*tp_as_mapping*/
|
|
|
393 0, /*tp_hash*/
|
|
|
394 };
|
|
|
395
|
|
|
396
|
|
|
397 /* -------------
|
|
|
398 **
|
|
|
399 ** Part three - The module interface
|
|
|
400 */
|
|
|
401
|
|
|
402 /* See if Speech manager available */
|
|
|
403
|
|
|
404 static object *
|
|
|
405 ms_Available(self, args)
|
|
|
406 object *self; /* Not used */
|
|
|
407 object *args;
|
|
|
408 {
|
|
|
409
|
|
|
410 if (!getnoarg(args))
|
|
|
411 return NULL;
|
|
2027
|
412 return newintobject(speech_available);
|
|
1922
|
413 }
|
|
|
414
|
|
|
415 /* Count number of busy speeches */
|
|
|
416
|
|
|
417 static object *
|
|
|
418 ms_Busy(self, args)
|
|
|
419 object *self; /* Not used */
|
|
|
420 object *args;
|
|
|
421 {
|
|
|
422 short result;
|
|
|
423
|
|
|
424 if (!getnoarg(args))
|
|
|
425 return NULL;
|
|
2027
|
426 if ( !check_available() )
|
|
|
427 return NULL;
|
|
1922
|
428 result = SpeechBusy();
|
|
|
429 return newintobject(result);
|
|
|
430 }
|
|
|
431
|
|
|
432 /* Say something */
|
|
|
433
|
|
|
434 static object *
|
|
|
435 ms_SpeakString(self, args)
|
|
|
436 object *self; /* Not used */
|
|
|
437 object *args;
|
|
|
438 {
|
|
|
439 OSErr err;
|
|
|
440 char *str;
|
|
|
441 int len;
|
|
|
442
|
|
|
443 if (!getstrarg(args, &str))
|
|
|
444 return NULL;
|
|
2027
|
445 if ( !check_available())
|
|
|
446 return NULL;
|
|
1922
|
447 if (CurrentSpeech) {
|
|
|
448 /* Free the old speech, after killing it off
|
|
|
449 ** (note that speach is async and c2pstr works inplace)
|
|
|
450 */
|
|
|
451 SpeakString("\p");
|
|
|
452 free(CurrentSpeech);
|
|
|
453 }
|
|
|
454 len = strlen(str);
|
|
|
455 CurrentSpeech = malloc(len+1);
|
|
|
456 strcpy(CurrentSpeech, str);
|
|
|
457 err = SpeakString(c2pstr(CurrentSpeech));
|
|
2027
|
458 if ( err ) {
|
|
|
459 PyErr_Mac(ms_error_object, err);
|
|
|
460 return NULL;
|
|
|
461 }
|
|
1922
|
462 INCREF(None);
|
|
|
463 return None;
|
|
|
464 }
|
|
|
465
|
|
|
466
|
|
|
467 /* Count number of available voices */
|
|
|
468
|
|
|
469 static object *
|
|
|
470 ms_CountVoices(self, args)
|
|
|
471 object *self; /* Not used */
|
|
|
472 object *args;
|
|
|
473 {
|
|
|
474 short result;
|
|
|
475
|
|
|
476 if (!getnoarg(args))
|
|
|
477 return NULL;
|
|
2027
|
478 if ( !check_available())
|
|
|
479 return NULL;
|
|
1922
|
480 CountVoices(&result);
|
|
|
481 return newintobject(result);
|
|
|
482 }
|
|
|
483
|
|
|
484 static object *
|
|
|
485 ms_GetIndVoice(self, args)
|
|
|
486 object *self; /* Not used */
|
|
|
487 object *args;
|
|
|
488 {
|
|
|
489 mvobject *rv;
|
|
|
490 long ind;
|
|
|
491
|
|
|
492 if( !getargs(args, "i", &ind))
|
|
2027
|
493 return NULL;
|
|
|
494 if ( !check_available() )
|
|
|
495 return NULL;
|
|
1922
|
496 rv = newmvobject();
|
|
|
497 if ( !initmvobject(rv, ind) ) {
|
|
|
498 DECREF(rv);
|
|
|
499 return NULL;
|
|
|
500 }
|
|
|
501 return (object *)rv;
|
|
|
502 }
|
|
|
503
|
|
|
504
|
|
2027
|
505 static object *
|
|
|
506 ms_Version(self, args)
|
|
|
507 object *self; /* Not used */
|
|
|
508 object *args;
|
|
|
509 {
|
|
|
510 NumVersion v;
|
|
|
511
|
|
|
512 if (!getnoarg(args))
|
|
|
513 return NULL;
|
|
|
514 if ( !check_available())
|
|
|
515 return NULL;
|
|
|
516 v = SpeechManagerVersion();
|
|
|
517 return newintobject(*(int *)&v);
|
|
|
518 }
|
|
|
519
|
|
1922
|
520
|
|
|
521 /* List of functions defined in the module */
|
|
|
522
|
|
|
523 static struct methodlist ms_methods[] = {
|
|
|
524 {"Available", ms_Available},
|
|
|
525 {"CountVoices", ms_CountVoices},
|
|
|
526 {"Busy", ms_Busy},
|
|
|
527 {"SpeakString", ms_SpeakString},
|
|
|
528 {"GetIndVoice", ms_GetIndVoice},
|
|
2027
|
529 {"Version", ms_Version},
|
|
1922
|
530 {NULL, NULL} /* sentinel */
|
|
|
531 };
|
|
|
532
|
|
|
533 /* Initialization function for the module (*must* be called initmacspeech) */
|
|
|
534
|
|
|
535 void
|
|
|
536 initmacspeech()
|
|
|
537 {
|
|
|
538 object *m, *d;
|
|
|
539
|
|
2027
|
540 speech_available = init_available();
|
|
1922
|
541 /* Create the module and add the functions */
|
|
|
542 m = initmodule("macspeech", ms_methods);
|
|
|
543
|
|
|
544 /* Add some symbolic constants to the module */
|
|
|
545 d = getmoduledict(m);
|
|
|
546 ms_error_object = newstringobject("macspeech.error");
|
|
|
547 dictinsert(d, "error", ms_error_object);
|
|
|
548
|
|
|
549 /* Check for errors */
|
|
|
550 if (err_occurred())
|
|
|
551 fatal("can't initialize module macspeech");
|
|
|
552 }
|