00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 #include <fcntl.h>
00073 #include <stdio.h>
00074 #include <stdlib.h>
00075 #include <string.h>
00076 #include <alsa/asoundlib.h>
00077 #include <errno.h>
00078 #include <config.h>
00079 #include <unistd.h>
00080
00081 #include "prim_type.h"
00082 #include "ad.h"
00083
00084
00085 #define AUDIO_FORMAT SND_PCM_SFMT_S16_LE
00086 #define INPUT_GAIN (85)
00087 #define SPS_EPSILON 200
00088
00089 static int
00090 setparams(int32 sps, snd_pcm_t * handle)
00091 {
00092 snd_pcm_hw_params_t *hwparams;
00093 unsigned int out_sps, buffer_time, period_time;
00094 int err;
00095
00096 snd_pcm_hw_params_alloca(&hwparams);
00097 err = snd_pcm_hw_params_any(handle, hwparams);
00098 if (err < 0) {
00099 fprintf(stderr, "Can not configure this PCM device: %s\n",
00100 snd_strerror(err));
00101 return -1;
00102 }
00103
00104 err =
00105 snd_pcm_hw_params_set_access(handle, hwparams,
00106 SND_PCM_ACCESS_RW_INTERLEAVED);
00107 if (err < 0) {
00108 fprintf(stderr,
00109 "Failed to set PCM device to interleaved: %s\n",
00110 snd_strerror(err));
00111 return -1;
00112 }
00113
00114 err =
00115 snd_pcm_hw_params_set_format(handle, hwparams, SND_PCM_FORMAT_S16);
00116 if (err < 0) {
00117 fprintf(stderr,
00118 "Failed to set PCM device to 16-bit signed PCM: %s\n",
00119 snd_strerror(err));
00120 return -1;
00121 }
00122
00123 err = snd_pcm_hw_params_set_channels(handle, hwparams, 1);
00124 if (err < 0) {
00125 fprintf(stderr, "Failed to set PCM device to mono: %s\n",
00126 snd_strerror(err));
00127 return -1;
00128 }
00129
00130 out_sps = sps;
00131 err =
00132 snd_pcm_hw_params_set_rate_near(handle, hwparams, &out_sps, NULL);
00133 if (err < 0) {
00134 fprintf(stderr, "Failed to set sampling rate: %s\n",
00135 snd_strerror(err));
00136 return -1;
00137 }
00138 if (abs(out_sps - sps) > SPS_EPSILON) {
00139 fprintf(stderr,
00140 "Available samping rate %d is too far from requested %d\n",
00141 out_sps, sps);
00142 return -1;
00143 }
00144
00145
00146 err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
00147 period_time = buffer_time / 4;
00148 err = snd_pcm_hw_params_set_period_time_near(handle, hwparams,
00149 &period_time, 0);
00150 if (err < 0) {
00151 fprintf(stderr, "Failed to set period time to %u: %s\n",
00152 period_time, snd_strerror(err));
00153 return -1;
00154 }
00155 err = snd_pcm_hw_params_set_buffer_time_near(handle, hwparams,
00156 &buffer_time, 0);
00157 if (err < 0) {
00158 fprintf(stderr, "Failed to set buffer time to %u: %s\n",
00159 buffer_time, snd_strerror(err));
00160 return -1;
00161 }
00162
00163 err = snd_pcm_hw_params(handle, hwparams);
00164 if (err < 0) {
00165 fprintf(stderr, "Failed to set hwparams: %s\n", snd_strerror(err));
00166 return -1;
00167 }
00168
00169 err = snd_pcm_nonblock(handle, 1);
00170 if (err < 0) {
00171 fprintf(stderr, "Failed to set non-blocking mode: %s\n",
00172 snd_strerror(err));
00173 return -1;
00174 }
00175 return 0;
00176 }
00177
00178 static int
00179 setlevels(const char *dev)
00180 {
00181 snd_mixer_t *handle;
00182 snd_mixer_selem_id_t *sid;
00183 snd_mixer_elem_t *elem;
00184 int err;
00185 char *mixer_dev, *c;
00186
00187
00188 if ((err = snd_mixer_open(&handle, 0)) < 0) {
00189 fprintf(stderr, "Mixer open failed: %s\n", snd_strerror(err));
00190 return -1;
00191 }
00192
00193 mixer_dev = strdup(dev);
00194 if (strncmp(mixer_dev, "plug", 4) == 0)
00195 memmove(mixer_dev, mixer_dev + 4, strlen(mixer_dev) - 4 + 1);
00196 if ((c = strchr(mixer_dev, ',')))
00197 *c = '\0';
00198 if ((err = snd_mixer_attach(handle, mixer_dev)) < 0) {
00199 fprintf(stderr, "Mixer attach to %s failed: %s\n",
00200 mixer_dev, snd_strerror(err));
00201 free(mixer_dev);
00202 snd_mixer_close(handle);
00203 return -1;
00204 }
00205 free(mixer_dev);
00206 if ((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
00207 fprintf(stderr, "Mixer register failed: %s\n", snd_strerror(err));
00208 snd_mixer_close(handle);
00209 return -1;
00210 }
00211 if ((err = snd_mixer_load(handle)) < 0) {
00212 fprintf(stderr, "Mixer load failed: %s\n", snd_strerror(err));
00213 snd_mixer_close(handle);
00214 return -1;
00215 }
00216 snd_mixer_selem_id_alloca(&sid);
00217 snd_mixer_selem_id_set_name(sid, "Mic");
00218 if ((elem = snd_mixer_find_selem(handle, sid)) == NULL) {
00219 fprintf(stderr, "Warning: Could not find Mic element\n");
00220 }
00221 else {
00222 if ((err = snd_mixer_selem_set_capture_switch_all(elem, 1)) < 0) {
00223 fprintf(stderr,
00224 "Failed to enable microphone capture: %s\n",
00225 snd_strerror(err));
00226 snd_mixer_close(handle);
00227 return -1;
00228 }
00229 }
00230 snd_mixer_selem_id_set_name(sid, "Capture");
00231 if ((elem = snd_mixer_find_selem(handle, sid)) == NULL) {
00232 fprintf(stderr, "Warning: Could not find Capture element\n");
00233 }
00234 else {
00235 if ((err = snd_mixer_selem_set_capture_switch_all(elem, 1)) < 0) {
00236 fprintf(stderr,
00237 "Failed to enable microphone capture: %s\n",
00238 snd_strerror(err));
00239 snd_mixer_close(handle);
00240 return -1;
00241 }
00242 }
00243
00244 return 0;
00245 }
00246
00247 ad_rec_t *
00248 ad_open_dev(const char *dev, int32 sps)
00249 {
00250 ad_rec_t *handle;
00251 snd_pcm_t *dspH;
00252
00253 int err;
00254
00255 if (dev == NULL)
00256 dev = DEFAULT_DEVICE;
00257
00258 err = snd_pcm_open(&dspH, dev, SND_PCM_STREAM_CAPTURE, 0);
00259 if (err < 0) {
00260 fprintf(stderr,
00261 "Error opening audio device %s for capture: %s\n",
00262 dev, snd_strerror(err));
00263 return NULL;
00264 }
00265
00266 if (setparams(sps, dspH) < 0) {
00267 return NULL;
00268 }
00269 if (setlevels(dev) < 0) {
00270 return NULL;
00271 }
00272 if ((handle = (ad_rec_t *) calloc(1, sizeof(ad_rec_t))) == NULL) {
00273 fprintf(stderr, "calloc(%d) failed\n", (int)sizeof(ad_rec_t));
00274 abort();
00275 }
00276
00277 handle->dspH = dspH;
00278 handle->recording = 0;
00279 handle->sps = sps;
00280 handle->bps = sizeof(int16);
00281
00282 return (handle);
00283 }
00284
00285 ad_rec_t *
00286 ad_open_sps(int32 sps)
00287 {
00288 return ad_open_dev(DEFAULT_DEVICE, sps);
00289 }
00290
00291 ad_rec_t *
00292 ad_open(void)
00293 {
00294 return ad_open_sps(DEFAULT_SAMPLES_PER_SEC);
00295 }
00296
00297
00298 int32
00299 ad_close(ad_rec_t * handle)
00300 {
00301 if (handle->dspH == NULL)
00302 return AD_ERR_NOT_OPEN;
00303
00304 if (handle->recording) {
00305 if (ad_stop_rec(handle) < 0)
00306 return AD_ERR_GEN;
00307 }
00308 snd_pcm_close(handle->dspH);
00309 free(handle);
00310
00311 return (0);
00312 }
00313
00314
00315 int32
00316 ad_start_rec(ad_rec_t * handle)
00317 {
00318 int err;
00319
00320 if (handle->dspH == NULL)
00321 return AD_ERR_NOT_OPEN;
00322
00323 if (handle->recording)
00324 return AD_ERR_GEN;
00325
00326 err = snd_pcm_prepare(handle->dspH);
00327 if (err < 0) {
00328 fprintf(stderr, "snd_pcm_prepare failed: %s\n", snd_strerror(err));
00329 return AD_ERR_GEN;
00330 }
00331 err = snd_pcm_start(handle->dspH);
00332 if (err < 0) {
00333 fprintf(stderr, "snd_pcm_start failed: %s\n", snd_strerror(err));
00334 return AD_ERR_GEN;
00335 }
00336 handle->recording = 1;
00337
00338 return (0);
00339 }
00340
00341
00342 int32
00343 ad_stop_rec(ad_rec_t * handle)
00344 {
00345 int err;
00346
00347 if (handle->dspH == NULL)
00348 return AD_ERR_NOT_OPEN;
00349
00350 if (!handle->recording)
00351 return AD_ERR_GEN;
00352
00353 err = snd_pcm_drop(handle->dspH);
00354 if (err < 0) {
00355 fprintf(stderr, "snd_pcm_drop failed: %s\n", snd_strerror(err));
00356 return AD_ERR_GEN;
00357 }
00358 handle->recording = 0;
00359
00360 return (0);
00361 }
00362
00363
00364 int32
00365 ad_read(ad_rec_t * handle, int16 * buf, int32 max)
00366 {
00367 int32 length, err;
00368
00369 if (!handle->recording)
00370 return AD_EOF;
00371
00372 length = snd_pcm_readi(handle->dspH, buf, max);
00373 if (length == -EAGAIN) {
00374 length = 0;
00375 }
00376 else if (length == -EPIPE) {
00377 fprintf(stderr, "Input overrrun (non-fatal)\n");
00378 err = snd_pcm_prepare(handle->dspH);
00379 if (err < 0) {
00380 fprintf(stderr, "Can't recover from underrun: %s\n",
00381 snd_strerror(err));
00382 return AD_ERR_GEN;
00383 }
00384 length = 0;
00385 }
00386 else if (length == -ESTRPIPE) {
00387 fprintf(stderr, "Resuming sound driver (non-fatal)\n");
00388 while ((err = snd_pcm_resume(handle->dspH)) == -EAGAIN)
00389 usleep(10000);
00390 if (err < 0) {
00391 err = snd_pcm_prepare(handle->dspH);
00392 if (err < 0) {
00393 fprintf(stderr, "Can't recover from underrun: %s\n",
00394 snd_strerror(err));
00395 return AD_ERR_GEN;
00396 }
00397 }
00398 length = 0;
00399 }
00400 else if (length < 0) {
00401 fprintf(stderr, "Audio read error: %s\n",
00402 snd_strerror(length));
00403 return AD_ERR_GEN;
00404 }
00405 return length;
00406 }