home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / datafiles / text / c_manual / devices / narratordevice / example3.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  7KB  |  250 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:    Example3.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 demonstrates how to translate a string into */
  21. /* a phonetical string which is then read by the narrator   */
  22. /* device.                                                  */
  23.  
  24.  
  25.  
  26. /* Declares the datatypes like STRPTR etc: */
  27. #include <exec/types.h>
  28.  
  29. /* We are using the narrator device: */
  30. #include <devices/narrator.h>
  31.  
  32.  
  33.  
  34. /* Size of the phonetic string buffer. */
  35. #define PHONETIC_BUFFER_SIZE 200
  36.  
  37. /* The audio channels: */
  38.  
  39. /* Values: */
  40. #define LEFT0B   0
  41. #define RIGHT0B  1
  42. #define RIGHT1B  2
  43. #define LEFT1B   3
  44.  
  45. /* Bit fields: */
  46. #define LEFT0F   (1<<LEFT0B)
  47. #define RIGHT0F  (1<<RIGHT0B)
  48. #define RIGHT1F  (1<<RIGHT1B)
  49. #define LEFT1F   (1<<LEFT1B)
  50.  
  51.  
  52.  
  53. /* Pointer to the translator library: */
  54. struct Library *TranslatorBase;
  55.  
  56.  
  57.  
  58. /* Declare a pointer to our reply port: */
  59. struct MsgPort *replymp = NULL;
  60.  
  61. /* Declare a pointer to our narrator request block: */
  62. struct narrator_rb *narrator_req = NULL;
  63.  
  64. /* The text should be read in stereo: */
  65. UBYTE allocation_array[]=
  66. {
  67.   LEFT0F|RIGHT0F, /* First left and first right channel.   */
  68.   LEFT0F|RIGHT1F, /* First left and second right channel.  */
  69.   LEFT1F|RIGHT0F, /* Second left and first right channel.  */
  70.   LEFT1F|RIGHT1F  /* Second left and second right channel. */
  71. };
  72.  
  73.  
  74.  
  75. /* Declare our functions: */
  76. void main();
  77. void clean_up( STRPTR text );
  78.  
  79.  
  80.  
  81. void main()
  82. {
  83.   /* Number of translated characters, or zero */
  84.   /* if all characters were translated:       */
  85.   int char_translated;
  86.   
  87.   /* The original string: */
  88.   char *original_string = "Hello world, this is the Amiga speeking!";
  89.   
  90.   /* The phonetic string: */
  91.   char phonetic_string[ PHONETIC_BUFFER_SIZE ];
  92.  
  93.   /* Store error values here: */
  94.   BYTE error;
  95.  
  96.  
  97.  
  98.  
  99.   /* Open the translator library: */
  100.   TranslatorBase = (struct Library *)
  101.     OpenLibrary( "translator.library", 0 );
  102.  
  103.   /* Have we successfully opened the library? */
  104.   if( !TranslatorBase )
  105.     clean_up( "Could not open the translator library!" );
  106.  
  107.  
  108.  
  109.   /* The translator library has now been opened, so    */
  110.   /* we may now start to use the Translate() function. */
  111.   
  112.  
  113.  
  114.   /* Translate our string into phonetics: (The Translate() */
  115.   /* functipon can sadly only translate English text, but  */
  116.   /* with small modifications of the phonetic string it    */
  117.   /* can be used with most languages.)                     */
  118.   char_translated =
  119.     Translate( original_string, strlen( original_string ),
  120.                phonetic_string, PHONETIC_BUFFER_SIZE );
  121.  
  122.   /* If all characters could successfully be translated */
  123.   /* and stored in the phonetic string Translate()      */ 
  124.   /* returns zero, else a negativa value is returned    */
  125.   /* which tells us how many characters were actually   */
  126.   /* translated: (Note that we put a minus sign infront */
  127.   /* of the variable to make it positive.)              */
  128.   if( char_translated )
  129.     printf( "Translated only %d characters!\n", -char_translated );
  130.   else
  131.     printf( "All characters successfully translated!\n" );
  132.  
  133.  
  134.  
  135.   /* Show the user what we got: */
  136.   printf( "Original string: %s\n", original_string );
  137.   printf( "Phonetic string: %s\n", phonetic_string );
  138.  
  139.  
  140.  
  141.   /* Get a reply port: (No name, priority 0) */
  142.   replymp = (struct MsgPort *)
  143.     CreatePort( NULL, 0 );
  144.   if( !replymp )
  145.     clean_up( "Could not create the reply port!" );
  146.  
  147.  
  148.  
  149.   /* Allocate and preinitialize a narrator request block: */
  150.   narrator_req = (struct narrator_rb *)
  151.     CreateExtIO( replymp, sizeof( struct narrator_rb ) );
  152.   if( !narrator_req )
  153.     clean_up( "Not enough memory for the narrator request!" );
  154.  
  155.  
  156.  
  157.   /* Open the Narrator Device: */
  158.   error = OpenDevice( "narrator.device", 0, narrator_req, 0 );
  159.   if( error )
  160.   {
  161.     /* Clear the "io_Device" flag since */
  162.     /* we have not opened the device:   */
  163.     narrator_req->message.io_Device = NULL;
  164.   
  165.     /* Quit: */
  166.     clean_up( "Could not open the Narrator Device!" );
  167.   }
  168.  
  169.  
  170.  
  171.   /* Set our requirements: */
  172.  
  173.   /* Set the length of the phonetic string: */
  174.   narrator_req->message.io_Length = strlen( phonetic_string );
  175.  
  176.   /* Give it a pointer to the phonetic string: */
  177.   narrator_req->message.io_Data = (APTR) phonetic_string;
  178.  
  179.   /* Send (write) the text to the device: */
  180.   narrator_req->message.io_Command = CMD_WRITE;
  181.  
  182.   /* Desired channel combinations: */
  183.   narrator_req->ch_masks = allocation_array;
  184.  
  185.   /* Size of the allocation array: */
  186.   narrator_req->nm_masks = sizeof( allocation_array );
  187.  
  188.  
  189.  
  190.   /* Start to read: */
  191.   SendIO( narrator_req );
  192.  
  193.   /* Tell the user that his/her Amiga is talking: */
  194.   printf( "The Amiga is speaking!\n" );
  195.     
  196.   /* Wait for the narrator to finish reading the text: */
  197.   error = WaitIO( narrator_req );
  198.  
  199.   /* Were there any errors? */
  200.   if( error )
  201.   {
  202.     printf( "Error code: %d\n", narrator_req->message.io_Error );
  203.     clean_up( "Error while reading!" );
  204.   }
  205.  
  206.  
  207.  
  208.   /* Clean up and quit: */
  209.   clean_up( "The End!" );
  210. }
  211.  
  212.  
  213.  
  214. /* Close and return everything that has been */
  215. /* opened and allocated before we quit:      */
  216.  
  217. void clean_up( STRPTR text )
  218. {
  219.   /* If we have a request block and the "io_Device" field  */
  220.   /* is not zero, we know that the device has successfully */
  221.   /* been opened and must now be closed:                   */ 
  222.   if( narrator_req && narrator_req->message.io_Device )
  223.     CloseDevice( narrator_req );
  224.  
  225.   /* Empty the reply port: */
  226.   while( GetMsg( replymp ) )
  227.     printf( "Collected a message at the reply port.\n" );
  228.  
  229.   /* Remove the replyport: */
  230.   if( replymp )
  231.     DeletePort( replymp);
  232.  
  233.   /* Dealocate the narrator request block: */
  234.   if( narrator_req )
  235.     DeleteExtIO( narrator_req, sizeof( struct narrator_rb ) );
  236.  
  237.   /* Close the translator library: */
  238.   if( TranslatorBase )
  239.     CloseLibrary( TranslatorBase );
  240.  
  241.  
  242.  
  243.   /* Print the last message: */
  244.   printf( "%s\n", text );
  245.  
  246.   /* Quit: */
  247.   exit( 0 );
  248. }
  249.  
  250.