/* st_au_lib.c */ /* * Copyright (c) 2001 Bruce R. Montague * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF * SUCH DAMAGE. */ /* * A very elementary audio library, really just * a wrapper for open with standard ioctls. * A read buffer is allocated based on the read sample size. */ #include #include #include void st_err( char *msg ); static int au_dev; static int err; static int dsp_blk_size; static int bytes_per_sample; static char *buf; /*--------------------------------------------*/ void open_au_dev( char *dev, int mode ) { int tmp; /*--- Access driver. */ au_dev = open( dev, mode, 0 ); if( 0 > au_dev ) { perror( "Cant access audio device!" ); st_err( "\n Cant access /dev/dsp\n" ); } /*--- Get blocksize, alloc buffer.*/ err = ioctl( au_dev, SNDCTL_DSP_GETBLKSIZE, &dsp_blk_size ); if( 0 > err ) st_err( "\n Cant get /dev/dsp blocksize!\n" ); /* printf( "\n dsp block size=%d.\n", dsp_blk_size ); */ if( (32 > dsp_blk_size) || ((64*1024) < dsp_blk_size) ) { printf( "\n Bad dsp_blk_size=%d.\n", dsp_blk_size ); st_err( "Bad blk size" ); } buf = (char *)malloc( dsp_blk_size ); if( !buf ) st_err( "Cant malloc!" ); /*--- Set bits in sample (16).*/ bytes_per_sample = 2; tmp = 16; err = ioctl( au_dev, SNDCTL_DSP_SAMPLESIZE, &tmp ); if( 0 > err ) { perror( "SNDCTL_DSP_SAMPLESIZE ioctl error" ); st_err( "\n Cant set sample size!\n" ); } /*--- Set stereo mode (mono is 0).*/ tmp = 1; err = ioctl( au_dev, SNDCTL_DSP_STEREO, &tmp ); if( 0 > err ) st_err( "\n Cant set stereo mode!\n" ); /*--- Set sampling rate.*/ /* tmp = 48000; */ tmp = 22050; err = ioctl( au_dev, SNDCTL_DSP_SPEED, &tmp ); if( 0 > err ) st_err( "\n Cant set speed!\n" ); } /*--------------------------------------------*/ void close_au_dev() { err = close( au_dev ); if( err ) { printf( "\n close err=%d \n", err ); } if( buf ) free( buf ); } /*--------------------------------------------*/ int read_au_dev( char **buf_p ) { int len; len = read( au_dev, buf, dsp_blk_size ); if( 0 > len ) { printf( "\n read len=%d.\n", len ); st_err( "read" ); } *buf_p = buf; return( len ); } /*--------------------------------------------*/ int write_au_dev( char *play_buf, int buf_len ) { int len; len = write( au_dev, play_buf, buf_len ); if( 0 > len ) { printf( "\n write len=%d.\n", len ); st_err( "write" ); } return( len ); } /*--------------------------------------------*/ void st_err( char *msg ) { printf( "\n st error: <%s> \n", msg ); exit(1); }