blob: a6fbddd645eea207651a22a09cbc2793ed3ecc21 [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"
Jack Jansen9ae898b2000-07-11 21:16:03 +000027#include "macglue.h"
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000028
Jack Jansen4a8c54e1997-02-24 13:56:59 +000029#include <Gestalt.h>
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000030#include "Speech.h"
31
Jack Jansen114ca5c1994-12-14 13:34:35 +000032#ifdef __MWERKS__
Jack Jansene79dc762000-06-02 21:35:07 +000033#define OLDP2C 1
Jack Janseneeccca91997-05-07 15:46:31 +000034#include <TextUtils.h>
Jack Jansene79dc762000-06-02 21:35:07 +000035#ifndef c2pstr
Jack Jansen114ca5c1994-12-14 13:34:35 +000036#define c2pstr C2PStr
Jack Jansene79dc762000-06-02 21:35:07 +000037#endif
38#ifndef p2cstr
Jack Jansen114ca5c1994-12-14 13:34:35 +000039#define p2cstr P2CStr
Jack Jansene79dc762000-06-02 21:35:07 +000040#endif
Jack Jansen114ca5c1994-12-14 13:34:35 +000041#else
42#include "pascal.h"
43#endif /* __MWERKS__ */
44
45#ifdef __powerc
Jack Jansen4a8c54e1997-02-24 13:56:59 +000046#include <CodeFragments.h>
Jack Jansen114ca5c1994-12-14 13:34:35 +000047int lib_available;
48#endif /* __powerc */
49
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000050/* Somehow the Apple Fix2X and X2Fix don't do what I expect */
51#define fixed2double(x) (((double)(x))/32768.0)
52#define double2fixed(x) ((Fixed)((x)*32768.0))
53
54char *CurrentSpeech;
Jack Jansenf5c20571997-01-30 15:48:07 +000055PyObject *ms_error_object;
Jack Jansen114ca5c1994-12-14 13:34:35 +000056int speech_available;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000057
Jack Jansen114ca5c1994-12-14 13:34:35 +000058static
59init_available() {
60 OSErr err;
61 long result;
62
63#ifdef __powerc
64 lib_available = ((ProcPtr)SpeakString != (ProcPtr)0);
65#endif
66 err = Gestalt(gestaltSpeechAttr, &result);
67 if ( err == noErr && (result & (1<<gestaltSpeechMgrPresent)))
68 return 1;
69 return 0;
70}
71
72static
73check_available() {
74 if ( !speech_available ) {
Jack Jansenf5c20571997-01-30 15:48:07 +000075 PyErr_SetString(ms_error_object, "Speech Mgr not available");
Jack Jansen114ca5c1994-12-14 13:34:35 +000076 return 0;
77 }
78#ifdef __powerc
79 if ( !lib_available ) {
Jack Jansenf5c20571997-01-30 15:48:07 +000080 PyErr_SetString(ms_error_object, "Speech Mgr available, but shared lib missing");
Jack Jansen114ca5c1994-12-14 13:34:35 +000081 return 0;
82 }
83#endif
84 return 1;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000085}
86
87/* -------------
88**
89** Part one - the speech channel object
90*/
91typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000092 PyObject_HEAD
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000093 SpeechChannel chan;
Jack Jansenf5c20571997-01-30 15:48:07 +000094 PyObject *curtext; /* If non-NULL current text being spoken */
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000095} scobject;
96
Jack Jansenf5c20571997-01-30 15:48:07 +000097staticforward PyTypeObject sctype;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +000098
99#define is_scobject(v) ((v)->ob_type == &sctype)
100
101static scobject *
102newscobject(arg)
103 VoiceSpec *arg;
104{
Jack Jansen114ca5c1994-12-14 13:34:35 +0000105 scobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000106 OSErr err;
107
Jack Jansenf5c20571997-01-30 15:48:07 +0000108 self = PyObject_NEW(scobject, &sctype);
Jack Jansen114ca5c1994-12-14 13:34:35 +0000109 if (self == NULL)
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000110 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000111 if ( (err=NewSpeechChannel(arg, &self->chan)) != 0) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000112 Py_DECREF(self);
Jack Jansen114ca5c1994-12-14 13:34:35 +0000113 return (scobject *)PyErr_Mac(ms_error_object, err);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000114 }
Jack Jansen114ca5c1994-12-14 13:34:35 +0000115 self->curtext = NULL;
116 return self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000117}
118
119/* sc methods */
120
121static void
Jack Jansen114ca5c1994-12-14 13:34:35 +0000122sc_dealloc(self)
123 scobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000124{
Jack Jansen114ca5c1994-12-14 13:34:35 +0000125 DisposeSpeechChannel(self->chan);
Jack Jansenf5c20571997-01-30 15:48:07 +0000126 PyMem_DEL(self);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000127}
128
Jack Jansenf5c20571997-01-30 15:48:07 +0000129static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000130sc_Stop(self, args)
131 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000132 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000133{
134 OSErr err;
135
Jack Jansenf5c20571997-01-30 15:48:07 +0000136 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000137 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000138 if ((err=StopSpeech(self->chan)) != 0) {
139 PyErr_Mac(ms_error_object, err);
140 return NULL;
141 }
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000142 if ( self->curtext ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000143 Py_DECREF(self->curtext);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000144 self->curtext = NULL;
145 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000146 Py_INCREF(Py_None);
147 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000148}
149
Jack Jansenf5c20571997-01-30 15:48:07 +0000150static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000151sc_SpeakText(self, args)
152 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000153 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000154{
155 OSErr err;
156 char *str;
157 int len;
158
Jack Janseneeccca91997-05-07 15:46:31 +0000159 if (!PyArg_Parse(args, "s#", &str, &len))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000160 return NULL;
161 if ( self->curtext ) {
162 StopSpeech(self->chan);
Jack Jansenf5c20571997-01-30 15:48:07 +0000163 Py_DECREF(self->curtext);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000164 self->curtext = NULL;
165 }
Jack Jansen114ca5c1994-12-14 13:34:35 +0000166 if ((err=SpeakText(self->chan, (Ptr)str, (long)len)) != 0) {
167 PyErr_Mac(ms_error_object, err);
168 return 0;
169 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000170 (void)PyArg_Parse(args, "O", &self->curtext); /* Or should I check this? */
171 Py_INCREF(self->curtext);
172 Py_INCREF(Py_None);
173 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000174}
175
Jack Jansenf5c20571997-01-30 15:48:07 +0000176static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000177sc_GetRate(self, args)
178 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000179 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000180{
181 OSErr err;
182 Fixed farg;
183
Jack Jansenf5c20571997-01-30 15:48:07 +0000184 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000185 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000186 if ((err=GetSpeechRate(self->chan, &farg)) != 0) {
187 PyErr_Mac(ms_error_object, err);
188 return 0;
189 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000190 return PyFloat_FromDouble(fixed2double(farg));
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000191}
192
Jack Jansenf5c20571997-01-30 15:48:07 +0000193static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000194sc_GetPitch(self, args)
195 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000196 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000197{
198 OSErr err;
199 Fixed farg;
200
Jack Jansenf5c20571997-01-30 15:48:07 +0000201 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000202 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000203 if ((err=GetSpeechPitch(self->chan, &farg)) != 0) {
204 PyErr_Mac(ms_error_object, err);
205 return 0;
206 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000207 return PyFloat_FromDouble(fixed2double(farg));
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000208}
209
Jack Jansenf5c20571997-01-30 15:48:07 +0000210static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000211sc_SetRate(self, args)
212 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000213 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000214{
215 OSErr err;
216 double darg;
217
Jack Jansenf5c20571997-01-30 15:48:07 +0000218 if (!PyArg_Parse(args, "d", &darg))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000219 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000220 if ((err=SetSpeechRate(self->chan, double2fixed(darg))) != 0) {
221 PyErr_Mac(ms_error_object, err);
222 return 0;
223 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000224 Py_INCREF(Py_None);
225 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000226}
227
Jack Jansenf5c20571997-01-30 15:48:07 +0000228static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000229sc_SetPitch(self, args)
230 scobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000231 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000232{
233 OSErr err;
234 double darg;
235
Jack Jansenf5c20571997-01-30 15:48:07 +0000236 if (!PyArg_Parse(args, "d", &darg))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000237 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000238 if ((err=SetSpeechPitch(self->chan, double2fixed(darg))) != 0) {
239 PyErr_Mac(ms_error_object, err);
240 return NULL;
241 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000242 Py_INCREF(Py_None);
243 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000244}
245
Jack Jansenf5c20571997-01-30 15:48:07 +0000246static struct PyMethodDef sc_methods[] = {
247 {"Stop", (PyCFunction)sc_Stop},
248 {"SetRate", (PyCFunction)sc_SetRate},
249 {"GetRate", (PyCFunction)sc_GetRate},
250 {"SetPitch", (PyCFunction)sc_SetPitch},
251 {"GetPitch", (PyCFunction)sc_GetPitch},
252 {"SpeakText", (PyCFunction)sc_SpeakText},
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000253 {NULL, NULL} /* sentinel */
254};
255
Jack Jansenf5c20571997-01-30 15:48:07 +0000256static PyObject *
Jack Jansen114ca5c1994-12-14 13:34:35 +0000257sc_getattr(self, name)
258 scobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000259 char *name;
260{
Jack Jansenf5c20571997-01-30 15:48:07 +0000261 return Py_FindMethod(sc_methods, (PyObject *)self, name);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000262}
263
Jack Jansenf5c20571997-01-30 15:48:07 +0000264static PyTypeObject sctype = {
265 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000266 0, /*ob_size*/
267 "MacSpeechChannel", /*tp_name*/
268 sizeof(scobject), /*tp_basicsize*/
269 0, /*tp_itemsize*/
270 /* methods */
271 (destructor)sc_dealloc, /*tp_dealloc*/
272 0, /*tp_print*/
273 (getattrfunc)sc_getattr, /*tp_getattr*/
Jack Jansen114ca5c1994-12-14 13:34:35 +0000274 0, /*tp_setattr*/
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000275 0, /*tp_compare*/
276 0, /*tp_repr*/
277 0, /*tp_as_number*/
278 0, /*tp_as_sequence*/
279 0, /*tp_as_mapping*/
280 0, /*tp_hash*/
281};
282
283/* -------------
284**
285** Part two - the voice object
286*/
287typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +0000288 PyObject_HEAD
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000289 int initialized;
290 VoiceSpec vs;
291 VoiceDescription vd;
292} mvobject;
293
Jack Jansenf5c20571997-01-30 15:48:07 +0000294staticforward PyTypeObject mvtype;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000295
296#define is_mvobject(v) ((v)->ob_type == &mvtype)
297
298static mvobject *
299newmvobject()
300{
Jack Jansen114ca5c1994-12-14 13:34:35 +0000301 mvobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000302 self = PyObject_NEW(mvobject, &mvtype);
Jack Jansen114ca5c1994-12-14 13:34:35 +0000303 if (self == NULL)
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000304 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000305 self->initialized = 0;
306 return self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000307}
308
309static int
310initmvobject(self, ind)
311 mvobject *self;
312 int ind;
313{
314 OSErr err;
315
316 if ( (err=GetIndVoice((short)ind, &self->vs)) != 0 ) {
Jack Jansen114ca5c1994-12-14 13:34:35 +0000317 PyErr_Mac(ms_error_object, err);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000318 return 0;
319 }
320 if ( (err=GetVoiceDescription(&self->vs, &self->vd, sizeof self->vd)) != 0) {
Jack Jansen114ca5c1994-12-14 13:34:35 +0000321 PyErr_Mac(ms_error_object, err);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000322 return 0;
323 }
324 self->initialized = 1;
325 return 1;
326}
327/* mv methods */
328
329static void
Jack Jansen114ca5c1994-12-14 13:34:35 +0000330mv_dealloc(self)
331 mvobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000332{
Jack Jansenf5c20571997-01-30 15:48:07 +0000333 PyMem_DEL(self);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000334}
335
Jack Jansenf5c20571997-01-30 15:48:07 +0000336static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000337mv_getgender(self, args)
338 mvobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000339 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000340{
Jack Jansenf5c20571997-01-30 15:48:07 +0000341 PyObject *rv;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000342
Jack Jansenf5c20571997-01-30 15:48:07 +0000343 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000344 return NULL;
345 if (!self->initialized) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000346 PyErr_SetString(ms_error_object, "Uninitialized voice");
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000347 return NULL;
348 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000349 rv = PyInt_FromLong(self->vd.gender);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000350 return rv;
351}
352
Jack Jansenf5c20571997-01-30 15:48:07 +0000353static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000354mv_newchannel(self, args)
355 mvobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000356 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000357{
Jack Jansenf5c20571997-01-30 15:48:07 +0000358 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000359 return NULL;
360 if (!self->initialized) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000361 PyErr_SetString(ms_error_object, "Uninitialized voice");
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000362 return NULL;
363 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000364 return (PyObject *)newscobject(&self->vs);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000365}
366
Jack Jansenf5c20571997-01-30 15:48:07 +0000367static struct PyMethodDef mv_methods[] = {
368 {"GetGender", (PyCFunction)mv_getgender},
369 {"NewChannel", (PyCFunction)mv_newchannel},
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000370 {NULL, NULL} /* sentinel */
371};
372
Jack Jansenf5c20571997-01-30 15:48:07 +0000373static PyObject *
Jack Jansen114ca5c1994-12-14 13:34:35 +0000374mv_getattr(self, name)
375 mvobject *self;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000376 char *name;
377{
Jack Jansenf5c20571997-01-30 15:48:07 +0000378 return Py_FindMethod(mv_methods, (PyObject *)self, name);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000379}
380
Jack Jansenf5c20571997-01-30 15:48:07 +0000381static PyTypeObject mvtype = {
382 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000383 0, /*ob_size*/
384 "MacVoice", /*tp_name*/
385 sizeof(mvobject), /*tp_basicsize*/
386 0, /*tp_itemsize*/
387 /* methods */
388 (destructor)mv_dealloc, /*tp_dealloc*/
389 0, /*tp_print*/
390 (getattrfunc)mv_getattr, /*tp_getattr*/
Jack Jansen114ca5c1994-12-14 13:34:35 +0000391 0, /*tp_setattr*/
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000392 0, /*tp_compare*/
393 0, /*tp_repr*/
394 0, /*tp_as_number*/
395 0, /*tp_as_sequence*/
396 0, /*tp_as_mapping*/
397 0, /*tp_hash*/
398};
399
400
401/* -------------
402**
403** Part three - The module interface
404*/
405
406/* See if Speech manager available */
407
Jack Jansenf5c20571997-01-30 15:48:07 +0000408static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000409ms_Available(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000410 PyObject *self; /* Not used */
411 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000412{
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000413
Jack Jansenf5c20571997-01-30 15:48:07 +0000414 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000415 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000416 return PyInt_FromLong(speech_available);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000417}
418
419/* Count number of busy speeches */
420
Jack Jansenf5c20571997-01-30 15:48:07 +0000421static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000422ms_Busy(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000423 PyObject *self; /* Not used */
424 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000425{
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000426 short result;
427
Jack Jansenf5c20571997-01-30 15:48:07 +0000428 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000429 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000430 if ( !check_available() )
431 return NULL;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000432 result = SpeechBusy();
Jack Jansenf5c20571997-01-30 15:48:07 +0000433 return PyInt_FromLong(result);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000434}
435
436/* Say something */
437
Jack Jansenf5c20571997-01-30 15:48:07 +0000438static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000439ms_SpeakString(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000440 PyObject *self; /* Not used */
441 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000442{
443 OSErr err;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000444 char *str;
445 int len;
446
Jack Jansenf5c20571997-01-30 15:48:07 +0000447 if (!PyArg_Parse(args, "s", &str))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000448 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000449 if ( !check_available())
450 return NULL;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000451 if (CurrentSpeech) {
452 /* Free the old speech, after killing it off
453 ** (note that speach is async and c2pstr works inplace)
454 */
455 SpeakString("\p");
456 free(CurrentSpeech);
457 }
458 len = strlen(str);
459 CurrentSpeech = malloc(len+1);
460 strcpy(CurrentSpeech, str);
461 err = SpeakString(c2pstr(CurrentSpeech));
Jack Jansen114ca5c1994-12-14 13:34:35 +0000462 if ( err ) {
463 PyErr_Mac(ms_error_object, err);
464 return NULL;
465 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000466 Py_INCREF(Py_None);
467 return Py_None;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000468}
469
470
471/* Count number of available voices */
472
Jack Jansenf5c20571997-01-30 15:48:07 +0000473static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000474ms_CountVoices(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000475 PyObject *self; /* Not used */
476 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000477{
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000478 short result;
479
Jack Jansenf5c20571997-01-30 15:48:07 +0000480 if (!PyArg_NoArgs(args))
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000481 return NULL;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000482 if ( !check_available())
483 return NULL;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000484 CountVoices(&result);
Jack Jansenf5c20571997-01-30 15:48:07 +0000485 return PyInt_FromLong(result);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000486}
487
Jack Jansenf5c20571997-01-30 15:48:07 +0000488static PyObject *
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000489ms_GetIndVoice(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000490 PyObject *self; /* Not used */
491 PyObject *args;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000492{
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000493 mvobject *rv;
494 long ind;
495
Jack Jansenf5c20571997-01-30 15:48:07 +0000496 if( !PyArg_Parse(args, "i", &ind))
Jack Jansen114ca5c1994-12-14 13:34:35 +0000497 return NULL;
498 if ( !check_available() )
499 return NULL;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000500 rv = newmvobject();
501 if ( !initmvobject(rv, ind) ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000502 Py_DECREF(rv);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000503 return NULL;
504 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000505 return (PyObject *)rv;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000506}
507
508
Jack Jansenf5c20571997-01-30 15:48:07 +0000509static PyObject *
Jack Jansen114ca5c1994-12-14 13:34:35 +0000510ms_Version(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000511 PyObject *self; /* Not used */
512 PyObject *args;
Jack Jansen114ca5c1994-12-14 13:34:35 +0000513{
Jack Jansen114ca5c1994-12-14 13:34:35 +0000514 NumVersion v;
515
Jack Jansenf5c20571997-01-30 15:48:07 +0000516 if (!PyArg_NoArgs(args))
Jack Jansen114ca5c1994-12-14 13:34:35 +0000517 return NULL;
518 if ( !check_available())
519 return NULL;
520 v = SpeechManagerVersion();
Jack Jansenf5c20571997-01-30 15:48:07 +0000521 return PyInt_FromLong(*(int *)&v);
Jack Jansen114ca5c1994-12-14 13:34:35 +0000522}
523
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000524
525/* List of functions defined in the module */
526
Jack Jansenf5c20571997-01-30 15:48:07 +0000527static struct PyMethodDef ms_methods[] = {
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000528 {"Available", ms_Available},
529 {"CountVoices", ms_CountVoices},
530 {"Busy", ms_Busy},
531 {"SpeakString", ms_SpeakString},
532 {"GetIndVoice", ms_GetIndVoice},
Jack Jansen114ca5c1994-12-14 13:34:35 +0000533 {"Version", ms_Version},
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000534 {NULL, NULL} /* sentinel */
535};
536
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000537/* Initialization function for the module (*must* be called initmacspeech) */
538
539void
540initmacspeech()
541{
Jack Jansenf5c20571997-01-30 15:48:07 +0000542 PyObject *m, *d;
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000543
Jack Jansen114ca5c1994-12-14 13:34:35 +0000544 speech_available = init_available();
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000545 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +0000546 m = Py_InitModule("macspeech", ms_methods);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000547
548 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +0000549 d = PyModule_GetDict(m);
Jack Jansen55e39271997-10-07 21:47:25 +0000550 ms_error_object = PyErr_NewException("macspeech.error", NULL, NULL);
Jack Jansenf5c20571997-01-30 15:48:07 +0000551 PyDict_SetItemString(d, "error", ms_error_object);
Guido van Rossum3a80c8d1994-10-02 11:33:59 +0000552}