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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
|
/*
* Purpose: Full duplex sample program using the single device approach.
*
* Description:
* This sample program explains how to use the one and twodevicefile based
* methods for full duplex.
*
* This program uses full duplex for echo-like processing (usefull for
* applications like guitar effect processors). However this task is
* actually very challenging. Applications that require almost zero
* latencies will need to be run on very high priority levels. The exact method
* required for this depends on the operating system and is beyond the scope
* of this simplistic sample program.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/select.h>
#include <soundcard.h>
char *dspname = "/dev/dsp";
char *dspname_in = NULL;
int fd_out = -1, fd_in = -1;
char buffer[32 * 1024]; /* Max 32k local buffer */
int rate = 48000;
int fragsize;
static void
open_one_device (char *dspname)
{
/*
* Open the device file. The one device full duplex scheme requires that
* the device file is opened with O_RDWR. See the description of the
* {!nlink open} system call for more info.
*/
oss_audioinfo ai;
int fd;
int tmp;
int devcaps;
int channels = 2;
int format;
int frag;
if ((fd = open (dspname, O_RDWR, 0)) == -1)
{
perror (dspname);
exit (-1);
}
ai.dev = -1;
if (ioctl (fd, SNDCTL_ENGINEINFO, &ai) != -1)
{
printf ("\nUsing audio engine %d=%s for duplex\n\n", ai.dev, ai.name);
}
#ifdef USE_RAW_FORMATS
/*
* In some cases it's recommended that all sample rate and format
* conversions are disabled. These conversions may make timing very
* tricky. However the drawback of disabling format conversions is that
* the application must be able to handle the format and rate
* conversions itself.
*
* We don't do any error checking because SNDCTL_DSP_COOKEDMODE is an optional
* ioctl call that may not be supported by all OSS implementations (in such
* cases there are no format conversions anyway).
*/
tmp = 0;
ioctl (fd, SNDCTL_DSP_COOKEDMODE, &tmp);
#endif
/*
* Check that the device supports full duplex. Otherwise there is no point in
* continuing.
*/
if (ioctl (fd, SNDCTL_DSP_GETCAPS, &devcaps) == -1)
{
perror ("SNDCTL_DSP_GETCAPS");
exit (-1);
}
if (!(devcaps & PCM_CAP_DUPLEX))
{
fprintf (stderr,
"%s doesn't support one device based full duplex scheme\n",
dspname);
fprintf (stderr, "Please use the two device scheme.\n");
exit (-1);
}
#if 0
/*
* There is no point in calling SNDCTL_DSP_SETDUPLEX any more. This call has not had any
* effect since SB16.
*/
if (ioctl (fd, SNDCTL_DSP_SETDUPLEX, NULL) == -1)
{
perror ("SNDCTL_DSP_SETDUPLEX");
exit (-1);
}
#endif
/*
* Try to set the fragment size to suitable level.
*/
frag = 0x7fff000a; /* Unlimited number of 1k fragments */
if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &frag) == -1)
{
perror ("SNDCTL_DSP_SETFRAGMENT");
exit (-1);
}
/*
* Set up the sampling rate and other sample parameters.
*/
tmp = channels;
if (ioctl (fd, SNDCTL_DSP_CHANNELS, &tmp) == -1)
{
perror ("SNDCTL_DSP_CHANNELS");
exit (-1);
}
if (tmp != channels)
{
fprintf (stderr, "%s doesn't support stereo (%d)\n", dspname, tmp);
exit (-1);
}
/*
* Request 16 bit native endian sample format.
*/
tmp = AFMT_S16_NE;
if (ioctl (fd, SNDCTL_DSP_SETFMT, &tmp) == -1)
{
perror ("SNDCTL_DSP_SETFMT");
exit (-1);
}
/*
* Note that most devices support only the usual litle endian (Intel)
* byte order. This may be a problem under big endian architectures such
* as Sparc. We accept also the opposite (alien) endianess but
* this may require different handling (byte swapping) in most applications.
* However we don't care about this issue because this applicaton doesn't
* do any processing on the data.
*/
if (tmp != AFMT_S16_NE && tmp != AFMT_S16_OE)
{
fprintf (stderr, "%s doesn't support 16 bit sample format (%x)\n",
dspname, tmp);
exit (-1);
}
format = tmp;
if (format == AFMT_S16_OE)
{
fprintf (stderr,
"Warning: Using 16 bit sample format with wrong endianess.\n");
}
tmp = rate;
if (ioctl (fd, SNDCTL_DSP_SPEED, &tmp) == -1)
{
perror ("SNDCTL_DSP_SPEED");
exit (-1);
}
if (tmp != rate)
{
fprintf (stderr, "%s doesn't support requested rate %d (%d)\n", dspname,
rate, tmp);
exit (-1);
}
/*
* Get the actual fragment size. SNDCTL_DSP_GETBLKSIZE gives a good
* compromise. If cooked mode is not disabled then the fragment sizes
* used for input and output may be different. Using
* {!nlink SNDCTL_DSP_GETOSPACE} or {!nlink SNDCTL_DSP_GETISPACE}
* may return different values. In this case SNDCTL_DSP_GETBLKSIZE will
* return a value that is between them.
*/
if (ioctl (fd, SNDCTL_DSP_GETBLKSIZE, &fragsize) == -1)
{
perror ("SNDCTL_DSP_GETBLKSIZE");
exit (-1);
}
if (fragsize > sizeof (buffer))
{
fprintf (stderr, "Too large fragment size %d\n", fragsize);
exit (-1);
}
printf ("Sample parameters set OK. Using fragment size %d\n", fragsize);
fd_in = fd_out = fd;
}
static void
open_two_devices (char *dspname_out, char *dspname_in)
{
/*
* Open the device file. The one device full duplex scheme requires that
* the device file is opened with O_RDWR. See the description of the
* {!nlink open} system call for more info.
*/
oss_audioinfo ai_in, ai_out;
int tmp;
int devcaps;
int channels = 2;
int format;
int frag = 0x7fff000a; /* Unlimited number of 1k fragments */
/*
* Open the output device
*/
if ((fd_out = open (dspname_out, O_WRONLY, 0)) == -1)
{
perror (dspname_out);
exit (-1);
}
ai_out.dev = -1;
if (ioctl (fd_out, SNDCTL_ENGINEINFO, &ai_out) != -1)
{
printf ("\nUsing audio engine %d=%s for output\n", ai_out.dev,
ai_out.name);
}
/*
* Open the input device
*/
if ((fd_in = open (dspname_in, O_RDONLY, 0)) == -1)
{
perror (dspname_in);
exit (-1);
}
ai_in.dev = -1;
if (ioctl (fd_in, SNDCTL_ENGINEINFO, &ai_in) != -1)
{
printf ("Using audio engine %d=%s for input\n\n", ai_in.dev,
ai_in.name);
}
if (ai_in.rate_source != ai_out.rate_source)
{
/*
* Input and output devices should have their sampling rates derived from
* the same crystal clock. Otherwise there will be drift in the sampling rates
* which may cause dropouts/hiccup (unless the application can handle the rate
* error). So check that the rate sources are the same.
*
* However two devices may still be driven by the same clock even if the
* rate sources are different. OSS has no way to know if the user is using some
* common sample clock (word clock) generator to syncronize all devices
* together (which is _THE_ practice in production environments). So do not
* overreact. Just let the user to know about the potential problem.
*/
fprintf (stderr,
"Note! %s and %s are not necessarily driven by the same clock.\n",
dspname_out, dspname_in);
}
#ifdef USE_RAW_FORMATS
/*
* In many cases it's recommended that all sample rate and format
* conversions are disabled. These conversions may make timing very
* tricky. However the drawback of disabling format conversions is that
* the application must be able to handle the format and rate
* conversions itself.
*
* We don't do any error checking because SNDCTL_DSP_COOKEDMODE is an optional
* ioctl call that may not be supported by all OSS implementations (in such
* cases there are no format conversions anyway).
*/
tmp = 0;
ioctl (fd_out, SNDCTL_DSP_COOKEDMODE, &tmp);
tmp = 0;
ioctl (fd_in, SNDCTL_DSP_COOKEDMODE, &tmp);
#endif
/*
* Check output device capabilities.
*/
if (ioctl (fd_out, SNDCTL_DSP_GETCAPS, &devcaps) == -1)
{
perror ("SNDCTL_DSP_GETCAPS");
exit (-1);
}
if (devcaps & PCM_CAP_DUPLEX)
{
fprintf (stderr,
"Device %s supports duplex so you may want to use the single device approach instead\n",
dspname_out);
}
if (!(devcaps & PCM_CAP_OUTPUT))
{
fprintf (stderr, "%s doesn't support output\n", dspname_out);
fprintf (stderr, "Please use different device.\n");
#if 0
/*
* NOTE! Earlier OSS versions don't necessarily support PCM_CAP_OUTPUT
* so don't panic.
*/
exit (-1);
#endif
}
/*
* Check input device capabilities.
*/
if (ioctl (fd_in, SNDCTL_DSP_GETCAPS, &devcaps) == -1)
{
perror ("SNDCTL_DSP_GETCAPS");
exit (-1);
}
if (devcaps & PCM_CAP_DUPLEX)
{
fprintf (stderr,
"Device %s supports duplex so you may want to use the single device approach instead\n",
dspname_in);
}
if (!(devcaps & PCM_CAP_INPUT))
{
fprintf (stderr, "%s doesn't support input\n", dspname_in);
fprintf (stderr, "Please use different device.\n");
#if 0
/*
* NOTE! Earlier OSS versions don't necessarily support PCM_CAP_INPUT
* so don't panic.
*/
exit (-1);
#endif
}
/*
* No need to turn on the full duplex mode when using separate input and output
* devices. In fact calling SNDCTL_DSP_SETDUPLEX in this mode would be a
* major mistake
*/
/*
* Try to set the fragment size to suitable level.
*/
if (ioctl (fd_out, SNDCTL_DSP_SETFRAGMENT, &frag) == -1)
{
perror ("SNDCTL_DSP_SETFRAGMENT");
exit (-1);
}
if (ioctl (fd_in, SNDCTL_DSP_SETFRAGMENT, &frag) == -1)
{
perror ("SNDCTL_DSP_SETFRAGMENT");
exit (-1);
}
/*
* Set up the sampling rate and other sample parameters.
*/
tmp = channels;
if (ioctl (fd_out, SNDCTL_DSP_CHANNELS, &tmp) == -1)
{
perror ("SNDCTL_DSP_CHANNELS");
exit (-1);
}
if (tmp != channels)
{
fprintf (stderr, "%s doesn't support stereo (%d)\n", dspname_out, tmp);
exit (-1);
}
if (ioctl (fd_in, SNDCTL_DSP_CHANNELS, &tmp) == -1)
{
perror ("SNDCTL_DSP_CHANNELS");
exit (-1);
}
if (tmp != channels)
{
fprintf (stderr, "%s doesn't support stereo (%d)\n", dspname_in, tmp);
exit (-1);
}
/*
* Request 16 bit native endian sample format.
*/
tmp = AFMT_S16_NE;
if (ioctl (fd_out, SNDCTL_DSP_SETFMT, &tmp) == -1)
{
perror ("SNDCTL_DSP_SETFMT");
exit (-1);
}
/*
* Note that most devices support only the usual litle endian (Intel)
* byte order. This may be a problem under big endian architectures such
* as Sparc. We accept also the opposite (alien) endianess but
* this may require different handling (byte swapping) in most applications.
* However we don't care about this issue because this applicaton doesn't
* do any processing on the data.
*/
if (tmp != AFMT_S16_NE && tmp != AFMT_S16_OE)
{
fprintf (stderr, "%s doesn't support 16 bit sample format (%x)\n",
dspname_out, tmp);
exit (-1);
}
format = tmp;
if (ioctl (fd_in, SNDCTL_DSP_SETFMT, &tmp) == -1)
{
perror ("SNDCTL_DSP_SETFMT");
exit (-1);
}
if (tmp != format)
{
fprintf (stderr,
"Error: Input and output devices use different formats (%x/%x)\n",
tmp, format);
exit (-1);
}
if (format == AFMT_S16_OE)
{
fprintf (stderr,
"Warning: Using 16 bit sample format with wrong endianess.\n");
}
/*
* It might be better to set the sampling rate firs on the input device and
* then use the same rate with the output device. The reason for this is that
* the vmix driver supports sample rate conversions for output. However input
* is locked to the master device rate.
*/
tmp = rate;
if (ioctl (fd_in, SNDCTL_DSP_SPEED, &tmp) == -1)
{
perror ("SNDCTL_DSP_SPEED");
exit (-1);
}
if (tmp != rate)
{
fprintf (stderr, "%s doesn't support requested rate %d (%d)\n",
dspname_out, rate, tmp);
exit (-1);
}
if (ioctl (fd_out, SNDCTL_DSP_SPEED, &tmp) == -1)
{
perror ("SNDCTL_DSP_SPEED");
exit (-1);
}
if (tmp != rate)
{
fprintf (stderr, "%s doesn't support the same rate %d!=%d as %s\n",
dspname_out, rate, tmp, dspname_in);
exit (-1);
}
/*
* Get the actual fragment size. SNDCTL_DSP_GETBLKSIZE gives a good
* compromise. If cooked mode is not disabled then the fragment sizes
* used for input and output may be different. Using
* {!nlink SNDCTL_DSP_GETOSPACE} or {!nlink SNDCTL_DSP_GETISPACE}
* may return different values. In this case SNDCTL_DSP_GETBLKSIZE will
* return a value that is between them.
*/
if (ioctl (fd_in, SNDCTL_DSP_GETBLKSIZE, &fragsize) == -1)
{
perror ("SNDCTL_DSP_GETBLKSIZE");
exit (-1);
}
if (fragsize > sizeof (buffer))
{
fprintf (stderr, "Too large fragment size %d\n", fragsize);
exit (-1);
}
/*
* Do not check the fragment sizes on both devices. It is perfectly normal
* that they are different. Instead check just the input fragment size because
* it is the one that matters.
*/
printf ("Sample parameters set OK. Using fragment size %d\n", fragsize);
}
static void
method_0 (int fd_out, int fd_in)
{
/*
* This function demonstrates how to implement an full duplex
* application in the easiest and most reliable way. This method uses
* blocking reads for synchronization with the device.
*/
int l;
int loopcount = 0;
/*
* Fragments can be as large as 32k (or even up to 64k)
* with certain devices.
*/
char silence[32 * 1024]; /* Buffer for one fragment of silence */
/*
* This is the record/play loop. This block uses simple model where recorded data
* is just read from the device and written back without use of any additional
* ioctl calls. This approach should be used if the application doesn't need
* to correlate the recorded samples with playback (for example for echo
* cancellation).
*
* In this approach OSS will automatically find out how large buffer is
* needed to handle the process without buffer underruns. However there will
* probably be few dropouts before the buffer gets adapted for the worst
* case latencies.
*
* There are two methods to avoid the initial dropouts. The easiest method is
* to write few milliseconds of silent sampels to the output before writing
* the first block of actual recorded data. Another approach is to warm up
* the device by replacing first seconds of recorded data with silecene
* before writing the data to the output.
*
* After few moments of run the output should settle. After that the lag
* between input and output should be very close to the minimum input-output
* latency that can be obtained without using applied woodoo. The latencies can
* possibly be reduced by optimizing the application itself (if it's CPU bound),
* by using shorter fragments (also check the max_intrate parameter in
* osscore.conf), by terminating unnecessary applications and by using
* higher (linear) priority.
*/
while ((l = read (fd_in, buffer, fragsize)) == fragsize)
{
int delay;
float t;
if (loopcount == 0)
{
/*
* Output one extra fragment filled with silence
* before starting to write the actual data. This must
* be done after we have recorded the first fragment
* Without this extra silence in the beginning playback
* data will run out microseconds before next recorded
* data becomes available.
*
* Number of silence bytes written doesn't need to be
* exactly one fragment or any multiple of it. Sometimes
* (under highly loaded system) more silence will be
* needed. Sometimes just few samples may be
* enough.
*/
memset (silence, 0, fragsize);
if (write (fd_out, silence, fragsize) != fragsize)
{
perror ("write");
exit (-1);
}
}
/*
* Compute the output delay (in milliseconds)
*/
if (ioctl (fd_out, SNDCTL_DSP_GETODELAY, &delay) == -1)
delay = 0;
delay /= 4; /* Get number of 16 bit stereo samples */
t = (float) delay / (float) rate; /* Get delay in seconds */
t *= 1000.0; /* Convert delay to milliseconds */
if ((l = write (fd_out, buffer, fragsize)) != fragsize)
{
perror ("write");
exit (-1);
}
#if 1
/*
* Printing the delay level will slow down the application which
* makes the delay longer. So don't print it by default.
*/
printf ("\rDelay=%5.3g msec", t);
fflush (stdout);
#endif
loopcount++;
}
perror ("read");
exit (-1);
}
static void
method_1 (int fd_out, int fd_in)
{
/*
* Many applications use select/poll or the equivivalent facilities provided
* by GUI libraries like GTK+ to handle I/O with multiple devices. We use
* select() in this example.
*
* This routine reads audio input, does some processing on the data and
* forwards it to the output device. Hitting ENTER terminates the program.
*
* The logic is that any input available on the device will be read. However
* the amount of space available on output is not checked because it's
* unnecessary. Checking both input and output status would be very tricky
* if not impossible.
*/
char silence[32 * 1024]; /* Buffer for one fragment of silence */
fd_set reads;
int n;
unsigned int trig;
int first_time = 1;
/*
* First we have to start the recording engine. Otherwise select() will never
* report available input and the application will just iddle.
*/
trig = 0; /* Trigger OFF */
if (ioctl (fd_in, SNDCTL_DSP_SETTRIGGER, &trig) == -1)
perror ("SETTRIGGER 0");
trig = PCM_ENABLE_INPUT; /* Trigger ON */
/*
* Trigger output too if using the single device mode. Otherwise all writes
* will fail.
*/
if (fd_in == fd_out)
trig |= PCM_ENABLE_OUTPUT;
if (ioctl (fd_in, SNDCTL_DSP_SETTRIGGER, &trig) == -1)
perror ("SETTRIGGER 1");
while (1) /* Infinite loop */
{
struct timeval time;
FD_ZERO (&reads);
FD_SET (0, &reads); /* stdin */
FD_SET (fd_in, &reads);
time.tv_sec = 1;
time.tv_usec = 0;
if ((n = select (fd_in + 1, &reads, NULL, NULL, &time)) == -1)
{
perror ("select");
exit (-1);
}
if (n == 0)
{
fprintf (stderr, "Timeout\n");
continue;
}
if (FD_ISSET (0, &reads)) /* Keyboard input */
{
printf ("Finished.\n");
exit (0);
}
if (FD_ISSET (fd_in, &reads))
{
/*
* Recorded data is available.
*/
int l, n;
struct audio_buf_info info;
if (ioctl (fd_in, SNDCTL_DSP_GETISPACE, &info) == -1)
{
perror ("GETISPACE");
exit (-1);
}
n = info.bytes; /* How much */
if ((l = read (fd_in, buffer, n)) == n)
{
printf ("\r %5d bytes ", n);
fflush (stdout);
if (first_time)
{
/*
* Write one fragment of silence before the actual data.
* This is necessary to avoid regular underruns while
* waiting for more data.
*/
memset (silence, 0, fragsize);
if (write (fd_out, silence, fragsize) != fragsize)
{
perror ("write");
exit (-1);
}
first_time = 0;
}
/*
* This is the place where you can add your processing.
* There are 'l' bytes of audio data stored in 'buffer'. This
* program uses 16 bit native endian format and two channels
* (stereo).
*/
if (write (fd_out, buffer, l) != l)
{
perror ("write");
exit (-1);
}
continue;
}
}
perror ("read");
exit (-1);
}
}
int
main (int argc, char *argv[])
{
int method = 0;
/*
* Check the working method
*/
if (argc > 1)
method = atoi (argv[1]);
/*
* Check if the sampling rate was given on command line.
*/
if (argc > 2)
{
rate = atoi (argv[2]);
if (rate == 0)
rate = 48000;
}
/*
* Check if the device name is given on command line.
*/
if (argc > 3)
dspname = argv[3];
/*
* Check if anotherdevice name is given for input.
*/
if (argc > 4)
dspname_in = argv[4];
if (dspname_in == NULL)
open_one_device (dspname);
else
open_two_devices (dspname, dspname_in);
switch (method)
{
case 0:
method_0 (fd_out, fd_in);
break;
case 1:
method_1 (fd_out, fd_in);
break;
default:
fprintf (stderr, "Method %d not defined\n", method);
exit (-1);
}
exit (0);
}
|