00001
00040
00041
00042
00043
00044
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
00065 #include <multimedia/libaudio.h>
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
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
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
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
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;
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
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
00135 if((ioctl(afd , I_FLUSH , FLUSHRW)) == -1) {
00136 jlog("Error: adin_sun4: cannot flush input buffer\n");
00137 return(FALSE);
00138 }
00139
00140
00141 pfd.fd = afd;
00142 pfd.events = POLLIN;
00143
00144
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
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
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
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) {
00210 jlog("Erorr: adin_sun4: failed to read sample\n");
00211 return(-2);
00212 } else {
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));
00222 }