home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Carousel
/
CAROUSEL.cdr
/
mactosh
/
code
/
c_macula.sit
< prev
next >
Wrap
Text File
|
1988-06-20
|
10KB
|
496 lines
18-Jun-88 14:51:15-MDT,10295;000000000000
Return-Path: <u-lchoqu%sunset@cs.utah.edu>
Received: from cs.utah.edu by SIMTEL20.ARPA with TCP; Sat, 18 Jun 88 14:51:02 MDT
Received: by cs.utah.edu (5.54/utah-2.0-cs)
id AA22805; Sat, 18 Jun 88 14:51:02 MDT
Received: by sunset.utah.edu (5.54/utah-2.0-leaf)
id AA24880; Sat, 18 Jun 88 14:50:59 MDT
Date: Sat, 18 Jun 88 14:50:59 MDT
From: u-lchoqu%sunset@cs.utah.edu (Lee Choquette)
Message-Id: <8806182050.AA24880@sunset.utah.edu>
To: rthum@simtel20.arpa
Subject: macula.c
/*
* Macula:
*
* A Megamax C (we used version 2.1) program to read and speak a
* file of TEXT messages. For a good time, make up a file of
* phrases, attach a speaker to your Macintosh, and hide it outside
* your door on Halloween night. (Hide the speaker, not the Mac!)
*
* The system-file "Macintalk" will need to be present; if in doubt,
* put it into the same folder as Macula.
* Upon startup, pick a TEXT file of phrases.
* Change the pitch, rate, etc. by menu choices.
* The robotic/natural speech choice is non-functional because the
* Macintalk documentation didn't tell us what values to plug in
* to effect the change.
*
* By Neil Groundwater and Mike O'Dell
*/
#include <event.h>
#include <pack.h>
#include <menu.h>
#include <win.h>
#include <mem.h>
#include <qdvars.h>
#include <misc.h>
#include <stdio.h>
#define MAXPHRASES 500
#define MAXPHLENGTH 2048
#define ABOUT 1
#define REREAD 1
#define QUIT 3
MenuHandle menus[5];
#define APPLE_ID 200
#define FILE_ID 201
#define DELAY_ID 202
#define PITCH_ID 203
#define RATE_ID 204
short repeat_rate;
short doneFlag = 0;
short duration = 1;
Handle thespeech = NULL; /* Handle to speech subsystem */
Handle phonemes = NULL; /* output Handle used for the Reader() */
char *phrases[MAXPHRASES];
int phrasecount = 0;
SFReply infile;
FILE *input;
#define TICKS 6L /* 1/10 second */
main()
{
int i, previous = -1;
long now;
extern char *slurpline();
extern int rand();
InitSys();
_autowin("Son of Macula");
fflush(stdout);
srand(now); /* randomize the seed */
if (speakinit() == -1) {
printf("speechinit() failed; exiting\n");
ExitToShell();
}
loadtext();
while (!doneFlag) {
i = pickone();
/* don't repeat immediately */
if (i == previous)
continue;
previous = i;
output(i);
if (repeat_rate)
duration = repeat_rate * TICKS;
else
duration = randrange(5*TICKS, 20*TICKS);
for (i = 0; i < duration; i++) {
Delay(10L, &now);
SystemTask();
EventTask();
}
}
speakuninit();
ExitToShell();
}
InitSys()
{
char s1[5];
InitGraf(&thePort);
InitFonts();
InitWindows();
InitDialogs(NULL);
InitCursor();
InitMenus();
s1[0] = 20;
s1[1] = 0;
menus[0] = NewMenu(APPLE_ID, s1);
AppendMenu(menus[0],"About Macula...");
/* AddResMenu(menus[0], "DRVR"); */
InsertMenu(menus[0], APPLE_ID);
menus[1] = NewMenu(FILE_ID, "File");
AppendMenu(menus[1], "Reread Phrases/R;(-;Quit/Q");
InsertMenu(menus[1],FILE_ID);
repeat_rate = 3;
menus[2] = NewMenu(DELAY_ID, "Delay");
AppendMenu(menus[2], "Random;(-;1 sec;5 secs;10 secs;15 secs");
InsertMenu(menus[2],DELAY_ID);
CheckItem(menus[2],3,TRUE);
menus[3] = NewMenu(PITCH_ID, "Pitch");
AppendMenu(menus[3],
"Natural;Robotic;(-;Very High;High;Medium;Low");
InsertMenu(menus[3],PITCH_ID);
CheckItem(menus[3],1,TRUE);
CheckItem(menus[3],6,TRUE);
menus[4] = NewMenu(RATE_ID, "Rate");
AppendMenu(menus[4], "Very Fast;Fast;Medium;Slow;Very Slow");
InsertMenu(menus[4],RATE_ID);
CheckItem(menus[4],4,TRUE);
DrawMenuBar();
FlushEvents(everyEvent, 0);
}
EventTask()
{
EventRecord event;
char c;
short windowcode;
WindowPtr mouseWindow;
if (GetNextEvent(everyEvent, &event))
switch (event.what) {
case autoKey:
case keyDown:
c = (char) event.message;
if ((event.modifiers & cmdKey))
HandleMenu(MenuKey(c));
break;
case mouseDown:
windowcode = FindWindow(&event.where, &mouseWindow);
switch (windowcode) {
case inMenuBar:
HandleMenu(MenuSelect(&event.where));
break;
case inSysWindow:
SystemClick(&event, mouseWindow);
break;
}
}
}
HandleMenu(menuID, itemNumber)
int menuID, itemNumber;
{
char result;
extern Handle thespeech;
short i;
switch (menuID) {
case APPLE_ID:
if (itemNumber == ABOUT)
DoAbout();
else {
struct P_Str AccessoryName;
GetItem(menus[0], itemNumber, &AccessoryName);
OpenDeskAcc(&AccessoryName);
DrawMenuBar();
}
break;
case FILE_ID:
switch (itemNumber) {
case REREAD:
rereadtext();
break;
case QUIT:
doneFlag = 1;
}
break;
case DELAY_ID:
for (i=1; i<=6; i++)
CheckItem(menus[2],i,FALSE);
switch (itemNumber) {
case 1: repeat_rate = 0; CheckItem(menus[2],1,TRUE); break;
case 2: break;
case 3: repeat_rate = 1; CheckItem(menus[2],3,TRUE); break;
case 4: repeat_rate = 5; CheckItem(menus[2],4,TRUE); break;
case 5: repeat_rate = 10; CheckItem(menus[2],5,TRUE); break;
case 6: repeat_rate = 15; CheckItem(menus[2],6,TRUE); break;
}
break;
case PITCH_ID:
if (itemNumber > 3)
for (i=4; i<=7; i++)
CheckItem(menus[3],i,FALSE);
switch (itemNumber) {
case 1:
/* I can't figure out the values for natural/robotic mode */
SpeechPitch(thespeech, 0, 1);
CheckItem(menus[3],1,TRUE); CheckItem(menus[3],2,FALSE);
break;
case 2:
SpeechPitch(thespeech, 0, 0);
CheckItem(menus[3],1,FALSE); CheckItem(menus[3],2,TRUE);
break;
case 3:
break;
case 4:
SpeechPitch(thespeech, 300, 0); CheckItem(menus[3],4,TRUE); break;
case 5:
SpeechPitch(thespeech, 180, 0); CheckItem(menus[3],5,TRUE); break;
case 6:
SpeechPitch(thespeech, 95, 0); CheckItem(menus[3],6,TRUE); break;
case 7:
SpeechPitch(thespeech, 70, 0); CheckItem(menus[3],7,TRUE); break;
}
duration = 0; /* break out of delay loop */
break;
case RATE_ID:
for (i=1; i<=5; i++)
CheckItem(menus[4],i,FALSE);
switch (itemNumber) {
case 1: SpeechRate(thespeech, 240); CheckItem(menus[4],1,TRUE); break;
case 2: SpeechRate(thespeech, 180); CheckItem(menus[4],2,TRUE); break;
case 3: SpeechRate(thespeech, 150); CheckItem(menus[4],3,TRUE); break;
case 4: SpeechRate(thespeech, 120); CheckItem(menus[4],4,TRUE); break;
case 5: SpeechRate(thespeech, 88); CheckItem(menus[4],5,TRUE); break;
}
}
if ((menuID >= APPLE_ID) && (menuID <= RATE_ID))
duration = 0; /* break out of delay loop */
HiliteMenu(0);
}
DoAbout()
{
WindowPtr aboutWindow;
Rect rectAbout;
SetRect(&rectAbout,80,100,430,160);
aboutWindow = NewWindow(0L,&rectAbout,"",TRUE,DBoxProc,0L,0L);
SetPort(aboutWindow);
TextFont(systemFont);
MoveTo(20,20);
DrawString("Macula - by Neil Groundwater and Mike O'Dell");
MoveTo(120,44);
DrawString("Happy Halloween!");
while(!Button());
while(Button());
DisposeWindow(aboutWindow);
}
/*
* loadtext(file) char *file;
* Load the text
* and construct the headers
*/
loadtext(file)
char *file;
{
Point wh;
wh.a.v = 70;
wh.a.h = 70;
do {
SFGetFile (&wh, NULL, NULL, 1, "TEXT", NULL, &infile);
if (infile.good == FALSE)
exit();
} while (!(input = fopen(&infile.fName, "r")));
readtext();
}
readtext()
{
char *slurpline();
for (phrasecount = 0; phrasecount < MAXPHRASES; phrasecount++)
if ((phrases[phrasecount] = slurpline(input)) == NULL)
break;
fclose(input);
/*
printf("Read %d phrases from the sayings file\n", phrasecount);
fflush(stdout);
*/
if (phrasecount <= 0) {
printf("loadtext() failed\n");
fflush(stdout);
ExitToShell();
}
}
rereadtext()
{
int i;
for (i = 0; i < phrasecount; i++)
free(phrases[i]);
input = fopen(&infile.fName, "r");
readtext();
}
char *slurpline(in)
FILE *in;
{
char *p;
int c, i;
char buf[MAXPHLENGTH+2];
char *malloc();
for (i = 0, p = buf; i < MAXPHLENGTH; i++, p++) {
if ((c = fgetc(in)) == EOF) {
return(NULL);
}
if (c == '\n')
break;
else
*p = c;
}
*p = '\0';
i++;
if ((p = malloc(i)) == NULL) {
printf("slurpline: malloc failed for i = %d\n", i);
return(NULL);
}
strcpy(p, buf);
return(p);
}
output(which)
int which;
{
/*
printf("[%d] %s\n", which, phrases[which]);
fflush(stdout);
*/
speakit(phrases[which]);
}
int posrand()
{
int rand();
int foo;
if ((foo = (rand() * rand())) < 0) {
return (-foo);
} else {
return(foo);
}
}
/*
* pick a number from [0..linecount]
*/
int pickone()
{
return (randrange(0, phrasecount));
}
/*
* return positive random int from [lo..hi]
*/
randrange(lo, hi)
int lo;
int hi;
{
int val;
for (val = posrand() % (hi+1);
val < lo || val > hi;
val = posrand() % (hi+1));
return(val);
}
/*
* say in (english) char *english;
* output the english string via the MacInTalk Reader()
*/
speakit(english)
char *english;
{
int i;
i = Reader(thespeech, english, (long)(strlen(english)), phonemes);
if (i != 0) {
printf("reader error %d processing\n\"%s\"\n", i, english);
fflush(stdout);
return(-1);
}
i = MacinTalk(thespeech, phonemes);
if (i != 0) {
printf("macintalk error %d. English was\n\"%s\"\n", i, english);
return(-1);
}
SetHandleSize(phonemes, 0L); /* scrap old phoneme data */
CompactMem(20000L); /* force a compaction */
return(0);
}
/*
* speakinit()
* Initialize the MacInTalk speech subsystem
*
* Returns -1 if initialization fails
*/
speakinit()
{
int i;
i = SpeechOn("", &thespeech); /* initialize the speach system */
if (i != 0) {
printf("Initializing MacInTalk got %d\n", i);
return(-1);
}
SpeechRate(thespeech, 120);
phonemes = NewHandle(0L); /* an empty Handle */
return(0);
}
/*
* speakuninit()
* Shut down the speech subsystem, release resources, dispose handles...
*/
speakuninit()
{
if (thespeech != NULL) {
SpeechOff(thespeech);
}
if (phonemes != NULL) {
DisposHandle(phonemes);
}
}