I'm trying to add sound to my 3D Swingball game, and I have some problems. The amiga-specific sound code looks like this:
#include <stdio.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <devices/ahi.h>
#include "audio.h"
#include "audio_amiga.h"
struct MsgPort *AHImp_sound = NULL;
struct AHIRequest *AHIios_sound[nosamples];
struct AHIRequest *AHIio_sound = NULL;
BYTE AHIDevice_sound = -1;
void Amiga_FreeAhi()
{
if(!AHIDevice_sound)
IExec->CloseDevice((struct IORequest *)AHIio_sound);
int i;
for(i = 0; i < nosamples; i++)
IExec->FreeSysObject(ASOT_IOREQUEST, AHIios_sound[i]);
IExec->FreeSysObject(ASOT_IOREQUEST, (struct IORequest *)AHIio_sound);
IExec->FreeSysObject(ASOT_PORT, AHImp_sound);
audioison = 0;
}
void Amiga_InitAhi()
{
if(audioison)
{
printf("Error: Audio already is on!\n");
return;
}
if((AHImp_sound = IExec->AllocSysObjectTags(ASOT_PORT, TAG_DONE)) != NULL)
{
if((AHIio_sound = (struct AHIRequest *) IExec->AllocSysObjectTags(ASOT_IOREQUEST,
ASOIOR_ReplyPort, AHImp_sound,
ASOIOR_Size,sizeof(struct AHIRequest),
TAG_DONE)) != NULL)
{
AHIio_sound->ahir_Version = 4;
AHIDevice_sound = IExec->OpenDevice(AHINAME,0,(struct IORequest *)AHIio_sound,0);
}
}
if(AHIDevice_sound)
{
printf("Unable to open %s/0 version 4\n",AHINAME);
Amiga_FreeAhi();
audioison = 0;
return;
}
int i;
for(i = 0; i < nosamples; i++)
{
AHIios_sound[i] = (struct AHIRequest *)IExec->AllocSysObjectTags(ASOT_IOREQUEST, ASOIOR_Duplicate, *AHIio_sound, TAG_DONE);
AHIios_sound[i]->ahir_Std.io_Message.mn_Node.ln_Pri = 127;
AHIios_sound[i]->ahir_Std.io_Command = CMD_WRITE;
AHIios_sound[i]->ahir_Std.io_Data = samples[i]->data;
AHIios_sound[i]->ahir_Std.io_Length = samples[i]->data_size;
AHIios_sound[i]->ahir_Std.io_Offset = 0;
AHIios_sound[i]->ahir_Frequency = samples[i]->sample_rate;
if(samples[i]->bits_per_sample == 8)
AHIios_sound[i]->ahir_Type = AHIST_M8S;
else
AHIios_sound[i]->ahir_Type = AHIST_M16S;
AHIios_sound[i]->ahir_Volume = 0x10000; // Full volume
AHIios_sound[i]->ahir_Position = 0x8000; // Centered
AHIios_sound[i]->ahir_Link = NULL;
}
Amiga_PlaySample(0);
Amiga_PlaySample(0);
audioison = 1;
}
void Amiga_PlaySample(int s)
{
if(IExec->CheckIO((struct IORequest *) AHIios_sound[s]) == NULL)
{
printf("Sample still playing, aborting IO...\n");
IExec->AbortIO((struct IORequest *) AHIios_sound[s]);
IExec->WaitIO((struct IORequest *) AHIios_sound[s]);
}
IExec->SendIO((struct IORequest *) AHIios_sound[s]);
}
The problem is this: In the middle of the game, I want to be able to abort playing a certain sample when another one pops up. Hence the CheckIO, AbortIO and WaitIO in the Amiga_PlaySample() function. But for some reason it doesn't work. CheckIO never returns NULL as expected, and SendIO messes up, so sound stops completely.
As you can see, I have inserted two test lines to the InitAhi() function, and here it "works" (it correctly prints "Sample still playing...(etc)".
What am I doing wrong??
Edited by alfkil on 2012/10/20 21:34:01