RCS_ID_C "$Id: beep.c,v 3.2 1994/05/16 22:13:18 ppessi Exp $";
/*
 * Most of this was stolen from the "beep" program from the Manx 3.6a
 * distribution.
 */

#include <string.h>

#include "amiga.h"
#include "display.h"

#include <devices/audio.h>
#include <clib/alib_protos.h>

UBYTE allocmap[] = { 1, 8, 2, 4 };
#ifdef USE_CHIP
CHIP UBYTE wave[] = { 69, 116, 126, 96, 36, 220, 160, 130, 140, 0 };
#else
UBYTE bwave[] = { 69, 116, 126, 96, 36, 220, 160, 130, 140, 0 };
#endif

void audiobell(void)
{
    struct MsgPort *mp = NULL;
    struct IOAudio *ar = NULL;
#ifndef USE_CHIP
    UBYTE *wave = NULL;
#endif

    if (!(mp = CreateMsgPort()) ||
#ifndef USE_CHIP
       !(wave = (UBYTE *)AllocMem(sizeof(bwave), MEMF_CHIP)) ||
#endif
       !(ar = (struct IOAudio *)CreateIORequest(mp, sizeof(*ar)))) {
        dsinvert(VISUAL_BELL);
	goto CleanReturn;
    }
#ifndef USE_CHIP
    memcpy(wave, bwave, sizeof(bwave));
#endif
    ar->ioa_Request.io_Message.mn_Node.ln_Pri = 20;
    ar->ioa_Data = allocmap;
    ar->ioa_Length = sizeof(allocmap);
    if (OpenDevice(AUDIONAME, 0, (struct IORequest *)ar, 0) == 0) {
        ar->ioa_Request.io_Command = CMD_WRITE;
	ar->ioa_Request.io_Flags = ADIOF_PERVOL;
	ar->ioa_Data = wave;
        ar->ioa_Length = 10;
	ar->ioa_Period = 421;
        ar->ioa_Volume = 64;
	ar->ioa_Cycles = 100;

	/* 
	 * We must use BeginIO/WaitIO pair, since
	 * DoIO/SendIO mangle the io_Flags
	 */
	BeginIO((struct IORequest *)ar);
	WaitIO((struct IORequest *)ar);
	CloseDevice((struct IORequest *)ar);
    } else dsinvert(VISUAL_BELL);

 CleanReturn:
#ifndef USE_CHIP
    if (wave) FreeMem(wave, sizeof(bwave)), wave = NULL;
#endif
    if (ar) DeleteIORequest((struct IORequest *)ar), ar = NULL;
    if (mp) DeleteMsgPort(mp), mp = NULL;
}
