home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / datafiles / text / c_manual / sound / includesound / example.c < prev    next >
Text File  |  1995-02-27  |  13KB  |  241 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: IncludeSound                Tulevagen 22       */
  8. /* File:    Example.c                   181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-10                                       */
  11. /* Version: 1.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. /* IncludeSound allows you to include the sound files as normal source       */
  21. /* code which is then compiled and linked together with your own programs.   */
  22. /* (Use the "PrintSound" program to convert sound files into source code.)   */
  23. /* The advantage with this compared to use EasySound is that your program    */
  24. /* do not need to load any sound files which means your programs will be     */
  25. /* faster. Since you do not need to load any sound files your program will   */
  26. /* also be smaller. Finally, since the sound is included with the program    */
  27. /* itself you do not need to include a lot of extra files together with      */
  28. /* your program, and your program will be more "portable".                   */
  29. /*                                                                           */
  30. /* Note that you should only use IncludeSound in programs that use short     */
  31. /* samples. If you use long ones (several seconds or so) it is best to       */
  32. /* stick to EasySound. Even short samples seems to generate very large       */
  33. /* files when converted to source code. (However, once the file has been     */
  34. /* compiled it will not use more memory than normal.)                        */
  35. /*                                                                           */
  36. /* IncludeSound is specially intended for programs that only use some        */
  37. /* small sound effects that does not need to be replaced. You can of course  */
  38. /* combine IncludeSound with EasySound in order to have some samples         */
  39. /* fixed (included in the program itself) and some "lose". If you want to    */
  40. /* do this you should only link your program with the EasySound.o module.    */
  41. /* (IncludeSound.o is exactly the same as EasySound.o except that all        */
  42. /* sound loading routines have been removed.)                                */
  43. /*                                                                           */
  44. /*                                                                           */
  45. /* I N S T R U C T I O N S                                                   */
  46. /* -----------------------                                                   */
  47. /*                                                                           */
  48. /* 1. Convert the sound file into source code with help of the PrintSound    */
  49. /*    program.                                                               */
  50. /*                                                                           */
  51. /*    PrintSound > "output file" "sound file" "name of array"                */
  52. /*                                                                           */
  53. /*    Example: "PrintSound > RAM:Data Explosion.snd Bang"                    */
  54. /*                                                                           */
  55. /*    PrintSound will load file "Explosion.snd", and print the sound         */
  56. /*    data in file "RAM:Data". The SoundInfo structure will be called        */
  57. /*    "Bang".                                                                */
  58. /*                                                                           */
  59. /*                                                                           */
  60. /* 2. Include the file PrintSound has generated into your own program.       */
  61. /*                                                                           */
  62. /*                                                                           */
  63. /* 3. Use the function PlaySound() to play the sound date.                   */
  64. /*                                                                           */
  65. /*    Synopsis: ok = PlaySound( ptr, vol, cha, pri, drate, times, ddata,     */
  66. /*                              time, wait );                                */
  67. /*                                                                           */
  68. /*    ok:    (BOOL) If the sound was played successfully TRUE is             */
  69. /*           returned, else FALSE.                                           */
  70. /*                                                                           */
  71. /*    ptr:   (struct SoundInfo *) Pointer to the SoundInfo structure which   */
  72. /*           has been generated by PrintSound.                               */
  73. /*                                                                           */
  74. /*    vol:   (UWORD) Volume, 0 to 64.                                        */
  75. /*                                                                           */
  76. /*    cha:   (UBYTE) Which channel should be used. (LEFT0, RIGHT0,           */
  77. /*           RIGHT1 or LEFT1)                                                */
  78. /*                                                                           */
  79. /*    pri    (BYTE) What sound priority should be used. For normal sound     */
  80. /*           effects, set the priority to "SOUND_PRI_EFFECT". See file       */
  81. /*           "IncludeSound.h" for a complete list of recommended priorities. */
  82. /*                                                                           */
  83. /*    drate: (WORD) Delta rate. When the sound is prepared, the record       */
  84. /*           rate is automatically stored in the SoundInfo structure,        */
  85. /*           so if you do not want to change the rate, write NORMALRATE.     */
  86. /*                                                                           */
  87. /*    times: (UWORD) How many times the sound should be played. If you       */
  88. /*           want to play the sound forever, write NONSTOP. (To stop a       */
  89. /*           sound call the function StopSound().)                           */
  90. /*                                                                           */
  91. /*    ddata: (ULONG) Where in the sound data we should start to play. (If    */
  92. /*           you want to start at the beginning of the sound data set ddata  */
  93. /*           to 0.)                                                          */
  94. /*                                                                           */
  95. /*    time:  (ULONG) For how long time the sound should be played. If you    */
  96. /*           want to play all of the sound data set time to 0.               */
  97. /*                                                                           */
  98. /*    wait:  (BOOL) If you want that the program waits for the sound to      */
  99. /*           to be completed set the flag "WAIT". If you do not want to      */
  100. /*           wait for the sound to be completed, set the flag "DO_NOT_WAIT". */
  101. /*                                                                           */
  102. /*                                                                           */
  103. /* 4. Remember to "stop" all sound channels you have used before your        */
  104. /*    program terminates. Use the function StopSound().                      */
  105. /*                                                                           */
  106. /*    Synopsis: StopSound( channel );                                        */
  107. /*                                                                           */
  108. /*    channel:  (UBYTE) The audio channel that should be stopped. (LEFT0,    */
  109. /*              LEFT1, RIGHT0 or RIGHT1.)                                    */
  110. /*                                                                           */
  111. /* 5. Compile your program as normal, and when you link it include the       */
  112. /*    file "IncludeSound.o". (See file Example.lnk for more information.)    */
  113. /*                                                                           */
  114. /*                                                                           */
  115. /* ------------------------------------------------------------------------- */
  116. /* Special thanks to Michael Loughman who wrote to me about his excellent    */
  117. /* idea to "include" sound.                                                  */
  118. /* ------------------------------------------------------------------------- */
  119.  
  120.  
  121.  
  122. /* Include some important header files: */
  123. #include <exec/types.h>   /* Declares CPTR, BOOL and STRPTR. */
  124. #include "IncludeSound.h" /* Declares LEFT0, LEFT1, RIGHT0, etc. */
  125.  
  126.  
  127.  
  128. /************************************/
  129. /* SoundData prepared by PrintSound */
  130. /* Anders Bjerin       Amiga C Club */
  131. /*                                  */
  132. /* File name:              Beep.snd */
  133. /* Record Rate:               10100 */
  134. /* File Length:                 602 */
  135. /************************************/
  136.  
  137. BYTE chip Beep_data[]=
  138. {
  139.   4,11,9,0,-11,-9,3,15,17,-6,-34,
  140.   -31,22,93,63,-20,-106,-85,0,104,84,-9,
  141.   -98,-68,17,114,57,-42,-109,-33,61,101,9,
  142.   -88,-95,7,101,46,-39,-109,-33,60,114,14,
  143.   -79,-79,19,112,46,-53,-111,-20,73,104,3,
  144.   -95,-80,22,111,36,-57,-111,-19,84,96,-11,
  145.   -103,-63,34,114,23,-71,-107,-7,90,82,-15,
  146.   -106,-55,47,119,22,-69,-103,-4,98,87,-20,
  147.   -107,-53,46,117,20,-77,-103,1,100,73,-25,
  148.   -111,-47,58,122,17,-77,-96,6,111,66,-26,
  149.   -107,-39,60,122,22,-79,-93,19,117,53,-39,
  150.   -109,-23,82,115,3,-90,-74,30,122,41,-57,
  151.   -111,-14,85,100,-6,-103,-68,42,123,28,-61,
  152.   -109,-11,93,101,-9,-103,-61,41,123,28,-68,
  153.   -106,-3,96,92,-11,-107,-58,52,123,19,-73,
  154.   -100,0,104,87,-20,-109,-49,55,119,12,-85,
  155.   -96,11,111,73,-25,-114,-41,68,115,4,-88,
  156.   -85,15,119,65,-38,-114,-30,73,112,4,-93,
  157.   -87,23,119,50,-42,-111,-25,79,114,3,-92,
  158.   -82,22,120,47,-52,-114,-19,82,107,-1,-100,
  159.   -73,38,122,28,-66,-101,-6,100,92,-25,-111,
  160.   -46,58,119,9,-88,-92,14,114,63,-36,-115,
  161.   -33,76,109,-4,-96,-76,23,127,58,-44,-115,
  162.   -26,76,103,0,-96,-85,19,119,57,-38,-114,
  163.   -34,74,103,-4,-96,-77,25,122,44,-57,-112,
  164.   -14,88,84,-19,-109,-61,47,123,23,-74,-103,
  165.   6,109,57,-44,-117,-33,69,111,6,-95,-88,
  166.   25,117,39,-53,-119,-25,85,100,-7,-104,-74,
  167.   33,123,36,-61,-115,-9,93,84,-15,-111,-63,
  168.   52,122,19,-73,-106,6,111,61,-33,-114,-38,
  169.   68,114,7,-84,-88,23,117,44,-50,-114,-17,
  170.   88,93,-4,-101,-76,41,120,28,-60,-107,-6,
  171.   103,80,-19,-109,-58,50,122,23,-71,-101,7,
  172.   109,68,-22,-111,-52,65,119,12,-79,-88,19,
  173.   120,58,-41,-114,-28,80,101,-1,-98,-73,42,
  174.   122,30,-61,-107,-4,106,77,-26,-106,-39,71,
  175.   109,7,-88,-88,30,119,42,-49,-109,-14,100,
  176.   92,-11,-101,-66,47,119,33,-63,-107,-1,103,
  177.   73,-22,-106,-49,69,109,11,-80,-90,22,111,
  178.   49,-46,-106,-22,84,88,-4,-92,-68,52,107,
  179.   23,-63,-95,3,101,66,-28,-98,-36,69,95,
  180.   7,-79,-69,38,100,31,-53,-87,-3,85,63,
  181.   -26,-88,-38,58,88,14,-68,-73,20,87,41,
  182.   -38,-80,-17,68,63,-14,-73,-42,42,79,22,
  183.   -50,-63,11,73,44,-25,-68,-22,53,65,0,
  184.   -58,-44,26,69,28,-41,-61,-3,58,46,-15,
  185.   -65,-38,34,65,17,-42,-53,3,57,38,-23,
  186.   -55,-17,41,49,-1,-52,-41,25,61,22,-38,
  187.   -58,-6,60,53,-7,-60,-39,31,68,25,-39,
  188.   -65,-4,57,47,-11,-60,-36,34,57,11,-44,
  189.   -58,-1,55,41,-12,-55,-31,30,53,17,-33,
  190.   -50,-4,44,39,-3,-42,-30,19,41,12,-26,
  191.   -36,-3,34,31,-3,-34,-28,7,42,20,-12,
  192.   -28,-9,17,22,1,-19,-15,3,15,7,-9,
  193.   -14,-3,9,9,-1,-7,-4,3
  194. };
  195.  
  196. struct SoundInfo Beep=
  197. {
  198.   Beep_data, /* WaveForm Buffers */
  199.       10100, /* Record Rate */
  200.         602  /* WaveForm Length */
  201. };
  202.  
  203. /************************************/
  204. /* End of sound:           Beep.snd */
  205. /************************************/
  206.  
  207.  
  208.  
  209. /* Declare the functions in this module: */
  210. void main();
  211.  
  212.  
  213. void main()
  214. {
  215.   int loop;
  216.   
  217.  
  218.   printf("\nINCLUDE-SOUND\n");
  219.   printf("Amiga C Club (ACC)\nAnders Bjerin\nTulevagen 22\n");
  220.   printf("181 41  LIDINGO\nSWEDEN\n\n");
  221.  
  222.  
  223.   for( loop=0; loop < 10; loop++ )
  224.   {
  225.     /* Play the sound: NOTE! Do NOT forget to put a "&" sign infront */
  226.     /* of the structure name!                                        */
  227.     if( PlaySound( &Beep, MAXVOLUME, RIGHT1, SOUND_PRI_EFFECT,
  228.                    NORMALRATE, ONCE, 0, 0, DO_NOT_WAIT ) )
  229.       printf( "OK!\n" );
  230.     else
  231.       printf( "ERROR!\n" );
  232.  
  233.     /* Wait one second... */
  234.     Delay( 1 * 50 );
  235.   }
  236.  
  237.  
  238.   /* Remember to stop all sound channels you have used! */
  239.   StopSound( RIGHT1 );
  240. }
  241.