libsent/src/adin/adin_mic_sun4.c

Go to the documentation of this file.
00001 
00040 /*
00041  * Copyright (c) 1991-2007 Kawahara Lab., Kyoto University
00042  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00043  * Copyright (c) 2005-2007 Julius project team, Nagoya Institute of Technology
00044  * All rights reserved
00045  */
00046 
00047 #define J_DEF_VOLUME 20         
00048 
00049 #include <sent/stddefs.h>
00050 #include <sent/adin.h>
00051 
00052 #include <sys/types.h>
00053 #include <sys/stat.h>
00054 #include <fcntl.h>
00055 #include <errno.h>
00056 #include <stropts.h>
00057 #include <poll.h>
00058 
00060 #define DEFAULT_DEVICE "/dev/audio"
00062 static int volume = J_DEF_VOLUME;
00063 
00064 /* sound header */
00065 #include <multimedia/libaudio.h>/* see man audio_device(3) */
00066 #include <multimedia/audio_device.h>
00067 static int afd;                 
00068 static struct pollfd pfd;       
00069 static audio_info_t ainfo;      
00070 
00079 boolean
00080 adin_mic_standby(int sfreq, void *dummy)
00081 {
00082   char *defaultdev = DEFAULT_DEVICE;
00083   char *devname;
00084   Audio_hdr Dev_hdr, old_hdr;
00085   double vol;
00086 
00087   /* get device name if specified in $AUDIODEV */
00088   if ((devname = getenv("AUDIODEV")) == NULL) {
00089     devname = defaultdev;
00090     jlog("Stat: adin_sun4: device name = %s\n", devname);
00091   } else {
00092     jlog("Stat: adin_sun4: device name obtained from AUDIODEV: %s\n", devname);
00093   }
00094 
00095   /* open the device */
00096   if ((afd = open(devname, O_RDONLY)) == -1) {
00097     if (errno == EBUSY) {
00098       jlog("Error: adin_sun4: audio device %s is busy\n", devname);
00099       return(FALSE);
00100     } else {
00101       jlog("Error: adin_sun4: unable to open %s\n",devname);
00102       return(FALSE);
00103     }
00104   }
00105 
00106   /* set recording port to microphone */
00107   AUDIO_INITINFO(&ainfo);
00108   ainfo.record.port = AUDIO_MICROPHONE;
00109   if (ioctl(afd, AUDIO_SETINFO, &ainfo) == -1) {
00110     jlog("Error: adin_sun4: failed to set recording port\n");
00111     return(FALSE);
00112   }
00113 
00114   /* set recording parameters */
00115   if (audio_get_record_config(afd, &Dev_hdr) != AUDIO_SUCCESS) {
00116     jlog("Error: adin_sun4: failed to get recording config\n"); return(FALSE);
00117   }
00118   Dev_hdr.sample_rate = sfreq;
00119   Dev_hdr.samples_per_unit = 1; /* ? I don't know this param. ? */
00120   Dev_hdr.bytes_per_unit = 2;
00121   Dev_hdr.channels = 1;
00122   Dev_hdr.encoding = AUDIO_ENCODING_LINEAR;
00123   if (audio_set_record_config(afd, &Dev_hdr) != AUDIO_SUCCESS) {
00124     jlog("Error: adin_sun4: failed to set recording config\n"); return(FALSE);
00125   }
00126 
00127   /* set volume */
00128   vol = (float)volume / (float)100;
00129   if (audio_set_record_gain(afd, &vol) != AUDIO_SUCCESS) {
00130     jlog("Error: adin_sun4: failed to set recording volume\n");
00131     return(FALSE);
00132   }
00133 
00134   /* flush buffer */
00135   if((ioctl(afd , I_FLUSH , FLUSHRW)) == -1) {
00136     jlog("Error: adin_sun4: cannot flush input buffer\n");
00137     return(FALSE);
00138   }
00139   
00140   /* setup polling */
00141   pfd.fd = afd;
00142   pfd.events = POLLIN;
00143 
00144   /* pause transfer */
00145   if (audio_pause_record(afd) == AUDIO_ERR_NOEFFECT) {
00146     jlog("Error: adin_sun4: cannot pause audio\n");
00147     return(FALSE);
00148   }
00149 
00150   return(TRUE);
00151 }
00152 
00158 boolean
00159 adin_mic_begin()
00160 {
00161   /* resume input */
00162   if (audio_resume_record(afd) == AUDIO_ERR_NOEFFECT) {
00163     jlog("Error: adin_sun4: cannot resume audio\n");
00164     return(FALSE);
00165   }
00166   return(TRUE);
00167 }
00168 
00174 boolean
00175 adin_mic_end()
00176 {
00177   /* pause input */
00178   if (audio_pause_record(afd) == AUDIO_ERR_NOEFFECT) {
00179     jlog("Error: adin_sun4: cannot pause audio\n");
00180     return(FALSE);
00181   }
00182   return(TRUE);
00183 }
00184 
00197 int
00198 adin_mic_read(SP16 *buf, int sampnum)
00199 {
00200   int bytes;
00201   int len;
00202 
00203   /* SunOS4.x needs special dealing when no samples are found */
00204   len = sampnum * sizeof(SP16);
00205   bytes = 0;
00206   while(bytes < len) {
00207     bytes = read(afd, buf, len);
00208     if (bytes < 0) {
00209       if (errno != EAGAIN) {    /* error */
00210         jlog("Erorr: adin_sun4: failed to read sample\n");
00211         return(-2);
00212       } else {                  /* retry */
00213         poll(&pfd, 1L, -1);
00214       }
00215     }
00216   }
00217   if (bytes < 0) {
00218     jlog("Error: adin_sun4: failed to read sample\n");
00219     return(-2);
00220   }
00221   return(bytes / sizeof(SP16)); /* success */
00222 }

Generated on Tue Dec 18 15:59:54 2007 for Julius by  doxygen 1.5.4