• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

src/libsphinxad/audio_utils_sunos.c

00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
00002 /* ====================================================================
00003  * Copyright (c) 1999-2001 Carnegie Mellon University.  All rights
00004  * reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer. 
00012  *
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in
00015  *    the documentation and/or other materials provided with the
00016  *    distribution.
00017  *
00018  * This work was supported in part by funding from the Defense Advanced 
00019  * Research Projects Agency and the National Science Foundation of the 
00020  * United States of America, and the CMU Sphinx Speech Consortium.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
00023  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
00024  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00025  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
00026  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * ====================================================================
00035  *
00036  */
00037 /*
00038  * audio_utils.c -- From Bob Brennan for Sun audio utilities.
00039  * 
00040  * HISTORY
00041  * 
00042  * 15-Jul-98    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
00043  *              Removed '#include sun/audioio.h'.
00044  * 
00045  * 02-Aug-95    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
00046  *              Created from Bob Brennan's original.
00047  */
00048 
00049 #include        <stdio.h>
00050 #include        <stdlib.h>
00051 #include        <sys/types.h>
00052 #include        <sys/ioctl.h>
00053 #include        <fcntl.h>
00054 #include        <stropts.h>
00055 #include        <errno.h>
00056 #include        <unistd.h>
00057 #include        "audio_utils_sunos.h"
00058 
00059 #define         AUDIO_DEF_GAIN  50
00060 
00061 #define QUIT(x)         {fprintf x; exit(-1);}
00062 
00063 static int audioFd = -1;
00064 static int sampleRate;
00065 static int samplePrecision;
00066 static int sampleEncoding;
00067 
00068 /*
00069  * Open audio, put it in paused state, and set IO to non-blocking mode.
00070  */
00071 int
00072 audioOpen(const char *dev, int rate)
00073 {
00074     int fd;
00075     int non_blocking;
00076 
00077     if (rate == 16000) {
00078         sampleRate = 16000;
00079         samplePrecision = 16;
00080         sampleEncoding = AUDIO_ENCODING_LINEAR;
00081     }
00082     else if (rate == 8000) {
00083         sampleRate = 8000;
00084         samplePrecision = 8;
00085         sampleEncoding = AUDIO_ENCODING_ULAW;
00086     }
00087     else
00088         QUIT((stderr, "audioOpen: unsupported rate %d\n", rate));
00089 
00090     /*
00091      * Open the device for read-only access and do not block (wait) if
00092      * the device is currently busy.
00093      */
00094     fd = open(dev, O_RDONLY | O_NDELAY);
00095     if (fd < 0) {
00096         if (errno == EBUSY)
00097             fprintf(stderr, "audioOpen: audio device is busy\n");
00098         else
00099             perror("audioOpen error");
00100         exit(1);
00101     }
00102     audioFd = fd;
00103 
00104     audioPause();
00105     audioFlush();
00106     non_blocking = 1;
00107     ioctl(audioFd, FIONBIO, &non_blocking);
00108 
00109     return fd;
00110 }
00111 
00112 void
00113 audioPause(void)
00114 {
00115     audio_info_t info;
00116     int err;
00117 
00118     /*
00119      * Make sure that the audio device is open
00120      */
00121     if (audioFd < 0)
00122         QUIT((stderr, "audioPauseAndFlush: audio device not open\n"));
00123 
00124     /*
00125      * Pause and flush the input stream
00126      */
00127     AUDIO_INITINFO(&info);
00128     info.record.pause = 1;
00129     err = ioctl(audioFd, AUDIO_SETINFO, &info);
00130     if (err)
00131         QUIT((stderr, "pause ioctl err %d\n", err));
00132 }
00133 
00134 void
00135 audioFlush(void)
00136 {
00137     audio_info_t info;
00138     int err;
00139 
00140     /*
00141      * Make sure that the audio device is open
00142      */
00143     if (audioFd < 0)
00144         QUIT((stderr, "audioPauseAndFlush: audio device not open\n"));
00145 
00146     /*
00147      * Flush the current input queue
00148      */
00149     err = ioctl(audioFd, I_FLUSH, FLUSHR);
00150     if (err)
00151         QUIT((stderr, "flush ioctl err %d\n", err));
00152 }
00153 
00154 void
00155 audioStartRecord(void)
00156 {
00157     audio_info_t info;
00158     int err;
00159 
00160     /*
00161      * Make sure that the audio device is open
00162      */
00163     if (audioFd < 0)
00164         QUIT((stderr, "audioStartRecord: audio device not open\n"));
00165 
00166     /*
00167      * Setup the input stream the way we want it and start recording.
00168      */
00169     AUDIO_INITINFO(&info);
00170     info.record.sample_rate = sampleRate;
00171     info.record.channels = 1;
00172     info.record.precision = samplePrecision;
00173     info.record.encoding = sampleEncoding;
00174 #if 0
00175     info.record.gain = AUDIO_DEF_GAIN;
00176     info.record.port = AUDIO_MICROPHONE;
00177 #else
00178     info.record.gain = 85;
00179     info.record.port = AUDIO_LINE_IN;
00180 #endif
00181     info.record.samples = 0;
00182     info.record.pause = 0;
00183     info.record.error = 0;
00184 
00185     err = ioctl(audioFd, AUDIO_SETINFO, &info);
00186     if (err)
00187         QUIT((stderr, "startRecord ioctl err %d\n", err));
00188 }
00189 
00190 void
00191 audioStopRecord(void)
00192 {
00193     audioPause();
00194 }
00195 
00196 void
00197 audioClose(void)
00198 {
00199     /*
00200      * Make sure that the audio device is open
00201      */
00202     if (audioFd < 0)
00203         QUIT((stderr, "audioRecord: audio device not open\n"));
00204     close(audioFd);
00205     audioFd = -1;
00206 }
00207 
00208 int
00209 audioSetRecordGain(int gain)
00210 {
00211     audio_info_t info;
00212     int err;
00213     int currentGain;
00214 
00215     /*
00216      * Make sure that the audio device is open
00217      */
00218     if (audioFd < 0)
00219         QUIT((stderr, "audioSetRecordGain: audio device not open\n"));
00220 
00221     /*
00222      * Fetch the current gain
00223      */
00224     err = ioctl(audioFd, AUDIO_GETINFO, &info);
00225     if (err)
00226         QUIT((stderr, "audioSetRecordGain ioctl err %d\n", err));
00227     currentGain = info.record.gain;
00228 
00229     /*
00230      * Make sure that desired gain is within an acceptable range
00231      */
00232     if (gain < AUDIO_MIN_GAIN || gain > AUDIO_MAX_GAIN) {
00233         fprintf(stderr,
00234                 "audioSetRecordGain: gain %d out of range %d-%d\n",
00235                 gain, AUDIO_MIN_GAIN, AUDIO_MAX_GAIN);
00236         return currentGain;
00237     }
00238 
00239     /*
00240      * Setup the input stream the way we want it and start recording.
00241      */
00242     AUDIO_INITINFO(&info);
00243     info.record.gain = gain;
00244     err = ioctl(audioFd, AUDIO_SETINFO, &info);
00245     if (err)
00246         QUIT((stderr, "startRecord ioctl err %d\n", err));
00247     return currentGain;
00248 }
00249 
00250 #ifdef MAIN
00251 void
00252 main(int argc, char *argv[])
00253 {
00254     unsigned short buf[16000];
00255     int audio_fd, i, j, k;
00256 
00257     audio_fd = audioOpen(16000);
00258     audioStartRecord();
00259 
00260     for (i = 0; i < 5; i++) {
00261         /* simulate compute for a while */
00262         for (j = 0; j < 200000; j++);
00263 
00264         /* read whatever accumulated data is available, upto 16k max */
00265         k = read(audio_fd, buf, 16000 * sizeof(short));
00266 
00267         /* print some of the new data */
00268         printf(" %5d", k);
00269         for (j = 0; j < 20; j++)
00270             printf(" %04x", buf[j]);
00271         printf("\n");
00272         fflush(stdout);
00273     }
00274 
00275     /* simulate compute for a while and stop recording */
00276     for (j = 0; j < 1000000; j++);
00277     audioStopRecord();
00278 
00279     /* read whatever accumulated data is available, upto 16k max */
00280     k = read(audio_fd, buf, 16000 * sizeof(short));
00281     printf(" %5d\n", k);
00282 
00283     audioClose();
00284 }
00285 #endif

Generated on Fri Jan 14 2011 for SphinxBase by  doxygen 1.7.1