| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | #ifdef FreeBSD |
|---|
| 42 | #include "config_unix.h" |
|---|
| 43 | #include "config_win32.h" |
|---|
| 44 | #include "assert.h" |
|---|
| 45 | #include "audio.h" |
|---|
| 46 | #include "util.h" |
|---|
| 47 | |
|---|
| 48 | static int can_read = FALSE; |
|---|
| 49 | static int can_write = FALSE; |
|---|
| 50 | static int iport = AUDIO_MICROPHONE; |
|---|
| 51 | static audio_format format; |
|---|
| 52 | static snd_chan_param pa; |
|---|
| 53 | #define BLOCKSIZE 320 |
|---|
| 54 | |
|---|
| 55 | #define bat_to_device(x) ((x) * 100 / MAX_AMP) |
|---|
| 56 | #define device_to_bat(x) ((x) * MAX_AMP / 100) |
|---|
| 57 | |
|---|
| 58 | static int |
|---|
| 59 | audio_open_rw(char rw) |
|---|
| 60 | { |
|---|
| 61 | int volume = 100; |
|---|
| 62 | int reclb = 0; |
|---|
| 63 | int audio_fd = -1; |
|---|
| 64 | |
|---|
| 65 | int d = -1; |
|---|
| 66 | char buf[64]; |
|---|
| 67 | char *thedev; |
|---|
| 68 | |
|---|
| 69 | switch (rw) { |
|---|
| 70 | case O_RDONLY: |
|---|
| 71 | can_read = TRUE; |
|---|
| 72 | can_write = FALSE; |
|---|
| 73 | break; |
|---|
| 74 | case O_WRONLY: |
|---|
| 75 | can_read = FALSE; |
|---|
| 76 | can_write = TRUE; |
|---|
| 77 | break; |
|---|
| 78 | case O_RDWR: |
|---|
| 79 | can_read = TRUE; |
|---|
| 80 | can_write = TRUE; |
|---|
| 81 | break; |
|---|
| 82 | default: |
|---|
| 83 | abort(); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | thedev = getenv("AUDIODEV"); |
|---|
| 87 | if (thedev == NULL) |
|---|
| 88 | thedev = "/dev/audio"; |
|---|
| 89 | else if (thedev[0] >= '0') { |
|---|
| 90 | d = atoi(thedev); |
|---|
| 91 | sprintf(buf, "/dev/audio%d", d); |
|---|
| 92 | thedev = buf; |
|---|
| 93 | } |
|---|
| 94 | audio_fd = open(thedev, rw); |
|---|
| 95 | if (audio_fd >= 0) { |
|---|
| 96 | struct snd_size sz; |
|---|
| 97 | snd_capabilities soundcaps; |
|---|
| 98 | ioctl(audio_fd, AIOGCAP, &soundcaps); |
|---|
| 99 | ioctl(audio_fd,SNDCTL_DSP_RESET,0); |
|---|
| 100 | pa.play_rate = pa.rec_rate = format.sample_rate; |
|---|
| 101 | pa.play_format = pa.rec_format = AFMT_S16_LE; |
|---|
| 102 | sz.play_size = sz.rec_size = format.blocksize; |
|---|
| 103 | |
|---|
| 104 | switch (soundcaps.formats & (AFMT_FULLDUPLEX | AFMT_WEIRD)) { |
|---|
| 105 | case AFMT_FULLDUPLEX: |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | break; |
|---|
| 110 | case AFMT_FULLDUPLEX | AFMT_WEIRD: |
|---|
| 111 | |
|---|
| 112 | pa.play_format = AFMT_S8; |
|---|
| 113 | sz.play_size = format.blocksize / 2; |
|---|
| 114 | break; |
|---|
| 115 | default: |
|---|
| 116 | if (rw == O_RDWR) { |
|---|
| 117 | fprintf(stderr, "sorry no full duplex support here\n"); |
|---|
| 118 | close(audio_fd); |
|---|
| 119 | return -1; |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | if (format.num_channels == 2) { |
|---|
| 124 | if (soundcaps.formats & AFMT_STEREO) { |
|---|
| 125 | pa.play_format |= AFMT_STEREO; |
|---|
| 126 | } else { |
|---|
| 127 | fprintf(stderr,"no stereo support for this soundcard\n"); |
|---|
| 128 | close(audio_fd); |
|---|
| 129 | return -1; |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | ioctl(audio_fd, AIOSFMT, &pa); |
|---|
| 134 | ioctl(audio_fd, AIOSSIZE, &sz); |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | ioctl(audio_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume); |
|---|
| 139 | ioctl(audio_fd, MIXER_WRITE(SOUND_MIXER_IGAIN), &volume); |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | audio_set_gain(audio_fd, MAX_AMP / 2); |
|---|
| 144 | audio_set_volume(audio_fd, MAX_AMP / 2); |
|---|
| 145 | |
|---|
| 146 | audio_set_iport(audio_fd, iport); |
|---|
| 147 | |
|---|
| 148 | ioctl(audio_fd, MIXER_WRITE(SOUND_MIXER_IMIX), &reclb); |
|---|
| 149 | { |
|---|
| 150 | char buf[64]; |
|---|
| 151 | read(audio_fd, buf, 64); |
|---|
| 152 | } |
|---|
| 153 | return audio_fd; |
|---|
| 154 | } else { |
|---|
| 155 | perror("audio_open"); |
|---|
| 156 | close(audio_fd); |
|---|
| 157 | return -1; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | int |
|---|
| 163 | audio_open(audio_format fmt) |
|---|
| 164 | { |
|---|
| 165 | format = fmt; |
|---|
| 166 | if (audio_duplex(-1)) { |
|---|
| 167 | return audio_open_rw(O_RDWR); |
|---|
| 168 | } else { |
|---|
| 169 | return audio_open_rw(O_WRONLY); |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | void |
|---|
| 175 | audio_close(int audio_fd) |
|---|
| 176 | { |
|---|
| 177 | if (audio_fd < 0) return; |
|---|
| 178 | audio_drain(audio_fd); |
|---|
| 179 | close(audio_fd); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | void |
|---|
| 184 | audio_drain(int audio_fd) |
|---|
| 185 | { |
|---|
| 186 | UNUSED(audio_fd); |
|---|
| 187 | debug_msg("WARNING: audio_drain not yet implemented!\n"); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | int |
|---|
| 191 | audio_duplex(int audio_fd) |
|---|
| 192 | { |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | snd_capabilities soundcaps; |
|---|
| 199 | int fd = audio_fd; |
|---|
| 200 | |
|---|
| 201 | if (audio_fd == -1) |
|---|
| 202 | fd = audio_open_rw(O_RDWR); |
|---|
| 203 | ioctl(fd, AIOGCAP, &soundcaps); |
|---|
| 204 | if (audio_fd == -1) |
|---|
| 205 | close(fd); |
|---|
| 206 | return (soundcaps.formats & AFMT_FULLDUPLEX) ? 1 : 0; |
|---|
| 207 | |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | int |
|---|
| 211 | audio_read(int audio_fd, sample *buf, int samples) |
|---|
| 212 | { |
|---|
| 213 | if (can_read) { |
|---|
| 214 | int l1, len0; |
|---|
| 215 | unsigned int len; |
|---|
| 216 | char *base = (char *) buf; |
|---|
| 217 | |
|---|
| 218 | ioctl(audio_fd, FIONREAD, &len); |
|---|
| 219 | if (len > (samples * BYTES_PER_SAMPLE)) |
|---|
| 220 | len = (samples * BYTES_PER_SAMPLE); |
|---|
| 221 | |
|---|
| 222 | for (len0 = len; len; len -= l1, base += l1) { |
|---|
| 223 | if ((l1 = read(audio_fd, base, len)) < 0) { |
|---|
| 224 | return 0; |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | return len0 / BYTES_PER_SAMPLE; |
|---|
| 228 | } else { |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | int i; |
|---|
| 232 | int diff; |
|---|
| 233 | static struct timeval last_time; |
|---|
| 234 | static struct timeval curr_time; |
|---|
| 235 | static int first_time = 0; |
|---|
| 236 | |
|---|
| 237 | if (first_time == 0) { |
|---|
| 238 | gettimeofday(&last_time, NULL); |
|---|
| 239 | first_time = 1; |
|---|
| 240 | } |
|---|
| 241 | gettimeofday(&curr_time, NULL); |
|---|
| 242 | diff = (((curr_time.tv_sec - last_time.tv_sec) * 1e6) + (curr_time.tv_usec - last_time.tv_usec)) / 125; |
|---|
| 243 | if (diff > samples) |
|---|
| 244 | diff = samples; |
|---|
| 245 | if (diff < 80) |
|---|
| 246 | diff = 80; |
|---|
| 247 | xmemchk(); |
|---|
| 248 | for (i = 0; i < diff; i++) { |
|---|
| 249 | buf[i] = L16_AUDIO_ZERO; |
|---|
| 250 | } |
|---|
| 251 | xmemchk(); |
|---|
| 252 | last_time = curr_time; |
|---|
| 253 | return diff; |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | int |
|---|
| 258 | audio_write(int audio_fd, sample *buf, int samples) |
|---|
| 259 | { |
|---|
| 260 | int done, len, slen; |
|---|
| 261 | char *p; |
|---|
| 262 | |
|---|
| 263 | if (can_write) { |
|---|
| 264 | p = (char *) buf; |
|---|
| 265 | slen = BYTES_PER_SAMPLE; |
|---|
| 266 | len = samples * BYTES_PER_SAMPLE; |
|---|
| 267 | if (pa.play_format != AFMT_S16_LE) { |
|---|
| 268 | |
|---|
| 269 | int i; |
|---|
| 270 | short *src = (short *) buf; |
|---|
| 271 | char *dst = (char *) buf; |
|---|
| 272 | for (i = 0; i < samples; i++) |
|---|
| 273 | dst[i] = src[i] >> 8; |
|---|
| 274 | len = samples; |
|---|
| 275 | slen = 1; |
|---|
| 276 | } |
|---|
| 277 | while (1) { |
|---|
| 278 | if ((done = write(audio_fd, p, len)) == len) { |
|---|
| 279 | break; |
|---|
| 280 | } |
|---|
| 281 | if (errno != EINTR) { |
|---|
| 282 | perror("Error writing device"); |
|---|
| 283 | return samples - ((len - done) / slen); |
|---|
| 284 | } |
|---|
| 285 | len -= done; |
|---|
| 286 | p += done; |
|---|
| 287 | } |
|---|
| 288 | return samples; |
|---|
| 289 | } else { |
|---|
| 290 | return samples; |
|---|
| 291 | } |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | void |
|---|
| 296 | audio_non_block(int audio_fd) |
|---|
| 297 | { |
|---|
| 298 | int frag = 1; |
|---|
| 299 | #ifdef DEBUG |
|---|
| 300 | fprintf(stderr, "called audio_non_block\n"); |
|---|
| 301 | #endif |
|---|
| 302 | if (ioctl(audio_fd, SNDCTL_DSP_NONBLOCK, &frag) == -1) { |
|---|
| 303 | perror("Setting non blocking i/o"); |
|---|
| 304 | } |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | void |
|---|
| 309 | audio_block(int audio_fd) |
|---|
| 310 | { |
|---|
| 311 | int frag = 0; |
|---|
| 312 | #ifdef DEBUG |
|---|
| 313 | fprintf(stderr, "called audio_block\n"); |
|---|
| 314 | #endif |
|---|
| 315 | if (ioctl(audio_fd, SNDCTL_DSP_NONBLOCK, &frag) == -1) { |
|---|
| 316 | perror("Setting blocking i/o"); |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | void |
|---|
| 324 | audio_set_volume(int audio_fd, int vol) |
|---|
| 325 | { |
|---|
| 326 | int volume; |
|---|
| 327 | |
|---|
| 328 | if (audio_fd < 0) |
|---|
| 329 | return; |
|---|
| 330 | volume = vol << 8 | vol; |
|---|
| 331 | if (ioctl(audio_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1) { |
|---|
| 332 | perror("Setting volume"); |
|---|
| 333 | } |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | int |
|---|
| 337 | audio_get_volume(int audio_fd) |
|---|
| 338 | { |
|---|
| 339 | int volume; |
|---|
| 340 | |
|---|
| 341 | if (audio_fd < 0) |
|---|
| 342 | return (0); |
|---|
| 343 | if (ioctl(audio_fd, MIXER_READ(SOUND_MIXER_PCM), &volume) == -1) { |
|---|
| 344 | perror("Getting volume"); |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | return device_to_bat(volume & 0xff); |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | void |
|---|
| 351 | audio_set_oport(int audio_fd, int port) |
|---|
| 352 | { |
|---|
| 353 | |
|---|
| 354 | UNUSED(audio_fd); |
|---|
| 355 | UNUSED(port); |
|---|
| 356 | return; |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | int |
|---|
| 360 | audio_get_oport(int audio_fd) |
|---|
| 361 | { |
|---|
| 362 | |
|---|
| 363 | UNUSED(audio_fd); |
|---|
| 364 | return AUDIO_HEADPHONE; |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | int |
|---|
| 368 | audio_next_oport(int audio_fd) |
|---|
| 369 | { |
|---|
| 370 | |
|---|
| 371 | UNUSED(audio_fd); |
|---|
| 372 | return AUDIO_HEADPHONE; |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | void |
|---|
| 376 | audio_set_gain(int audio_fd, int gain) |
|---|
| 377 | { |
|---|
| 378 | int volume = bat_to_device(gain) << 8 | bat_to_device(gain); |
|---|
| 379 | |
|---|
| 380 | if (audio_fd < 0) { |
|---|
| 381 | return; |
|---|
| 382 | } |
|---|
| 383 | switch (iport) { |
|---|
| 384 | case AUDIO_MICROPHONE: |
|---|
| 385 | if (ioctl(audio_fd, MIXER_WRITE(SOUND_MIXER_MIC), &volume) < 0) |
|---|
| 386 | perror("Setting gain"); |
|---|
| 387 | break; |
|---|
| 388 | case AUDIO_LINE_IN: |
|---|
| 389 | if (ioctl(audio_fd, MIXER_WRITE(SOUND_MIXER_LINE), &volume) < 0) |
|---|
| 390 | perror("Setting gain"); |
|---|
| 391 | break; |
|---|
| 392 | case AUDIO_CD: |
|---|
| 393 | if (ioctl(audio_fd, MIXER_WRITE(SOUND_MIXER_CD), &volume) < 0) |
|---|
| 394 | perror("Setting gain"); |
|---|
| 395 | break; |
|---|
| 396 | } |
|---|
| 397 | return; |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | int |
|---|
| 401 | audio_get_gain(int audio_fd) |
|---|
| 402 | { |
|---|
| 403 | int volume; |
|---|
| 404 | |
|---|
| 405 | if (audio_fd < 0) { |
|---|
| 406 | return (0); |
|---|
| 407 | } |
|---|
| 408 | switch (iport) { |
|---|
| 409 | case AUDIO_MICROPHONE: |
|---|
| 410 | if (ioctl(audio_fd, MIXER_READ(SOUND_MIXER_MIC), &volume) < 0) |
|---|
| 411 | perror("Getting gain"); |
|---|
| 412 | break; |
|---|
| 413 | case AUDIO_LINE_IN: |
|---|
| 414 | if (ioctl(audio_fd, MIXER_READ(SOUND_MIXER_LINE), &volume) < 0) |
|---|
| 415 | perror("Getting gain"); |
|---|
| 416 | break; |
|---|
| 417 | case AUDIO_CD: |
|---|
| 418 | if (ioctl(audio_fd, MIXER_READ(SOUND_MIXER_CD), &volume) < 0) |
|---|
| 419 | perror("Getting gain"); |
|---|
| 420 | break; |
|---|
| 421 | default: |
|---|
| 422 | printf("ERROR: Unknown iport in audio_set_gain!\n"); |
|---|
| 423 | abort(); |
|---|
| 424 | } |
|---|
| 425 | return (device_to_bat(volume & 0xff)); |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | void |
|---|
| 429 | audio_set_iport(int audio_fd, int port) |
|---|
| 430 | { |
|---|
| 431 | int recmask, gain, src; |
|---|
| 432 | |
|---|
| 433 | if (ioctl(audio_fd, MIXER_READ(SOUND_MIXER_RECMASK), &recmask) == -1) { |
|---|
| 434 | perror("Unable to read recording mask"); |
|---|
| 435 | return; |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | switch (port) { |
|---|
| 439 | case AUDIO_MICROPHONE: |
|---|
| 440 | src = SOUND_MASK_MIC; |
|---|
| 441 | break; |
|---|
| 442 | case AUDIO_LINE_IN: |
|---|
| 443 | src = SOUND_MASK_LINE; |
|---|
| 444 | break; |
|---|
| 445 | case AUDIO_CD: |
|---|
| 446 | src = SOUND_MASK_CD; |
|---|
| 447 | break; |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | gain = audio_get_gain(audio_fd); |
|---|
| 451 | audio_set_gain(audio_fd, 0); |
|---|
| 452 | if ((ioctl(audio_fd, MIXER_WRITE(SOUND_MIXER_RECSRC), &src) < 0)) { |
|---|
| 453 | perror("Unable to select recording source"); |
|---|
| 454 | return; |
|---|
| 455 | } |
|---|
| 456 | iport = port; |
|---|
| 457 | audio_set_gain(audio_fd, gain); |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | int |
|---|
| 461 | audio_get_iport(int audio_fd) |
|---|
| 462 | { |
|---|
| 463 | UNUSED(audio_fd); |
|---|
| 464 | return iport; |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | int |
|---|
| 468 | audio_next_iport(int audio_fd) |
|---|
| 469 | { |
|---|
| 470 | switch (iport) { |
|---|
| 471 | case AUDIO_MICROPHONE: |
|---|
| 472 | audio_set_iport(audio_fd, AUDIO_LINE_IN); |
|---|
| 473 | break; |
|---|
| 474 | case AUDIO_LINE_IN: |
|---|
| 475 | audio_set_iport(audio_fd, AUDIO_CD); |
|---|
| 476 | break; |
|---|
| 477 | case AUDIO_CD: |
|---|
| 478 | audio_set_iport(audio_fd, AUDIO_MICROPHONE); |
|---|
| 479 | break; |
|---|
| 480 | default: |
|---|
| 481 | printf("Unknown audio source!\n"); |
|---|
| 482 | } |
|---|
| 483 | return (iport); |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | void |
|---|
| 487 | audio_switch_out(int audio_fd, struct s_cushion_struct *ap) |
|---|
| 488 | { |
|---|
| 489 | UNUSED(ap); |
|---|
| 490 | if (!audio_duplex(audio_fd) && !can_write) { |
|---|
| 491 | audio_close(audio_fd); |
|---|
| 492 | audio_open_rw(O_WRONLY); |
|---|
| 493 | } |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | void |
|---|
| 497 | audio_switch_in(int audio_fd) |
|---|
| 498 | { |
|---|
| 499 | if (!audio_duplex(audio_fd) && !can_read) { |
|---|
| 500 | audio_close(audio_fd); |
|---|
| 501 | audio_open_rw(O_RDONLY); |
|---|
| 502 | } |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | int |
|---|
| 506 | audio_get_blocksize() |
|---|
| 507 | { |
|---|
| 508 | return format.blocksize; |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 511 | int |
|---|
| 512 | audio_get_channels() |
|---|
| 513 | { |
|---|
| 514 | return format.num_channels; |
|---|
| 515 | } |
|---|
| 516 | |
|---|
| 517 | int |
|---|
| 518 | audio_get_freq() |
|---|
| 519 | { |
|---|
| 520 | return format.sample_rate; |
|---|
| 521 | } |
|---|
| 522 | |
|---|
| 523 | #endif |
|---|