/dev/audio examples / applications?

dave "who can do? ratmandu!" ratcliffe dave at sgi.com
Sat Jul 28 06:13:02 AEST 1990


In article <guido.649079335 at piring.cwi.nl> guido at cwi.nl (Guido van Rossum) writes:
>We just discovered /dev/audio in IRIX 3.2 for the PI, but there seem to
>be no standard programs to use it.  Anybody got interesting software to
>apply this?  Or even pointers?

in 3.3.1 (coming "soon" to your town) we've got 1 (sorry, only 1 for now...)
audio prog down in /usr/people/4Dgifts/examples/audio.  to tide you over,
the Makefile, README, and audio.c files follow:

***************  /usr/people/4Dgifts/examples/audio/Makefile:

SHELL = /bin/sh
PROGS=audio
CFLAGS= -O -s

all:    $(PROGS)

clean: 
        rm -f *.o $(PROGS)

.c:
        $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)



***************  /usr/people/4Dgifts/examples/audio/README:

                    README for audio programming examples


      The capability for audio input/output currently only exists on
      4D/2* machines (i.e. Personal IRIS machines ONLY).  Admittedly, 
      information about this functionality is sparse, but things WILL 
      be improving fairly soon now.  At this stage, a little bit of 
      source code example is better than none...



      audio.c:  demos how to digitally record microphone input to a 
                Personal IRIS (4D/20 or 4D/25) and place the data 
                into a file, as well as playing back the sounds
                already recorded.



***************  /usr/people/4Dgifts/examples/audio/audio.c:
/* 
 *  audio.c:                                          
 *
 * This test program was initially written to demonstrate how one would
 * digitally record microphone input to a Personal Iris and place the data
 * into a file (execute "audio -i -opts").  Alternately, you can play back
 * the sounds or voice that you have recorded (execute "audio -o -opts").
 * All that is needed to use this program is an 8 ohm speaker connected to
 * the audio output and a 600 ohm microphone connected to the microphone
 * input. The other audio input may be used to connect a higher level audio
 * signal that doesn't require pre-amplification.
 *
 *     resusitated by (& hats off to) Frank Demcak  --   February, 1990
 */

#include <stdio.h>
#include <fcntl.h>
#include <sys/audio.h>


main(argc, argv)
int argc;
char *argv[];
{
	int size, gain=100, rate=2, iflag, oflag, gflag, sflag, fflag,rflag;
	char *file, *buffer;
	int in, out, audio,i;


	if ( argc == 1 ) {
	    usage:
		fprintf(stderr,"usage: %s -i [-g (0-255)gain] ", argv[0]);
		fprintf(stderr,"[-r (0-3)rate] -f file [-s size]\n");
		fprintf(stderr,"       %s -o [-g (0-255)gain] ", argv[0]);
		fprintf(stderr,"[-r (0-3)rate] -f file [-s size]\n");
		exit(1);
	}

 /* Parse the record/playback options */
	
	for ( i = 1; i < argc; i++ ) {

		if ( !strcmp(argv[i],"-i") )
			iflag++;
		else if ( !strcmp(argv[i],"-o") ) 
			oflag++;
		else if ( !strcmp(argv[i],"-g") ) {
			gflag++;
			gain = atoi(argv[++i]);
		} else if ( !strcmp(argv[i],"-r") ) {
			rflag++;
			rate = atoi(argv[++i]);
		} else if ( !strcmp(argv[i],"-f") ) {
			fflag++;
			file = argv[++i];
	        } else if ( !strcmp(argv[i],"-s") ) {
			sflag++;
			size = atoi(argv[++i]);
		} else
			goto usage;

        }
 /* Open input or output file for record/playback */


	if ( iflag ) {
		if (fflag) {
			/* Create a Read Write file and set permissions */
			out = open(file, O_CREAT|O_RDWR); 
			chmod (file,00666);
		} else
			out = 1;
	} else if ( oflag ) {
		if (fflag)
			/* Just open file for reading */
			in = open(file, O_RDONLY);
		else
			in = 0;
	} else	{
		fprintf(stderr, "must specify one of -o or -i\n");
		exit(1);
	}

 /* printf test diag statements 
	printf("iflag=%i\n",iflag);
	printf("oflag=%i\n",oflag);
	printf("gflag=%i\n",gflag);
	printf("   gain =%d\n",gain);
	printf("rflag=%i\n",rflag);
	printf("   rate =%d\n",rate);
	printf("fflag=%i\n",fflag);
	printf("   file Name=%s\n",file);
	printf("sflag=%i\n",sflag);
	printf("   file size=%d\n",size);
 */

	/* Open audio port as a character device */	

	audio = open( "/dev/audio", O_RDWR );
	

	/* This part doesn't seem to work as advertised */
	/* Investigating how to turn off output while input is being recorded */

	if ( gflag )
		if ( iflag ) {
			ioctl(audio, AUDIOCSETINGAIN, gain);
			ioctl(audio, AUDIOCSETOUTGAIN, 0);
		} else {
			ioctl(audio, AUDIOCSETINGAIN, 0);
			ioctl(audio, AUDIOCSETOUTGAIN, gain);
		}
	/* set audio sampling rate 0=none?,1=8k,2=16k,3=32k samples per sec */	

	if ( rflag ) 
		ioctl(audio, AUDIOCSETRATE, rate);
	
	if ( iflag ) {
		if ( ! sflag ) {
			fprintf(stderr,"must set a size for input\n");
			exit (1);
		}

		buffer = (char *)malloc( size );

		if ( ! buffer ) {
			fprintf(stderr,"unable to get %d byte buffer\n",size);
			exit(1);
		}

		read(audio,buffer,size);
		write(out,buffer,size);

	} else {

		/* Assume this is an sound output request */
		/* Default buffer size = 16k if nothing is specified */

		if ( ! sflag ) 
			size = 16536;

		buffer = (char *)malloc( size );
	
		if ( ! buffer ) {
			fprintf(stderr,"unable to get %d byte buffer\n",size);
			exit(1);
		}

		/*
		Write to audio port the contents of specified file until EOF
		This can be improved for simultaneous read/writes
		Insert user customization for specific file or usr needs i.e.

		while (read(in,buffer,size)!=EOF) write (audio,buffer,size);
		*/

		read (in,buffer,size);
		write(audio,buffer,size);

	}

        close(file);

}



More information about the Comp.sys.sgi mailing list