home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 286.lha / TalkerHack_v2.5 / Talker.c < prev    next >
C/C++ Source or Header  |  1989-08-09  |  9KB  |  341 lines

  1.              /*  Talker Hack Version 2.5 03-22-89  */
  2.  
  3. #define ERR 1L
  4. #define SUCCESS 0L
  5. #define REV 0L
  6.  
  7. #include "exec/types.h"
  8. #include "exec/exec.h"
  9. #include "intuition/intuition.h"
  10. #include "intuition/intuitionbase.h"
  11. #include "devices/keymap.h"
  12. #include "hardware/blit.h"
  13. #include "devices/narrator.h"
  14. #include "libraries/translator.h"
  15. #include "stdio.h"
  16. #include "clib/macros.h"
  17. #include "face.h"
  18. #include "eyes.h"
  19. #include "mouthy.h"
  20. #include "mouthx.h"
  21. #include "stdio.h"
  22.  
  23. ULONG mclass,newmouth,neweyes;
  24. UBYTE in_string[256], out_string[1024];
  25.  
  26.          /* select which audio channels to use */
  27.  
  28. BYTE audio_chan[] = {3, 5, 10, 12};
  29.  
  30.        struct Library *OpenLibrary();
  31.        struct Library *TranslatorBase = NULL;
  32.        struct MsgPort *voice_port; /* audio message port */
  33.        struct MsgPort *mouth_port; /* video message port */
  34.        struct mouth_rb *mouth_io;
  35.        struct narrator_rb *voice_io;
  36.        struct IntuitionBase *IntuitionBase = NULL;
  37.        struct GfxBase   *GfxBase   = NULL;
  38.        struct Screen *TalkerScreen = NULL;
  39.        struct Window *TalkerWindow = NULL;
  40.        struct ViewPort *Vprt       = NULL;
  41.        struct RastPort *rast       = NULL;
  42.        struct IntuiMessage *mesg   = NULL;
  43. ULONG EyeArray[4] = { &eyes0_image,&eyes1_image,&eyes2_image,&eyes0_image };
  44.  
  45. ULONG *HArray[16] = {
  46.                         &mouth0_image, &mouth0_image,
  47.             &mouth1_image, &mouth3_image,
  48.                         &mouth3_image, &mouth4_image,
  49.                         &mouth5_image, &mouth6_image,
  50.                         &mouth7_image, &mouth7_image,
  51.                         &mouth8_image, &mouth9_image,
  52.                         &mouth10_image, &mouthx11_image,
  53.                         &mouthx11_image, &mouthx12_image 
  54.                       };
  55. ULONG *WArray[16] = {
  56.                         &mouthx0_image, &mouthx0_image,
  57.             &mouthx1_image, &mouthx2_image,
  58.                         &mouthx3_image, &mouthx3_image,
  59.                         &mouthx4_image, &mouthx5_image,
  60.                         &mouthx6_image, &mouthx7_image,
  61.                         &mouthx7_image, &mouthx8_image,
  62.                         &mouthx9_image, &mouthx10_image,
  63.                         &mouthx11_image, &mouthx12_image 
  64.                       };
  65. ULONG *BArray[16] = {
  66.                         &mouth0_image, &mouthx1_image,
  67.             &mouth2_image, &mouthx3_image,
  68.                         &mouthx4_image, &mouthx5_image,
  69.                         &mouthx6_image, &mouthx7_image,
  70.                         &mouthx8_image, &mouthx8_image,
  71.                         &mouthx9_image, &mouthx9_image,
  72.                         &mouthx10_image, &mouthx11_image,
  73.                         &mouthx12_image, &mouthx12_image 
  74.                       };
  75. static struct NewScreen NewScreenStructure = {
  76.     0,0,    /* screen XY origin relative to View */
  77.     640,200,    /* screen width and height */
  78.     3,    /* screen depth (number of bitplanes) */
  79.     0,1,    /* detail and block pens */
  80.     HIRES,    /* display modes for this screen */
  81.     CUSTOMSCREEN,    /* screen type */
  82.     NULL,    /* pointer to default screen font */
  83.     (UBYTE *)"© 1989 ",   /* screen title */
  84.     NULL,    /* first in list of custom screen gadgets */
  85.     NULL    /* pointer to custom BitMap structure */
  86. };
  87.  
  88. #define NEWSCREENSTRUCTURE NewScreenStructure
  89.  
  90. USHORT Palette[] = {
  91.    0x024C,   /* color #0 */
  92.    0x0FFF,   /* color #1 */
  93.    0x0000,   /* color #2 */
  94.    0x0851,   /* color #3 */
  95.    0x0FB9,   /* color #4 */
  96.    0x0EA8,   /* color #5 */
  97.    0x0C85,   /* color #6 */
  98.    0x0730   /* color #7 */
  99. #define PaletteColorCount 8
  100. };
  101.  
  102. #define PALETTE Palette
  103.  
  104. static struct NewWindow NewWindowStructure1 = {
  105.     400,0,    /* window XY origin relative to TopLeft of screen */
  106.     160,86, /* window width and height */
  107.     0,1,    /* detail and block pens */
  108.     WINDOWCLOSE,    /* IDCMP flags */
  109.     WINDOWDRAG+WINDOWCLOSE+SMART_REFRESH,/* window flags*/
  110.     NULL,    /* first gadget in gadget list */
  111.     NULL,    /* custom CHECKMARK imagery */
  112.     (UBYTE *)"Talker",    /* window title */
  113.     NULL,    /* custom screen pointer */
  114.     NULL,    /* custom bitmap */
  115.     160,86, /* minimum width and height */
  116.     -1,-1, /* maximum width and height  */
  117.     CUSTOMSCREEN    /* destination screen type */
  118. };
  119.  
  120.  
  121.       /* end of PowerWindows source generation */
  122.  
  123. main(argc,argv)
  124.  int argc;
  125.  char *argv[];
  126.  {
  127.   OpenLibs();
  128.   OpenWind();
  129.   prep_to_talk();
  130.    if( !argv[1] )
  131.      enter_text();
  132.    else
  133.       {
  134.        Delay(50);
  135.        Blink(5L);
  136.        say_file(argv[1]);
  137.        Delay(25);
  138.       };
  139.  
  140.   CloseWind(NULL);
  141.  }
  142.  
  143. changestring(in,inlen,out,outlen)
  144.  UBYTE *in,*out;
  145.  SHORT inlen,outlen;
  146.  {
  147.   if(Translate(in,inlen,out,outlen))
  148.     CloseWind("translation error");
  149.   return;
  150.  }
  151.  
  152. talk_to_me(speech)
  153.  UBYTE *speech;
  154.  {
  155.   voice_io->message.io_Data=(APTR)speech;
  156.   voice_io->message.io_Length=strlen(speech);
  157.   voice_io->ch_masks=audio_chan;
  158.   voice_io->nm_masks=sizeof(audio_chan);
  159.  
  160.   voice_io->mouths=1;
  161.   voice_io->volume=64;
  162.   voice_io->rate=150;
  163.   voice_io->sex=MALE;
  164.   voice_io->pitch=DEFPITCH;
  165.   voice_io->sampfreq=23000;
  166.   mouth_io->voice.message.io_Error = 0;
  167.  
  168.  SendIO(voice_io);
  169.  
  170.  while(mouth_io->voice.message.io_Error == 0)
  171.      {
  172.       DoIO(mouth_io);
  173.       if(mouth_io->height >= mouth_io->width)
  174.         newmouth = HArray[mouth_io->height];
  175.       else
  176.          newmouth = WArray[mouth_io->width];
  177.          DrawImage(rast,newmouth,13L,15L);
  178.  }
  179.   WaitIO(voice_io);
  180.   Closemouth(mouth_io->height,mouth_io->width);
  181.   Blink(2L);
  182.  
  183.    return;
  184. }
  185.  
  186. prep_to_talk()
  187. {
  188. if((voice_port=(struct MsgPort *)CreatePort(0,0))==NULL)
  189.    return(ERR);
  190.  
  191. if((mouth_port=(struct MsgPort *)CreatePort(0L,0L))==NULL)
  192.    return(ERR);
  193.  
  194. if((voice_io=(struct narrator_rb *)
  195.       CreateExtIO(voice_port,sizeof(struct narrator_rb)))==NULL)
  196.    return(ERR);
  197.  
  198. if((mouth_io=(struct mouth_rb *)
  199.       CreateExtIO(mouth_port,sizeof(struct mouth_rb)))==NULL)
  200.    return(ERR);
  201.  
  202. voice_io->message.io_Command=CMD_WRITE;
  203.  
  204. if(OpenDevice("narrator.device",0L,voice_io,0L)!=0L)
  205.    return(ERR);
  206.  
  207. mouth_io->voice.message.io_Device=voice_io->message.io_Device;
  208. mouth_io->voice.message.io_Unit=voice_io->message.io_Unit;
  209. mouth_io->width = 0;
  210. mouth_io->height = 0;
  211. mouth_io->voice.message.io_Command = CMD_READ;
  212. mouth_io->voice.message.io_Error = 0;
  213.  
  214.    return(SUCCESS);
  215.  
  216. }
  217.  
  218. OpenLibs()
  219.  {
  220.   TranslatorBase = (struct Library *)OpenLibrary("translator.library",REV); 
  221.   if(TranslatorBase == NULL)
  222.    {
  223.     CloseWind("couldn't open translator library");
  224.    }          
  225.   IntuitionBase = (struct Library *)OpenLibrary("intuition.library",0);
  226.   if(IntuitionBase == NULL)
  227.    {
  228.     CloseWind("couldn't open Intuition library");
  229.    }
  230.   GfxBase = (struct Library *)OpenLibrary("graphics.library",0); 
  231.   if(GfxBase == NULL)
  232.    {
  233.     CloseWind("couldn't open Graphics library");
  234.    }
  235.   return;
  236.  }
  237.  
  238. OpenWind()
  239.  {
  240.   if((TalkerScreen = (struct Screen *)OpenScreen(&NewScreenStructure)) == NULL)
  241.    {
  242.     CloseWind("Couldn't Open screen");
  243.    }
  244.   NewWindowStructure1.Screen = TalkerScreen;
  245.   if((TalkerWindow = (struct Window *)OpenWindow(&NewWindowStructure1)) == NULL)
  246.     CloseWind("Couldn't Open window");
  247.  
  248.   Vprt = (struct ViewPort *)ViewPortAddress(TalkerWindow);
  249.   LoadRGB4(Vprt,&PALETTE,PaletteColorCount);
  250.   rast = TalkerWindow->RPort;
  251.   DrawImage(rast,&face0_image,13L,15L);
  252.  
  253.    return;
  254.  }
  255. CloseWind(errorstring)
  256.  char *errorstring;
  257.  {
  258.   if(errorstring)
  259.     puts(errorstring);
  260.   if(voice_port)
  261.     DeletePort(voice_port);
  262.   if(mouth_port)
  263.     DeletePort(mouth_port); 
  264.   if(voice_io)
  265.    {
  266.     CloseDevice(voice_io); 
  267.     DeleteExtIO(voice_io); 
  268.    }
  269.   if(mouth_io)
  270.     DeleteExtIO(mouth_io); 
  271.   if(TalkerWindow)
  272.    {
  273.     while( mesg = (struct IntuiMessage *)GetMsg(TalkerWindow->UserPort) )
  274.          ReplyMsg(mesg);
  275.     CloseWindow(TalkerWindow);
  276.    }
  277.   if(TalkerScreen)
  278.     CloseScreen(TalkerScreen);
  279.   exit(0);
  280.  }
  281.  
  282. Blink(pauseme)
  283.  ULONG pauseme;
  284.  {
  285.   int x;
  286.   for(x = 0; x < 4; x++)
  287.    {
  288.     neweyes = EyeArray[x];
  289.     DrawImage(rast,neweyes,13L,15L);
  290.     Delay(pauseme);
  291.     }
  292. return;
  293.  }
  294.  
  295. Closemouth(high,wide)
  296.  UBYTE high,wide;
  297.  {
  298.   ULONG x;
  299.   if(high > wide)
  300.    {
  301.     for(x = high; x > 0; x--)
  302.        DrawImage(rast,HArray[x],13L,15L);
  303.    }
  304.   if(wide >= high)
  305.    {
  306.     for(x = wide; x > 0; x--)
  307.      DrawImage(rast,WArray[x],13L,15L);
  308.    }
  309.   return;
  310.  }
  311.  
  312. enter_text()
  313.  { 
  314.   Delay(25);
  315.   ScreenToBack(TalkerScreen);
  316.   printf("enter string: ");
  317.   gets(in_string);
  318.   ScreenToFront(TalkerScreen);
  319.   Delay(10);
  320.   Blink(10L);
  321.   changestring(in_string,strlen(in_string),out_string,1024L);
  322.   Delay(5);
  323.   Blink(5L);
  324.   talk_to_me(out_string);
  325.   return;
  326.  }
  327.  
  328. say_file(name)
  329.  char *name;
  330.  {
  331.   FILE   *f;      /* file pointer */
  332.   if ((f = fopen(name,"r")) == NULL)
  333.    CloseWind("can't open file\n" );
  334.   while(fgets(in_string,255,f) != 0L)
  335.       {
  336.        changestring(in_string,(long)strlen(in_string),out_string,1024L); 
  337.        talk_to_me(&out_string);
  338.       }
  339.   return;
  340.  }
  341.