Julius 4.2
libsent/src/adin/adin_mic_sun4.c
説明を見る。
00001 
00040 /*
00041  * Copyright (c) 1991-2011 Kawahara Lab., Kyoto University
00042  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00043  * Copyright (c) 2005-2011 Julius project team, Nagoya Institute of Technology
00044  * All rights reserved
00045  */
00046 
00047 #define J_DEF_VOLUME 20         ///< Recording volume (range=0-99)
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"
00061 
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 srate;               
00068 static int afd;                 
00069 static struct pollfd pfd;       
00070 static audio_info_t ainfo;      
00071 static char *defaultdev = DEFAULT_DEVICE;
00072 static char devname[MAXPATHLEN];
00073 
00082 boolean
00083 adin_mic_standby(int sfreq, void *dummy)
00084 {
00085   /* store required sampling rate for checking after opening device */
00086   srate = sfreq;
00087   return TRUE;
00088 }
00089 
00097 static boolean
00098 adin_mic_open(char *devstr)
00099 {
00100   Audio_hdr Dev_hdr, old_hdr;
00101   double vol;
00102 
00103   /* open the device */
00104   if ((afd = open(devstr, O_RDONLY)) == -1) {
00105     if (errno == EBUSY) {
00106       jlog("Error: adin_sun4: audio device %s is busy\n", devstr);
00107       return(FALSE);
00108     } else {
00109       jlog("Error: adin_sun4: unable to open %s\n",devstr);
00110       return(FALSE);
00111     }
00112   }
00113 
00114   /* set recording port to microphone */
00115   AUDIO_INITINFO(&ainfo);
00116   ainfo.record.port = AUDIO_MICROPHONE;
00117   if (ioctl(afd, AUDIO_SETINFO, &ainfo) == -1) {
00118     jlog("Error: adin_sun4: failed to set recording port\n");
00119     return(FALSE);
00120   }
00121 
00122   /* set recording parameters */
00123   if (audio_get_record_config(afd, &Dev_hdr) != AUDIO_SUCCESS) {
00124     jlog("Error: adin_sun4: failed to get recording config\n"); return(FALSE);
00125   }
00126   Dev_hdr.sample_rate = srate;
00127   Dev_hdr.samples_per_unit = 1; /* ? I don't know this param. ? */
00128   Dev_hdr.bytes_per_unit = 2;
00129   Dev_hdr.channels = 1;
00130   Dev_hdr.encoding = AUDIO_ENCODING_LINEAR;
00131   if (audio_set_record_config(afd, &Dev_hdr) != AUDIO_SUCCESS) {
00132     jlog("Error: adin_sun4: failed to set recording config\n"); return(FALSE);
00133   }
00134 
00135   /* set volume */
00136   vol = (float)volume / (float)100;
00137   if (audio_set_record_gain(afd, &vol) != AUDIO_SUCCESS) {
00138     jlog("Error: adin_sun4: failed to set recording volume\n");
00139     return(FALSE);
00140   }
00141 
00142   /* flush buffer */
00143   if((ioctl(afd , I_FLUSH , FLUSHRW)) == -1) {
00144     jlog("Error: adin_sun4: cannot flush input buffer\n");
00145     return(FALSE);
00146   }
00147   
00148   /* setup polling */
00149   pfd.fd = afd;
00150   pfd.events = POLLIN;
00151 
00152 #if 0
00153   /* pause transfer */
00154   if (audio_pause_record(afd) == AUDIO_ERR_NOEFFECT) {
00155     jlog("Error: adin_sun4: cannot pause audio\n");
00156     return(FALSE);
00157   }
00158 #endif
00159 
00160   return(TRUE);
00161 }
00162 
00170 boolean
00171 adin_mic_begin(char *pathname)
00172 {
00173   char *p;
00174 
00175   /* set device name */
00176   if (pathname != NULL) {
00177     strncpy(devname, pathname, MAXPATHLEN);
00178     jlog("Stat: adin_sun4: device name = %s (from argument)\n", devname);
00179   } else if ((p = getenv("AUDIODEV")) != NULL) {
00180     strncpy(devname, p, MAXPATHLEN);
00181     jlog("Stat: adin_sun4: device name = %s (from AUDIODEV)\n", devname);
00182   } else {    
00183     strncpy(devname, defaultdev, MAXPATHLEN);
00184     jlog("Stat: adin_sun4: device name = %s (application default)\n", devname);
00185   }
00186 
00187   /* open the device */
00188   if (adin_mic_open(devname) == FALSE) return FALSE;
00189 
00190 #if 0
00191   /* resume input */
00192   if (audio_resume_record(afd) == AUDIO_ERR_NOEFFECT) {
00193     jlog("Error: adin_sun4: cannot resume audio\n");
00194     return(FALSE);
00195   }
00196 #endif
00197 
00198   return(TRUE);
00199 }
00200 
00206 boolean
00207 adin_mic_end()
00208 {
00209 #if 1
00210   close(afd);
00211 #else
00212   /* pause input */
00213   if (audio_pause_record(afd) == AUDIO_ERR_NOEFFECT) {
00214     jlog("Error: adin_sun4: cannot pause audio\n");
00215     return(FALSE);
00216   }
00217 #endif
00218   return(TRUE);
00219 }
00220 
00233 int
00234 adin_mic_read(SP16 *buf, int sampnum)
00235 {
00236   int bytes;
00237   int len;
00238 
00239   /* SunOS4.x needs special dealing when no samples are found */
00240   len = sampnum * sizeof(SP16);
00241   bytes = 0;
00242   while(bytes < len) {
00243     bytes = read(afd, buf, len);
00244     if (bytes < 0) {
00245       if (errno != EAGAIN) {    /* error */
00246         jlog("Erorr: adin_sun4: failed to read sample\n");
00247         return(-2);
00248       } else {                  /* retry */
00249         poll(&pfd, 1L, -1);
00250       }
00251     }
00252   }
00253   if (bytes < 0) {
00254     jlog("Error: adin_sun4: failed to read sample\n");
00255     return(-2);
00256   }
00257   return(bytes / sizeof(SP16)); /* success */
00258 }
00259 
00267 char *
00268 adin_mic_input_name()
00269 {
00270   return(devname);
00271 }
00272 
00273 /* end of file */