home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff343.lzh / SnakePit / SnakePitSrc.lzh / Snake_Sound.c < prev    next >
C/C++ Source or Header  |  1988-07-04  |  5KB  |  221 lines

  1. /*
  2.  * MKSoft SnakePit  Copyright (c) 1988  by Michael Sinz
  3.  *
  4.  * Sound Effects part of the program...
  5.  */
  6.  
  7. #include    "Snake.h"
  8.  
  9. /* These are the values needed for the sounds... */
  10. #define    Beat_2_Size        16512L
  11. extern    UBYTE    Beat_2[Beat_2_Size];
  12.  
  13. #define    Beat_4_Size        16618L
  14. extern    UBYTE    Beat_4[Beat_4_Size];
  15.  
  16. #define    Beat_6_Size        16572L
  17. extern    UBYTE    Beat_6[Beat_6_Size];
  18.  
  19. #define    DeathSound_Size        18944L
  20. extern    UBYTE    DeathSound[DeathSound_Size];
  21.  
  22. #define    ControlSound_Size    14432L
  23. extern    UBYTE    ControlSound[ControlSound_Size];
  24.  
  25. #define    RushSound_Size        800L
  26. #define        RushSound    DeathSound
  27.  
  28. #define    EatSound_Size        800L
  29. #define        EatSound    Beat_2
  30.  
  31. /*
  32.  * Note that the sound effects are linked into the executable...
  33.  */
  34.  
  35. static    struct    IOAudio    *Voices[4]={NULL,NULL,NULL,NULL};
  36.  
  37. static        UBYTE    AllocMap[]={1,2,4,8,1,2,4};
  38.  
  39. static    struct    MsgPort    *AudioPort=NULL;
  40.  
  41. static    Play_Sound(Sound,Size,Period,x,v)    register    UBYTE    *Sound;
  42.                                 long    Size;
  43.                                 long    Period;
  44.                                 short    x;
  45.                     register    struct    IOAudio    *v;
  46. {
  47.     if (v)
  48.     {
  49.         if (!CheckIO(v)) AbortIO(v);
  50.  
  51.         v->ioa_Length=Size;
  52.         v->ioa_Data=Sound;
  53.         v->ioa_Cycles=x;
  54.         v->ioa_Period=(UWORD)Period;
  55.  
  56.         BeginIO(v);
  57.         while (GetMsg(AudioPort));    /* Just to prevent a fireball death */
  58.     }
  59. }
  60.  
  61. /* This routine plays the stereo movement sound... */
  62. VOID    Sound_Eat(x)    register    short    x;
  63. {
  64. register        short    loop;
  65.  
  66.     for (loop=2;loop<4;loop++) if (Voices[loop])
  67.     {
  68.         if (!CheckIO(Voices[loop])) AbortIO(Voices[loop]);
  69.         Voices[loop]->ioa_Length=EatSound_Size;
  70.         Voices[loop]->ioa_Data=EatSound;
  71.         Voices[loop]->ioa_Cycles=1;
  72.         Voices[loop]->ioa_Period=180;
  73.         Voices[loop]->ioa_Volume=64-((loop==2)?(40-x):x);
  74.     }
  75.     if (Voices[2]) BeginIO(Voices[2]);
  76.     if (Voices[3]) BeginIO(Voices[3]);
  77.     if (AudioPort) while (GetMsg(AudioPort));
  78. }
  79.  
  80. /* This routine plays the stereo movement sound... */
  81. VOID    Sound_Move(x)    register    short    x;
  82. {
  83. register        short    loop;
  84.  
  85.     for (loop=2;loop<4;loop++) if (Voices[loop])
  86.     {
  87.         if (!CheckIO(Voices[loop])) AbortIO(Voices[loop]);
  88.         Voices[loop]->ioa_Length=RushSound_Size;
  89.         Voices[loop]->ioa_Data=RushSound;
  90.         Voices[loop]->ioa_Cycles=1;
  91.         Voices[loop]->ioa_Period=186;
  92.         Voices[loop]->ioa_Volume=64-((loop==2)?(40-x):x);
  93.     }
  94.     if (Voices[2]) BeginIO(Voices[2]);
  95.     if (Voices[3]) BeginIO(Voices[3]);
  96.     if (AudioPort) while (GetMsg(AudioPort));
  97. }
  98.  
  99. VOID    Sound_Dead()    /* This plays the death sound... */
  100. {
  101.     Play_Sound(DeathSound,DeathSound_Size,SoundPeriod,1,Voices[1]);
  102. }
  103.  
  104. VOID    Sound_Finish()    /* This plays the sound when the exit is found... */
  105. {
  106.     Play_Sound(ControlSound,ControlSound_Size,SoundPeriod+50L,1,Voices[1]);
  107. }
  108.  
  109. /*
  110.  * Values for Sound_Play:
  111.  *
  112.  *    1    First beat...
  113.  *    2    Second beat...
  114.  *    4    Third beat...
  115.  *
  116.  *    Bit combinations:
  117.  *
  118.  *    3    First and Second...
  119.  *    6    Second and Third...
  120.  *    7    First, Second, and Third...
  121.  *
  122.  *    The value 5 is illegal but not tested for.
  123.  *
  124.  */
  125. VOID    Sound_Play(x,y,z)    /* This plays one of the "During Play" sounds... */
  126.             register    short    x;
  127.             register    short    y;
  128.             register    short    z;
  129. {
  130. register    UBYTE    *Sound;
  131. register    long    Size=0;
  132.  
  133.     if (x & 4)
  134.     {
  135.         Sound=Beat_6;
  136.         Size+=Beat_6_Size;
  137.     }
  138.     if (x & 2)
  139.     {
  140.         Sound=Beat_4;
  141.         Size+=Beat_4_Size;
  142.     }
  143.     if (x & 1)
  144.     {
  145.         Sound=Beat_2;
  146.         Size+=Beat_2_Size;
  147.     }
  148.     if (Size) Play_Sound(Sound,Size,SoundPeriod-((long)z),y,Voices[0]);
  149. }
  150.  
  151. VOID    Start_This_Channel(loop)    register    short    loop;
  152. {
  153.     if (!Voices[loop]) if (Voices[loop]=(struct IOAudio *)AllocMem((long)sizeof(struct IOAudio),MEMF_PUBLIC|MEMF_CLEAR))
  154.     {
  155.         if (!OpenDevice(AUDIONAME,NULL,Voices[loop],NULL))
  156.         {
  157.             Voices[loop]->ioa_Request.io_Message.mn_ReplyPort=AudioPort;
  158.             Voices[loop]->ioa_Request.io_Message.mn_Node.ln_Pri=120-loop;
  159.             Voices[loop]->ioa_Request.io_Command=ADCMD_ALLOCATE;
  160.             Voices[loop]->ioa_Request.io_Flags=ADIOF_NOWAIT;
  161.             Voices[loop]->ioa_AllocKey=0;
  162.             Voices[loop]->ioa_Data=&AllocMap[loop];
  163.             Voices[loop]->ioa_Length=4;
  164.             BeginIO(Voices[loop]);
  165.             if (!WaitIO(Voices[loop]))
  166.             {
  167.                 Voices[loop]->ioa_Request.io_Flags=ADIOF_PERVOL;
  168.                 Voices[loop]->ioa_Request.io_Command=CMD_WRITE;
  169.                 Voices[loop]->ioa_Volume=(loop ? 64:28);
  170.                 Voices[loop]->ioa_Cycles=1;    /* default to 1 */
  171.             }
  172.             else
  173.             {
  174.                 CloseDevice(Voices[loop]);
  175.                 FreeMem(Voices[loop],(long)sizeof(struct IOAudio));
  176.                 Voices[loop]=NULL;
  177.             }
  178.         }
  179.         else
  180.         {
  181.             FreeMem(Voices[loop],(long)sizeof(struct IOAudio));
  182.             Voices[loop]=NULL;
  183.         }
  184.     }
  185. }
  186.  
  187. VOID    Init_Sound()    /* This sets up all of the good stuff... */
  188. {
  189. register    short    loop;
  190.  
  191.     if (!AudioPort) AudioPort=CreatePort(NULL,NULL);
  192.     if (AudioPort) for (loop=0;loop<4;loop++) Start_This_Channel(loop);
  193. }
  194.  
  195. VOID    Stop_This_Channel(loop)    register    short    loop;
  196. {
  197.     if (Voices[loop])
  198.     {
  199.         if (!CheckIO(Voices[loop])) AbortIO(Voices[loop]);
  200.  
  201.         while (GetMsg(AudioPort));    /* Make sure port is empty */
  202.  
  203.         CloseDevice(Voices[loop]);
  204.  
  205.         FreeMem(Voices[loop],(long)sizeof(struct IOAudio));
  206.         Voices[loop]=NULL;
  207.     }
  208. }
  209.  
  210. VOID    Stop_Sound()    /* This closes the whole thing down... */
  211. {
  212. register    short    loop;
  213.  
  214.     for (loop=0;loop<4;loop++) Stop_This_Channel(loop);
  215.     if (AudioPort)
  216.     {
  217.         DeletePort(AudioPort);
  218.         AudioPort=NULL;
  219.     }
  220. }
  221.