1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/*
* SC_Speech.h
* SC3lang
*
* Created by jan truetzschler v. falkenstein on Wed Apr 16 2003.
* Copyright (c) 2003 sampleAndHold.org. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <Carbon/Carbon.h>
#include "InitAlloc.h"
#include "SCBase.h"
#include "VMGlobals.h"
#include "PyrSymbolTable.h"
#include "PyrInterpreter.h"
#include "PyrKernel.h"
#include "PyrPrimitive.h"
#include "PyrObjectProto.h"
#include "PyrPrimitiveProto.h"
#include "PyrKernelProto.h"
#include "SC_InlineUnaryOp.h"
#include "SC_InlineBinaryOp.h"
#include "PyrSched.h"
#include "GC.h"
/////////////////////
const int kMaxSpeechChannels = 32;
PyrSymbol * s_speech;
PyrSymbol * s_speechwordAction;
PyrSymbol * s_speechdoneAction;
SpeechChannel fCurSpeechChannel[kMaxSpeechChannels];
char *speechStrings[kMaxSpeechChannels];
pascal void OurSpeechDoneCallBackProc ( SpeechChannel inSpeechChannel, long inRefCon );
pascal void OurSpeechDoneCallBackProc ( SpeechChannel inSpeechChannel, long inRefCon )
{
//call action here;
// post("text done");
pthread_mutex_lock (&gLangMutex);
VMGlobals *g = gMainVMGlobals;
g->canCallOS = true;
++g->sp; SetObject(g->sp, s_speech->u.classobj); // Set the class
//set arguments:
++g->sp;SetInt(g->sp, (int) inRefCon); //src
runInterpreter(g, s_speechdoneAction, 2);
if(speechStrings[(int) inRefCon] != NULL){
free(speechStrings[(int) inRefCon]);
speechStrings[(int) inRefCon] = NULL;
}
g->canCallOS = false;
pthread_mutex_unlock (&gLangMutex);
}
pascal void OurWordCallBackProc ( SpeechChannel inSpeechChannel, long inRefCon, long inWordPos, short inWordLen);
pascal void OurWordCallBackProc ( SpeechChannel inSpeechChannel, long inRefCon, long inWordPos, short inWordLen) {
//post("word done");
pthread_mutex_lock (&gLangMutex);
VMGlobals *g = gMainVMGlobals;
g->canCallOS = true;
++g->sp; SetObject(g->sp, s_speech->u.classobj);
//set arguments:
++g->sp; SetInt(g->sp, (int) inRefCon); //src
runInterpreter(g, s_speechwordAction, 2);
g->canCallOS = false;
pthread_mutex_unlock (&gLangMutex);
}
int prInitSpeech(struct VMGlobals *g, int numArgsPushed);
int prInitSpeech(struct VMGlobals *g, int numArgsPushed){
OSErr theErr = noErr;
//PyrSlot *a = g->sp-1;
PyrSlot *b = g->sp;
int chan;
slotIntVal(b, &chan);
if (chan < 0 || chan >= kMaxSpeechChannels) return errIndexOutOfRange;
for (int i=0; i<chan; ++i) {
if(fCurSpeechChannel[i]) DisposeSpeechChannel(fCurSpeechChannel[i]);
NewSpeechChannel( NULL, fCurSpeechChannel+i );
theErr = SetSpeechInfo (fCurSpeechChannel[i], soSpeechDoneCallBack, (const void*)OurSpeechDoneCallBackProc);
theErr = SetSpeechInfo (fCurSpeechChannel[i], soWordCallBack, (const void*)OurWordCallBackProc);
theErr = SetSpeechInfo (fCurSpeechChannel[i], soRefCon, (void*) i);
}
return errNone;
}
//NewSpeechDoneUPP(SpeechDoneProcPtr userRoutine);
//theErr = SetSpeechInfo (fCurSpeechChannel, soSpeechDoneCallBack, OurSpeechDoneCallBackProc);
int prSpeakText(struct VMGlobals *g, int numArgsPushed);
int prSpeakText(struct VMGlobals *g, int numArgsPushed){
OSErr theErr = noErr;
PyrSlot *obj = g->sp-2;
PyrSlot *a = g->sp-1;
PyrSlot *str = g->sp;
int chan;
slotIntVal(a, &chan);
chan = sc_clip(chan, 0, kMaxSpeechChannels);
if(speechStrings[chan] != NULL) {
post("voice %i already speaking\n", chan);
return errNone;
} else {
// speechStrings[chan] = (char*)pyr_pool_compile->Alloc((slotRawObject(a)->size + 1)* sizeof(char));
speechStrings[chan] = (char*) malloc((slotRawObject(str)->size + 1)* sizeof(char));
MEMFAIL(speechStrings[chan]);
slotStrVal(str, speechStrings[chan], slotRawObject(str)->size+1);
//if(!fCurSpeechChannel) theErr = NewSpeechChannel( NULL, &fCurSpeechChannel );
theErr = SpeakText( fCurSpeechChannel[chan], speechStrings[chan], strlen(speechStrings[chan]));
//should be freed only after the text was spoken!
// todo move this bit to the callback!
// pyr_pool_compile->Free(theTextToSpeak);
}
return errNone;
}
int prSetSpeechRate(struct VMGlobals *g, int numArgsPushed);
int prSetSpeechRate(struct VMGlobals *g, int numArgsPushed){
OSErr theErr = noErr;
//PyrSlot *a = g->sp-2;
PyrSlot *b = g->sp-1;
PyrSlot *c = g->sp;
double val;
int chan;
slotIntVal(b, &chan);
slotDoubleVal(c, &val);
Fixed newRate = (Fixed)(val * 65536.0);
// if(!fCurSpeechChannel) theErr = NewSpeechChannel( NULL, &fCurSpeechChannel );
theErr = SetSpeechInfo (fCurSpeechChannel[chan], soRate, &newRate);
return errNone;
}
int prSetSpeechPitch(struct VMGlobals *g, int numArgsPushed);
int prSetSpeechPitch(struct VMGlobals *g, int numArgsPushed){
OSErr theErr = noErr;
//PyrSlot *a = g->sp-2;
PyrSlot *b = g->sp-1;
PyrSlot *c = g->sp;
double val;
int chan;
slotIntVal(b, &chan);
slotDoubleVal(c, &val);
Fixed newVal = (Fixed)(val * 65536.0);
//if(!fCurSpeechChannel) theErr = NewSpeechChannel( NULL, &fCurSpeechChannel );
theErr = SetSpeechPitch (fCurSpeechChannel[chan], newVal);
return errNone;
}
int prSetSpeechPitchMod(struct VMGlobals *g, int numArgsPushed);
int prSetSpeechPitchMod(struct VMGlobals *g, int numArgsPushed){
OSErr theErr = noErr;
//PyrSlot *a = g->sp-2;
PyrSlot *b = g->sp-1;
PyrSlot *c = g->sp;
double val;
int chan;
slotIntVal(b, &chan);
slotDoubleVal(c, &val);
Fixed newVal = (Fixed)(val * 65536.0);
// if(!fCurSpeechChannel) theErr = NewSpeechChannel( NULL, &fCurSpeechChannel );
theErr = SetSpeechInfo (fCurSpeechChannel[chan], soPitchMod, &newVal);
return errNone;
}
int prSetSpeechVolume(struct VMGlobals *g, int numArgsPushed);
int prSetSpeechVolume(struct VMGlobals *g, int numArgsPushed) {
OSErr theErr = noErr;
//PyrSlot *a = g->sp-2;
PyrSlot *b = g->sp-1;
PyrSlot *c = g->sp;
double val;
int chan;
slotIntVal(b, &chan);
slotDoubleVal(c, &val);
Fixed newVal = (Fixed)(val * 65536.0);
// if(!fCurSpeechChannel) theErr = NewSpeechChannel( NULL, &fCurSpeechChannel );
theErr = SetSpeechInfo (fCurSpeechChannel[chan], soVolume, &newVal);
return errNone;
}
// theErr = PauseSpeechAt (fCurSpeechChannel, kImmediate);
// theErr = ContinueSpeech (fCurSpeechChannel);
int prSetSpeechPause(struct VMGlobals *g, int numArgsPushed);
int prSetSpeechPause(struct VMGlobals *g, int numArgsPushed){
OSErr theErr = noErr;
//PyrSlot *a = g->sp-2;
PyrSlot *b = g->sp-1;
PyrSlot *c = g->sp;
int val;
int chan;
slotIntVal(b, &chan);
slotIntVal(c, &val);
//Fixed newVal = (Fixed)(val * 65536.0);
if(val) {
theErr = ContinueSpeech(fCurSpeechChannel[chan] );
} else {
theErr = PauseSpeechAt(fCurSpeechChannel[chan], kImmediate);
}
return errNone;
}
int prSetSpeechStop(struct VMGlobals *g, int numArgsPushed);
int prSetSpeechStop(struct VMGlobals *g, int numArgsPushed){
OSErr theErr = noErr;
//PyrSlot *a = g->sp-2;
PyrSlot *b = g->sp-1;
PyrSlot *c = g->sp;
int selector [3] = {kImmediate, kEndOfWord, kEndOfWord};
int val;
int chan;
slotIntVal(b, &chan);
slotIntVal(c, &val);
StopSpeechAt(fCurSpeechChannel[chan], selector[val]);
if(speechStrings[chan] != NULL) {
free(speechStrings[chan]);
speechStrings[chan] = NULL;
}
return errNone;
}
int prSetSpeechVoice(struct VMGlobals *g, int numArgsPushed);
int prSetSpeechVoice(struct VMGlobals *g, int numArgsPushed){
OSErr theErr = noErr;
//PyrSlot *a = g->sp-2;
PyrSlot *b = g->sp-1;
PyrSlot *c = g->sp;
int val;
int chan;
VoiceSpec theVoiceSpec;
slotIntVal(b, &chan);
slotIntVal(c, &val);
theErr = GetIndVoice (val, &theVoiceSpec);
if (SetSpeechInfo (fCurSpeechChannel[chan], soCurrentVoice, &theVoiceSpec) == incompatibleVoice) return (!errNone);
return errNone;
}
int prSpeechVoiceIsSpeaking(struct VMGlobals *g, int numArgsPushed);
int prSpeechVoiceIsSpeaking(struct VMGlobals *g, int numArgsPushed){
PyrSlot *out = g->sp-1;
PyrSlot *b = g->sp;
int chan;
slotIntVal(b, &chan);
if(speechStrings[chan] != NULL) SetTrue(out);
else SetFalse(out);
return errNone;
}
void initSpeechPrimitives ()
{
int base, index;
base = nextPrimitiveIndex();
index = 0;
s_speechwordAction = getsym("doWordAction");
s_speechdoneAction = getsym("doSpeechDoneAction");
s_speech = getsym("Speech");
definePrimitive(base, index++, "_SpeakText", prSpeakText, 3, 0);
definePrimitive(base, index++, "_InitSpeech", prInitSpeech, 2, 0);
definePrimitive(base, index++, "_SetSpeechRate", prSetSpeechRate, 3, 0);
definePrimitive(base, index++, "_SetSpeechPitch", prSetSpeechPitch, 3, 0);
definePrimitive(base, index++, "_SetSpeechPitchMod", prSetSpeechPitchMod, 3, 0);
definePrimitive(base, index++, "_SetSpeechVoice", prSetSpeechVoice, 3, 0);
definePrimitive(base, index++, "_SetSpeechVolume", prSetSpeechVolume, 3, 0);
definePrimitive(base, index++, "_SetSpeechPause", prSetSpeechPause, 3, 0); //0 pause, 1 continue
definePrimitive(base, index++, "_SetSpeechStopAt", prSetSpeechStop, 3, 0); //0 kImmediate, 1 kEndOfWord, 2 kEndOfSentence
definePrimitive(base, index++, "_SpeechVoiceIsSpeaking", prSpeechVoiceIsSpeaking, 2, 0);
for(int i=0; i<kMaxSpeechChannels; ++i){
speechStrings[i] = NULL;
if(fCurSpeechChannel[i]) DisposeSpeechChannel(fCurSpeechChannel[i]);
fCurSpeechChannel[i] = NULL;
}
}
|