home *** CD-ROM | disk | FTP | other *** search
-
- /*
- *
- * Module : sound.c
- *
- * Description : Sound effects for hilow game.
- *
- * Author : Simon J Raybould. (sie@fulcrum.bt.co.uk)
- *
- * Date : 9th September 1990.
- *
- */
-
- #include <stdio.h>
- #include <fcntl.h>
- #include <intuition/intuition.h>
- #include <exec/types.h>
- #include <exec/memory.h>
- #include <devices/audio.h>
- #include <graphics/gfxbase.h>
-
- #include "sound.h"
-
- struct MySound {
- ULONG Len;
- ULONG Rate;
- char *Data;
- };
-
- static struct MySound SoundArray[NSOUNDS];
- static struct Remember *SoundRKey = NULL;
-
- extern struct MsgPort *CreatePort();
- extern struct GfxBase *GfxBase;
- static struct IOAudio *IOASound;
-
- static UBYTE WhichChannel[] = { 1, 2, 4, 8 };
- static int ODRet = -1; /* Assume Open Device has failed until it succeeds */
-
- InitSounds()
- {
- char *Sound[] = {"Deal", "Turn", "Win", "Lose"};
- char *AllocRemember(), Buffer[BUFSIZ];
- int SoundNo, Len, Rate, fd;
-
- for(SoundNo=0; SoundNo<NSOUNDS; SoundNo++) {
- sprintf(Buffer, "%s%s", SOUNDDIR, Sound[SoundNo]);
- if((fd = open(Buffer, O_RDONLY)) == -1)
- return -1;
- if(read(fd, &Len, sizeof(ULONG)) < sizeof(ULONG))
- return -1;
- if(read(fd, &Rate, sizeof(ULONG)) < sizeof(ULONG))
- return -1;
- SoundArray[SoundNo].Len = Len;
- SoundArray[SoundNo].Rate = Rate;
- if(!(SoundArray[SoundNo].Data = AllocRemember(&SoundRKey, Len,
- MEMF_CHIP))) {
- fprintf(stderr, "InitSound() - out of memory\n");
- return -1;
- }
- if(read(fd, SoundArray[SoundNo].Data, Len) < Len)
- return -1;
- close(fd);
- }
- /* Set up audio device */
- if(!(IOASound = (struct IOAudio *)AllocRemember(&SoundRKey,
- sizeof(struct IOAudio), MEMF_CHIP|MEMF_CLEAR)))
- return -1;
- if(!(IOASound->ioa_Request.io_Message.mn_ReplyPort=CreatePort("p3", 0)))
- return -1;
- IOASound->ioa_Request.io_Message.mn_Node.ln_Pri = 0;
- IOASound->ioa_Request.io_Command = ADCMD_ALLOCATE;
- IOASound->ioa_Request.io_Flags = ADIOF_NOWAIT;
- IOASound->ioa_AllocKey = 0;
- IOASound->ioa_Data = WhichChannel;
- IOASound->ioa_Length = (ULONG)sizeof(WhichChannel);
-
- if((ODRet = OpenDevice(AUDIONAME, 0L, IOASound, 0L))) {
- fprintf(stderr, "Cannot open audio device\n");
- return -1;
- }
- return 0;
- }
-
- FreeSounds()
- {
- if(!ODRet)
- CloseDevice(IOASound);
- FreeRemember(&SoundRKey, TRUE);
- return 0;
- }
-
- void
- PlaySound(SoundNo)
- UBYTE SoundNo;
- {
- ULONG Clock;
-
- if(SoundNo>=NSOUNDS)
- return;
-
- if(GfxBase && (GfxBase->DisplayFlags & PAL))
- Clock = 3546895L;
- else
- Clock = 3579545L;
-
- IOASound->ioa_Request.io_Command = CMD_WRITE;
- IOASound->ioa_Request.io_Flags = ADIOF_PERVOL;
- IOASound->ioa_Data = SoundArray[SoundNo].Data;
- IOASound->ioa_Cycles = 1;
- IOASound->ioa_Length = SoundArray[SoundNo].Len;
- IOASound->ioa_Period = Clock / SoundArray[SoundNo].Rate;
- IOASound->ioa_Volume = 64;
-
- BeginIO(IOASound);
- WaitIO(IOASound);
- }
-