From: sugino@tippler.unisys.co.jp (Junsei Sugino)
Subject: Re: How to use /dev/audio.
Reply-To: sugino@tippler.unisys.co.jp
Organization: NIHON UNISYS, Ltd.
Distribution: fj
Date: Fri, 13 Nov 1992 04:02:15 GMT

In article <SUGINO.92Nov12212710@mars.tippler.unisys.co.jp> sugino@tippler.unisys.co.jp (Junsei Sugino) writes:

$@$3$s$K$A$O!"?yLn!wF|K\%f%K%7%9(J$@$G$9!#(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$&(J$@$K$J$C$?$N$G$9$,!"$I$&$b!"OB2;$r=P$=$&$H$9$k$H$&$^(J
 >$@$/$$$-$^$;$s!#C1=c$KJ?6Q$r$H$C$?$@$1$G$OBLL\$_$?(J$@$$$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(J$@$l$^$9!#!W$H$"$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(J$@;H$C$F$_$?$H$3$m!"$=$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
 >$@Ev(J$@A3!*!)(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$?(J$@$+!"$4B8CN$NJ}$O$I$&$+65$($F2<$5$$!#(J

$@$H$$$&<ALd$r$7$?$H$3$m!"(JE-mail $@$GEz$($F$$$?$@$-!"$=$NDL$j$d$j$^(J$@$7$?$i(J
$@$&$^$/$$$-$^$7$?$N$GJs9p$7$^$9!#(J

$@7k6I(J u-law $@$O<!$N$h$&$JJQ49$r$9$l$P$$$$$h$&$G$9!#(J

F(x) = sgn(x) * (ln(1+myu*abs(x))) / ln(1+myu)

$@$3$3$G!"CM$NHO0O$O(J

	myu  : 1.0
$@$N$H$-(J
	x    : -1.0 -> 1.0
	F(x) : -1.0 -> 1.0

$@$G$9$+$i!"$"$H$OE,Ev$KJd@5$r$+$1$l$P$$$$$h$&$G$9!#(J

$@$3$N>pJs$r65$($F$$$?$@$$$?!"0J2<(J$@$NJ}$K46<U$$$?$7$^$9!#(J

	$@4X8}!wElBg>pJsMM(J (cocoa@is.s.u-tokyo.ac.jp)
	$@EDJU!wElBg5!3#MM(J (t20280@mech.t.u-tokyo.ac.jp)

$@$I$&$b!"$"$j$,$H$&$4$6$$$^$7$?!#(J

$@$^$?!"$=$l$rF~$l$?%W%m%0%i%`$rEj9F$7$^$9!#Nc$K$h$C(J$@$F(J v $@$d(J t $@$O%5%]!<%H(J
$@$7$F$$$^$;$s!#;H$$J}$O(J

playcode cg8cg8fa8fa8cg8cg8o4go5d8o4go5d8o4g2. o6l8cegcegcfacfacegcego5bo6dgo5bo6dgc2. >/dev/audio

$@$J$I$H$d$C$F2<$5$$!#(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 ------------------------*/
/*
F(x) = sgn(x) * (ln(1+myu*abs(x))) / ln(1+myu)

F(x) : $@=PNO(J/127
x: $@F~NO(J/32768
myu: $@Dj?t(J255
*/

#define	MYU	255.0
#define	SGN(x)	(((x)<0)?-1:1)
#define ABS(x)	(((x)<0)?(-x):(x))

int	u_law( x )
dtype	x;
{
    double dx = ((double)x)/65536.0;
    return (int)(SGN(x) * 127.0 * log(1.0+MYU*ABS(dx)) / log(1.0+MYU));
}

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_law((dtype)(x/n));
	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)