home *** CD-ROM | disk | FTP | other *** search
/ The Amiga Game Guide / AmigaGameGuide_CD.iso / Amiga / PD-Games / DogFight! / Sources.lha / sound.c < prev    next >
C/C++ Source or Header  |  1994-05-21  |  9KB  |  251 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <exec/types.h>
  4. #include <exec/memory.h>
  5. #include <dos/dos.h>
  6. #include <devices/audio.h>
  7. #include <clib/alib_protos.h>
  8. #include <clib/exec_protos.h>
  9. #include <proto/exec.h>
  10. #include <proto/dos.h>
  11.  
  12. #include "snd.h"         /* Definitions for the sound stuff */
  13.  
  14. #ifndef MakeID
  15. #define MakeID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
  16. #endif
  17.  
  18. #define VHDR    MakeID('V', 'H', 'D', 'R')
  19. #define BODY    MakeID('B', 'O', 'D', 'Y')
  20.  
  21. #define clock   3546895
  22.  
  23. UBYTE                           *bufdata[] = {NULL, NULL, NULL};
  24. Voice8Header    vhdr[3];
  25. ULONG                           size[] = {0L, 0L, 0L};
  26. struct IOAudio  *playeriob[] = {NULL, NULL, NULL, NULL};
  27.  
  28. enum {ER_NONE, ER_OPENFILE, ER_READFILE, ER_IOB, ER_ALLOCMEM};
  29.  
  30. struct IOAudio *GetAudioChannel(UBYTE *);
  31. void FreeAudioChannel(struct IOAudio *);
  32.  
  33. int setupsound(void) {
  34.         BPTR            file;
  35.         UBYTE           buffer[8];
  36.         ULONG           filebuffer_len;
  37.         UBYTE           *buf, *bufstart;
  38.         BPTR            lock;
  39.         char            t[40];
  40.         char            u[55];
  41.         int             itt, itter;
  42.         UBYTE           map[1];
  43.         int             error = ER_NONE;
  44.  
  45.         lock = GetProgramDir();
  46.         NameFromLock(lock, t, 40);
  47.         
  48.         sprintf(u, "%s/sounds.8svx", t);
  49.         
  50.         if (file = (BPTR)Open(u, MODE_OLDFILE)) {
  51.                 if (Read(file, (APTR)buffer, 8L) == 8) {
  52.                         
  53.                         filebuffer_len = ((struct Chunk *)buffer)->ckSize;
  54.                         bufstart = buf = (UBYTE *)AllocMem(filebuffer_len, MEMF_PUBLIC | MEMF_CLEAR);
  55.  
  56.                         if (Read(file, (APTR)buf, filebuffer_len) == filebuffer_len) {
  57.  
  58.                                 for (itt = 0; itt < 3; itt++) {
  59.                                         while(((struct Chunk *)buf)->ckID != VHDR)             buf++;
  60.                                         buf += 8L;
  61.                                         vhdr[itt].samplesPerSec = ((Voice8Header *)(buf))->samplesPerSec;
  62.                                         while (((struct Chunk *)buf)->ckID != BODY)    buf++;
  63.                                         size[itt] = ((struct Chunk *)buf)->ckSize;
  64.                                         buf += 8L;
  65.                                         
  66.                                         bufdata[itt] = (UBYTE *)AllocMem(size[itt], MEMF_CHIP | MEMF_CLEAR);
  67.                                         if (!bufdata[itt]) {
  68.                                                 FreeMem((APTR)bufstart, filebuffer_len);
  69.                                                 Close(file);
  70.                                                 return ER_ALLOCMEM;
  71.                                         }
  72.                                         
  73.                                         for (itter = 0;itter<size[itt];itter++)
  74.                                                 *((bufdata[itt])+itter) = *(buf++);
  75.                                 }
  76.                         }
  77.                         else error = ER_READFILE;
  78.                 }
  79.                 else error = ER_READFILE;
  80.         }
  81.         else error = ER_OPENFILE;
  82.         
  83.         if (file)               Close(file);
  84.         if (bufstart)   FreeMem((APTR)bufstart, filebuffer_len);
  85.  
  86.         if (error) return error;
  87.  
  88.         /*******************************************
  89.          *  Set up the player AudioRequests
  90.          *******************************************/
  91.  
  92.         for (itt = 0; itt < 4; itt++) {
  93.                 map[0] = 1 << itt;
  94.                 playeriob[itt] = GetAudioChannel(map);
  95.                 if (!playeriob[itt]) return ER_IOB;
  96.         }
  97.         
  98.         return ER_NONE;
  99.  
  100. }
  101.  
  102. enum {EXPLODE, START, SHOOT};
  103.  
  104. void explode(int player) {
  105.         
  106. /*******************************************
  107.  *  Do explosionIO
  108.  *******************************************/
  109.         
  110.         struct IOAudio *iob;
  111.         
  112.         iob = playeriob[player - 1];
  113.  
  114.         if (iob) {
  115.                 iob->ioa_Request.io_Command = ADCMD_FINISH;
  116.                 iob->ioa_Request.io_Flags =     IOF_QUICK;
  117.                 BeginIO((struct IORequest *)iob);
  118.  
  119.                 iob->ioa_Request.io_Command =   CMD_WRITE;
  120.                 iob->ioa_Request.io_Flags =     ADIOF_PERVOL;
  121.                 iob->ioa_Data =                                         bufdata[EXPLODE];
  122.                 iob->ioa_Length =                               size[EXPLODE];
  123.                 iob->ioa_Period =                               clock / vhdr[EXPLODE].samplesPerSec;
  124.                 iob->ioa_Volume =                               64;
  125.                 iob->ioa_Cycles =                               1;
  126.                 BeginIO((struct IORequest *)iob);
  127.         }
  128. }
  129.  
  130. void start(int player) {
  131.         
  132. /*******************************************
  133.  *  Do startIO
  134.  *******************************************/
  135.  
  136.         struct IOAudio *iob;
  137.         
  138.         iob = playeriob[player - 1];
  139.         
  140.         if (iob) {
  141.                 iob->ioa_Request.io_Command = ADCMD_FINISH;
  142.                 iob->ioa_Request.io_Flags =     IOF_QUICK;
  143.                 BeginIO((struct IORequest *)iob);
  144.  
  145.                 iob->ioa_Request.io_Command = CMD_WRITE;
  146.                 iob->ioa_Request.io_Flags =     ADIOF_PERVOL;
  147.                 iob->ioa_Data =                                         bufdata[START];
  148.                 iob->ioa_Length =                               size[START];
  149.                 iob->ioa_Period =                               clock / vhdr[START].samplesPerSec;
  150.                 iob->ioa_Volume =                               64;
  151.                 iob->ioa_Cycles =                               1;
  152.                 BeginIO((struct IORequest *)iob);
  153.         }
  154. }
  155.  
  156. void shoot(int player) {
  157.         
  158. /*******************************************
  159.  *  Do shootIO
  160.  *******************************************/
  161.  
  162.         struct IOAudio *iob;
  163.         
  164.         iob = playeriob[player - 1];
  165.  
  166.         if (iob) {
  167.                 iob->ioa_Request.io_Command = ADCMD_FINISH;
  168.                 iob->ioa_Request.io_Flags =     IOF_QUICK;
  169.                 BeginIO((struct IORequest *)iob);
  170.  
  171.                 iob->ioa_Request.io_Command = CMD_WRITE;
  172.                 iob->ioa_Request.io_Flags =     ADIOF_PERVOL;
  173.                 iob->ioa_Data =                                 bufdata[SHOOT];
  174.                 iob->ioa_Length =                               size[SHOOT];
  175.                 iob->ioa_Period =                               clock / vhdr[SHOOT].samplesPerSec;
  176.                 iob->ioa_Volume =                               32;
  177.                 iob->ioa_Cycles =                               1;
  178.                 BeginIO((struct IORequest *)iob);
  179.         }
  180. }
  181.  
  182. void closesound(void) {
  183.         int itt;
  184.         
  185.         for (itt = 0; itt < 4; itt++)
  186.                 FreeAudioChannel(playeriob[itt]);
  187.         
  188.         for (itt = 0; itt < 3; itt++)
  189.                 if (bufdata[itt]) FreeMem(bufdata[itt], size[itt]);
  190. }
  191.  
  192. /* Get an audio channel */
  193. struct IOAudio *GetAudioChannel(UBYTE *allocationMap) {
  194.         struct IOAudio *aIOB;
  195.         struct MsgPort *aPort;
  196.         long err;
  197.  
  198.         aPort=CreateMsgPort();
  199.         if(!aPort)      return(NULL);
  200.  
  201.         aIOB=(struct IOAudio *)AllocMem(sizeof(struct IOAudio),
  202.                                                                                                 MEMF_PUBLIC | MEMF_CLEAR);
  203.         if(!aIOB) {
  204.                 DeletePort(aPort);
  205.                 return(NULL);
  206.         }
  207.  
  208.         /* Set up the IOAudio to allocate the command channel */
  209.         aIOB->ioa_Request.io_Message.mn_Node.ln_Pri =   0;
  210.         aIOB->ioa_Request.io_Message.mn_ReplyPort =             aPort;
  211.         aIOB->ioa_Length =                                                                              1;
  212.         aIOB->ioa_Data =                                                                                        allocationMap;
  213.         aIOB->ioa_Request.io_Command =                                          ADCMD_ALLOCATE;
  214.  
  215.         /*Open the audio device*/
  216.         err = OpenDevice(AUDIONAME, 0, (struct IORequest *)aIOB, 0);
  217.  
  218.         if(!aIOB->ioa_AllocKey) {  /*There was an error*/
  219.                 DeletePort(aPort);
  220.                 FreeMem(aIOB, sizeof(struct IOAudio));
  221.                 return(NULL);
  222.         }
  223.         else {
  224.                 /* Set up the IOAudio for writes */
  225.                 aIOB->ioa_Request.io_Command =  CMD_WRITE;
  226.                 aIOB->ioa_Request.io_Flags =            ADIOF_PERVOL;
  227.                 aIOB->ioa_Length =                                      0;
  228.                 return(aIOB);
  229.         }
  230. }
  231.  
  232. /* Free an allocated audio channel */
  233. void FreeAudioChannel(struct IOAudio *aIOB) {
  234.         if(aIOB == NULL)
  235.                 return;
  236.  
  237.         /* Free the audio channel */
  238.         aIOB->ioa_Request.io_Command=ADCMD_FREE;
  239.         BeginIO((struct IORequest *)aIOB);
  240.         WaitIO((struct IORequest *)aIOB);
  241.         DeletePort(aIOB->ioa_Request.io_Message.mn_ReplyPort);
  242.  
  243.         /* Close the audio channel */
  244.         CloseDevice((struct IORequest *)aIOB);
  245.  
  246.         /* Free the IOAudio structure */
  247.         FreeMem(aIOB,sizeof(struct IOAudio));
  248.         return;
  249. }
  250.  
  251.