cpython/Mac/Modules/macspeechmodule.c

547 lines
11 KiB
C
Raw Normal View History

1994-10-02 08:33:59 -03:00
/***********************************************************
Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
1995-01-08 10:33:34 -04:00
The Netherlands.
1994-10-02 08:33:59 -03:00
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
1997-01-30 11:48:07 -04:00
#include "Python.h"
#include "macglue.h"
1994-10-02 08:33:59 -03:00
#include <Gestalt.h>
1994-10-02 08:33:59 -03:00
#include "Speech.h"
#ifdef __MWERKS__
#define OLDP2C 1
#include <TextUtils.h>
#ifndef c2pstr
#define c2pstr C2PStr
#endif
#ifndef p2cstr
#define p2cstr P2CStr
#endif
#else
#include "pascal.h"
#endif /* __MWERKS__ */
#include <CodeFragments.h>
int lib_available;
1994-10-02 08:33:59 -03:00
/* Somehow the Apple Fix2X and X2Fix don't do what I expect */
#define fixed2double(x) (((double)(x))/32768.0)
#define double2fixed(x) ((Fixed)((x)*32768.0))
char *CurrentSpeech;
1997-01-30 11:48:07 -04:00
PyObject *ms_error_object;
int speech_available;
1994-10-02 08:33:59 -03:00
static
init_available() {
OSErr err;
long result;
lib_available = ((ProcPtr)SpeakString != (ProcPtr)0);
err = Gestalt(gestaltSpeechAttr, &result);
if ( err == noErr && (result & (1<<gestaltSpeechMgrPresent)))
return 1;
return 0;
}
static
check_available() {
if ( !speech_available ) {
1997-01-30 11:48:07 -04:00
PyErr_SetString(ms_error_object, "Speech Mgr not available");
return 0;
}
if ( !lib_available ) {
1997-01-30 11:48:07 -04:00
PyErr_SetString(ms_error_object, "Speech Mgr available, but shared lib missing");
return 0;
}
return 1;
1994-10-02 08:33:59 -03:00
}
/* -------------
**
** Part one - the speech channel object
*/
typedef struct {
1997-01-30 11:48:07 -04:00
PyObject_HEAD
1994-10-02 08:33:59 -03:00
SpeechChannel chan;
1997-01-30 11:48:07 -04:00
PyObject *curtext; /* If non-NULL current text being spoken */
1994-10-02 08:33:59 -03:00
} scobject;
1997-01-30 11:48:07 -04:00
staticforward PyTypeObject sctype;
1994-10-02 08:33:59 -03:00
#define is_scobject(v) ((v)->ob_type == &sctype)
static scobject *
newscobject(arg)
VoiceSpec *arg;
{
scobject *self;
1994-10-02 08:33:59 -03:00
OSErr err;
1997-01-30 11:48:07 -04:00
self = PyObject_NEW(scobject, &sctype);
if (self == NULL)
1994-10-02 08:33:59 -03:00
return NULL;
if ( (err=NewSpeechChannel(arg, &self->chan)) != 0) {
1997-01-30 11:48:07 -04:00
Py_DECREF(self);
return (scobject *)PyErr_Mac(ms_error_object, err);
1994-10-02 08:33:59 -03:00
}
self->curtext = NULL;
return self;
1994-10-02 08:33:59 -03:00
}
/* sc methods */
static void
sc_dealloc(self)
scobject *self;
1994-10-02 08:33:59 -03:00
{
DisposeSpeechChannel(self->chan);
1997-01-30 11:48:07 -04:00
PyMem_DEL(self);
1994-10-02 08:33:59 -03:00
}
1997-01-30 11:48:07 -04:00
static PyObject *
1994-10-02 08:33:59 -03:00
sc_Stop(self, args)
scobject *self;
1997-01-30 11:48:07 -04:00
PyObject *args;
1994-10-02 08:33:59 -03:00
{
OSErr err;
1997-01-30 11:48:07 -04:00
if (!PyArg_NoArgs(args))
1994-10-02 08:33:59 -03:00
return NULL;
if ((err=StopSpeech(self->chan)) != 0) {
PyErr_Mac(ms_error_object, err);
return NULL;
}
1994-10-02 08:33:59 -03:00
if ( self->curtext ) {
1997-01-30 11:48:07 -04:00
Py_DECREF(self->curtext);
1994-10-02 08:33:59 -03:00
self->curtext = NULL;
}
1997-01-30 11:48:07 -04:00
Py_INCREF(Py_None);
return Py_None;
1994-10-02 08:33:59 -03:00
}
1997-01-30 11:48:07 -04:00
static PyObject *
1994-10-02 08:33:59 -03:00
sc_SpeakText(self, args)
scobject *self;
1997-01-30 11:48:07 -04:00
PyObject *args;
1994-10-02 08:33:59 -03:00
{
OSErr err;
char *str;
int len;
if (!PyArg_Parse(args, "s#", &str, &len))
1994-10-02 08:33:59 -03:00
return NULL;
if ( self->curtext ) {
StopSpeech(self->chan);
1997-01-30 11:48:07 -04:00
Py_DECREF(self->curtext);
1994-10-02 08:33:59 -03:00
self->curtext = NULL;
}
if ((err=SpeakText(self->chan, (Ptr)str, (long)len)) != 0) {
PyErr_Mac(ms_error_object, err);
return 0;
}
1997-01-30 11:48:07 -04:00
(void)PyArg_Parse(args, "O", &self->curtext); /* Or should I check this? */
Py_INCREF(self->curtext);
Py_INCREF(Py_None);
return Py_None;
1994-10-02 08:33:59 -03:00
}
1997-01-30 11:48:07 -04:00
static PyObject *
1994-10-02 08:33:59 -03:00
sc_GetRate(self, args)
scobject *self;
1997-01-30 11:48:07 -04:00
PyObject *args;
1994-10-02 08:33:59 -03:00
{
OSErr err;
Fixed farg;
1997-01-30 11:48:07 -04:00
if (!PyArg_NoArgs(args))
1994-10-02 08:33:59 -03:00
return NULL;
if ((err=GetSpeechRate(self->chan, &farg)) != 0) {
PyErr_Mac(ms_error_object, err);
return 0;
}
1997-01-30 11:48:07 -04:00
return PyFloat_FromDouble(fixed2double(farg));
1994-10-02 08:33:59 -03:00
}
1997-01-30 11:48:07 -04:00
static PyObject *
1994-10-02 08:33:59 -03:00
sc_GetPitch(self, args)
scobject *self;
1997-01-30 11:48:07 -04:00
PyObject *args;
1994-10-02 08:33:59 -03:00
{
OSErr err;
Fixed farg;
1997-01-30 11:48:07 -04:00
if (!PyArg_NoArgs(args))
1994-10-02 08:33:59 -03:00
return NULL;
if ((err=GetSpeechPitch(self->chan, &farg)) != 0) {
PyErr_Mac(ms_error_object, err);
return 0;
}
1997-01-30 11:48:07 -04:00
return PyFloat_FromDouble(fixed2double(farg));
1994-10-02 08:33:59 -03:00
}
1997-01-30 11:48:07 -04:00
static PyObject *
1994-10-02 08:33:59 -03:00
sc_SetRate(self, args)
scobject *self;
1997-01-30 11:48:07 -04:00
PyObject *args;
1994-10-02 08:33:59 -03:00
{
OSErr err;
double darg;
1997-01-30 11:48:07 -04:00
if (!PyArg_Parse(args, "d", &darg))
1994-10-02 08:33:59 -03:00
return NULL;
if ((err=SetSpeechRate(self->chan, double2fixed(darg))) != 0) {
PyErr_Mac(ms_error_object, err);
return 0;
}
1997-01-30 11:48:07 -04:00
Py_INCREF(Py_None);
return Py_None;
1994-10-02 08:33:59 -03:00
}
1997-01-30 11:48:07 -04:00
static PyObject *
1994-10-02 08:33:59 -03:00
sc_SetPitch(self, args)
scobject *self;
1997-01-30 11:48:07 -04:00
PyObject *args;
1994-10-02 08:33:59 -03:00
{
OSErr err;
double darg;
1997-01-30 11:48:07 -04:00
if (!PyArg_Parse(args, "d", &darg))
1994-10-02 08:33:59 -03:00
return NULL;
if ((err=SetSpeechPitch(self->chan, double2fixed(darg))) != 0) {
PyErr_Mac(ms_error_object, err);
return NULL;
}
1997-01-30 11:48:07 -04:00
Py_INCREF(Py_None);
return Py_None;
1994-10-02 08:33:59 -03:00
}
1997-01-30 11:48:07 -04:00
static struct PyMethodDef sc_methods[] = {
{"Stop", (PyCFunction)sc_Stop},
{"SetRate", (PyCFunction)sc_SetRate},
{"GetRate", (PyCFunction)sc_GetRate},
{"SetPitch", (PyCFunction)sc_SetPitch},
{"GetPitch", (PyCFunction)sc_GetPitch},
{"SpeakText", (PyCFunction)sc_SpeakText},
1994-10-02 08:33:59 -03:00
{NULL, NULL} /* sentinel */
};
1997-01-30 11:48:07 -04:00
static PyObject *
sc_getattr(self, name)
scobject *self;
1994-10-02 08:33:59 -03:00
char *name;
{
1997-01-30 11:48:07 -04:00
return Py_FindMethod(sc_methods, (PyObject *)self, name);
1994-10-02 08:33:59 -03:00
}
1997-01-30 11:48:07 -04:00
static PyTypeObject sctype = {
PyObject_HEAD_INIT(&PyType_Type)
1994-10-02 08:33:59 -03:00
0, /*ob_size*/
"MacSpeechChannel", /*tp_name*/
sizeof(scobject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)sc_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)sc_getattr, /*tp_getattr*/
0, /*tp_setattr*/
1994-10-02 08:33:59 -03:00
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
};
/* -------------
**
** Part two - the voice object
*/
typedef struct {
1997-01-30 11:48:07 -04:00
PyObject_HEAD
1994-10-02 08:33:59 -03:00
int initialized;
VoiceSpec vs;
VoiceDescription vd;
} mvobject;
1997-01-30 11:48:07 -04:00
staticforward PyTypeObject mvtype;
1994-10-02 08:33:59 -03:00
#define is_mvobject(v) ((v)->ob_type == &mvtype)
static mvobject *
newmvobject()
{
mvobject *self;
1997-01-30 11:48:07 -04:00
self = PyObject_NEW(mvobject, &mvtype);
if (self == NULL)
1994-10-02 08:33:59 -03:00
return NULL;
self->initialized = 0;
return self;
1994-10-02 08:33:59 -03:00
}
static int
initmvobject(self, ind)
mvobject *self;
int ind;
{
OSErr err;
if ( (err=GetIndVoice((short)ind, &self->vs)) != 0 ) {
PyErr_Mac(ms_error_object, err);
1994-10-02 08:33:59 -03:00
return 0;
}
if ( (err=GetVoiceDescription(&self->vs, &self->vd, sizeof self->vd)) != 0) {
PyErr_Mac(ms_error_object, err);
1994-10-02 08:33:59 -03:00
return 0;
}
self->initialized = 1;
return 1;
}
/* mv methods */
static void
mv_dealloc(self)
mvobject *self;
1994-10-02 08:33:59 -03:00
{
1997-01-30 11:48:07 -04:00
PyMem_DEL(self);
1994-10-02 08:33:59 -03:00
}
1997-01-30 11:48:07 -04:00
static PyObject *
1994-10-02 08:33:59 -03:00
mv_getgender(self, args)
mvobject *self;
1997-01-30 11:48:07 -04:00
PyObject *args;
1994-10-02 08:33:59 -03:00
{
1997-01-30 11:48:07 -04:00
PyObject *rv;
1994-10-02 08:33:59 -03:00
1997-01-30 11:48:07 -04:00
if (!PyArg_NoArgs(args))
1994-10-02 08:33:59 -03:00
return NULL;
if (!self->initialized) {
1997-01-30 11:48:07 -04:00
PyErr_SetString(ms_error_object, "Uninitialized voice");
1994-10-02 08:33:59 -03:00
return NULL;
}
1997-01-30 11:48:07 -04:00
rv = PyInt_FromLong(self->vd.gender);
1994-10-02 08:33:59 -03:00
return rv;
}
1997-01-30 11:48:07 -04:00
static PyObject *
1994-10-02 08:33:59 -03:00
mv_newchannel(self, args)
mvobject *self;
1997-01-30 11:48:07 -04:00
PyObject *args;
1994-10-02 08:33:59 -03:00
{
1997-01-30 11:48:07 -04:00
if (!PyArg_NoArgs(args))
1994-10-02 08:33:59 -03:00
return NULL;
if (!self->initialized) {
1997-01-30 11:48:07 -04:00
PyErr_SetString(ms_error_object, "Uninitialized voice");
1994-10-02 08:33:59 -03:00
return NULL;
}
1997-01-30 11:48:07 -04:00
return (PyObject *)newscobject(&self->vs);
1994-10-02 08:33:59 -03:00
}
1997-01-30 11:48:07 -04:00
static struct PyMethodDef mv_methods[] = {
{"GetGender", (PyCFunction)mv_getgender},
{"NewChannel", (PyCFunction)mv_newchannel},
1994-10-02 08:33:59 -03:00
{NULL, NULL} /* sentinel */
};
1997-01-30 11:48:07 -04:00
static PyObject *
mv_getattr(self, name)
mvobject *self;
1994-10-02 08:33:59 -03:00
char *name;
{
1997-01-30 11:48:07 -04:00
return Py_FindMethod(mv_methods, (PyObject *)self, name);
1994-10-02 08:33:59 -03:00
}
1997-01-30 11:48:07 -04:00
static PyTypeObject mvtype = {
PyObject_HEAD_INIT(&PyType_Type)
1994-10-02 08:33:59 -03:00
0, /*ob_size*/
"MacVoice", /*tp_name*/
sizeof(mvobject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)mv_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)mv_getattr, /*tp_getattr*/
0, /*tp_setattr*/
1994-10-02 08:33:59 -03:00
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
};
/* -------------
**
** Part three - The module interface
*/
/* See if Speech manager available */
1997-01-30 11:48:07 -04:00
static PyObject *
1994-10-02 08:33:59 -03:00
ms_Available(self, args)
1997-01-30 11:48:07 -04:00
PyObject *self; /* Not used */
PyObject *args;
1994-10-02 08:33:59 -03:00
{
1997-01-30 11:48:07 -04:00
if (!PyArg_NoArgs(args))
1994-10-02 08:33:59 -03:00
return NULL;
1997-01-30 11:48:07 -04:00
return PyInt_FromLong(speech_available);
1994-10-02 08:33:59 -03:00
}
/* Count number of busy speeches */
1997-01-30 11:48:07 -04:00
static PyObject *
1994-10-02 08:33:59 -03:00
ms_Busy(self, args)
1997-01-30 11:48:07 -04:00
PyObject *self; /* Not used */
PyObject *args;
1994-10-02 08:33:59 -03:00
{
short result;
1997-01-30 11:48:07 -04:00
if (!PyArg_NoArgs(args))
1994-10-02 08:33:59 -03:00
return NULL;
if ( !check_available() )
return NULL;
1994-10-02 08:33:59 -03:00
result = SpeechBusy();
1997-01-30 11:48:07 -04:00
return PyInt_FromLong(result);
1994-10-02 08:33:59 -03:00
}
/* Say something */
1997-01-30 11:48:07 -04:00
static PyObject *
1994-10-02 08:33:59 -03:00
ms_SpeakString(self, args)
1997-01-30 11:48:07 -04:00
PyObject *self; /* Not used */
PyObject *args;
1994-10-02 08:33:59 -03:00
{
OSErr err;
char *str;
int len;
1997-01-30 11:48:07 -04:00
if (!PyArg_Parse(args, "s", &str))
1994-10-02 08:33:59 -03:00
return NULL;
if ( !check_available())
return NULL;
1994-10-02 08:33:59 -03:00
if (CurrentSpeech) {
/* Free the old speech, after killing it off
** (note that speach is async and c2pstr works inplace)
*/
SpeakString("\p");
free(CurrentSpeech);
}
len = strlen(str);
CurrentSpeech = malloc(len+1);
strcpy(CurrentSpeech, str);
err = SpeakString(c2pstr(CurrentSpeech));
if ( err ) {
PyErr_Mac(ms_error_object, err);
return NULL;
}
1997-01-30 11:48:07 -04:00
Py_INCREF(Py_None);
return Py_None;
1994-10-02 08:33:59 -03:00
}
/* Count number of available voices */
1997-01-30 11:48:07 -04:00
static PyObject *
1994-10-02 08:33:59 -03:00
ms_CountVoices(self, args)
1997-01-30 11:48:07 -04:00
PyObject *self; /* Not used */
PyObject *args;
1994-10-02 08:33:59 -03:00
{
short result;
1997-01-30 11:48:07 -04:00
if (!PyArg_NoArgs(args))
1994-10-02 08:33:59 -03:00
return NULL;
if ( !check_available())
return NULL;
1994-10-02 08:33:59 -03:00
CountVoices(&result);
1997-01-30 11:48:07 -04:00
return PyInt_FromLong(result);
1994-10-02 08:33:59 -03:00
}
1997-01-30 11:48:07 -04:00
static PyObject *
1994-10-02 08:33:59 -03:00
ms_GetIndVoice(self, args)
1997-01-30 11:48:07 -04:00
PyObject *self; /* Not used */
PyObject *args;
1994-10-02 08:33:59 -03:00
{
mvobject *rv;
long ind;
1997-01-30 11:48:07 -04:00
if( !PyArg_Parse(args, "i", &ind))
return NULL;
if ( !check_available() )
return NULL;
1994-10-02 08:33:59 -03:00
rv = newmvobject();
if ( !initmvobject(rv, ind) ) {
1997-01-30 11:48:07 -04:00
Py_DECREF(rv);
1994-10-02 08:33:59 -03:00
return NULL;
}
1997-01-30 11:48:07 -04:00
return (PyObject *)rv;
1994-10-02 08:33:59 -03:00
}
1997-01-30 11:48:07 -04:00
static PyObject *
ms_Version(self, args)
1997-01-30 11:48:07 -04:00
PyObject *self; /* Not used */
PyObject *args;
{
NumVersion v;
1997-01-30 11:48:07 -04:00
if (!PyArg_NoArgs(args))
return NULL;
if ( !check_available())
return NULL;
v = SpeechManagerVersion();
1997-01-30 11:48:07 -04:00
return PyInt_FromLong(*(int *)&v);
}
1994-10-02 08:33:59 -03:00
/* List of functions defined in the module */
1997-01-30 11:48:07 -04:00
static struct PyMethodDef ms_methods[] = {
1994-10-02 08:33:59 -03:00
{"Available", ms_Available},
{"CountVoices", ms_CountVoices},
{"Busy", ms_Busy},
{"SpeakString", ms_SpeakString},
{"GetIndVoice", ms_GetIndVoice},
{"Version", ms_Version},
1994-10-02 08:33:59 -03:00
{NULL, NULL} /* sentinel */
};
/* Initialization function for the module (*must* be called initmacspeech) */
void
initmacspeech()
{
1997-01-30 11:48:07 -04:00
PyObject *m, *d;
1994-10-02 08:33:59 -03:00
speech_available = init_available();
1994-10-02 08:33:59 -03:00
/* Create the module and add the functions */
1997-01-30 11:48:07 -04:00
m = Py_InitModule("macspeech", ms_methods);
1994-10-02 08:33:59 -03:00
/* Add some symbolic constants to the module */
1997-01-30 11:48:07 -04:00
d = PyModule_GetDict(m);
1997-10-07 18:47:25 -03:00
ms_error_object = PyErr_NewException("macspeech.error", NULL, NULL);
1997-01-30 11:48:07 -04:00
PyDict_SetItemString(d, "error", ms_error_object);
1994-10-02 08:33:59 -03:00
}