home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / code_examples / cmanual_456 / easysound / example.c < prev    next >
C/C++ Source or Header  |  1990-11-24  |  7KB  |  211 lines

  1. /*   Example.c
  2.  
  3.      EEEEE  AAA   SSS Y   Y       SSS   OOO  U   U N   N DDD
  4.      E     A   A S     Y Y       S     O   O U   U NN  N D  D
  5.      EEEE  AAAAA  SSS   Y   ===   SSS  O   O U   U N N N D   D
  6.      E     A   A     S  Y            S O   O U   U N  NN D  D
  7.      EEEEE A   A SSSS   Y        SSSS   OOO   UUU  N   N DDD
  8.  
  9.  
  10.              EEEEE X   X  AAA  M   M PPPP  L     EEEEE
  11.              E      X X  A   A MM MM P   P L     E
  12.              EEEE    X   AAAAA M M M PPPP  L     EEEE
  13.              E      X X  A   A M   M P     L     E
  14.              EEEEE X   X A   A M   M P     LLLLL EEEEE
  15.  
  16.  
  17.    EASY-SOUND    EXAMPLE    V2.00    1990-09-23    ANDERS BJERIN
  18.  
  19. Now at last you can easily write C programs that plays digitized
  20. sound. You simply use four functions that will take care of all the
  21. work of allocating memory, loading the files, opening the ports and
  22. reserving the sound channels. Despite the simplicity you can still
  23. decide what volume and rate, which channel, and how many times the
  24. sound should be played. The functions contain full error checking,
  25. and will close and return everything that have been taken.
  26.  
  27. EASY-SOUND was written by Anders Bjerin, and is distributed as
  28. public domain with NO RIGHTS RESERVED. That means that you can do
  29. what ever you want with the program. You may use EASY-SOUND in your
  30. own programs, commercial or not, and do not even need to mention
  31. that you have used it. You may alter the source code to fit your
  32. needs, and you may spread it to anyone.
  33.  
  34. V2.00: You can now play the same sound in one or more channels at the
  35. same time. Before you could only use one of the four channels for
  36. each sound. Some code improvements have also been done.
  37.  
  38.  
  39. I N S T R U C T I O N S
  40.  
  41. 1. There are four functions that you need to use. The first one is called
  42. PrepareSound(), and must be called before you can play the soundeffect.
  43. You simply give it a file name as the only parameter, and it will
  44. allocate space and load the sound file. It will also prepare some other
  45. things that is needed before you may play the sound. If PrepareSound()
  46. has successfully done its task, it will return a normal memory pointer
  47. (CPTR), else it will return NULL which means something went wrong.
  48.  
  49. Synopsis: pointer = PrepareSound( filename );
  50.  
  51. pointer:  (CPTR) PrepareSound() will return a normal memory pointer if
  52.           the sound was prepared successfully, else it will return NULL
  53.                     which means something went wrong.
  54.  
  55. filename: (STRPTR) Pointer to a string containing the name of the
  56.           sound file.
  57.  
  58.  
  59.  
  60. 2. After you have prepared the sound, you may play it. You do it by calling
  61. the function PlaySound(). If the sound was played successfully, TRUE is
  62. returned, else FALSE is returned which means something went wrong.
  63.  
  64. Synopsis: ok = PlaySound( pointer, volume, channel, drate, times );
  65.  
  66. ok:       (BOOL) If the sound was played successfully TRUE is
  67.           returned, else FALSE.
  68.  
  69. pointer:  (CPTR) The pointer that was returned by PrepareSound().
  70.  
  71. volume:   (UWORD) Volume, 0 to 64. (MINVOLUME - MAXVOLUME)
  72.  
  73. channel:  (UBYTE) Which channel should be used. (LEFT0, RIGHT0,
  74.           RIGHT1 or LEFT1)
  75.  
  76. drate:    (WORD) Delta rate. When the sound is prepared, the record
  77.           rate is automatically stored in the SoundInfo structure,
  78.           so if you do not want to change the rate, write NORMALRATE.
  79.                     However, if you want to increase/decrease the speed, you
  80.                     simply write the desired delta value.
  81.  
  82. times:    (UWORD) How many times the sound should be played. If you
  83.           want to play the sound forever, write 0.
  84.  
  85.  
  86.  
  87. 3. If you want to stop an audio channel you simply call the function
  88. StopSound(). (It is not dangerous to stop a sound that has already
  89. terminated, or has not started.)
  90.  
  91. Synopsis: StopSound( channel );
  92.  
  93. channel:  (UBYTE) Which channel should be stopped. (LEFT0, RIGHT0,
  94.           RIGHT1 or LEFT1)
  95.  
  96.  
  97.  
  98. 4. Before your program terminates you must call the function RemoveSound()
  99. which will deallocate all memory that was allocated by the PrepareSound()
  100. function. IMPORTANT! All sound channels that is currentely playing the
  101. sound must have been stopped before this function may be called!
  102.  
  103. Synopsis: RemoveSound( pointer );
  104.  
  105. pointer:  (CPTR) The pointer that was returned by PrepareSound().
  106.  
  107.  
  108.  
  109. I hope you will have a lot of use of EASY-SOUND and happy programming,
  110.  
  111. Anders Bjerin
  112.  
  113. AMIGA C CLUB (ACC)
  114. Tulevagen 22
  115. 181 41  LIDINGO
  116. SWEDEN
  117.  
  118. */
  119.  
  120.  
  121. /* Include some important header files: */
  122. #include "exec/types.h" /* Declares CPTR, BOOL and STRPTR. */
  123. #include "EasySound.h"  /* Declares LEFT0, LEFT1, RIGHT0, etc. */
  124.  
  125.  
  126. /* Pointers to the three sound effects: */
  127. CPTR fire;
  128. CPTR explosion;
  129. CPTR background;
  130.  
  131.  
  132. /* Declare the functions in this module: */
  133. void main();
  134. void free_memory();
  135. void pause();
  136.  
  137.  
  138. void main()
  139. {
  140.     printf("\nE A S Y - S O U N D\n");
  141.     printf("Amiga C Club (ACC)\nAnders Bjerin\nTulevagen 22\n");
  142.     printf("181 41  LIDINGO\nSWEDEN\n\n");
  143.   printf("1. Prepare the sound Fire.snd\n");
  144.     fire = PrepareSound( "Fire.snd" );
  145.   if( !fire )
  146.       free_memory( "Could not prepare the sound effect!" );
  147.  
  148.   printf("   Prepare the sound Explosion.snd\n");
  149.     explosion = PrepareSound( "Explosion.snd" );
  150.   if( !explosion )
  151.       free_memory( "Could not prepare the sound effect!" );
  152.  
  153.   printf("   Prepare the sound Background.snd\n");
  154.     background = PrepareSound( "Background.snd" );
  155.   if( !background )
  156.       free_memory( "Could not prepare the sound effect!" );
  157.  
  158.  
  159.  
  160.   printf("2. Play the sound\n");
  161.  
  162.   /* Start with some atmospheric background sounds: */
  163.   PlaySound( background, MAXVOLUME/2, LEFT0, NORMALRATE, NONSTOP );
  164.   PlaySound( background, MAXVOLUME/2, RIGHT0, NORMALRATE, NONSTOP );
  165.     pause( 500 );
  166.  
  167.   PlaySound( fire, MAXVOLUME, LEFT1, NORMALRATE, 2 );
  168.   pause( 400 );
  169.  
  170.   PlaySound( explosion, MAXVOLUME, RIGHT1, NORMALRATE, 2 );
  171.   pause( 1000 );
  172.  
  173.  
  174.  
  175.   printf("3. Stop the audio channels\n");
  176.   StopSound( LEFT0 );
  177.   StopSound( LEFT1 );
  178.   StopSound( RIGHT0 );
  179.   StopSound( RIGHT1 );
  180.  
  181.  
  182.   printf("4. Remove the sound effects\n");
  183.   free_memory( "THE END" );
  184. }
  185.  
  186.  
  187. void free_memory( message )
  188. STRPTR message;
  189. {
  190.     printf("%s\n\n", message );
  191.  
  192.   /* It is not dangerous to try to remove a sound that has not been     */
  193.     /* prepared. We can therefore try to remove all sounds, even if some  */
  194.     /* have not been initialized. (However, all channels that are playing */
  195.     /* the sound must have been stopped before you may remove the sound!  */
  196.   RemoveSound( fire );
  197.   RemoveSound( explosion );
  198.   RemoveSound( background );
  199.  
  200.   exit();
  201. }
  202.  
  203.  
  204. void pause( time )
  205. int time;
  206. {
  207.     int loop;
  208.     for( loop=0; loop < time*100; loop++ )
  209.       ;
  210. }
  211.