home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / datafiles / text / c_manual / devices / narratordevice / example4.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  7KB  |  262 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Devices                 Amiga C Club       */
  7. /* Chapter: Narrator Device             Tulevagen 22       */
  8. /* File:    Example4.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-26                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example very similar to the previous one, but this */
  21. /* time are we using a different voice. By altering the    */
  22. /* rate, pitch, mode, sex and volume, you can produce very */
  23. /* different sounds.                                       */
  24.  
  25.  
  26.  
  27. /* Declares the datatypes like STRPTR etc: */
  28. #include <exec/types.h>
  29.  
  30. /* We are using the narrator device: */
  31. #include <devices/narrator.h>
  32.  
  33.  
  34.  
  35. /* Size of the phonetic string buffer. */
  36. #define PHONETIC_BUFFER_SIZE 200
  37.  
  38. /* The audio channels: */
  39.  
  40. /* Values: */
  41. #define LEFT0B   0
  42. #define RIGHT0B  1
  43. #define RIGHT1B  2
  44. #define LEFT1B   3
  45.  
  46. /* Bit fields: */
  47. #define LEFT0F   (1<<LEFT0B)
  48. #define RIGHT0F  (1<<RIGHT0B)
  49. #define RIGHT1F  (1<<RIGHT1B)
  50. #define LEFT1F   (1<<LEFT1B)
  51.  
  52.  
  53.  
  54. /* Pointer to the translator library: */
  55. struct Library *TranslatorBase;
  56.  
  57.  
  58.  
  59. /* Declare a pointer to our reply port: */
  60. struct MsgPort *replymp = NULL;
  61.  
  62. /* Declare a pointer to our narrator request block: */
  63. struct narrator_rb *narrator_req = NULL;
  64.  
  65. /* The text should be read with all channels: */
  66. UBYTE allocation_array[]={ LEFT0F | RIGHT0F | RIGHT1F | LEFT1F };
  67.  
  68.  
  69.  
  70. /* Declare our functions: */
  71. void main();
  72. void clean_up( STRPTR text );
  73.  
  74.  
  75.  
  76. void main()
  77. {
  78.   /* Number of translated characters, or zero */
  79.   /* if all characters were translated:       */
  80.   int char_translated;
  81.   
  82.   /* The original string: */
  83.   char *original_string = "My darling! my love! my user!";
  84.   
  85.   /* The phonetic string: */
  86.   char phonetic_string[ PHONETIC_BUFFER_SIZE ];
  87.  
  88.   /* Store error values here: */
  89.   BYTE error;
  90.  
  91.  
  92.  
  93.  
  94.   /* Open the translator library: */
  95.   TranslatorBase = (struct Library *)
  96.     OpenLibrary( "translator.library", 0 );
  97.  
  98.   /* Have we successfully opened the library? */
  99.   if( !TranslatorBase )
  100.     clean_up( "Could not open the translator library!" );
  101.  
  102.  
  103.  
  104.   /* The translator library has now been opened, so    */
  105.   /* we may now start to use the Translate() function. */
  106.   
  107.  
  108.  
  109.   /* Translate our string into phonetics: (The Translate() */
  110.   /* functipon can sadly only translate English text, but  */
  111.   /* with small modifications of the phonetic string it    */
  112.   /* can be used with most languages.)                     */
  113.   char_translated =
  114.     Translate( original_string, strlen( original_string ),
  115.                phonetic_string, PHONETIC_BUFFER_SIZE );
  116.  
  117.   /* If all characters could successfully be translated */
  118.   /* and stored in the phonetic string Translate()      */ 
  119.   /* returns zero, else a negativa value is returned    */
  120.   /* which tells us how many characters were actually   */
  121.   /* translated: (Note that we put a minus sign infront */
  122.   /* of the variable to make it positive.)              */
  123.   if( char_translated )
  124.     printf( "Translated only %d characters!\n", -char_translated );
  125.   else
  126.     printf( "All characters successfully translated!\n" );
  127.  
  128.  
  129.  
  130.   /* Show the user what we got: */
  131.   printf( "Original string: %s\n", original_string );
  132.   printf( "Phonetic string: %s\n", phonetic_string );
  133.  
  134.  
  135.  
  136.   /* Get a reply port: (No name, priority 0) */
  137.   replymp = (struct MsgPort *)
  138.     CreatePort( NULL, 0 );
  139.   if( !replymp )
  140.     clean_up( "Could not create the reply port!" );
  141.  
  142.  
  143.  
  144.   /* Allocate and preinitialize a narrator request block: */
  145.   narrator_req = (struct narrator_rb *)
  146.     CreateExtIO( replymp, sizeof( struct narrator_rb ) );
  147.   if( !narrator_req )
  148.     clean_up( "Not enough memory for the narrator request!" );
  149.  
  150.  
  151.  
  152.   /* Open the Narrator Device: */
  153.   error = OpenDevice( "narrator.device", 0, narrator_req, 0 );
  154.   if( error )
  155.   {
  156.     /* Clear the "io_Device" flag since */
  157.     /* we have not opened the device:   */
  158.     narrator_req->message.io_Device = NULL;
  159.   
  160.     /* Quit: */
  161.     clean_up( "Could not open the Narrator Device!" );
  162.   }
  163.  
  164.  
  165.  
  166.   /* Set our requirements: */
  167.  
  168.   /* Set the length of the phonetic string: */
  169.   narrator_req->message.io_Length = strlen( phonetic_string );
  170.  
  171.   /* Give it a pointer to the phonetic string: */
  172.   narrator_req->message.io_Data = (APTR) phonetic_string;
  173.  
  174.   /* Send (write) the text to the device: */
  175.   narrator_req->message.io_Command = CMD_WRITE;
  176.  
  177.   /* Desired channel combinations: */
  178.   narrator_req->ch_masks = allocation_array;
  179.  
  180.   /* Size of the allocation array: */
  181.   narrator_req->nm_masks = sizeof( allocation_array );
  182.  
  183.  
  184.  
  185.   /* Use default rate: (Read 150 words per minute.)  */
  186.   narrator_req->rate = DEFRATE;
  187.  
  188.   /* Use a bit higher pitch than the default (110): */
  189.   narrator_req->pitch = DEFPITCH + 80;
  190.  
  191.   /* Use the "natrual" voice: */
  192.   narrator_req->mode = NATURALF0;
  193.   
  194.   /* An Amiga girl is reading: (Hope she is nicer than she sounds...) */
  195.   narrator_req->sex = FEMALE;
  196.  
  197.   /* Use a soft (hmmm) voice: */
  198.   narrator_req->volume = MAXVOL / 2;
  199.  
  200.  
  201.  
  202.   /* Start to read: */
  203.   SendIO( narrator_req );
  204.  
  205.   /* Tell the user that the Amiga is reading: */
  206.   printf( "Computers can have emotions!\n" );
  207.     
  208.   /* Wait for the narrator to finish reading the text: */
  209.   error = WaitIO( narrator_req );
  210.  
  211.   /* Were there any errors? */
  212.   if( error )
  213.   {
  214.     printf( "Error code: %d\n", narrator_req->message.io_Error );
  215.     clean_up( "Error while reading!" );
  216.   }
  217.  
  218.  
  219.  
  220.   /* Clean up and quit: */
  221.   clean_up( "The End!" );
  222. }
  223.  
  224.  
  225.  
  226. /* Close and return everything that has been */
  227. /* opened and allocated before we quit:      */
  228.  
  229. void clean_up( STRPTR text )
  230. {
  231.   /* If we have a request block and the "io_Device" field  */
  232.   /* is not zero, we know that the device has successfully */
  233.   /* been opened and must now be closed:                   */ 
  234.   if( narrator_req && narrator_req->message.io_Device )
  235.     CloseDevice( narrator_req );
  236.  
  237.   /* Empty the reply port: */
  238.   while( GetMsg( replymp ) )
  239.     printf( "Collected a message at the reply port.\n" );
  240.  
  241.   /* Remove the replyport: */
  242.   if( replymp )
  243.     DeletePort( replymp);
  244.  
  245.   /* Dealocate the narrator request block: */
  246.   if( narrator_req )
  247.     DeleteExtIO( narrator_req, sizeof( struct narrator_rb ) );
  248.  
  249.   /* Close the translator library: */
  250.   if( TranslatorBase )
  251.     CloseLibrary( TranslatorBase );
  252.  
  253.  
  254.  
  255.   /* Print the last message: */
  256.   printf( "%s\n", text );
  257.  
  258.   /* Quit: */
  259.   exit( 0 );
  260. }
  261.  
  262.