home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / datafiles / text / c_manual / sound / easysound / example.c < prev    next >
Text File  |  1995-02-27  |  12KB  |  231 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Sound                   Amiga C Club       */
  7. /* Chapter: EasySound                   Tulevagen 22       */
  8. /* File:    Example.c                   181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-10                                       */
  11. /* Version: 2.15                                           */
  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. /* Now at last you can easily write C programs that plays digitized          */
  21. /* sound. You simply use four functions that will take care of all the       */
  22. /* work of allocating memory, loading the files, opening the ports and       */
  23. /* reserving the sound channels. Despite the simplicity you can still        */
  24. /* decide what volume and rate, which channel, and how many times the        */
  25. /* sound should be played etc. The functions contain full error checking,    */
  26. /* and will close and return everything that have been taken.                */
  27. /*                                                                           */
  28. /* I N S T R U C T I O N S                                                   */
  29. /* -----------------------                                                   */
  30. /*                                                                           */
  31. /* 1. There are four functions that you need to use. The first one is        */
  32. /* called ESPrepareSound(), and must be called before you can play the       */
  33. /* soundeffect. You simply give it a file name as the only parameter, and    */
  34. /* it will allocate space and load the sound file. It will also prepare      */
  35. /* some other things that is needed before you may play the sound. If        */
  36. /* ESPrepareSound() has successfully done its task, it will return a         */
  37. /* pointer to a SoundInfo structure (struct SoundInfo *), else it will       */
  38. /* return NULL which means something went wrong.                             */
  39. /*                                                                           */
  40. /*   Synopsis: pointer = ESPrepareSound( filename );                         */
  41. /*                                                                           */
  42. /*   pointer:  (struct SoundInfo *) ESPrepareSound() will return a pointer   */
  43. /*             to a SoundInfo structure if the sound was successfully        */
  44. /*             prepared, else it will return NULL which means something went */
  45. /*             wrong.                                                        */
  46. /*                                                                           */
  47. /*   filename: (STRPTR) Pointer to a string containing the name of the       */
  48. /*             sound file.                                                   */
  49. /*                                                                           */
  50. /*                                                                           */
  51. /*                                                                           */
  52. /* 2. After you have prepared the sound, you may play it. You do it by       */
  53. /* calling the function ESPlaySound(). If the sound was played               */
  54. /* successfully, TRUE is returned, else FALSE is returned which means        */
  55. /* something went wrong.                                                     */
  56. /*                                                                           */
  57. /*    Synopsis: ok = ESPlaySound( ptr, vol, cha, pri, drate, times, ddata,   */
  58. /*                                time, wait );                              */
  59. /*                                                                           */
  60. /*    ok:    (BOOL) If the sound was played successfully TRUE is             */
  61. /*           returned, else FALSE.                                           */
  62. /*                                                                           */
  63. /*    ptr:   (struct SoundInfo *) Pointer to a SoundInfo structure. This     */
  64. /*           pointer was returned by ESPrepareSound().                       */
  65. /*                                                                           */
  66. /*    vol:   (UWORD) Volume, 0 to 64.                                        */
  67. /*                                                                           */
  68. /*    cha:   (UBYTE) Which channel should be used. (LEFT0, RIGHT0,           */
  69. /*           RIGHT1 or LEFT1)                                                */
  70. /*                                                                           */
  71. /*    pri    (BYTE) What sound priority should be used. For normal sound     */
  72. /*           effects, set the priority to "SOUND_PRI_EFFECT". See file       */
  73. /*           "EasySound.h" for a complete list of recommended priorities.    */
  74. /*                                                                           */
  75. /*    drate: (WORD) Delta rate. When the sound is prepared, the record       */
  76. /*           rate is automatically stored in the SoundInfo structure,        */
  77. /*           so if you do not want to change the rate, write NORMALRATE.     */
  78. /*                                                                           */
  79. /*    times: (UWORD) How many times the sound should be played. If you       */
  80. /*           want to play the sound forever, write NONSTOP. (To stop a       */
  81. /*           sound call the function ESStopSound().)                         */
  82. /*                                                                           */
  83. /*    ddata: (ULONG) Where in the sound data we should start to play. (If    */
  84. /*           you want to start at the beginning of the sound data set ddata  */
  85. /*           to 0.)                                                          */
  86. /*                                                                           */
  87. /*    time:  (ULONG) For how long time the sound should be played. If you    */
  88. /*           want to play all of the sound data set time to 0.               */
  89. /*                                                                           */
  90. /*    wait:  (BOOL) If you want that the program waits for the sound to      */
  91. /*           to be completed set the flag "WAIT". If you do not want to      */
  92. /*           wait for the sound to be completed, set the flag "DO_NOT_WAIT". */
  93. /*                                                                           */
  94. /*                                                                           */
  95. /*                                                                           */
  96. /* 3. If you want to stop an audio channel you simply call the function      */
  97. /* ESStopSound(). (It is not dangerous to stop a sound that has already      */
  98. /* terminated, or has not started.)                                          */
  99. /*                                                                           */
  100. /*   Synopsis: ESStopSound( channel );                                       */
  101. /*                                                                           */
  102. /*   channel:  (UBYTE) Which channel should be stopped. (LEFT0, RIGHT0,      */
  103. /*             RIGHT1 or LEFT1)                                              */
  104. /*                                                                           */
  105. /*                                                                           */
  106. /*                                                                           */
  107. /* 4. Before your program terminates you must call the function              */
  108. /* ESRemoveSound() which will deallocate all memory that was allocated by    */
  109. /* the ESPrepareSound() function. IMPORTANT! All sound channels that is      */
  110. /* currentely playing the sound must have been stopped before this function  */
  111. /* may be called!                                                            */
  112. /*                                                                           */
  113. /*   Synopsis: ESRemoveSound( pointer );                                     */
  114. /*                                                                           */
  115. /*   pointer:  (struct SoundInfo *) The pointer that was returned by         */
  116. /*             ESPrepareSound().                                             */
  117. /*                                                                           */
  118. /*                                                                           */
  119. /*                                                                           */
  120. /* I hope you will have a lot of use of EASY-SOUND and happy programming, AB */
  121.  
  122.  
  123.  
  124. /* This file should be compiled separately and then linked together    */
  125. /* with the EasySound.o module. If you have the SAS/Lattice C compiler */
  126. /* you can do like this: (Do not include the quotations.)              */
  127. /*   1. Compile this file. Write: "lc Example"                         */
  128. /*   2. If you have changed something in the EasySound.c file you must */
  129. /*      recompile it. However, if you have not modified it you do not  */
  130. /*      have to recompile it. To compile it: "lc EasySound"            */
  131. /*   3. Finally you have to link these two files together. You can use */
  132. /*      the already prepared LNK file "Example.lnk" to do this. Simply */
  133. /*      write: "blink with Example.lnk"                                */
  134.  
  135.  
  136.  
  137. /* Include some important header files: */
  138. #include <exec/types.h> /* Declares CPTR, BOOL and STRPTR. */
  139. #include "EasySound.h"  /* Declares LEFT0, LEFT1, RIGHT0, etc. */
  140.  
  141.  
  142.  
  143. /* Pointers to the three sound effects: */
  144. struct SoundInfo *fire;
  145. struct SoundInfo *explosion;
  146. struct SoundInfo *background;
  147.  
  148.  
  149.  
  150. /* Declare the functions in this module: */
  151.  
  152. void main();
  153. void free_memory( STRPTR message );
  154.  
  155.  
  156.  
  157. void main()
  158. {
  159.   printf("\nE A S Y - S O U N D\n");
  160.   printf("Amiga C Club (ACC)\nAnders Bjerin\nTulevagen 22\n");
  161.   printf("181 41  LIDINGO\nSWEDEN\n\n");
  162.   printf("1. Prepare the sound Fire.snd\n");
  163.   fire = ESPrepareSound( "Fire.snd" );
  164.   if( !fire )
  165.     free_memory( "Could not prepare the sound effect!" );
  166.  
  167.   printf("   Prepare the sound Explosion.snd\n");
  168.   explosion = ESPrepareSound( "Explosion.snd" );
  169.   if( !explosion )
  170.     free_memory( "Could not prepare the sound effect!" );
  171.  
  172.   printf("   Prepare the sound Background.snd\n");
  173.   background = ESPrepareSound( "Background.snd" );
  174.   if( !background )
  175.     free_memory( "Could not prepare the sound effect!" );
  176.  
  177.  
  178.  
  179.   printf("2. Play the sound\n");
  180.  
  181.   /* Start with some atmospheric background sounds: */
  182.   ESPlaySound( background, MAXVOLUME/2, LEFT0, SOUND_PRI_EFFECT,
  183.                NORMALRATE, NONSTOP, 0, 0, DO_NOT_WAIT );
  184.   ESPlaySound( background, MAXVOLUME/2, RIGHT0, SOUND_PRI_EFFECT,
  185.                NORMALRATE, NONSTOP, 0, 0, DO_NOT_WAIT  );
  186.  
  187.   /* Wait four seconds: */
  188.   Delay( 4 * 50 );
  189.  
  190.   ESPlaySound( fire, MAXVOLUME, LEFT1, SOUND_PRI_EFFECT,
  191.                NORMALRATE, 2, 0, 0, DO_NOT_WAIT  );
  192.  
  193.   /* Wait three seconds: */
  194.   Delay( 3 * 50 );
  195.  
  196.   ESPlaySound( explosion, MAXVOLUME, RIGHT1,SOUND_PRI_EFFECT,
  197.                NORMALRATE, 2, 0, 0, DO_NOT_WAIT  );
  198.  
  199.   /* Wait four seconds: */
  200.   Delay( 4 * 50 );
  201.  
  202.  
  203.  
  204.   printf("3. Stop the audio channels\n");
  205.   ESStopSound( LEFT0 );
  206.   ESStopSound( LEFT1 );
  207.   ESStopSound( RIGHT0 );
  208.   ESStopSound( RIGHT1 );
  209.  
  210.  
  211.   printf("4. Remove the sound effects\n");
  212.   free_memory( "THE END" );
  213. }
  214.  
  215.  
  216. void free_memory( STRPTR message )
  217. {
  218.   printf("%s\n\n", message );
  219.  
  220.   /* It is not dangerous to try to remove a sound that has not been    */
  221.   /* prepared. We can therefore try to remove all sounds, even if some */
  222.   /* have not been initialized. However, all channels that are playing */
  223.   /* the sound must have been stopped before you may remove the sound! */
  224.   ESRemoveSound( fire );
  225.   ESRemoveSound( explosion );
  226.   ESRemoveSound( background );
  227.  
  228.   exit();
  229. }
  230.  
  231.