From: sugino@tippler.unisys.co.jp (Junsei Sugino)
Subject: How to use /dev/audio.
Reply-To: sugino@tippler.unisys.co.jp
Organization: NIHON UNISYS, Ltd.
Distribution: fj
Date: Thu, 12 Nov 1992 12:27:10 GMT


$@$3$s$K$A$O!"?yLn!wF|K\%f%K%7%9$G$9!#$3$3$K!"Ej9F$9$k;~$O$$$D$b<ALd$P$+(J
$@$j$G?=$7Lu$"$j$^$;(J$@$s!#(J

$@:#!"(J/dev/audio $@$r;H$C$F$$$m$$$m2;$r=P$9%W%m%0%i%`$r:n$C$F$$$^$9!#C12;(J
$@$O$-$l$$$K=P$;$k$h$&$K$J$C(J$@$?$N$G$9$,!"$I$&$b!"OB2;$r=P$=$&$H$9$k$H$&$^(J
$@$/$$$-$^$;$s!#C1=c$KJ?6Q$r$H$C$?$@$1$G$OBLL\$_$?$$$G$9!#(J

$@$=$3$G!"(Jman audio $@$r$b$&0lEY8+$F$_$?$i!V?.9f$N6/$5$O(J u-law $@E>495!G=$K(J
$@=>$C$F%3!<%I2=$5$l$^$9!#!W$H$"(J$@$j$^$7$?!#;d$O!"!V$-$C$H%j%K%"$8$c$J$$C1(J
$@=cA}2C4X?t$K0c$$$J$$!#!W$H;W$$!"(Jsqrt() $@$J$I$r;H$C$F$_$?$H$3$m(J$@!"$=$3$=(J
$@$3=P$k$h$&$K$O$J$C$?$N$G$9$,!"$d$O$j!"By$C$?2;$7$+=P$^$;$s!#(J($@$=$j$c!"(J
$@EvA3!*!)(J)

$@$d$O$j!"(Ju-law $@$J$k$b$N$r$-$A$s$HM}2r$7$?$$$H;W$$!"Ej9F$9$k$3$H$r;W$$$D(J
$@$-$^$7$?!#$I$J$?$+!"$4B8CN$NJ}$O$I$&$+65$((J$@$F2<$5$$!#(J
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$@$A$J$_$K(J($@By$C$?2;$@$1$G$9$,(J)$@2;3Z$rAU$G(J$@$k%W%m%0%i%`$r$D$$$G$KEj9F$7$^$9!#(J
LDLIBS=-lm $@$G%3%s%Q%$%k$7$F2<$5$$!#;H$$J}$O(J

	playcode code ... > /dev/audio

$@$H$$$&$U$&$K;H$$!#%I%_%=$NOB2;$J$i(J

	playcode cego6c2 rrrc2 rrre2 rrrg2 > /dev/audio

$@$J$I(J$@$H$7$^$9!#%3!<%I$N=q$-J}$O(J BASIC $@$J$I$N(J PLAY $@J8$HF1$8$G$9!#$?$@$7!"(J
$@%F%s%]$N(J t $@$d2;NL$N(J v $@$J$I$O%5%]!<(J$@%H$7$F$$$^$;$s!#;H$($k$N$O(J a-g,o,r,l
$@$@$1$G$9!#(J

$@$=$l$G$O!"$h$m$7$/$*4j$$$7$^$9!#(J

------------------------ playcode.c (start) ------------------------
#include <stdio.h>
#include <math.h>
#define N2S(N) (((-N)&0200)|((~((N<0?-N:N)))&0177))

#define SHz	(8000.0)	/* sampling frequency */
#define AHz	(442.0)		/* Base frequency (sound A) */
#define PAI	(3.14159265358979)

#define	New(type)	((type*)malloc(sizeof(type)))
#define	News(type,n)	((type*)malloc(sizeof(type)*(n)))

/*------------------------ Play ------------------------*/
typedef struct Play {
    int	code;
    int length;
} Play;

typedef struct Plays {
    Play*	play;
    int		len;
} Plays;

#define	REST	(-5000)

Plays*	parsecode( str )
char*	str;
{
    int mode = 1;	/* 0 : char, 1 : number */
    int cmd;
    int val;
    int oct = 5;	/* OCTERV */
    int len = 4;	/* LENGTH */
    int i = 0;
    int* valp;
    Play array[256];
    Plays* ret;

    while ( *str ) {
	int cnt = 0;
	switch ( *str++ ) {
	  case 'B':
	  case 'b': /* cnt == 11 */
	    ++cnt;
	    ++cnt;
	  case 'A':
	  case 'a': /* cnt == 9 */
	    ++cnt;
	    ++cnt;
	  case 'G':
	  case 'g': /* cnt == 7 */
	    ++cnt;
	    ++cnt;
	  case 'F':
	  case 'f': /* cnt == 5 */
	    ++cnt;
	  case 'E':
	  case 'e': /* cnt == 4 */
	    ++cnt;
	    ++cnt;
	  case 'D':
	  case 'd': /* cnt == 2 */
	    ++cnt;
	    ++cnt;
	  case 'C':
	  case 'c': /* cnt == 0 */
	    for (;;) {
		if ( *str == '-' ) {
		    --cnt;
		} else if ( *str == '+' ) {
		    ++cnt;
		} else {
		    break;
		}
		++str;
	    }
	    array[i].code = (oct-5)*12+cnt+3;
	    valp = &val;
	    break;
	  case 'R':
	  case 'r':
	    array[i].code = REST;
	    valp = &val;
	    break;
	  case 'L':
	  case 'l':
	    valp = &len;
	    break;
	  case 'O':
	  case 'o':
	    valp = &oct;
	    break;
	  deafult:
	    fprintf( "unknown command '%c'\n", str[-1] );
	    return NULL;
	}
	for ( *valp = 0; isdigit(*str); ++str ) {
	    *valp *= 10;
	    *valp += *str - '0';
	}
	if ( valp == &val ) {
	    int dot;
	    for ( dot = 1; *str == '.'; ++str ) dot *= 2;
	    if ( !*valp ) *valp = len;
	    array[i].length = 24000 * (dot*2-1) / dot / *valp;
	    ++i;
	}
    }
    ret = New(Plays);
    ret->len = i;
    ret->play = News(Play,i);
    while ( i-- ) {
	ret->play[i] = array[i];
    }
    return ret;
}

/*------------------------ Sound ------------------------*/
typedef int	dtype;

typedef dtype	(*soundFunc)();

typedef struct Sound {
    soundFunc	fn;
} Sound;

#define	PLAY(sound)	(*(sound)->fn)(sound)

/*------------------------ SinData ------------------------*/
typedef struct SinData {
    Sound	sound;
    double	sin_a;
    double	sin_b;
    double	cos_a;
    double	cos_b;
} SinData;

static	dtype	playSinData( self )
SinData*	self;
{
    double sin_x;
    double cos_x;

    int n = self->sin_a;
    sin_x = self->sin_a*self->cos_b + self->cos_a*self->sin_b;
    cos_x = self->cos_a*self->cos_b - self->sin_a*self->sin_b;
    self->sin_a = sin_x;
    self->cos_a = cos_x;
    return (dtype)n;
}

static	Sound*	makeSinData( data )
int	data;	/* 0 : A, 1 : A#, 2 : B, 3 : C ... */
{
    SinData*	ret = New(SinData);

    double step = 2.0*PAI*AHz*pow(2.0,((double)data/12.0))/SHz;
    ret->sin_a = 0.0;
    ret->sin_b = sin(step);
    ret->cos_a = 32767.0;
    ret->cos_b = cos(step);
    ret->sound.fn = playSinData;
    return &ret->sound;
}

/*------------------------ playcode ------------------------*/
int	u_low( x )
dtype	x;
{
    int sign;
    if ( x < 0 ) {
	sign = -1;
	x = -x;
    } else {
	sign = 1;
    }
    x = (int)sqrt( (double)x );	/*$@$3$3$,%/%5%$$o$1$G$9$M!#$O$C$O$C$O!#(J*/
    if ( x > 127 ) x = 127;
    return x*sign;
}

void	playcode( plays, n )
Plays*	plays[];
{
    struct Data {
	Sound*	sound;
	int	j;
	int	k;
    } data[10];

    int		i;

    for ( i = 0; i < n; ++i ) {
	data[i].j = data[i].k = 0;
    }

#define J (data[i].j)
#define K (data[i].k)
    for (;;) {
	dtype x = 0;
	int xn;
	int flag = 0;
	for ( i = 0; i < n; ++i ) {
	    if ( K <= 0 ) {
		if ( J >= plays[i]->len ) continue;
		data[i].sound = makeSinData( plays[i]->play[J].code );
		K = plays[i]->play[J].length;
		++J;
	    }
	    flag = 1;
	    x += PLAY(data[i].sound);
	    /*printf( "[%d:%d:%d]", i, x, plays[i]->len ); fflush(stdout);*/
	    --K;
	}
	if ( !flag ) break;
	/*printf( "%d\n", x );*/
	xn = u_low(x);
	putchar(N2S(xn));
    }
#undef J
#undef K
}

main( argc, argv )
int	argc;
char*	argv[];
{
    Plays* plays[10];
    int ret;
    int l=0;

    for ( ++argv, --argc; argc > 0; ++argv, --argc ) {
	plays[l++] = parsecode( *argv );
    }
    playcode( plays, l );
}
------------------------ playcode.c (end) ------------------------
--
$@?yLn=g@6(J (Junsei Sugino)
$@F|K\%f%K%7%9(J($@3t(J) $@CN<1%7%9%F%`It(J $@3+H/5;=Q2](J
(Nihon Unisys co. Knowledge Systems Development Technology)
TEL 03-5562-1213 FAX 03-5562-1299
(TEL +81-3-5562-1213 FAX +81-3-5562-1299)
Newsgroups: fj.sys.sun