@asman, post #29
A propos kombinacji WaitPort/GetMsg, to masz na myśli WaitPort a potem w pętli odbieranie wiadomości GetMsg aż będzię null ?
@mschulz, post #31
@asman, post #27
Nie rozumiem czemu sprawdzasz czy udało się stworzyć iorequest dla AudioIO[1] a dla Audio[0] już nie.
nie musisz sprawdzać czy się powiodło stworzenie message portu bo nawet jeśli zwróci null, to CreateIORequest wyjdzie z nullem jak dobrze pamiętam. Wtedy to uprości kod.
@Phibrizzo, post #33
Nie wiem czy dobrze zalozylem
jesli wykonuje po sobie ciag tych samych funkcji to jesli ostatnia sie powiodla to poprzednie tez musialy.
nie musisz sprawdzać czy się powiodło stworzenie message portu bo nawet jeśli zwróci null, to CreateIORequest wyjdzie z nullem jak dobrze pamiętam. Wtedy to uprości kod.
Jesli jestes tego pewny to ten fragment tez zmienie dla uproszczenia.
@mschulz, post #34
@asman, post #35
Musze przyznać, że jakoś nad tym się nie zastanawiałem, myślałem że uniknięcie jednego ifa to świetna okazja by kod był prostszy.
Ale w sumie jak ktoś zacznie go przeglądać to będzie się zastanawiał czy autor nie popełnił błędu bo nie ma sprawdzania w funkcji tworzącej coś.
@Hexmage960, post #30
Wait() and WaitPort()
These functions can be used to put your task to sleep while a sound plays. Wait() takes a wake-up mask as its argument. The wake-up mask is usually the mp_SigBit of a MsgPort that you have set up to get replies back from the audio device.
WaitPort() will put your task to sleep while a sound plays. The argument to WaitPort() is a pointer to a MsgPort that you have set up to get replies back from the audio device.
AbortIO()
This function can be used to cancel requests for ADCMD_ALLOCATE, ADCMD_LOCK, CMD_WRITE, or ADCMD_WAITCYCLE. When used with the audio device, AbortIO() always succeeds.
@Phibrizzo, post #37
AbortIO((struct IORequest *)AudioIO); /* Abort any pending requests */ WaitPort(AudioMP); /* Wait for abort message */ GetMsg(AudioMP); /* Get abort message */
@asman, post #39
@Phibrizzo, post #40
@Hexmage960, post #42
#include "sound.h" #include "resources.h" #include <clib/alib_protos.h> #include <proto/exec.h> #include <proto/exec.h> #include <devices/audio.h> #include <devices/ahi.h> extern int g_bSoundType; /*==========================================================================*/ typedef struct { int prio; } AudioChannel; static struct MsgPort* m_pMsgAudioPort = NULL; static struct IOAudio* m_pAudioIO = NULL; static BYTE* m_SfxData[15]; static LONG m_clock = 3546895; //clock constant for PAL static UBYTE whichannel[] = { 1,2,4,8 }; static AudioChannel sfxChannel; static int m_sfxTabPrio[15] = { 15, 2, 1, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 15, 13, /* SND_BOOM = 0, SND_SHOT = 1, SND_STUK = 2, SND_TELEPORT = 3, SND_SCREW = 4, SND_LIFE = 5, SND_DOOR = 6, SND_BULLETS = 7, SND_BOX = 8, SND_KEY = 9, SND_KILL = 10, SND_BEGIN = 11, SND_UKN = 12, SND_EXIT = 13, SND_MAGNES = 14, */ }; static void PlayAhiSfx(int sfxNumber); static void PlayPaulaSfx(int sfxNumber); /*------------------------------------------------------*/ static int initSoundPaula(void); static void killSoundPaula(void); static int initSoundAhi(void); static void killSoundAhi(void); static struct MsgPort* m_pAhiMp = NULL; static struct AHIRequest* m_pAhiIO = NULL; static BYTE m_AhiDevice = 0; /*==========================================================================*/ void PlaySfx(int sfxNumber) { if (SOUND_PAULA == g_bSoundType) { PlayPaulaSfx(sfxNumber); } else if (SOUND_AHI == g_bSoundType) { PlayAhiSfx(sfxNumber); } } /*==========================================================================*/ static void PlayAhiSfx(int sfxNumber) { if (0 == CheckIO((struct IORequest*)m_pAhiIO)) { int nPrio = m_sfxTabPrio[sfxNumber]; if (nPrio < sfxChannel.prio) { return; } else { AbortIO((struct IORequest*)m_pAhiIO); } } WaitIO((struct IORequest*)m_pAhiIO); m_pAhiIO->ahir_Std.io_Message.mn_Node.ln_Pri = -50; m_pAhiIO->ahir_Std.io_Command = CMD_WRITE; m_pAhiIO->ahir_Std.io_Data = m_SfxData[sfxNumber]; m_pAhiIO->ahir_Std.io_Length = m_sfxSize[sfxNumber]; m_pAhiIO->ahir_Std.io_Offset = 0; m_pAhiIO->ahir_Frequency = 22050; m_pAhiIO->ahir_Type = AHIST_M8S; m_pAhiIO->ahir_Volume = 0x10000; m_pAhiIO->ahir_Position = 0x8000; m_pAhiIO->ahir_Link = 0; BeginIO((struct IORequest*)m_pAhiIO); sfxChannel.prio = m_sfxTabPrio[sfxNumber]; } /*==========================================================================*/ static void PlayPaulaSfx(int sfxNumber) { if (0 == CheckIO((struct IORequest*)m_pAudioIO)) { int nPrio = m_sfxTabPrio[sfxNumber]; if (nPrio < sfxChannel.prio) { return; } else { m_pAudioIO->ioa_Request.io_Command = ADCMD_FINISH; m_pAudioIO->ioa_Request.io_Flags = ADIOF_SYNCCYCLE; BeginIO((struct IORequest*)m_pAudioIO); //Też działa sprawdzone //AbortIO((struct IORequest*)m_pAudioIO); //WaitPort(m_pMsgAudioPort); //GetMsg(m_pMsgAudioPort); } } WaitIO((struct IORequest*)m_pAudioIO); m_pAudioIO->ioa_Request.io_Command = CMD_WRITE; m_pAudioIO->ioa_Request.io_Flags = ADIOF_PERVOL; m_pAudioIO->ioa_Data = m_SfxData[sfxNumber]; m_pAudioIO->ioa_Length = m_sfxSize[sfxNumber]; m_pAudioIO->ioa_Volume = 64; m_pAudioIO->ioa_Cycles = 1; BeginIO((struct IORequest*)m_pAudioIO); sfxChannel.prio = m_sfxTabPrio[sfxNumber]; } /*==========================================================================*/ int initSound(void) { int iResult = 0; int i; UBYTE* pSfx; pSfx = g_pSoundsData; for (i = 0; i < 15; i++) { m_SfxData[i] = (BYTE*)pSfx; pSfx += m_sfxSize[i]; } if (SOUND_PAULA == g_bSoundType) { iResult = initSoundPaula(); } else if (SOUND_AHI == g_bSoundType) { iResult = initSoundAhi(); } return iResult; } /*==========================================================================*/ void killSound(void) { if (SOUND_PAULA == g_bSoundType) { killSoundPaula(); } else if (SOUND_AHI == g_bSoundType) { killSoundAhi(); } } /*==========================================================================*/ static int initSoundAhi(void) { m_pAhiMp = CreateMsgPort(); if (0 == m_pAhiMp) { return -1; } m_pAhiIO = CreateIORequest(m_pAhiMp, sizeof(struct AHIRequest)); if (0 == m_pAhiIO) { return -1; } m_pAhiIO->ahir_Version = 4; m_AhiDevice = OpenDevice(AHINAME, AHI_DEFAULT_UNIT, (struct IORequest*)m_pAhiIO, 0UL); if (0 != m_AhiDevice) { return -1; } m_pAhiIO->ahir_Std.io_Message.mn_Node.ln_Pri = -50; m_pAhiIO->ahir_Std.io_Command = CMD_WRITE; m_pAhiIO->ahir_Std.io_Data = m_SfxData[SND_BOOM]; m_pAhiIO->ahir_Std.io_Length = (ULONG)0; m_pAhiIO->ahir_Std.io_Offset = 0; m_pAhiIO->ahir_Std.io_Flags = 0; m_pAhiIO->ahir_Frequency = (ULONG)22050; m_pAhiIO->ahir_Type = AHIST_M8S; m_pAhiIO->ahir_Volume = 0x10000; m_pAhiIO->ahir_Position = 0x8000; m_pAhiIO->ahir_Link = NULL; SendIO((struct IORequest*)m_pAhiIO); return 0; } /*==========================================================================*/ static void killSoundAhi(void) { if (m_AhiDevice) { AbortIO((struct IORequest*)m_pAhiIO); WaitPort(m_pAhiMp); GetMsg(m_pAhiMp); CloseDevice((struct IORequest*)m_pAhiIO); } if (m_pAhiIO) { DeleteIORequest(m_pAhiIO); } if (m_pAhiMp) { DeleteMsgPort(m_pAhiMp); } } /*==========================================================================*/ static int initSoundPaula(void) { int i; LONG error; sfxChannel.prio = 0; m_pMsgAudioPort = CreateMsgPort(); if (0 == m_pMsgAudioPort) { return -1; } m_pAudioIO = (struct IOAudio*) CreateIORequest(m_pMsgAudioPort, sizeof(struct IOAudio)); if (0 == m_pAudioIO) { return -1; } m_pAudioIO->ioa_Request.io_Message.mn_ReplyPort = m_pMsgAudioPort; m_pAudioIO->ioa_Request.io_Message.mn_Node.ln_Pri = 127; m_pAudioIO->ioa_Request.io_Command = ADCMD_ALLOCATE; m_pAudioIO->ioa_Request.io_Flags = ADIOF_NOWAIT; m_pAudioIO->ioa_AllocKey = 0; m_pAudioIO->ioa_Data = whichannel; m_pAudioIO->ioa_Length = sizeof(whichannel); error = OpenDevice(AUDIONAME, 0L, (struct IORequest*)m_pAudioIO, 0L); if (0 != error) { return -1; } m_pAudioIO->ioa_Request.io_Message.mn_ReplyPort = m_pMsgAudioPort; m_pAudioIO->ioa_Request.io_Command = CMD_WRITE; m_pAudioIO->ioa_Request.io_Flags = ADIOF_PERVOL; m_pAudioIO->ioa_Data = m_SfxData[SND_STUK]; m_pAudioIO->ioa_Length = 2; m_pAudioIO->ioa_Period = m_clock / 22050; m_pAudioIO->ioa_Volume = 64; m_pAudioIO->ioa_Cycles = 1; BeginIO((struct IORequest*)m_pAudioIO); return 0; } /*==========================================================================*/ static void killSoundPaula(void) { if (m_pAudioIO && m_pAudioIO->ioa_Request.io_Device) { AbortIO((struct IORequest*)m_pAudioIO); WaitPort(m_pMsgAudioPort); GetMsg(m_pMsgAudioPort); CloseDevice((struct IORequest*)m_pAudioIO); } if (m_pAudioIO) { DeleteIORequest(m_pAudioIO); } if (m_pMsgAudioPort) { DeleteMsgPort(m_pMsgAudioPort); } } /*==========================================================================*/
@asman, post #43
BOOL PaulaInit(void) { BOOL L = FALSE, P = FALSE; UBYTE ChannelL[] = {1}; UBYTE ChannelP[] = {2}; AudioPort[0] = CreateMsgPort(); AudioPort[1] = CreateMsgPort(); if(AudioPort[0] && AudioPort[1]) { AudioIO[0] = (struct IOAudio*)CreateIORequest(AudioPort[0], sizeof(struct IOAudio)); AudioIO[1] = (struct IOAudio*)CreateIORequest(AudioPort[1], sizeof(struct IOAudio)); if(AudioIO[0] && AudioIO[1]) { AudioIO[0]->ioa_Request.io_Message.mn_ReplyPort = AudioPort[0]; AudioIO[0]->ioa_Request.io_Message.mn_Node.ln_Pri = 127; AudioIO[0]->ioa_Request.io_Command = ADCMD_ALLOCATE; AudioIO[0]->ioa_Request.io_Flags = ADIOF_NOWAIT; AudioIO[0]->ioa_AllocKey = 0; AudioIO[0]->ioa_Data = ChannelL; AudioIO[0]->ioa_Length = sizeof(ChannelL); if(OpenDevice(AUDIONAME, 0L, (struct IORequest*)AudioIO[0], 0L) == NULL) { L = TRUE; } AudioIO[1]->ioa_Request.io_Message.mn_ReplyPort = AudioPort[1]; AudioIO[1]->ioa_Request.io_Message.mn_Node.ln_Pri = 127; AudioIO[1]->ioa_Request.io_Command = ADCMD_ALLOCATE; AudioIO[1]->ioa_Request.io_Flags = ADIOF_NOWAIT; AudioIO[1]->ioa_AllocKey = 0; AudioIO[1]->ioa_Data = ChannelP; AudioIO[1]->ioa_Length = sizeof(ChannelP); if(OpenDevice(AUDIONAME, 0L, (struct IORequest*)AudioIO[1], 0L) == NULL) { P = TRUE; } } } return((BOOL)(L & P)); } void PaulaKill(void) { if(AudioIO[1] && AudioIO[1]->ioa_Request.io_Device) { AbortIO ((struct IORequest*)AudioIO[1]); // WaitIO ((struct IORequest*)AudioIO[1]); WaitPort(AudioPort[1]); GetMsg (AudioPort[1]); CloseDevice((struct IORequest*)AudioIO[1]); } if(AudioIO[0] && AudioIO[0]->ioa_Request.io_Device) { AbortIO ((struct IORequest*)AudioIO[0]); // WaitIO ((struct IORequest*)AudioIO[0]); WaitPort(AudioPort[0]); GetMsg (AudioPort[0]); CloseDevice((struct IORequest*)AudioIO[0]); } if(AudioIO[1]) { DeleteIORequest(AudioIO[0]); DeleteIORequest(AudioIO[1]); } if(AudioPort[0]) DeleteMsgPort(AudioPort[0]); if(AudioPort[1]) DeleteMsgPort(AudioPort[1]); } void PaulaPlayL(int sfx) { if(CheckIO((struct IORequest*)AudioIO[0]) == NULL) { AbortIO ((struct IORequest*)AudioIO[0]); // WaitIO ((struct IORequest*)AudioIO[0]); WaitPort(AudioPort[0]); GetMsg (AudioPort[0]); } AudioIO[0]->ioa_Request.io_Command = CMD_WRITE; AudioIO[0]->ioa_Request.io_Flags = ADIOF_PERVOL; AudioIO[0]->ioa_Request.io_Unit = (APTR)(1); AudioIO[0]->ioa_Data = Dzwieki[sfx].Adres; AudioIO[0]->ioa_Length = Dzwieki[sfx].Len; AudioIO[0]->ioa_Period = (UWORD)(Dzwieki[sfx].Period); AudioIO[0]->ioa_Volume = (UWORD)(63); AudioIO[0]->ioa_Cycles = 1; BeginIO((struct IORequest*)AudioIO[0]); void PaulaPlayP(int sfx) { if(CheckIO((struct IORequest*)AudioIO[1]) == NULL) { AbortIO ((struct IORequest*)AudioIO[1]); // WaitIO ((struct IORequest*)AudioIO[1]); WaitPort(AudioPort[1]); GetMsg (AudioPort[1]); } AudioIO[1]->ioa_Request.io_Command = CMD_WRITE; AudioIO[1]->ioa_Request.io_Flags = ADIOF_PERVOL; AudioIO[1]->ioa_Request.io_Unit = (APTR)(2); AudioIO[1]->ioa_Data = Dzwieki[sfx].Adres; AudioIO[1]->ioa_Length = Dzwieki[sfx].Len; AudioIO[1]->ioa_Period = (UWORD)(Dzwieki[sfx].Period); AudioIO[1]->ioa_Volume = (UWORD)(63); AudioIO[1]->ioa_Cycles = 1; BeginIO((struct IORequest*)AudioIO[1]); }
@Phibrizzo, post #44
@asman, post #45
m_pAudioIOLeft->ioa_Request.io_Message.mn_ReplyPort = m_pMsgAudioPortLeft; m_pAudioIOLeft->ioa_AllocKey = 0; error = OpenDevice(AUDIONAME, 0L, (struct IORequest*)m_pAudioIOLeft, 0L); if (0 != error) { return -1; }
static int tryToAllocateChannel(struct IOAudio* pIOAudio, struct MsgPort* pMsgPortAudio) { pIOAudio->ioa_Request.io_Message.mn_ReplyPort = pMsgPortAudio; pIOAudio->ioa_Request.io_Message.mn_Node.ln_Pri = 127; pIOAudio->ioa_Request.io_Command = ADCMD_ALLOCATE; pIOAudio->ioa_Request.io_Flags = ADIOF_NOWAIT; pIOAudio->ioa_AllocKey = 0; /* ** io_Device, io_Unit ,ioa_Data, ioa_Length, must be set earlier */ BeginIO((struct IORequest*)pIOAudio); WaitPort(pMsgPortAudio); while (TRUE) { if (0 == GetMsg(pMsgPortAudio)) { break; } } return pIOAudio->ioa_Request.io_Error; } /***** tu przyklad alokowania ***/ m_pAudioIOLeft->ioa_Data = channelLeft; m_pAudioIOLeft->ioa_Length = sizeof(channelLeft); error = tryToAllocateChannel(m_pAudioIOLeft, m_pMsgAudioPortLeft); if (0 != error) { return error; } m_pAudioIORight->ioa_AllocKey = m_pAudioIOLeft->ioa_AllocKey; m_pAudioIORight->ioa_Request.io_Device = m_pAudioIOLeft->ioa_Request.io_Device; m_pAudioIORight->ioa_Request.io_Unit = m_pAudioIOLeft->ioa_Request.io_Unit; m_pAudioIORight->ioa_Data = channelRight; m_pAudioIORight->ioa_Length = sizeof(channelRight); error = tryToAllocateChannel(m_pAudioIORight, m_pMsgAudioPortRight); if (0 != error) { return error; }
static void PlayPaulaSfx(int sfxNumber) { if (0 == CheckIO((struct IORequest*)m_pAudioIO)) { int nPrio = m_sfxTabPrio[sfxNumber]; if (nPrio < sfxChannel.prio) { return; } else { m_pAudioIO->ioa_Request.io_Command = ADCMD_FINISH; m_pAudioIO->ioa_Request.io_Flags = ADIOF_SYNCCYCLE; BeginIO((struct IORequest*)m_pAudioIO); } } WaitPort(m_pMsgAudioPort); GetMsg(m_pMsgAudioPort); m_pAudioIO->ioa_Request.io_Command = CMD_WRITE; m_pAudioIO->ioa_Request.io_Flags = ADIOF_PERVOL; m_pAudioIO->ioa_Data = m_SfxData[sfxNumber]; m_pAudioIO->ioa_Length = m_sfxSize[sfxNumber]; m_pAudioIO->ioa_Volume = 64; m_pAudioIO->ioa_Cycles = 1; BeginIO((struct IORequest*)m_pAudioIO); sfxChannel.prio = m_sfxTabPrio[sfxNumber]; }
@Hexmage960, post #49
@Phibrizzo, post #50
@asman, post #51
@Hexmage960, post #52
@asman, post #53
@cholok, post #55
@Hexmage960, post #58
/*==========================================================================*/ void PlaySfx(int sfxNumber) { if (0 == CheckIO((struct IORequest*)m_pAudioIO)) { m_pAudioIO->ioa_Request.io_Command = ADCMD_FINISH; m_pAudioIO->ioa_Request.io_Flags = IOF_QUICK; BeginIO((struct IORequest*)m_pAudioIO); } m_pAudioIO->ioa_Request.io_Command = CMD_WRITE; m_pAudioIO->ioa_Request.io_Flags = ADIOF_PERVOL; m_pAudioIO->ioa_Data = m_sfx[sfxNumber].address; m_pAudioIO->ioa_Length = m_sfx[sfxNumber].length; m_pAudioIO->ioa_Volume = m_sfx[sfxNumber].volume; m_pAudioIO->ioa_Period = m_sfx[sfxNumber].period; m_pAudioIO->ioa_Cycles = 1; BeginIO((struct IORequest*)m_pAudioIO); } /*==========================================================================*/ static int initSoundPaula(void) { LONG error; m_pMsgAudioPort = CreateMsgPort(); if (0 == m_pMsgAudioPort) { return -1; } m_pAudioIO = (struct IOAudio*) CreateIORequest(m_pMsgAudioPort, sizeof(struct IOAudio)); if (0 == m_pAudioIO) { return -1; } g_nAudioPortSignal = 1L << m_pMsgAudioPort->mp_SigBit; error = OpenDevice(AUDIONAME, 0L, (struct IORequest*)m_pAudioIO, 0L); if (0 != error) { return -1; } m_pAudioIO->ioa_Request.io_Message.mn_Node.ln_Pri = 127; m_pAudioIO->ioa_Request.io_Message.mn_ReplyPort = m_pMsgAudioPort; m_pAudioIO->ioa_Request.io_Command = ADCMD_ALLOCATE; m_pAudioIO->ioa_Request.io_Flags = ADIOF_NOWAIT; m_pAudioIO->ioa_AllocKey = 0; m_pAudioIO->ioa_Data = m_whiChannel; m_pAudioIO->ioa_Length = sizeof(m_whiChannel); BeginIO((struct IORequest*)m_pAudioIO); if (0 != m_pAudioIO->ioa_Request.io_Error) { return -1; } return 0; } /*==========================================================================*/ static void killSoundPaula(void) { if (m_pAudioIO) { if (m_pAudioIO->ioa_Request.io_Device) { if (0 == CheckIO((struct IORequest*)m_pAudioIO)) { AbortIO((struct IORequest*)m_pAudioIO); WaitPort(m_pMsgAudioPort); GetMsg(m_pMsgAudioPort); } CloseDevice((struct IORequest*)m_pAudioIO); } DeleteIORequest(m_pAudioIO); } if (m_pMsgAudioPort) { DeleteMsgPort(m_pMsgAudioPort); } } /*==========================================================================*/ void GetSoundMsg(void) { GetMsg(m_pMsgAudioPort); } /*==========================================================================*/
while (!g_bExitFromGame) { ULONG sigSet = g_nWindowSignal | g_nMainTimerSig| g_nDemoTimerSig | g_nGamePortSignal | g_nAudioPortSignal | SIGBREAKF_CTRL_C; if (g_bEditMode) { sigSet |= g_nEditorWinSignal; } const ULONG signals = Wait(sigSet); if (signals & SIGBREAKF_CTRL_C) { g_bExitFromGame = TRUE; } if (signals & g_nAudioPortSignal) { GetSoundMsg(); } /* wywaliłem reszte rzeczy żeby nie zaciemniać */