home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 486.lha / Hi-Low_v1.0 / src / sound.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-11  |  2.7 KB  |  118 lines

  1.  
  2. /*
  3.  *
  4.  * Module    : sound.c
  5.  *
  6.  * Description    : Sound effects for hilow game.
  7.  *
  8.  * Author    : Simon J Raybould.    (sie@fulcrum.bt.co.uk)
  9.  *
  10.  * Date        :  9th September 1990.
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <fcntl.h>
  16. #include <intuition/intuition.h>
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <devices/audio.h>
  20. #include <graphics/gfxbase.h>
  21.  
  22. #include "sound.h"
  23.  
  24. struct MySound {
  25.     ULONG    Len;
  26.     ULONG    Rate;
  27.     char    *Data;
  28. };
  29.  
  30. static struct MySound SoundArray[NSOUNDS];
  31. static struct Remember *SoundRKey = NULL;
  32.  
  33. extern struct MsgPort *CreatePort();
  34. extern struct GfxBase *GfxBase;
  35. static struct IOAudio *IOASound;
  36.  
  37. static UBYTE WhichChannel[] = { 1, 2, 4, 8 };
  38. static int ODRet = -1;    /* Assume Open Device has failed until it succeeds */
  39.  
  40. InitSounds()
  41. {
  42.     char *Sound[] = {"Deal", "Turn", "Win", "Lose"};
  43.     char *AllocRemember(), Buffer[BUFSIZ];
  44.     int SoundNo, Len, Rate, fd;
  45.  
  46.     for(SoundNo=0; SoundNo<NSOUNDS; SoundNo++) {
  47.         sprintf(Buffer, "%s%s", SOUNDDIR, Sound[SoundNo]);
  48.         if((fd = open(Buffer, O_RDONLY)) == -1)
  49.             return -1;
  50.         if(read(fd, &Len, sizeof(ULONG)) < sizeof(ULONG))
  51.             return -1;
  52.         if(read(fd, &Rate, sizeof(ULONG)) < sizeof(ULONG))
  53.             return -1;
  54.         SoundArray[SoundNo].Len = Len;
  55.         SoundArray[SoundNo].Rate = Rate;
  56.         if(!(SoundArray[SoundNo].Data = AllocRemember(&SoundRKey, Len,
  57.                     MEMF_CHIP))) {
  58.             fprintf(stderr, "InitSound() - out of memory\n");
  59.             return -1;
  60.         }
  61.         if(read(fd, SoundArray[SoundNo].Data, Len) < Len)
  62.             return -1;
  63.         close(fd);
  64.     }
  65.     /* Set up audio device */
  66.     if(!(IOASound = (struct IOAudio *)AllocRemember(&SoundRKey,
  67.                 sizeof(struct IOAudio), MEMF_CHIP|MEMF_CLEAR)))
  68.         return -1;
  69.     if(!(IOASound->ioa_Request.io_Message.mn_ReplyPort=CreatePort("p3", 0)))
  70.         return -1;
  71.     IOASound->ioa_Request.io_Message.mn_Node.ln_Pri = 0;
  72.     IOASound->ioa_Request.io_Command = ADCMD_ALLOCATE;
  73.     IOASound->ioa_Request.io_Flags = ADIOF_NOWAIT;
  74.     IOASound->ioa_AllocKey = 0;
  75.     IOASound->ioa_Data = WhichChannel;
  76.     IOASound->ioa_Length = (ULONG)sizeof(WhichChannel);
  77.  
  78.     if((ODRet = OpenDevice(AUDIONAME, 0L, IOASound, 0L))) {
  79.         fprintf(stderr, "Cannot open audio device\n");
  80.         return -1;
  81.     }
  82.     return 0;
  83. }
  84.  
  85. FreeSounds()
  86. {
  87.     if(!ODRet)
  88.         CloseDevice(IOASound);
  89.     FreeRemember(&SoundRKey, TRUE);
  90.     return 0;
  91. }
  92.  
  93. void
  94. PlaySound(SoundNo)
  95. UBYTE SoundNo;
  96. {
  97.     ULONG Clock;
  98.  
  99.     if(SoundNo>=NSOUNDS)
  100.         return;
  101.  
  102.     if(GfxBase && (GfxBase->DisplayFlags & PAL))
  103.         Clock = 3546895L;
  104.     else
  105.         Clock = 3579545L;
  106.  
  107.     IOASound->ioa_Request.io_Command = CMD_WRITE;
  108.     IOASound->ioa_Request.io_Flags = ADIOF_PERVOL;
  109.     IOASound->ioa_Data = SoundArray[SoundNo].Data;
  110.     IOASound->ioa_Cycles = 1;
  111.     IOASound->ioa_Length = SoundArray[SoundNo].Len;
  112.     IOASound->ioa_Period = Clock / SoundArray[SoundNo].Rate;
  113.     IOASound->ioa_Volume = 64;
  114.  
  115.     BeginIO(IOASound);
  116.     WaitIO(IOASound);
  117. }
  118.