Files
amiga/RetroIRC/bell.c
T

73 lines
3.1 KiB
C

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/nodes.h>
#include <devices/audio.h>
#include <graphics/gfxbase.h>
#include <proto/exec.h>
#include <stdio.h>
#include "bell.h"
extern struct GfxBase *GfxBase;
/* BeginIO is the device entry at -30, NOT exec SendIO (which strips
ADIOF_PERVOL and loses the sample's volume/period settings). */
void __bellBeginIO(__reg("a6")void*,__reg("a1")struct IORequest*)="\tjsr\t-30(a6)";
static struct MsgPort *port;
static struct IOAudio *audio,*right;
static UBYTE *sample;
static ULONG length,lastBell,allocated;
static int opened,active,played;
int bell_init(void){FILE*f;long n;
f=fopen("PROGDIR:bell.raw","rb");if(!f)return 0;
fseek(f,0,SEEK_END);n=ftell(f);rewind(f);
if(n<2||n>16384||(n&1)){fclose(f);return 0;}
length=n;sample=AllocMem(length,MEMF_CHIP|MEMF_PUBLIC);
if(!sample){fclose(f);return 0;}
if(fread(sample,1,length,f)!=length){fclose(f);bell_exit();return 0;}fclose(f);
port=CreateMsgPort();if(!port){bell_exit();return 0;}
audio=(struct IOAudio*)CreateIORequest(port,sizeof(struct IOAudio));
right=(struct IOAudio*)CreateIORequest(port,sizeof(struct IOAudio));
if(!audio||!right){bell_exit();return 0;}return 1;
}
void bell_poll(void){struct Message *m;
if(!port)return;
while((m=GetMsg(port))!=0){
if(m==(struct Message*)audio)active&=~1;
if(m==(struct Message*)right)active&=~2;
}
if(opened&&!active){
audio->ioa_Request.io_Unit=(struct Unit*)allocated;
CloseDevice((struct IORequest*)audio);opened=0;
}
}
static void start(struct IOAudio *io,ULONG unit){
io->ioa_Request.io_Message.mn_Node.ln_Type=NT_MESSAGE;
io->ioa_Request.io_Unit=(struct Unit*)unit;
io->ioa_Request.io_Command=CMD_WRITE;
io->ioa_Request.io_Flags=ADIOF_PERVOL;
io->ioa_Data=sample;io->ioa_Length=length;
io->ioa_Period=((GfxBase->DisplayFlags&PAL)?3546895UL:3579545UL)/11025;
io->ioa_Volume=18;io->ioa_Cycles=1;
__bellBeginIO(io->ioa_Request.io_Device,(struct IORequest*)io);
}
void bell_play(unsigned long now){static UBYTE channels[]={3,12,5,10};ULONG first,second;
bell_poll();if(!audio||active||(played&&now-lastBell<18))return;
played=1;lastBell=now;
/* One channel per ear, minimum allocation precedence, no waiting. */
audio->ioa_Request.io_Message.mn_Node.ln_Pri=-128;
audio->ioa_Request.io_Flags=ADIOF_NOWAIT;
audio->ioa_AllocKey=0;audio->ioa_Data=channels;audio->ioa_Length=sizeof(channels);
if(OpenDevice(AUDIONAME,0,(struct IORequest*)audio,0))return;
opened=1;allocated=(ULONG)audio->ioa_Request.io_Unit;
*right=*audio;first=allocated&(~allocated+1);second=allocated&~first;
active=3;start(audio,first);start(right,second);
}
void bell_exit(void){
if(active&1)if(!CheckIO((struct IORequest*)audio))AbortIO((struct IORequest*)audio);
if(active&2)if(!CheckIO((struct IORequest*)right))AbortIO((struct IORequest*)right);
while(active){WaitPort(port);bell_poll();}
if(opened){audio->ioa_Request.io_Unit=(struct Unit*)allocated;CloseDevice((struct IORequest*)audio);opened=0;}
if(right){DeleteIORequest((struct IORequest*)right);right=0;}
if(audio){DeleteIORequest((struct IORequest*)audio);audio=0;}
if(port){DeleteMsgPort(port);port=0;}
if(sample){FreeMem(sample,length);sample=0;}
}