home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 98 / af098sub.adf / asteroids2.LZX / asteroids2 / rotx / sound.c < prev    next >
C/C++ Source or Header  |  2009-05-20  |  6KB  |  220 lines

  1. #include <libraries/iffparse.h>
  2. #include <workbench/startup.h>
  3. #include <graphics/gfxbase.h>
  4. #include <exec/execbase.h>
  5. #include <dos/dosextens.h>
  6. #include <devices/audio.h>
  7. #include <exec/memory.h>
  8. #include <clib/iffparse_protos.h>
  9. #include <clib/exec_protos.h>
  10. #include <clib/alib_protos.h>
  11. #include <clib/dos_protos.h>
  12. #include <h/sound.h>
  13. #include <h/rot.h>
  14.  
  15.  
  16. extern struct GfxBase *GfxBase;
  17. extern struct control control;
  18.  
  19. struct IOAudio *ConChannel;
  20. struct IOAudio *audio[4];
  21. struct MsgPort *audioport;
  22.  
  23. #define SAMPLENUM 14
  24. struct sounddata sd[SAMPLENUM];
  25.  
  26. LoadAllSounds()
  27. {
  28. Load8SVX("sounds/playerfire.8svx",0);    /* player fire */
  29. Load8SVX("sounds/thrust.8svx",1);  /* thrust */
  30. Load8SVX("sounds/boom.8svx",2);    /* asteroid/fighter explosion */
  31. Load8SVX("sounds/explosion.8svx",3);    /* player/saucer explsion */
  32. Load8SVX("sounds/saucerfire.8svx",4);       /* saucer fire */
  33. Load8SVX("sounds/enemyfire.8svx",5);       /* enemy fire */
  34. Load8SVX("sounds/break.8svx",6);    /* enemy explosion */
  35. Load8SVX("sounds/hyperspace.8svx",7);   /* hyperspace */
  36. Load8SVX("sounds/shield.8svx",8);   /* shield whack */
  37. Load8SVX("sounds/tech.8svx",9);    /* tech box */
  38. Load8SVX("sounds/level.8svx",10);/* level note */
  39. Load8SVX("sounds/shieldpop.8svx",11);/* shield box */
  40. Load8SVX("sounds/extra.8svx",12);       /* life box */
  41. Load8SVX("sounds/fighterexp.8svx",13); /* fighter explosion */
  42.  
  43. sd[1].volume = 20;
  44. }
  45.  
  46.  
  47. makesound(num,chan)
  48. LONG num,chan;
  49. {
  50.  
  51. if (control.audio == TRUE)
  52.     {
  53.     StopSound(chan);
  54.     audio[chan] ->ioa_Request.io_Command= CMD_WRITE;
  55.     audio[chan] ->ioa_Request.io_Flags    = ADIOF_PERVOL;
  56.     audio[chan] ->ioa_Period            = sd[num].rate;
  57.     audio[chan] ->ioa_Volume            = sd[num].volume;
  58.     audio[chan] ->ioa_Cycles            = 1;
  59.     audio[chan] ->ioa_Data            = sd[num].audiodata;
  60.     audio[chan] ->ioa_Length            = sd[num].length;
  61.     BeginIO((struct IORequest *)audio[chan]);
  62.     }
  63. }
  64.  
  65. FlushSound(num)
  66. LONG num;
  67. {
  68. if (control.audio == TRUE)
  69.     {
  70.     audio[num] ->ioa_Request.io_Command    = CMD_FLUSH;
  71.     BeginIO((struct IORequest *)audio[num]);
  72.     }
  73. }
  74.  
  75.  
  76. StopSound(num)
  77. LONG num;
  78. {
  79. audio[num] ->ioa_Request.io_Command    = ADCMD_FINISH;
  80. BeginIO((struct IORequest *)audio[num]);
  81. }
  82.  
  83.  
  84. initaudio()
  85. {
  86. LONG error,x;
  87.  
  88. audioport = CreatePort("rot",0);
  89. if (audioport == NULL)
  90.     {
  91.     makerequest("audioport Error");
  92.     makerequest("Game Will Proceed Without Sound");
  93.     control.audio = FALSE;
  94.     }
  95. else
  96.     {
  97.     for (x=0;x<4;x++)
  98.         {
  99.         audio[x]    = (struct IOAudio *)AllocMem(sizeof(struct IOAudio),MEMF_PUBLIC | MEMF_CLEAR);
  100.         if (audio[x] == NULL)
  101.             {
  102.             makerequest("Audio Channel Allocation Error");
  103.             makerequest("Game Will Proceed Without Sound");
  104.             control.audio = FALSE;
  105.             }
  106.         }
  107.  
  108.     if (control.audio == TRUE)
  109.         {
  110.         ConChannel = (struct IOAudio *)AllocMem(sizeof(struct IOAudio),MEMF_PUBLIC | MEMF_CLEAR);
  111.         if (ConChannel == NULL)
  112.             {
  113.             makerequest("Control Channel Allocation Error");
  114.             makerequest("Game Will Proceed Without Sound");
  115.             control.audio = FALSE;
  116.             }
  117.         else
  118.             {
  119.             ConChannel->ioa_Request.io_Message.mn_Node.ln_Type= NT_MESSAGE;
  120.             ConChannel->ioa_Request.io_Message.mn_Length           = sizeof(struct IOAudio);
  121.             ConChannel->ioa_Request.io_Command                   = ADCMD_ALLOCATE;
  122.             ConChannel->ioa_Request.io_Flags                = ADIOF_NOWAIT;
  123.             ConChannel->ioa_Data                        = Channel;
  124.             ConChannel->ioa_Length                        = sizeof(Channel);
  125.             ConChannel->ioa_AllocKey                        = 0;
  126.             ConChannel->ioa_Request.io_Message.mn_ReplyPort    = audioport;
  127.             ConChannel->ioa_Request.io_Message.mn_Node.ln_Pri    = 64;
  128.  
  129.             error = OpenDevice("audio.device",0,(struct IORequest *)ConChannel,0);
  130.             if (error != NULL)
  131.                 {
  132.                 makerequest("audio.port Error");
  133.                 makerequest("Game Will Proceed Without Sound");
  134.                 control.audio = FALSE;
  135.                 }
  136.  
  137.             *audio[L2] = *audio[R1] = *audio[R2] = *audio[L1] = *ConChannel;
  138.  
  139.             audio[L1]->ioa_Request.io_Unit = (APTR)(((ULONG)ConChannel->ioa_Request.io_Unit) & 1);
  140.             audio[R1]->ioa_Request.io_Unit = (APTR)(((ULONG)ConChannel->ioa_Request.io_Unit) & 2);
  141.             audio[L2]->ioa_Request.io_Unit = (APTR)(((ULONG)ConChannel->ioa_Request.io_Unit) & 4);
  142.             audio[R2]->ioa_Request.io_Unit = (APTR)(((ULONG)ConChannel->ioa_Request.io_Unit) & 8);
  143.             }
  144.         }
  145.     }
  146. }
  147.  
  148. closeaudio()
  149. {
  150. LONG x;
  151.  
  152. if (ConChannel) CloseDevice((struct IORequest *)ConChannel);
  153.  
  154. if (audioport) DeletePort(audioport);
  155.  
  156. for (x=0;x<4;x++)
  157.     if (audio[x]) FreeMem(audio[x],sizeof(struct IOAudio));
  158.  
  159. if (ConChannel) FreeMem(ConChannel,sizeof(struct IOAudio));
  160.  
  161. for (x=0;x<SAMPLENUM;x++)
  162.     if (sd[x].audiodata) FreeVec(sd[x].audiodata);
  163. }
  164.  
  165.  
  166.  
  167. Load8SVX(STRPTR Name,LONG num)
  168. {
  169. struct IFFHandle    *Handle;
  170. struct StoredProperty    *Prop;
  171. struct Voice8Header    *VoiceHeader;
  172. struct ContextNode *ContextNode;
  173. LONG    Rate,Length,Volume;
  174.  
  175. if(Handle = AllocIFF())                          /* Allocate an IFF handle. */
  176.     {
  177.     
  178. if(Handle->iff_Stream=Open(Name,MODE_OLDFILE))        /* Open the sound file for reading. */
  179.     {
  180.     InitIFFasDOS(Handle);                        /* Open the file for reading. */
  181.     if(!OpenIFF(Handle,IFFF_READ))
  182.         {
  183.         if(!PropChunk(Handle,ID_8SVX,VHDR))         /* Remember the voice header chunk if encountered. */
  184.             {
  185.             if(!StopChunk(Handle,ID_8SVX,BODY))     /* Stop in front of the data body chunk. */
  186.                 {
  187.                 if(!ParseIFF(Handle,IFFPARSE_SCAN))    /* Scan the file... */
  188.                     {
  189.                     if(Prop = FindProp(Handle,ID_8SVX,VHDR))    /* Try to find the voice header chunk. */
  190.                         {
  191.                          VoiceHeader = (struct Voice8Header *)Prop -> sp_Data;
  192.                          if(!VoiceHeader->sCompression&&VoiceHeader->ctOctave == 1)        /* No compression and only a single octave, please! */
  193.                             {
  194.                             if(ContextNode = CurrentChunk(Handle))  /* Get information on the current chunk. */
  195.                                 {
  196.                                 Length= VoiceHeader->oneShotHiSamples ? VoiceHeader->oneShotHiSamples : VoiceHeader->repeatHiSamples;
  197.                                 Rate     = (GfxBase -> DisplayFlags & PAL ? 3546895 : 3579545) / VoiceHeader -> samplesPerSec;
  198.                                 Volume= (VoiceHeader -> volume * 64) / 0x10000;
  199.  
  200.                                 sd[num].audiodata = AllocVec(Length,MEMF_CHIP);
  201.                                 sd[num].volume = Volume;
  202.     /* sound data insertion */        sd[num].length = Length;
  203.                                 sd[num].rate     = Rate;
  204.  
  205.                                 if (ReadChunkBytes(Handle,sd[num].audiodata,Length) != Length) printf("Load 8SVX problem \n");
  206.                                 }
  207.                             }
  208.                           else printf("compressed!!!!\n");
  209.                         }
  210.                     }
  211.                 }
  212.             }
  213.             CloseIFF(Handle);
  214.         }
  215.         Close(Handle -> iff_Stream);
  216.     }
  217.     FreeIFF(Handle);
  218.     }
  219. }
  220.