It is possible to use low-level interface without interrupting other processes. The device.audio driver comes for that purpose.
However the low-level interface is meant to play
music. The sounds can be played with high-level interface without problems so use Kas1e's example in that matter.
These steps will allow you to use low-level AHI functions:
- Create message port and AHIRequest with appropriate functions from exec.library.
- Open the device with OpenDevice() giving AHI_NO_UNIT as a unit.
- Get interface to the library with GetInterface() giving as the first parameter io_Device field of IORequest.
struct AHIIFace *IAHI;
struct Library *AHIBase;
struct AHIRequest *ahi_request;
struct MsgPort *mp;
if (mp = IExec->CreateMsgPort())
{
if (ahi_request = (struct AHIRequest *)IExec->CreateIORequest(mp, sizeof(struct AHIRequest)))
{
ahi_request->ahir_Version = 4;
if (IExec->OpenDevice("ahi.device", AHI_NO_UNIT, (struct IORequest *)ahi_request, 0) == 0)
{
AHIBase = (struct Library *)ahi_request->ahir_Std.io_Device;
if (IAHI = (struct AHIIFace *)IExec->GetInterface(AHIBase, "main", 1, NULL))
{
// Interface got, we can now use AHI functions
// ...
// Once we are done we have to drop interface and free resources
IExec->DropInterface((struct Interface *)IAHI);
}
IExec->CloseDevice((struct IORequest *)ahi_request);
}
IExec->DeleteIORequest((struct IORequest *)ahi_request);
}
IExec->DeleteMsgPort(mp);
}
Once you get the AHI interface you can use its functions. To start playing sounds you need to allocate audio (optionally you can ask user for Audio mode and frequency). Then you need to Load samples to use with AHI. You do it with AHI_AllocAudio(), AHI_ControlAudio() and AHI_LoadSound().
struct AHIAudioCtrl *ahi_ctrl;
if (ahi_ctrl = IAHI->AHI_AllocAudio(
AHIA_AudioID, AHI_DEFAULT_ID,
AHIA_MixFreq, AHI_DEFAULT_FREQ,
AHIA_Channels, NUMBER_OF_CHANNELS, // the desired number of channels
AHIA_Sounds, NUMBER_OF_SOUNDS, // maximum number of sounds used
TAG_DONE))
{
IAHI->AHI_ControlAudio(ahi_ctrl, AHIC_Play, TRUE, TAG_DONE);
int i;
for (i = 0; i < NUMBER_OF_SOUNDS; i++)
{
// These variables need to be initialized
uint32 type;
APTR samplearray;
uint32 length;
struct AHISampleInfo sample;
sample.ahisi_Type = type;
// where type is the type of sample, for example AHIST_M8S for 8-bit mono sound
sample.ahisi_Address = samplearray;
// where samplearray must point to sample data
sample.ahisi_Length = length / IAHI->AHI_SampleFrameSize(type);
if (IAHI->AHI_LoadSound(i + 1, AHIST_SAMPLE, &sample, ahi_ctrl)) != 0)
{
// error while loading sound, cleanup
}
}
// everything OK, play the sounds
// ...
// then unload sounds and free the audio
for (i = 0; i < NUMBER_OF_SOUNDS; i++)
IAHI->AHI_UnloadSound(i + 1, ahi_ctrl);
IAHI->AHI_ControlAudio(ahi_ctrl, AHIC_Play, FALSE, TAG_DONE);
IAHI->AHI_FreeAudio(ahi_ctrl);
}
When everything is set up you can use the functions AHI_SetVol() to set volume, AHI_SetFreq() to set frequency, AHI_SetSound() to play the sounds.
As for M68k you don't need to convert these functions to M68k as there are working assembler examples in AHI developer's archive.
I'm writing the source code by hand so sorry for mistakes ;)
Edited by RNS-AMiGA-Club on 2011/8/20 20:32:57