[comp.sys.amiga.tech] Need help with ports, messages, audio.device...

aycock@cs-sun-fsa.cpsc.ucalgary.ca (John Aycock) (01/04/91)

While trying to compile a large program that was prone to errors, I thought
it would be nice for it to beep if it stopped.  So I wrote this program to
do just that.  Unfortunately, it causes intermittent machine crashes, so I'm
obviously doing something wrong.  I suspect it has to do with something not
initialized properly, or me not waiting/responding correctly.  It seems to
be a delayed reaction, though... it doesn't usually crash right away, but
later, when I start up the editor, say.  Thanks in advance for any help.
BTW, is there a good book that would desribe ports and messages (as opposed
to flipping through 7 sections in two ungainly RKMs)???
:ja

/*
 *	Quick hack to beep the speaker... beep [<n> [<delay]]
 *	The waveform && period are from the Hardware Manual, page 143.
 *	John Aycock, 1991
 */

#include <stdio.h>
#include <string.h>
#include <libraries/dos.h>

char *progname;

#include <exec/exec.h>
#include <devices/audio.h>

#define PRI_EMERG	100
#define WAVELEN		8

typedef unsigned char bool;
typedef unsigned char byte;

int beep ()
{
    int i, error = 0;
    bool audioopen = FALSE;
    byte *wave = NULL;
    struct MsgPort *port = NULL;
    struct IOAudio *IOB = NULL;
    static byte ch_alloc[] = { 1, 2, 4, 8 };
    static byte sinewave[] = { 0, 90, 127, 90, 0, -90, -127, -90 };
    
    if (!(port = (struct MsgPort *) CreatePort (NULL, 0L))) goto crapout;
    if (!(IOB = (struct IOAudio *) AllocMem (sizeof(struct IOAudio),
        MEMF_PUBLIC|MEMF_CLEAR))) goto crapout;
    IOB->ioa_Request.io_Message.mn_Node.ln_Type = NT_MESSAGE;
    IOB->ioa_Request.io_Message.mn_Node.ln_Pri = PRI_EMERG;
    IOB->ioa_Request.io_Message.mn_ReplyPort = port;
    IOB->ioa_Request.io_Message.mn_Length = sizeof(struct IOAudio);
    IOB->ioa_Data = ch_alloc;
    IOB->ioa_Length = 4;
    if (OpenDevice (AUDIONAME, 0, IOB, 0)) goto crapout;
    audioopen = TRUE;
    if (!(wave = (byte *) AllocMem (WAVELEN, MEMF_CHIP))) goto crapout;
    for (i = 0; i < WAVELEN; i++) wave[i] = sinewave[i];
    IOB->ioa_Request.io_Command = CMD_WRITE;
    IOB->ioa_Request.io_Flags = ADIOF_PERVOL;
    IOB->ioa_Data = wave;
    IOB->ioa_Length = WAVELEN;
    IOB->ioa_Period = 447;
    IOB->ioa_Volume = 64;	/* maximum volume */
    IOB->ioa_Cycles = 125;	/* a guess */
    BeginIO (IOB);
    WaitIO (IOB);
    error = 1;
crapout:
    error -= 1;
    if (port) DeletePort (port);
    if (IOB) FreeMem (IOB, sizeof(struct IOAudio));
    if (audioopen) CloseDevice (IOB);
    if (wave) FreeMem (wave, WAVELEN);
    return (error);
}

void main (argc, argv)
int argc;
char *argv[];
{
    int n = 0, deltime = 0;
    long sigs;
    
    for (progname = argv[0] + strlen(argv[0]) - 1;
	progname != argv[0] && *(progname-1) != '/' && *(progname-1) != ':';
	--progname) ;
    if (argc > 3 || (argc == 2 && !strncmp ("-h", argv[1], 2))) {
	fprintf (stderr, "Usage: %s [<n> [<time>]]\n", progname);
	fprintf (stderr, "       %s -h[elp]\n", progname);
	exit (0);
    }
    if (argc > 1) n = atoi (argv[1]);
    if (argc > 2) deltime = atoi (argv[2]);
    do {
	beep();
	if (!(sigs = SetSignal(0, 0) & SIGBREAKF_CTRL_C) && n) Delay (deltime);
    } while (!sigs && --n > 0);
    exit (0);
}

-- 

  //\|_||\|	John D. Aycock		aycock@cpsc.ucalgary.ca
\/ \/| || |	(403) 285-8727