blob: accb7b28bbb2a06608c28108be40bb6ac867380c [file] [log] [blame]
Guido van Rossum3a80c8d1994-10-02 11:33:59 +00001/***********************************************************
Jack Jansen42218ce1997-01-31 16:15:11 +00002Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
Guido van Rossum99546991995-01-08 14:33:34 +00003The Netherlands.
Guido van Rossum3a80c8d1994-10-02 11:33:59 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000025
Jack Jansenf5c20571997-01-30 15:48:07 +000026#include "Python.h"
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000027
Jack Jansen4a8c54e1997-02-24 13:56:59 +000028#include <Gestalt.h>
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000029#include "Speech.h"
30
Jack Jansen114ca5c1994-12-14 13:34:35 +000031#ifdef __MWERKS__
Jack Jansene79dc762000-06-02 21:35:07 +000032#define OLDP2C 1
Jack Janseneeccca91997-05-07 15:46:31 +000033#include <TextUtils.h>
Jack Jansene79dc762000-06-02 21:35:07 +000034#ifndef c2pstr
Jack Jansen114ca5c1994-12-14 13:34:35 +000035#define c2pstr C2PStr
Jack Jansene79dc762000-06-02 21:35:07 +000036#endif
37#ifndef p2cstr
Jack Jansen114ca5c1994-12-14 13:34:35 +000038#define p2cstr P2CStr
Jack Jansene79dc762000-06-02 21:35:07 +000039#endif
Jack Jansen114ca5c1994-12-14 13:34:35 +000040#else
41#include "pascal.h"
42#endif /* __MWERKS__ */
43
44#ifdef __powerc
Jack Jansen4a8c54e1997-02-24 13:56:59 +000045#include <CodeFragments.h>
Jack Jansen114ca5c1994-12-14 13:34:35 +000046int lib_available;
47#endif /* __powerc */
48
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000049/* Somehow the Apple Fix2X and X2Fix don't do what I expect */
50#define fixed2double(x) (((double)(x))/32768.0)
51#define double2fixed(x) ((Fixed)((x)*32768.0))
52
53char *CurrentSpeech;
Jack Jansenf5c20571997-01-30 15:48:07 +000054PyObject *ms_error_object;
Jack Jansen114ca5c1994-12-14 13:34:35 +000055int speech_available;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000056
Jack Jansen114ca5c1994-12-14 13:34:35 +000057static
58init_available() {
59 OSErr err;
60 long result;
61
62#ifdef __powerc
63 lib_available = ((ProcPtr)SpeakString != (ProcPtr)0);
64#endif
65 err = Gestalt(gestaltSpeechAttr, &result);
66 if ( err == noErr && (result & (1<<gestaltSpeechMgrPresent)))
67 return 1;
68 return 0;
69}
70
71static
72check_available() {
73 if ( !speech_available ) {
Jack Jansenf5c20571997-01-30 15:48:07 +000074 PyErr_SetString(ms_error_object, "Speech Mgr not available");
Jack Jansen114ca5c1994-12-14 13:34:35 +000075 return 0;
76 }
77#ifdef __powerc
78 if ( !lib_available ) {
Jack Jansenf5c20571997-01-30 15:48:07 +000079 PyErr_SetString(ms_error_object, "Speech Mgr available, but shared lib missing");
Jack Jansen114ca5c1994-12-14 13:34:35 +000080 return 0;
81 }
82#endif
83 return 1;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000084}
85
86/* -------------
87**
88** Part one - the speech channel object
89*/
90typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000091 PyObject_HEAD
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000092 SpeechChannel chan;
Jack Jansenf5c20571997-01-30 15:48:07 +000093 PyObject *curtext; /* If non-NULL current text being spoken */
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000094} scobject;
95
Jack Jansenf5c20571997-01-30 15:48:07 +000096staticforward PyTypeObject sctype;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000097
98#define is_scobject(v) ((v)->ob_type == &sctype)
99
100static scobject *
101newscobject(arg)
102 VoiceSpec *arg;
103{
Jack Jansen114ca5c1994-12-14 13:34:35 +0000104 scobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000105 OSErr err;
106
Jack Jansenf5c20571997-01-30 15:48:07 +0000107 self = PyObject_NEW(scobject, &sctype);
Jack Jansen114ca5c1994-12-14 13:34:35 +0000108 if (self == NULL)
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000109 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000110 if ( (err=NewSpeechChannel(arg, &self->chan)) != 0) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000111 Py_DECREF(self);
Jack Jansen114ca5c1994-12-14 13:34:35 +0000112 return (scobject *)PyErr_Mac(ms_error_object, err);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000113 }
Jack Jansen114ca5c1994-12-14 13:34:35 +0000114 self->curtext = NULL;
115 return self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000116}
117
118/* sc methods */
119
120static void
Jack Jansen114ca5c1994-12-14 13:34:35 +0000121sc_dealloc(self)
122 scobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000123{
Jack Jansen114ca5c1994-12-14 13:34:35 +0000124 DisposeSpeechChannel(self->chan);
Jack Jansenf5c20571997-01-30 15:48:07 +0000125 PyMem_DEL(self);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000126}
127
Jack Jansenf5c20571997-01-30 15:48:07 +0000128static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000129sc_Stop(self, args)
130 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000131 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000132{
133 OSErr err;
134
Jack Jansenf5c20571997-01-30 15:48:07 +0000135 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000136 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000137 if ((err=StopSpeech(self->chan)) != 0) {
138 PyErr_Mac(ms_error_object, err);
139 return NULL;
140 }
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000141 if ( self->curtext ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000142 Py_DECREF(self->curtext);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000143 self->curtext = NULL;
144 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000145 Py_INCREF(Py_None);
146 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000147}
148
Jack Jansenf5c20571997-01-30 15:48:07 +0000149static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000150sc_SpeakText(self, args)
151 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000152 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000153{
154 OSErr err;
155 char *str;
156 int len;
157
Jack Janseneeccca91997-05-07 15:46:31 +0000158 if (!PyArg_Parse(args, "s#", &str, &len))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000159 return NULL;
160 if ( self->curtext ) {
161 StopSpeech(self->chan);
Jack Jansenf5c20571997-01-30 15:48:07 +0000162 Py_DECREF(self->curtext);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000163 self->curtext = NULL;
164 }
Jack Jansen114ca5c1994-12-14 13:34:35 +0000165 if ((err=SpeakText(self->chan, (Ptr)str, (long)len)) != 0) {
166 PyErr_Mac(ms_error_object, err);
167 return 0;
168 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000169 (void)PyArg_Parse(args, "O", &self->curtext); /* Or should I check this? */
170 Py_INCREF(self->curtext);
171 Py_INCREF(Py_None);
172 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000173}
174
Jack Jansenf5c20571997-01-30 15:48:07 +0000175static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000176sc_GetRate(self, args)
177 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000178 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000179{
180 OSErr err;
181 Fixed farg;
182
Jack Jansenf5c20571997-01-30 15:48:07 +0000183 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000184 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000185 if ((err=GetSpeechRate(self->chan, &farg)) != 0) {
186 PyErr_Mac(ms_error_object, err);
187 return 0;
188 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000189 return PyFloat_FromDouble(fixed2double(farg));
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000190}
191
Jack Jansenf5c20571997-01-30 15:48:07 +0000192static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000193sc_GetPitch(self, args)
194 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000195 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000196{
197 OSErr err;
198 Fixed farg;
199
Jack Jansenf5c20571997-01-30 15:48:07 +0000200 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000201 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000202 if ((err=GetSpeechPitch(self->chan, &farg)) != 0) {
203 PyErr_Mac(ms_error_object, err);
204 return 0;
205 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000206 return PyFloat_FromDouble(fixed2double(farg));
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000207}
208
Jack Jansenf5c20571997-01-30 15:48:07 +0000209static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000210sc_SetRate(self, args)
211 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000212 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000213{
214 OSErr err;
215 double darg;
216
Jack Jansenf5c20571997-01-30 15:48:07 +0000217 if (!PyArg_Parse(args, "d", &darg))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000218 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000219 if ((err=SetSpeechRate(self->chan, double2fixed(darg))) != 0) {
220 PyErr_Mac(ms_error_object, err);
221 return 0;
222 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000223 Py_INCREF(Py_None);
224 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000225}
226
Jack Jansenf5c20571997-01-30 15:48:07 +0000227static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000228sc_SetPitch(self, args)
229 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000230 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000231{
232 OSErr err;
233 double darg;
234
Jack Jansenf5c20571997-01-30 15:48:07 +0000235 if (!PyArg_Parse(args, "d", &darg))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000236 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000237 if ((err=SetSpeechPitch(self->chan, double2fixed(darg))) != 0) {
238 PyErr_Mac(ms_error_object, err);
239 return NULL;
240 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000241 Py_INCREF(Py_None);
242 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000243}
244
Jack Jansenf5c20571997-01-30 15:48:07 +0000245static struct PyMethodDef sc_methods[] = {
246 {"Stop", (PyCFunction)sc_Stop},
247 {"SetRate", (PyCFunction)sc_SetRate},
248 {"GetRate", (PyCFunction)sc_GetRate},
249 {"SetPitch", (PyCFunction)sc_SetPitch},
250 {"GetPitch", (PyCFunction)sc_GetPitch},
251 {"SpeakText", (PyCFunction)sc_SpeakText},
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000252 {NULL, NULL} /* sentinel */
253};
254
Jack Jansenf5c20571997-01-30 15:48:07 +0000255static PyObject *
Jack Jansen114ca5c1994-12-14 13:34:35 +0000256sc_getattr(self, name)
257 scobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000258 char *name;
259{
Jack Jansenf5c20571997-01-30 15:48:07 +0000260 return Py_FindMethod(sc_methods, (PyObject *)self, name);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000261}
262
Jack Jansenf5c20571997-01-30 15:48:07 +0000263static PyTypeObject sctype = {
264 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000265 0, /*ob_size*/
266 "MacSpeechChannel", /*tp_name*/
267 sizeof(scobject), /*tp_basicsize*/
268 0, /*tp_itemsize*/
269 /* methods */
270 (destructor)sc_dealloc, /*tp_dealloc*/
271 0, /*tp_print*/
272 (getattrfunc)sc_getattr, /*tp_getattr*/
Jack Jansen114ca5c1994-12-14 13:34:35 +0000273 0, /*tp_setattr*/
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000274 0, /*tp_compare*/
275 0, /*tp_repr*/
276 0, /*tp_as_number*/
277 0, /*tp_as_sequence*/
278 0, /*tp_as_mapping*/
279 0, /*tp_hash*/
280};
281
282/* -------------
283**
284** Part two - the voice object
285*/
286typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +0000287 PyObject_HEAD
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000288 int initialized;
289 VoiceSpec vs;
290 VoiceDescription vd;
291} mvobject;
292
Jack Jansenf5c20571997-01-30 15:48:07 +0000293staticforward PyTypeObject mvtype;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000294
295#define is_mvobject(v) ((v)->ob_type == &mvtype)
296
297static mvobject *
298newmvobject()
299{
Jack Jansen114ca5c1994-12-14 13:34:35 +0000300 mvobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000301 self = PyObject_NEW(mvobject, &mvtype);
Jack Jansen114ca5c1994-12-14 13:34:35 +0000302 if (self == NULL)
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000303 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000304 self->initialized = 0;
305 return self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000306}
307
308static int
309initmvobject(self, ind)
310 mvobject *self;
311 int ind;
312{
313 OSErr err;
314
315 if ( (err=GetIndVoice((short)ind, &self->vs)) != 0 ) {
Jack Jansen114ca5c1994-12-14 13:34:35 +0000316 PyErr_Mac(ms_error_object, err);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000317 return 0;
318 }
319 if ( (err=GetVoiceDescription(&self->vs, &self->vd, sizeof self->vd)) != 0) {
Jack Jansen114ca5c1994-12-14 13:34:35 +0000320 PyErr_Mac(ms_error_object, err);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000321 return 0;
322 }
323 self->initialized = 1;
324 return 1;
325}
326/* mv methods */
327
328static void
Jack Jansen114ca5c1994-12-14 13:34:35 +0000329mv_dealloc(self)
330 mvobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000331{
Jack Jansenf5c20571997-01-30 15:48:07 +0000332 PyMem_DEL(self);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000333}
334
Jack Jansenf5c20571997-01-30 15:48:07 +0000335static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000336mv_getgender(self, args)
337 mvobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000338 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000339{
Jack Jansenf5c20571997-01-30 15:48:07 +0000340 PyObject *rv;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000341
Jack Jansenf5c20571997-01-30 15:48:07 +0000342 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000343 return NULL;
344 if (!self->initialized) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000345 PyErr_SetString(ms_error_object, "Uninitialized voice");
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000346 return NULL;
347 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000348 rv = PyInt_FromLong(self->vd.gender);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000349 return rv;
350}
351
Jack Jansenf5c20571997-01-30 15:48:07 +0000352static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000353mv_newchannel(self, args)
354 mvobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000355 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000356{
Jack Jansenf5c20571997-01-30 15:48:07 +0000357 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000358 return NULL;
359 if (!self->initialized) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000360 PyErr_SetString(ms_error_object, "Uninitialized voice");
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000361 return NULL;
362 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000363 return (PyObject *)newscobject(&self->vs);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000364}
365
Jack Jansenf5c20571997-01-30 15:48:07 +0000366static struct PyMethodDef mv_methods[] = {
367 {"GetGender", (PyCFunction)mv_getgender},
368 {"NewChannel", (PyCFunction)mv_newchannel},
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000369 {NULL, NULL} /* sentinel */
370};
371
Jack Jansenf5c20571997-01-30 15:48:07 +0000372static PyObject *
Jack Jansen114ca5c1994-12-14 13:34:35 +0000373mv_getattr(self, name)
374 mvobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000375 char *name;
376{
Jack Jansenf5c20571997-01-30 15:48:07 +0000377 return Py_FindMethod(mv_methods, (PyObject *)self, name);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000378}
379
Jack Jansenf5c20571997-01-30 15:48:07 +0000380static PyTypeObject mvtype = {
381 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000382 0, /*ob_size*/
383 "MacVoice", /*tp_name*/
384 sizeof(mvobject), /*tp_basicsize*/
385 0, /*tp_itemsize*/
386 /* methods */
387 (destructor)mv_dealloc, /*tp_dealloc*/
388 0, /*tp_print*/
389 (getattrfunc)mv_getattr, /*tp_getattr*/
Jack Jansen114ca5c1994-12-14 13:34:35 +0000390 0, /*tp_setattr*/
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000391 0, /*tp_compare*/
392 0, /*tp_repr*/
393 0, /*tp_as_number*/
394 0, /*tp_as_sequence*/
395 0, /*tp_as_mapping*/
396 0, /*tp_hash*/
397};
398
399
400/* -------------
401**
402** Part three - The module interface
403*/
404
405/* See if Speech manager available */
406
Jack Jansenf5c20571997-01-30 15:48:07 +0000407static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000408ms_Available(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000409 PyObject *self; /* Not used */
410 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000411{
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000412
Jack Jansenf5c20571997-01-30 15:48:07 +0000413 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000414 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000415 return PyInt_FromLong(speech_available);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000416}
417
418/* Count number of busy speeches */
419
Jack Jansenf5c20571997-01-30 15:48:07 +0000420static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000421ms_Busy(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000422 PyObject *self; /* Not used */
423 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000424{
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000425 short result;
426
Jack Jansenf5c20571997-01-30 15:48:07 +0000427 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000428 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000429 if ( !check_available() )
430 return NULL;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000431 result = SpeechBusy();
Jack Jansenf5c20571997-01-30 15:48:07 +0000432 return PyInt_FromLong(result);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000433}
434
435/* Say something */
436
Jack Jansenf5c20571997-01-30 15:48:07 +0000437static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000438ms_SpeakString(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000439 PyObject *self; /* Not used */
440 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000441{
442 OSErr err;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000443 char *str;
444 int len;
445
Jack Jansenf5c20571997-01-30 15:48:07 +0000446 if (!PyArg_Parse(args, "s", &str))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000447 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000448 if ( !check_available())
449 return NULL;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000450 if (CurrentSpeech) {
451 /* Free the old speech, after killing it off
452 ** (note that speach is async and c2pstr works inplace)
453 */
454 SpeakString("\p");
455 free(CurrentSpeech);
456 }
457 len = strlen(str);
458 CurrentSpeech = malloc(len+1);
459 strcpy(CurrentSpeech, str);
460 err = SpeakString(c2pstr(CurrentSpeech));
Jack Jansen114ca5c1994-12-14 13:34:35 +0000461 if ( err ) {
462 PyErr_Mac(ms_error_object, err);
463 return NULL;
464 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000465 Py_INCREF(Py_None);
466 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000467}
468
469
470/* Count number of available voices */
471
Jack Jansenf5c20571997-01-30 15:48:07 +0000472static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000473ms_CountVoices(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000474 PyObject *self; /* Not used */
475 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000476{
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000477 short result;
478
Jack Jansenf5c20571997-01-30 15:48:07 +0000479 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000480 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000481 if ( !check_available())
482 return NULL;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000483 CountVoices(&result);
Jack Jansenf5c20571997-01-30 15:48:07 +0000484 return PyInt_FromLong(result);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000485}
486
Jack Jansenf5c20571997-01-30 15:48:07 +0000487static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000488ms_GetIndVoice(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000489 PyObject *self; /* Not used */
490 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000491{
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000492 mvobject *rv;
493 long ind;
494
Jack Jansenf5c20571997-01-30 15:48:07 +0000495 if( !PyArg_Parse(args, "i", &ind))
Jack Jansen114ca5c1994-12-14 13:34:35 +0000496 return NULL;
497 if ( !check_available() )
498 return NULL;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000499 rv = newmvobject();
500 if ( !initmvobject(rv, ind) ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000501 Py_DECREF(rv);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000502 return NULL;
503 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000504 return (PyObject *)rv;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000505}
506
507
Jack Jansenf5c20571997-01-30 15:48:07 +0000508static PyObject *
Jack Jansen114ca5c1994-12-14 13:34:35 +0000509ms_Version(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000510 PyObject *self; /* Not used */
511 PyObject *args;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000512{
Jack Jansen114ca5c1994-12-14 13:34:35 +0000513 NumVersion v;
514
Jack Jansenf5c20571997-01-30 15:48:07 +0000515 if (!PyArg_NoArgs(args))
Jack Jansen114ca5c1994-12-14 13:34:35 +0000516 return NULL;
517 if ( !check_available())
518 return NULL;
519 v = SpeechManagerVersion();
Jack Jansenf5c20571997-01-30 15:48:07 +0000520 return PyInt_FromLong(*(int *)&v);
Jack Jansen114ca5c1994-12-14 13:34:35 +0000521}
522
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000523
524/* List of functions defined in the module */
525
Jack Jansenf5c20571997-01-30 15:48:07 +0000526static struct PyMethodDef ms_methods[] = {
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000527 {"Available", ms_Available},
528 {"CountVoices", ms_CountVoices},
529 {"Busy", ms_Busy},
530 {"SpeakString", ms_SpeakString},
531 {"GetIndVoice", ms_GetIndVoice},
Jack Jansen114ca5c1994-12-14 13:34:35 +0000532 {"Version", ms_Version},
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000533 {NULL, NULL} /* sentinel */
534};
535
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000536/* Initialization function for the module (*must* be called initmacspeech) */
537
538void
539initmacspeech()
540{
Jack Jansenf5c20571997-01-30 15:48:07 +0000541 PyObject *m, *d;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000542
Jack Jansen114ca5c1994-12-14 13:34:35 +0000543 speech_available = init_available();
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000544 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +0000545 m = Py_InitModule("macspeech", ms_methods);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000546
547 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +0000548 d = PyModule_GetDict(m);
Jack Jansen55e39271997-10-07 21:47:25 +0000549 ms_error_object = PyErr_NewException("macspeech.error", NULL, NULL);
Jack Jansenf5c20571997-01-30 15:48:07 +0000550 PyDict_SetItemString(d, "error", ms_error_object);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000551}