blob: 6bcc730a7ecd28e2cea7098a378e91feed897f5e [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__
32#include <Strings.h>
33#define c2pstr C2PStr
34#define p2cstr P2CStr
35#else
36#include "pascal.h"
37#endif /* __MWERKS__ */
38
39#ifdef __powerc
Jack Jansen4a8c54e1997-02-24 13:56:59 +000040#include <CodeFragments.h>
Jack Jansen114ca5c1994-12-14 13:34:35 +000041int lib_available;
42#endif /* __powerc */
43
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000044/* Somehow the Apple Fix2X and X2Fix don't do what I expect */
45#define fixed2double(x) (((double)(x))/32768.0)
46#define double2fixed(x) ((Fixed)((x)*32768.0))
47
48char *CurrentSpeech;
Jack Jansenf5c20571997-01-30 15:48:07 +000049PyObject *ms_error_object;
Jack Jansen114ca5c1994-12-14 13:34:35 +000050int speech_available;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000051
Jack Jansen114ca5c1994-12-14 13:34:35 +000052static
53init_available() {
54 OSErr err;
55 long result;
56
57#ifdef __powerc
58 lib_available = ((ProcPtr)SpeakString != (ProcPtr)0);
59#endif
60 err = Gestalt(gestaltSpeechAttr, &result);
61 if ( err == noErr && (result & (1<<gestaltSpeechMgrPresent)))
62 return 1;
63 return 0;
64}
65
66static
67check_available() {
68 if ( !speech_available ) {
Jack Jansenf5c20571997-01-30 15:48:07 +000069 PyErr_SetString(ms_error_object, "Speech Mgr not available");
Jack Jansen114ca5c1994-12-14 13:34:35 +000070 return 0;
71 }
72#ifdef __powerc
73 if ( !lib_available ) {
Jack Jansenf5c20571997-01-30 15:48:07 +000074 PyErr_SetString(ms_error_object, "Speech Mgr available, but shared lib missing");
Jack Jansen114ca5c1994-12-14 13:34:35 +000075 return 0;
76 }
77#endif
78 return 1;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000079}
80
81/* -------------
82**
83** Part one - the speech channel object
84*/
85typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000086 PyObject_HEAD
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000087 SpeechChannel chan;
Jack Jansenf5c20571997-01-30 15:48:07 +000088 PyObject *curtext; /* If non-NULL current text being spoken */
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000089} scobject;
90
Jack Jansenf5c20571997-01-30 15:48:07 +000091staticforward PyTypeObject sctype;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000092
93#define is_scobject(v) ((v)->ob_type == &sctype)
94
95static scobject *
96newscobject(arg)
97 VoiceSpec *arg;
98{
Jack Jansen114ca5c1994-12-14 13:34:35 +000099 scobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000100 OSErr err;
101
Jack Jansenf5c20571997-01-30 15:48:07 +0000102 self = PyObject_NEW(scobject, &sctype);
Jack Jansen114ca5c1994-12-14 13:34:35 +0000103 if (self == NULL)
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000104 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000105 if ( (err=NewSpeechChannel(arg, &self->chan)) != 0) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000106 Py_DECREF(self);
Jack Jansen114ca5c1994-12-14 13:34:35 +0000107 return (scobject *)PyErr_Mac(ms_error_object, err);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000108 }
Jack Jansen114ca5c1994-12-14 13:34:35 +0000109 self->curtext = NULL;
110 return self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000111}
112
113/* sc methods */
114
115static void
Jack Jansen114ca5c1994-12-14 13:34:35 +0000116sc_dealloc(self)
117 scobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000118{
Jack Jansen114ca5c1994-12-14 13:34:35 +0000119 DisposeSpeechChannel(self->chan);
Jack Jansenf5c20571997-01-30 15:48:07 +0000120 PyMem_DEL(self);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000121}
122
Jack Jansenf5c20571997-01-30 15:48:07 +0000123static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000124sc_Stop(self, args)
125 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000126 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000127{
128 OSErr err;
129
Jack Jansenf5c20571997-01-30 15:48:07 +0000130 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000131 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000132 if ((err=StopSpeech(self->chan)) != 0) {
133 PyErr_Mac(ms_error_object, err);
134 return NULL;
135 }
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000136 if ( self->curtext ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000137 Py_DECREF(self->curtext);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000138 self->curtext = NULL;
139 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000140 Py_INCREF(Py_None);
141 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000142}
143
Jack Jansenf5c20571997-01-30 15:48:07 +0000144static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000145sc_SpeakText(self, args)
146 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000147 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000148{
149 OSErr err;
150 char *str;
151 int len;
152
Jack Jansen4a8c54e1997-02-24 13:56:59 +0000153 if (!PyArg_Parse(args, "m#", &str, &len))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000154 return NULL;
155 if ( self->curtext ) {
156 StopSpeech(self->chan);
Jack Jansenf5c20571997-01-30 15:48:07 +0000157 Py_DECREF(self->curtext);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000158 self->curtext = NULL;
159 }
Jack Jansen114ca5c1994-12-14 13:34:35 +0000160 if ((err=SpeakText(self->chan, (Ptr)str, (long)len)) != 0) {
161 PyErr_Mac(ms_error_object, err);
162 return 0;
163 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000164 (void)PyArg_Parse(args, "O", &self->curtext); /* Or should I check this? */
165 Py_INCREF(self->curtext);
166 Py_INCREF(Py_None);
167 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000168}
169
Jack Jansenf5c20571997-01-30 15:48:07 +0000170static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000171sc_GetRate(self, args)
172 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000173 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000174{
175 OSErr err;
176 Fixed farg;
177
Jack Jansenf5c20571997-01-30 15:48:07 +0000178 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000179 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000180 if ((err=GetSpeechRate(self->chan, &farg)) != 0) {
181 PyErr_Mac(ms_error_object, err);
182 return 0;
183 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000184 return PyFloat_FromDouble(fixed2double(farg));
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000185}
186
Jack Jansenf5c20571997-01-30 15:48:07 +0000187static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000188sc_GetPitch(self, args)
189 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000190 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000191{
192 OSErr err;
193 Fixed farg;
194
Jack Jansenf5c20571997-01-30 15:48:07 +0000195 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000196 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000197 if ((err=GetSpeechPitch(self->chan, &farg)) != 0) {
198 PyErr_Mac(ms_error_object, err);
199 return 0;
200 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000201 return PyFloat_FromDouble(fixed2double(farg));
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000202}
203
Jack Jansenf5c20571997-01-30 15:48:07 +0000204static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000205sc_SetRate(self, args)
206 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000207 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000208{
209 OSErr err;
210 double darg;
211
Jack Jansenf5c20571997-01-30 15:48:07 +0000212 if (!PyArg_Parse(args, "d", &darg))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000213 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000214 if ((err=SetSpeechRate(self->chan, double2fixed(darg))) != 0) {
215 PyErr_Mac(ms_error_object, err);
216 return 0;
217 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000218 Py_INCREF(Py_None);
219 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000220}
221
Jack Jansenf5c20571997-01-30 15:48:07 +0000222static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000223sc_SetPitch(self, args)
224 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000225 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000226{
227 OSErr err;
228 double darg;
229
Jack Jansenf5c20571997-01-30 15:48:07 +0000230 if (!PyArg_Parse(args, "d", &darg))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000231 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000232 if ((err=SetSpeechPitch(self->chan, double2fixed(darg))) != 0) {
233 PyErr_Mac(ms_error_object, err);
234 return NULL;
235 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000236 Py_INCREF(Py_None);
237 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000238}
239
Jack Jansenf5c20571997-01-30 15:48:07 +0000240static struct PyMethodDef sc_methods[] = {
241 {"Stop", (PyCFunction)sc_Stop},
242 {"SetRate", (PyCFunction)sc_SetRate},
243 {"GetRate", (PyCFunction)sc_GetRate},
244 {"SetPitch", (PyCFunction)sc_SetPitch},
245 {"GetPitch", (PyCFunction)sc_GetPitch},
246 {"SpeakText", (PyCFunction)sc_SpeakText},
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000247 {NULL, NULL} /* sentinel */
248};
249
Jack Jansenf5c20571997-01-30 15:48:07 +0000250static PyObject *
Jack Jansen114ca5c1994-12-14 13:34:35 +0000251sc_getattr(self, name)
252 scobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000253 char *name;
254{
Jack Jansenf5c20571997-01-30 15:48:07 +0000255 return Py_FindMethod(sc_methods, (PyObject *)self, name);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000256}
257
Jack Jansenf5c20571997-01-30 15:48:07 +0000258static PyTypeObject sctype = {
259 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000260 0, /*ob_size*/
261 "MacSpeechChannel", /*tp_name*/
262 sizeof(scobject), /*tp_basicsize*/
263 0, /*tp_itemsize*/
264 /* methods */
265 (destructor)sc_dealloc, /*tp_dealloc*/
266 0, /*tp_print*/
267 (getattrfunc)sc_getattr, /*tp_getattr*/
Jack Jansen114ca5c1994-12-14 13:34:35 +0000268 0, /*tp_setattr*/
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000269 0, /*tp_compare*/
270 0, /*tp_repr*/
271 0, /*tp_as_number*/
272 0, /*tp_as_sequence*/
273 0, /*tp_as_mapping*/
274 0, /*tp_hash*/
275};
276
277/* -------------
278**
279** Part two - the voice object
280*/
281typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +0000282 PyObject_HEAD
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000283 int initialized;
284 VoiceSpec vs;
285 VoiceDescription vd;
286} mvobject;
287
Jack Jansenf5c20571997-01-30 15:48:07 +0000288staticforward PyTypeObject mvtype;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000289
290#define is_mvobject(v) ((v)->ob_type == &mvtype)
291
292static mvobject *
293newmvobject()
294{
Jack Jansen114ca5c1994-12-14 13:34:35 +0000295 mvobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000296 self = PyObject_NEW(mvobject, &mvtype);
Jack Jansen114ca5c1994-12-14 13:34:35 +0000297 if (self == NULL)
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000298 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000299 self->initialized = 0;
300 return self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000301}
302
303static int
304initmvobject(self, ind)
305 mvobject *self;
306 int ind;
307{
308 OSErr err;
309
310 if ( (err=GetIndVoice((short)ind, &self->vs)) != 0 ) {
Jack Jansen114ca5c1994-12-14 13:34:35 +0000311 PyErr_Mac(ms_error_object, err);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000312 return 0;
313 }
314 if ( (err=GetVoiceDescription(&self->vs, &self->vd, sizeof self->vd)) != 0) {
Jack Jansen114ca5c1994-12-14 13:34:35 +0000315 PyErr_Mac(ms_error_object, err);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000316 return 0;
317 }
318 self->initialized = 1;
319 return 1;
320}
321/* mv methods */
322
323static void
Jack Jansen114ca5c1994-12-14 13:34:35 +0000324mv_dealloc(self)
325 mvobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000326{
Jack Jansenf5c20571997-01-30 15:48:07 +0000327 PyMem_DEL(self);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000328}
329
Jack Jansenf5c20571997-01-30 15:48:07 +0000330static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000331mv_getgender(self, args)
332 mvobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000333 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000334{
Jack Jansenf5c20571997-01-30 15:48:07 +0000335 PyObject *rv;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000336
Jack Jansenf5c20571997-01-30 15:48:07 +0000337 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000338 return NULL;
339 if (!self->initialized) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000340 PyErr_SetString(ms_error_object, "Uninitialized voice");
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000341 return NULL;
342 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000343 rv = PyInt_FromLong(self->vd.gender);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000344 return rv;
345}
346
Jack Jansenf5c20571997-01-30 15:48:07 +0000347static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000348mv_newchannel(self, args)
349 mvobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000350 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000351{
Jack Jansenf5c20571997-01-30 15:48:07 +0000352 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000353 return NULL;
354 if (!self->initialized) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000355 PyErr_SetString(ms_error_object, "Uninitialized voice");
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000356 return NULL;
357 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000358 return (PyObject *)newscobject(&self->vs);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000359}
360
Jack Jansenf5c20571997-01-30 15:48:07 +0000361static struct PyMethodDef mv_methods[] = {
362 {"GetGender", (PyCFunction)mv_getgender},
363 {"NewChannel", (PyCFunction)mv_newchannel},
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000364 {NULL, NULL} /* sentinel */
365};
366
Jack Jansenf5c20571997-01-30 15:48:07 +0000367static PyObject *
Jack Jansen114ca5c1994-12-14 13:34:35 +0000368mv_getattr(self, name)
369 mvobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000370 char *name;
371{
Jack Jansenf5c20571997-01-30 15:48:07 +0000372 return Py_FindMethod(mv_methods, (PyObject *)self, name);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000373}
374
Jack Jansenf5c20571997-01-30 15:48:07 +0000375static PyTypeObject mvtype = {
376 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000377 0, /*ob_size*/
378 "MacVoice", /*tp_name*/
379 sizeof(mvobject), /*tp_basicsize*/
380 0, /*tp_itemsize*/
381 /* methods */
382 (destructor)mv_dealloc, /*tp_dealloc*/
383 0, /*tp_print*/
384 (getattrfunc)mv_getattr, /*tp_getattr*/
Jack Jansen114ca5c1994-12-14 13:34:35 +0000385 0, /*tp_setattr*/
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000386 0, /*tp_compare*/
387 0, /*tp_repr*/
388 0, /*tp_as_number*/
389 0, /*tp_as_sequence*/
390 0, /*tp_as_mapping*/
391 0, /*tp_hash*/
392};
393
394
395/* -------------
396**
397** Part three - The module interface
398*/
399
400/* See if Speech manager available */
401
Jack Jansenf5c20571997-01-30 15:48:07 +0000402static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000403ms_Available(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000404 PyObject *self; /* Not used */
405 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000406{
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000407
Jack Jansenf5c20571997-01-30 15:48:07 +0000408 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000409 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000410 return PyInt_FromLong(speech_available);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000411}
412
413/* Count number of busy speeches */
414
Jack Jansenf5c20571997-01-30 15:48:07 +0000415static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000416ms_Busy(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000417 PyObject *self; /* Not used */
418 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000419{
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000420 short result;
421
Jack Jansenf5c20571997-01-30 15:48:07 +0000422 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000423 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000424 if ( !check_available() )
425 return NULL;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000426 result = SpeechBusy();
Jack Jansenf5c20571997-01-30 15:48:07 +0000427 return PyInt_FromLong(result);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000428}
429
430/* Say something */
431
Jack Jansenf5c20571997-01-30 15:48:07 +0000432static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000433ms_SpeakString(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000434 PyObject *self; /* Not used */
435 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000436{
437 OSErr err;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000438 char *str;
439 int len;
440
Jack Jansenf5c20571997-01-30 15:48:07 +0000441 if (!PyArg_Parse(args, "s", &str))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000442 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000443 if ( !check_available())
444 return NULL;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000445 if (CurrentSpeech) {
446 /* Free the old speech, after killing it off
447 ** (note that speach is async and c2pstr works inplace)
448 */
449 SpeakString("\p");
450 free(CurrentSpeech);
451 }
452 len = strlen(str);
453 CurrentSpeech = malloc(len+1);
454 strcpy(CurrentSpeech, str);
455 err = SpeakString(c2pstr(CurrentSpeech));
Jack Jansen114ca5c1994-12-14 13:34:35 +0000456 if ( err ) {
457 PyErr_Mac(ms_error_object, err);
458 return NULL;
459 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000460 Py_INCREF(Py_None);
461 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000462}
463
464
465/* Count number of available voices */
466
Jack Jansenf5c20571997-01-30 15:48:07 +0000467static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000468ms_CountVoices(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000469 PyObject *self; /* Not used */
470 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000471{
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000472 short result;
473
Jack Jansenf5c20571997-01-30 15:48:07 +0000474 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000475 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000476 if ( !check_available())
477 return NULL;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000478 CountVoices(&result);
Jack Jansenf5c20571997-01-30 15:48:07 +0000479 return PyInt_FromLong(result);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000480}
481
Jack Jansenf5c20571997-01-30 15:48:07 +0000482static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000483ms_GetIndVoice(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000484 PyObject *self; /* Not used */
485 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000486{
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000487 mvobject *rv;
488 long ind;
489
Jack Jansenf5c20571997-01-30 15:48:07 +0000490 if( !PyArg_Parse(args, "i", &ind))
Jack Jansen114ca5c1994-12-14 13:34:35 +0000491 return NULL;
492 if ( !check_available() )
493 return NULL;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000494 rv = newmvobject();
495 if ( !initmvobject(rv, ind) ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000496 Py_DECREF(rv);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000497 return NULL;
498 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000499 return (PyObject *)rv;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000500}
501
502
Jack Jansenf5c20571997-01-30 15:48:07 +0000503static PyObject *
Jack Jansen114ca5c1994-12-14 13:34:35 +0000504ms_Version(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000505 PyObject *self; /* Not used */
506 PyObject *args;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000507{
Jack Jansen114ca5c1994-12-14 13:34:35 +0000508 NumVersion v;
509
Jack Jansenf5c20571997-01-30 15:48:07 +0000510 if (!PyArg_NoArgs(args))
Jack Jansen114ca5c1994-12-14 13:34:35 +0000511 return NULL;
512 if ( !check_available())
513 return NULL;
514 v = SpeechManagerVersion();
Jack Jansenf5c20571997-01-30 15:48:07 +0000515 return PyInt_FromLong(*(int *)&v);
Jack Jansen114ca5c1994-12-14 13:34:35 +0000516}
517
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000518
519/* List of functions defined in the module */
520
Jack Jansenf5c20571997-01-30 15:48:07 +0000521static struct PyMethodDef ms_methods[] = {
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000522 {"Available", ms_Available},
523 {"CountVoices", ms_CountVoices},
524 {"Busy", ms_Busy},
525 {"SpeakString", ms_SpeakString},
526 {"GetIndVoice", ms_GetIndVoice},
Jack Jansen114ca5c1994-12-14 13:34:35 +0000527 {"Version", ms_Version},
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000528 {NULL, NULL} /* sentinel */
529};
530
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000531/* Initialization function for the module (*must* be called initmacspeech) */
532
533void
534initmacspeech()
535{
Jack Jansenf5c20571997-01-30 15:48:07 +0000536 PyObject *m, *d;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000537
Jack Jansen114ca5c1994-12-14 13:34:35 +0000538 speech_available = init_available();
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000539 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +0000540 m = Py_InitModule("macspeech", ms_methods);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000541
542 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +0000543 d = PyModule_GetDict(m);
544 ms_error_object = PyString_FromString("macspeech.error");
545 PyDict_SetItemString(d, "error", ms_error_object);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000546
547 /* Check for errors */
Jack Jansenf5c20571997-01-30 15:48:07 +0000548 if (PyErr_Occurred())
549 Py_FatalError("can't initialize module macspeech");
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000550}