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 * ad.c -- Wraps a "sphinx-II standard" audio interface around the basic audio 00039 * utilities. 00040 * 00041 * ********************************************** 00042 * CMU ARPA Speech Project 00043 * 00044 * Copyright (c) 1996 Carnegie Mellon University. 00045 * ALL RIGHTS RESERVED. 00046 * ********************************************** 00047 * 00048 * HISTORY 00049 * 00050 * 11-Jun-96 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University. 00051 * Modified to new A/D API. 00052 * 00053 * 22-Apr-94 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University. 00054 * Adapted to latest ad.h interface. 00055 */ 00056 00057 #include <stdio.h> 00058 #include <stdlib.h> 00059 #include <string.h> 00060 #include <unistd.h> 00061 #include <assert.h> 00062 #include <config.h> 00063 00064 #include "audio_utils_sunos.h" 00065 #include "prim_type.h" 00066 #include "ad.h" 00067 00068 #define QUIT(x) {fprintf x; exit(-1);} 00069 00070 ad_rec_t * 00071 ad_open_dev(const char *dev, int32 sps) 00072 { 00073 ad_rec_t *r; 00074 00075 if ((r = (ad_rec_t *) calloc(1, sizeof(ad_rec_t))) == NULL) 00076 return NULL; 00077 00078 if (dev == NULL) 00079 dev = DEFAULT_DEVICE; 00080 r->audio_fd = audioOpen(dev, (int) sps); 00081 r->recording = 0; 00082 00083 return r; 00084 } 00085 00086 ad_rec_t * 00087 ad_open_sps(int32 sps) 00088 { 00089 return ad_open_dev(DEFAULT_DEVICE, sps); 00090 } 00091 00092 ad_rec_t * 00093 ad_open(void) 00094 { 00095 return ad_open_dev(DEFAULT_DEVICE, DEFAULT_SAMPLES_PER_SEC); 00096 } 00097 00098 int32 00099 ad_start_rec(ad_rec_t * r) 00100 { 00101 if (r->recording) 00102 return -1; 00103 00104 audioStartRecord(); 00105 r->recording = 1; 00106 00107 return 0; 00108 } 00109 00110 00111 int32 00112 ad_stop_rec(ad_rec_t * r) 00113 { 00114 if (!r->recording) 00115 return -1; 00116 00117 audioStopRecord(); 00118 r->recording = 0; 00119 00120 return 0; 00121 } 00122 00123 00124 int32 00125 ad_read(ad_rec_t * r, int16 * buf, int32 max) 00126 { 00127 int32 len; 00128 00129 /* Get whatever samples are available, upto max requested size */ 00130 len = max * AD_SAMPLE_SIZE; 00131 len = read(r->audio_fd, buf, len); 00132 00133 if (len > 0) { 00134 /* HACK!! Assume read returns complete samples, but check the assumption */ 00135 if (len & 0x1) 00136 QUIT((stderr, 00137 "%s(%d): **ERROR**, ad_read() returned odd #bytes\n", 00138 __FILE__, __LINE__)); 00139 00140 return (len >> 1); 00141 } 00142 00143 if ((len == 0) && (!r->recording)) 00144 return -1; /* EOF */ 00145 00146 return 0; 00147 } 00148 00149 00150 int32 00151 ad_close(ad_rec_t * r) 00152 { 00153 if (r->recording) 00154 ad_stop_rec(r); 00155 00156 audioClose(); 00157 free(r); 00158 00159 return 0; 00160 }