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 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
|
/*
sndlib.c:
Copyright (C) 2004 John ffitch
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "csoundCore.h" /* SNDLIB.C */
#include "soundio.h"
#include <stdlib.h>
#include <time.h>
#include <inttypes.h>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef PIPES
# if defined(SGI) || defined(LINUX) || defined(__BEOS__) || defined(NeXT) || \
defined(__MACH__)
# define _popen popen
# define _pclose pclose
# endif
extern FILE * _popen(const char *, const char *);
extern int _pclose(FILE *);
#endif
static void sndwrterr(CSOUND *, int, int);
static void sndfilein_noscale(CSOUND *csound);
#define STA(x) (csound->libsndStatics.x)
static inline void alloc_globals(CSOUND *csound)
{
csound->libsndStatics.nframes = (uint32)1;
}
/* The interface requires 2 functions:
spoutran to transfer nspout items to buffer
audtran to actually write the data
spoutran is called with nchnls*ksamps items and this need to be
buffered until outbufsiz items have been accumulated. It will call
audtran to flush when this happens.
*/
static void spoutsf(CSOUND *csound)
{
OPARMS *O = csound->oparms;
uint32_t chn = 0;
int n;
int spoutrem = csound->nspout;
MYFLT *sp = csound->spout;
MYFLT absamp = FL(0.0);
uint32 nframes = csound->libsndStatics.nframes;
MYFLT lim = O->limiter*csound->e0dbfs;
MYFLT rlim = lim==0 ? 0 : FL(1.0)/lim;
MYFLT k1 = FL(1.0)/TANH(FL(1.0)); /* 1.31304 */
nchk:
/* if nspout remaining > buf rem, prepare to send in parts */
if ((n = spoutrem) > (int) csound->libsndStatics.outbufrem) {
n = (int) csound->libsndStatics.outbufrem;
}
spoutrem -= n;
csound->libsndStatics.outbufrem -= n;
do {
// built inlimiter start ****
// There is a rather awkward problem in reporting out of range not being
// confused by the limited value but passing the clipped values to the
// output. Current solution is nasty and should be easier
if (O->limiter) {
MYFLT x = *sp;
absamp = x;
if (UNLIKELY(x>=lim))
x = lim;
else if (UNLIKELY(x<= -lim))
x = -lim;
else
x = lim*k1*TANH(x*rlim);
//printf("*** %g -> %g\n", *(sp-1), x);
*sp++ = x;
if (csound->libsndStatics.osfopen) {
*csound->libsndStatics.outbufp++ = (x * csound->dbfs_to_float);
}
}
// limiter end ****
else {
absamp = *sp++;
if (csound->libsndStatics.osfopen) {
*csound->libsndStatics.outbufp++ = (absamp * csound->dbfs_to_float);
}
}
if (absamp < FL(0.0)) {
absamp = -absamp;
}
if (absamp > csound->maxamp[chn]) { /* maxamp this seg */
csound->maxamp[chn] = absamp;
csound->maxpos[chn] = nframes;
}
if (absamp > csound->e0dbfs) { /* out of range? */
csound->rngcnt[chn]++; /* report it */
csound->rngflg = 1;
}
if (csound->multichan) {
if (++chn >= csound->nchnls) {
chn = 0;
nframes++;
}
} else {
nframes++;
}
} while (--n);
if (!csound->libsndStatics.outbufrem) {
if (csound->libsndStatics.osfopen) {
csound->nrecs++;
csound->audtran(csound, csound->libsndStatics.outbuf,
csound->libsndStatics.outbufsiz); /* Flush buffer */
csound->libsndStatics.outbufp = (MYFLT*) csound->libsndStatics.outbuf;
}
csound->libsndStatics.outbufrem = csound->oparms_.outbufsamps;
if (spoutrem) {
goto nchk;
}
}
csound->libsndStatics.nframes = nframes;
}
/* special version of spoutsf for "raw" floating point files */
static void spoutsf_noscale(CSOUND *csound)
{
uint32_t chn = 0;
int n, spoutrem = csound->nspout;
MYFLT *sp = csound->spout;
MYFLT absamp = FL(0.0);
uint32 nframes = csound->libsndStatics.nframes;
nchk:
/* if nspout remaining > buf rem, prepare to send in parts */
if ((n = spoutrem) > (int) csound->libsndStatics.outbufrem)
n = (int)csound->libsndStatics.outbufrem;
spoutrem -= n;
csound->libsndStatics.outbufrem -= n;
do {
absamp = *sp++;
if (csound->libsndStatics.osfopen)
*csound->libsndStatics.outbufp++ = absamp;
if (absamp < FL(0.0))
absamp = -absamp;
if (absamp > csound->maxamp[chn]) { /* maxamp this seg */
csound->maxamp[chn] = absamp;
csound->maxpos[chn] = nframes;
}
if (++chn >= csound->nchnls)
chn = 0, nframes++;
} while (--n);
if (!csound->libsndStatics.outbufrem) {
if (csound->libsndStatics.osfopen) {
csound->nrecs++;
csound->audtran(csound, csound->libsndStatics.outbuf,
csound->libsndStatics.outbufsiz); /* Flush buffer */
csound->libsndStatics.outbufp = (MYFLT*) csound->libsndStatics.outbuf;
}
csound->libsndStatics.outbufrem = csound->oparms_.outbufsamps;
if (spoutrem) goto nchk;
}
csound->libsndStatics.nframes = nframes;
}
/* diskfile write option for audtran's */
/* assigned during sfopenout() */
static void writesf(CSOUND *csound, const MYFLT *outbuf, int nbytes)
{
OPARMS *O = csound->oparms;
int n;
if (UNLIKELY(STA(outfile) == NULL))
return;
n = (int) sf_write_MYFLT(STA(outfile), (MYFLT*) outbuf,
nbytes / sizeof(MYFLT)) * (int) sizeof(MYFLT);
if (UNLIKELY(n < nbytes))
sndwrterr(csound, n, nbytes);
if (UNLIKELY(O->rewrt_hdr))
rewriteheader((void *)STA(outfile));
switch (O->heartbeat) {
case 1:
csound->MessageS(csound, CSOUNDMSG_REALTIME,
"%c\010", "|/-\\"[csound->nrecs & 3]);
break;
case 2:
csound->MessageS(csound, CSOUNDMSG_REALTIME, ".");
break;
case 3:
{
char s[512];
CS_SPRINTF(s, "%ld(%.3f)%n", (long) csound->nrecs,
csound->icurTime/csound->esr, &n);
if (n > 0) {
memset(&(s[n]), '\b', n);
s[n + n] = '\0';
csound->MessageS(csound, CSOUNDMSG_REALTIME, "%s", s);
}
}
break;
case 4:
csound->MessageS(csound, CSOUNDMSG_REALTIME, "%s", "\a");
break;
}
}
static void writesf_dither_16(CSOUND *csound, const MYFLT *outbuf, int nbytes)
{
OPARMS *O = csound->oparms;
int n;
int m = nbytes / sizeof(MYFLT);
MYFLT *buf = (MYFLT*) outbuf;
int dith;
if (UNLIKELY(STA(outfile) == NULL))
return;
dith = STA(dither);
for (n=0; n<m; n++) {
int tmp = ((dith * 15625) + 1) & 0xFFFF;
int rnd = ((tmp * 15625) + 1) & 0xFFFF;
MYFLT result;
dith = rnd;
rnd = (rnd+tmp)>>1; /* triangular distribution */
result = (MYFLT) (rnd - 0x8000) / ((MYFLT) 0x10000);
result /= ((MYFLT) 0x7fff);
buf[n] += result;
}
STA(dither) = dith;
n = (int) sf_write_MYFLT(STA(outfile), (MYFLT*) outbuf,
nbytes / sizeof(MYFLT)) * (int) sizeof(MYFLT);
if (UNLIKELY(n < nbytes))
sndwrterr(csound, n, nbytes);
if (UNLIKELY(O->rewrt_hdr))
rewriteheader(STA(outfile));
switch (O->heartbeat) {
case 1:
csound->MessageS(csound, CSOUNDMSG_REALTIME,
"%c\010", "|/-\\"[csound->nrecs & 3]);
break;
case 2:
csound->MessageS(csound, CSOUNDMSG_REALTIME, ".");
break;
case 3:
{
char s[512];
CS_SPRINTF(s, "%ld(%.3f)%n", (long) csound->nrecs,
csound->icurTime/csound->esr, &n);
if (n > 0) {
memset(&(s[n]), '\b', n);
s[n + n] = '\0';
csound->MessageS(csound, CSOUNDMSG_REALTIME, "%s", s);
}
}
break;
case 4:
csound->MessageS(csound, CSOUNDMSG_REALTIME, "\a");
break;
}
}
static void writesf_dither_8(CSOUND *csound, const MYFLT *outbuf, int nbytes)
{
OPARMS *O = csound->oparms;
int n;
int m = nbytes / sizeof(MYFLT);
MYFLT *buf = (MYFLT*) outbuf;
int dith;
if (UNLIKELY(STA(outfile) == NULL))
return;
dith = STA(dither);
for (n=0; n<m; n++) {
int tmp = ((dith * 15625) + 1) & 0xFFFF;
int rnd = ((tmp * 15625) + 1) & 0xFFFF;
MYFLT result;
dith = rnd;
rnd = (rnd+tmp)>>1; /* triangular distribution */
result = (MYFLT) (rnd - 0x8000) / ((MYFLT) 0x10000);
result /= ((MYFLT) 0x7f);
buf[n] += result;
}
STA(dither) = dith;
n = (int) sf_write_MYFLT(STA(outfile), (MYFLT*) outbuf,
nbytes / sizeof(MYFLT)) * (int) sizeof(MYFLT);
if (UNLIKELY(n < nbytes))
sndwrterr(csound, n, nbytes);
if (UNLIKELY(O->rewrt_hdr))
rewriteheader(STA(outfile));
switch (O->heartbeat) {
case 1:
csound->MessageS(csound, CSOUNDMSG_REALTIME,
"%c\010", "|/-\\"[csound->nrecs & 3]);
break;
case 2:
csound->MessageS(csound, CSOUNDMSG_REALTIME, ".");
break;
case 3:
{
char s[512];
CS_SPRINTF(s, "%ld(%.3f)%n", (long) csound->nrecs,
csound->icurTime/csound->esr, &n);
if (n > 0) {
memset(&(s[n]), '\b', n);
s[n + n] = '\0';
csound->MessageS(csound, CSOUNDMSG_REALTIME, "%s", s);
}
}
break;
case 4:
csound->MessageS(csound, CSOUNDMSG_REALTIME, "\a");
break;
}
}
static void writesf_dither_u16(CSOUND *csound, const MYFLT *outbuf, int nbytes)
{
OPARMS *O = csound->oparms;
int n;
int m = nbytes / sizeof(MYFLT);
MYFLT *buf = (MYFLT*) outbuf;
int dith;
if (UNLIKELY(STA(outfile) == NULL))
return;
dith = STA(dither);
for (n=0; n<m; n++) {
int rnd = ((dith * 15625) + 1) & 0xFFFF;
MYFLT result;
dith = rnd;
result = (MYFLT) (rnd - 0x8000) / ((MYFLT) 0x10000);
result /= ((MYFLT) 0x7fff);
buf[n] += result;
}
STA(dither) = dith;
n = (int) sf_write_MYFLT(STA(outfile), (MYFLT*) outbuf,
nbytes / sizeof(MYFLT)) * (int) sizeof(MYFLT);
if (UNLIKELY(n < nbytes))
sndwrterr(csound, n, nbytes);
if (UNLIKELY(O->rewrt_hdr))
rewriteheader(STA(outfile));
switch (O->heartbeat) {
case 1:
csound->MessageS(csound, CSOUNDMSG_REALTIME,
"%c\010", "|/-\\"[csound->nrecs & 3]);
break;
case 2:
csound->MessageS(csound, CSOUNDMSG_REALTIME, ".");
break;
case 3:
{
char s[512];
CS_SPRINTF(s, "%ld(%.3f)%n", (long) csound->nrecs,
csound->icurTime/csound->esr, &n);
if (n > 0) {
memset(&(s[n]), '\b', n);
s[n + n] = '\0';
csound->MessageS(csound, CSOUNDMSG_REALTIME, "%s", s);
}
}
break;
case 4:
csound->MessageS(csound, CSOUNDMSG_REALTIME, "%s", "\a");
break;
}
}
static void writesf_dither_u8(CSOUND *csound, const MYFLT *outbuf, int nbytes)
{
OPARMS *O = csound->oparms;
int n;
int m = nbytes / sizeof(MYFLT);
MYFLT *buf = (MYFLT*) outbuf;
int dith;
if (UNLIKELY(STA(outfile) == NULL))
return;
dith = STA(dither);
for (n=0; n<m; n++) {
int rnd = ((dith * 15625) + 1) & 0xFFFF;
MYFLT result;
STA(dither) = rnd;
result = (MYFLT) (rnd - 0x8000) / ((MYFLT) 0x10000);
result /= ((MYFLT) 0x7f);
buf[n] += result;
}
STA(dither) = dith;
n = (int) sf_write_MYFLT(STA(outfile), (MYFLT*) outbuf,
nbytes / sizeof(MYFLT)) * (int) sizeof(MYFLT);
if (UNLIKELY(n < nbytes))
sndwrterr(csound, n, nbytes);
if (UNLIKELY(O->rewrt_hdr))
rewriteheader(STA(outfile));
switch (O->heartbeat) {
case 1:
csound->MessageS(csound, CSOUNDMSG_REALTIME,
"%c\010", "|/-\\"[csound->nrecs & 3]);
break;
case 2:
csound->MessageS(csound, CSOUNDMSG_REALTIME, ".");
break;
case 3:
{
char s[512];
CS_SPRINTF(s, "%ld(%.3f)%n", (long) csound->nrecs,
csound->icurTime/csound->esr, &n);
if (n > 0) {
memset(&(s[n]), '\b', n);
s[n + n] = '\0';
csound->MessageS(csound, CSOUNDMSG_REALTIME, "%s", s);
}
}
break;
case 4:
csound->MessageS(csound, CSOUNDMSG_REALTIME, "\a");
break;
}
}
static int readsf(CSOUND *csound, MYFLT *inbuf, int inbufsize)
{
int i, n;
(void) csound;
n = inbufsize / (int) sizeof(MYFLT);
i = (int) sf_read_MYFLT(STA(infile), inbuf, n);
if (UNLIKELY(i < 0))
return inbufsize;
memset(&inbuf[i], 0, (n-i)*sizeof(MYFLT));
return inbufsize;
}
/* Checks if the specified file name is a real-time audio device. */
/* Returns the device number (defaults to 1024) if it is, and -1 otherwise. */
/* If a device name is specified, and 'devName' is not NULL, a pointer to it */
/* is stored in *devName. */
/* Called from musmon, str_ops and here */
int check_rtaudio_name(char *fName, char **devName, int isOutput)
{
char *s;
if (devName != NULL)
*devName = (char*) NULL;
if (fName == NULL)
return -1;
s = fName;
if ((isOutput && strncmp(fName, "dac", 3) == 0) ||
(!isOutput && strncmp(fName, "adc", 3) == 0))
s += 3;
else if (strncmp(fName, "devaudio", 8) == 0)
s += 8;
else
return -1;
if (*s == (char) '\0')
return 1024;
if (*s == (char) ':') {
if (devName != NULL) {
*devName = &(s[1]);
}
return 1024;
}
else {
int devNum = 0;
while (*s >= (char) '0' && *s <= (char) '9') {
devNum = devNum * 10 + ((int) *s - (int) '0');
if (devNum >= 1024)
break;
if (*(++s) == (char) '\0')
return devNum;
}
}
return -1;
}
void sfopenin(CSOUND *csound) /* init for continuous soundin */
{
OPARMS *O = csound->oparms;
char *sfname, *fullName;
SF_INFO sfinfo;
int fileType = (int) TYP_RAW;
int isfd = 0; /* stdin */
if(csound->inchnls < 1)
csound->Die(csound,
Str("error: cannot run input audio with nchnls_i=0"));
alloc_globals(csound);
STA(inbufrem) = (uint32) 0; /* start with empty buffer */
sfname = O->infilename;
if (UNLIKELY(sfname == NULL || sfname[0] == '\0'))
csound->Die(csound, Str("error: no input file name"));
if (strcmp(sfname, "stdin") == 0) {
STA(pipdevin) = 1;
}
#ifdef PIPES
else if (sfname[0] == '|') {
STA(pin) = _popen(sfname + 1, "r");
isfd = fileno(STA(pin));
STA(pipdevin) = 1;
}
#endif
else {
csRtAudioParams parm;
/* check for real time audio input, and get device name/number */
parm.devNum = check_rtaudio_name(sfname, &(parm.devName), 0);
if (parm.devNum >= 0) {
/* set device parameters */
parm.bufSamp_SW =
(unsigned int) O->inbufsamps / (unsigned int) csound->inchnls;
parm.bufSamp_HW = O->oMaxLag;
parm.nChannels = csound->inchnls;
parm.sampleFormat = O->informat;
parm.sampleRate = (float) csound->esr;
/* open devaudio for input */
if (UNLIKELY(csound->recopen_callback(csound, &parm) != 0))
csoundDie(csound, Str("Failed to initialise real time audio input"));
/* & redirect audio gets */
csound->audrecv = csound->rtrecord_callback;
STA(pipdevin) = 2; /* no backward seeks ! */
goto inset; /* no header processing */
}
}
/* open file */
memset(&sfinfo, 0, sizeof(SF_INFO));
if (STA(pipdevin)) {
STA(infile) = sf_open_fd(isfd, SFM_READ, &sfinfo, 0);
if (UNLIKELY(STA(infile) == NULL)) {
/* open failed: possibly raw file, but cannot seek back to try again */
const char *sfError = Str(sf_strerror(NULL));
csoundDie(csound, Str("isfinit: cannot open %s -- %s"), sfname, sfError);
}
}
else {
fullName = csoundFindInputFile(csound, sfname, "SFDIR;SSDIR");
if (UNLIKELY(fullName == NULL)) /* if not found */
csoundDie(csound, Str("isfinit: cannot open %s"), sfname);
STA(infile) = sf_open(fullName, SFM_READ, &sfinfo);
if (STA(infile) == NULL) {
/* open failed: maybe raw file ? */
memset(&sfinfo, 0, sizeof(SF_INFO));
sfinfo.samplerate = (int) MYFLT2LRND(csound->esr);
sfinfo.channels = csound->nchnls;
/* FIXME: assumes input sample format is same as output */
sfinfo.format = TYPE2SF(TYP_RAW) | FORMAT2SF(O->outformat);
STA(infile) = sf_open(fullName, SFM_READ, &sfinfo); /* try again */
}
if (UNLIKELY(STA(infile) == NULL)) {
const char *sfError = Str(sf_strerror(NULL));
csoundDie(csound, Str("isfinit: cannot open %s -- %s"), fullName, sfError);
}
/* only notify the host if we opened a real file, not stdin or a pipe */
csoundNotifyFileOpened(csound, fullName,
sftype2csfiletype(sfinfo.format), 0, 0);
sfname = fullName;
}
/* chk the hdr codes */
if (sfinfo.samplerate != (int) MYFLT2LRND(csound->esr)) {
csound->Warning(csound, Str("audio_in %s has sr = %d, orch sr = %d"),
sfname, (int) sfinfo.samplerate,
(int) MYFLT2LRND(csound->esr));
}
if (sfinfo.channels != csound->inchnls) {
csound->Warning(csound, Str("audio_in %s has %d chnls, orch %d chnls_i"),
sfname, (int) sfinfo.channels, csound->inchnls);
}
/* Do we care about the format? Can assume float?? */
O->informat = SF2FORMAT(sfinfo.format);
fileType = (int) SF2TYPE(sfinfo.format);
csound->audrecv = readsf; /* will use standard audio gets */
if ((O->informat == AE_FLOAT || O->informat == AE_DOUBLE) &&
!(fileType == TYP_WAV || fileType == TYP_AIFF || fileType == TYP_W64)) {
/* do not scale "raw" floating point files */
csound->spinrecv = sndfilein_noscale;
}
inset:
/* calc inbufsize reqd */
STA(inbufsiz) = (unsigned) (O->inbufsamps * sizeof(MYFLT));
STA(inbuf) = (MYFLT*) csound->Calloc(csound,
STA(inbufsiz)); /* alloc inbuf space */
if (STA(pipdevout) == 2) {
csound->Message(csound,
Str("reading %d sample blks of %lu-bit floats from %s\n"),
O->inbufsamps * O->sfsampsize,
(unsigned long) sizeof(MYFLT)*8, sfname);
}
else {
csound->Message(csound,
Str("reading %d-byte blks of %s from %s (%s)\n"),
O->inbufsamps * (int) sfsampsize(FORMAT2SF(O->informat)),
getstrformat(O->informat), sfname, type2string(fileType));
}
STA(isfopen) = 1;
}
static char* copyrightcode(int n)
{
char* a[] = {
"All Rights Reserved\n",
"Creative Commons Attribution-NonCommercial-NoDerivatives\nCC BY-NC-ND\n)",
"Creative Commons Attribution-NonCommercial-ShareAlike\nCC BY-NC-SA\n",
"Creative Commons Attribution-NonCommercial\nCC BY-NC\n",
"Creative Commons Attribution-NoDerivatives\nCC BY-ND\n",
"Creative Commons Attribution-ShareAlike\nCC BY-SA\n",
"Creative Commons Attribution\nCC BY\n",
"Licenced under BSD\n"
};
if (n>=8) n = 0;
return a[n];
}
void sfopenout(CSOUND *csound) /* init for sound out */
{ /* (not called if nosound) */
OPARMS *O = csound->oparms;
char *s, *fName, *fullName;
SF_INFO sfinfo;
int osfd = 1; /* stdout */
alloc_globals(csound);
if (O->outfilename == NULL) {
switch (O->filetyp) {
case TYP_WAV:
case TYP_W64:
case TYP_WAVEX:
case TYP_RF64:
O->outfilename = "test.wav";
break;
case TYP_AIFF:
O->outfilename = "test.aif";
break;
case TYP_AU:
O->outfilename = "test.au";
break;
case TYP_PAF:
O->outfilename = "test.paf";
break;
case TYP_SVX:
O->outfilename = "test.svx";
break;
case TYP_NIST:
O->outfilename = "test.sph";
break;
case TYP_VOC:
O->outfilename = "test.voc";
break;
/* case TYP_IRCAM: */
/* O->outfilename = ""; */
/* break; */
/* case TYP_MAT4: */
/* O->outfilename = ""; */
/* break; */
/* case TYP_MAT5: */
/* O->outfilename = ""; */
/* break; */
/* case TYP_PVF: */
/* O->outfilename = ""; */
/* break; */
case TYP_XI:
O->outfilename = "test.xi";
break;
/* case TYP_HTK: */
/* O->outfilename = ""; */
/* break; */
/* case TYP_SDS: */
/* O->outfilename = "test.sds"; */
/* break; */
case TYP_AVR:
O->outfilename = "test.avr";
break;
case TYP_SD2:
O->outfilename = "test.sd2";
break;
case TYP_FLAC:
O->outfilename = "test.flac";
break;
case TYP_CAF:
O->outfilename = "test.caf";
break;
case TYP_OGG:
O->outfilename = "test.ogg";
break;
case TYP_MPEG:
O->outfilename = "test.mp3";
break;
/* case TYP_MPC2K: */
/* O->outfilename = ""; */
/* break; */
default:
O->outfilename = "test";
break;
}
}
STA(sfoutname) = fName = O->outfilename;
if (strcmp(fName, "stdout") == 0) {
STA(pipdevout) = 1;
}
#ifdef PIPES
else if (fName[0] == '|') {
STA(pout) = _popen(fName+1, "w");
osfd = fileno(STA(pout));
STA(pipdevout) = 1;
if (O->filetyp == TYP_AIFF || O->filetyp == TYP_WAV) {
char fmt_name[6];
if (O->sfsampsize == 8) {
strcpy(fmt_name, "AU");
O->filetyp = TYP_AU;
}
else {
strcpy(fmt_name, "IRCAM");
O->filetyp = TYP_IRCAM;
}
csound->Message(csound, Str("Output file type changed to %s "
"for use in pipe\n"), fmt_name);
}
}
#endif
else {
csRtAudioParams parm;
/* check for real time audio output, and get device name/number */
parm.devNum = check_rtaudio_name(fName, &(parm.devName), 1);
if (parm.devNum >= 0) {
/* set device parameters */
parm.bufSamp_SW = (unsigned int) O->outbufsamps / csound->nchnls;
parm.bufSamp_HW = O->oMaxLag;
parm.nChannels = csound->nchnls;
parm.sampleFormat = O->outformat;
parm.sampleRate = (float) csound->esr;
csound->spoutran = spoutsf;
/* open devaudio for output */
if (UNLIKELY(csound->playopen_callback(csound, &parm) != 0))
csoundDie(csound, Str("Failed to initialise real time audio output"));
/* & redirect audio puts */
csound->audtran = csound->rtplay_callback;
STA(outbufrem) = parm.bufSamp_SW * parm.nChannels;
STA(pipdevout) = 2; /* no backward seeks ! */
if (O->realtime == 1) /* set realtime priority mode */
csound->realtime_audio_flag = 1;
goto outset; /* no header needed */
}
else if (strcmp(fName, "null") == 0) {
STA(outfile) = NULL;
if (csound->dither_output && csound->oparms->outformat!=AE_FLOAT &&
csound->oparms->outformat!=AE_DOUBLE) {
if (csound->oparms->outformat==AE_SHORT)
if (csound->dither_output==1)
csound->audtran = writesf_dither_16;
else
csound->audtran = writesf_dither_u16;
else if (csound->oparms->outformat==AE_CHAR)
if (csound->dither_output==1)
csound->audtran = writesf_dither_8;
else
csound->audtran = writesf_dither_u8;
else
csound->audtran = writesf;
}
else
csound->audtran = writesf;
goto outset;
}
}
/* set format parameters */
memset(&sfinfo, 0, sizeof(SF_INFO));
//sfinfo.frames = 0;
sfinfo.samplerate = (int) MYFLT2LRND(csound->esr);
sfinfo.channels = csound->nchnls;
sfinfo.format = TYPE2SF(O->filetyp) | FORMAT2SF(O->outformat);
/* open file */
if (STA(pipdevout)) {
STA(outfile) = sf_open_fd(osfd, SFM_WRITE, &sfinfo, 0);
#ifdef PIPES
if (STA(outfile) == NULL) {
char fmt_name[6];
if (O->sfsampsize == 8) {
if (UNLIKELY(O->filetyp == TYP_AU))
csoundDie(csound, Str("sfinit: cannot open fd %d\n%s"), osfd,
Str(sf_strerror(NULL)));
strcpy(fmt_name, "AU");
O->filetyp = TYP_AU;
}
else {
if (UNLIKELY(O->filetyp == TYP_IRCAM))
csoundDie(csound, Str("sfinit: cannot open fd %d\n%s"), osfd,
Str(sf_strerror(NULL)));
strcpy(fmt_name, "IRCAM");
O->filetyp = TYP_IRCAM;
}
csound->Message(csound, Str("Output file type changed to %s "
"for use in pipe\n"), fmt_name);
sfinfo.format = TYPE2SF(O->filetyp) | FORMAT2SF(O->outformat);
STA(outfile) = sf_open_fd(osfd, SFM_WRITE, &sfinfo, 0);
}
#endif
if (UNLIKELY(STA(outfile) == NULL))
csoundDie(csound, Str("sfinit: cannot open fd %d\n%s"), osfd,
Str(sf_strerror(NULL)));
sf_command(STA(outfile), SFC_SET_VBR_ENCODING_QUALITY,
&O->quality, sizeof(double));
}
else {
fullName = csoundFindOutputFile(csound, fName, "SFDIR");
if (UNLIKELY(fullName == NULL))
csoundDie(csound, Str("sfinit: cannot open %s"), fName);
STA(sfoutname) = fullName;
STA(outfile) = sf_open(fullName, SFM_WRITE, &sfinfo);
if (UNLIKELY(STA(outfile) == NULL))
csoundDie(csound, Str("sfinit: cannot open %s\n%s"),
fullName, sf_strerror (NULL));
sf_command(STA(outfile), SFC_SET_VBR_ENCODING_QUALITY,
&O->quality, sizeof(double));
/* only notify the host if we opened a real file, not stdout or a pipe */
csoundNotifyFileOpened(csound, fullName,
type2csfiletype(O->filetyp, O->outformat), 1, 0);
}
#ifdef SNDFILE_MP3
// VL: setting bitrate to constant improves quality
if(O->filetyp == TYP_MPEG) {
csound->Message(csound, "Setting MP3 bitrate to %s\n", csound->mp3_mode ? "variable" : "constant" );;
sf_command(STA(outfile), SFC_SET_BITRATE_MODE,
&(csound->mp3_mode), sizeof(int));
}
#endif
/* IV - Feb 22 2005: clip integer formats */
if (O->outformat != AE_FLOAT && O->outformat != AE_DOUBLE)
sf_command(STA(outfile), SFC_SET_CLIPPING, NULL, SF_TRUE);
sf_command(STA(outfile), SFC_SET_ADD_PEAK_CHUNK,
NULL, (csound->peakchunks ? SF_TRUE : SF_FALSE));
#ifdef SOME_FINE_DAY
if (csound->dither_output) { /* This may not be written yet!! */
SF_DITHER_INFO ditherInfo;
memset(&ditherInfo, 0, sizeof(SF_DITHER_INFO));
ditherInfo.type = SFD_TRIANGULAR_PDF | SFD_DEFAULT_LEVEL;
ditherInfo.level = 1.0;
ditherInfo.name = (char*) NULL;
sf_command(STA(outfile), SFC_SET_DITHER_ON_WRITE,
&ditherInfo, sizeof(SF_DITHER_INFO));
}
#endif
if (!(O->outformat == AE_FLOAT || O->outformat == AE_DOUBLE) ||
(O->filetyp == TYP_WAV || O->filetyp == TYP_AIFF ||
O->filetyp == TYP_W64))
csound->spoutran = spoutsf; /* accumulate output */
else
csound->spoutran = spoutsf_noscale;
if (csound->dither_output && csound->oparms->outformat!=AE_FLOAT &&
csound->oparms->outformat!=AE_DOUBLE) {
if (csound->oparms->outformat==AE_SHORT)
csound->audtran = writesf_dither_16;
else if (csound->oparms->outformat==AE_CHAR)
csound->audtran = writesf_dither_8;
else
csound->audtran = writesf;
}
else
csound->audtran = writesf;
/* Write any tags. */
if ((s = csound->SF_id_title) != NULL && *s != '\0')
sf_set_string(STA(outfile), SF_STR_TITLE, s);
if ((s = csound->SF_csd_licence) == NULL || *s == '\0')
s = csound->SF_id_copyright;
if (s != NULL && *s != '\0')
sf_set_string(STA(outfile), SF_STR_COPYRIGHT, s);
else if (csound->SF_id_scopyright>=0) {
char buff[256];
time_t tt = time(NULL);
strftime(buff, 256, "Copyright %Y: ", gmtime(&tt));
strncat(buff,copyrightcode(csound->SF_id_scopyright), 255);
buff[255] = '\0';
sf_set_string(STA(outfile), SF_STR_COPYRIGHT, buff);
}
if ((s = csound->SF_id_software) != NULL && *s != '\0')
sf_set_string(STA(outfile), SF_STR_SOFTWARE, s);
if ((s = csound->SF_id_artist) != NULL && *s != '\0')
sf_set_string(STA(outfile), SF_STR_ARTIST, s);
if ((s = csound->SF_id_comment) != NULL && *s != '\0')
sf_set_string(STA(outfile), SF_STR_COMMENT, s);
if ((s = csound->SF_id_date) != NULL && *s != '\0')
sf_set_string(STA(outfile), SF_STR_DATE, s);
/* file is now open */
STA(osfopen) = 1;
outset:
O->sfsampsize = (int) sfsampsize(FORMAT2SF(O->outformat));
/* calc outbuf size & alloc bufspace */
STA(outbufsiz) = O->outbufsamps * sizeof(MYFLT);
STA(outbufp) = STA(outbuf) = csound->Malloc(csound, STA(outbufsiz));
if (STA(pipdevout) == 2) {
csound->Message(csound,
Str("writing %d sample blks of %lu-bit floats to %s\n"),
O->outbufsamps, (unsigned long) sizeof(MYFLT)*8,
STA(sfoutname));
}
else {
csound->Message(csound, Str("writing %d-byte blks of %s to %s"),
O->outbufsamps * O->sfsampsize,
getstrformat(O->outformat), STA(sfoutname));
if (O->sfheader == 0)
csound->Message(csound, Str(" (raw)\n"));
else
csound->Message(csound, " (%s)\n", type2string(O->filetyp));
}
STA(osfopen) = 1;
STA(outbufrem) = O->outbufsamps;
}
void sfclosein(CSOUND *csound)
{
alloc_globals(csound);
if (!STA(isfopen))
return;
if (STA(pipdevin) == 2 && (!STA(osfopen) || STA(pipdevout) != 2)) {
/* close only if not open for output too */
csound->rtclose_callback(csound);
}
else if (STA(pipdevin) != 2) {
if (STA(infile) != NULL)
sf_close(STA(infile));
#ifdef PIPES
if (STA(pin) != NULL) {
_pclose(STA(pin));
STA(pin) = NULL;
}
#endif
STA(infile) = NULL;
}
STA(isfopen) = 0;
}
void sfcloseout(CSOUND *csound)
{
OPARMS *O = csound->oparms;
int nb;
alloc_globals(csound);
if (!STA(osfopen))
return;
if ((nb = (O->outbufsamps - STA(outbufrem)) * sizeof(MYFLT)) > 0) {
/* flush outbuffer */
csound->nrecs++;
csound->audtran(csound, STA(outbuf), nb);
}
if (STA(pipdevout) == 2 && (!STA(isfopen) || STA(pipdevin) != 2)) {
/* close only if not open for input too */
csound->rtclose_callback(csound);
}
if (STA(pipdevout) == 2)
goto report;
if (STA(outfile) != NULL) {
if (!STA(pipdevout) && O->outformat != AE_VORBIS)
sf_command(STA(outfile), SFC_UPDATE_HEADER_NOW, NULL, 0);
sf_close(STA(outfile));
STA(outfile) = NULL;
}
#ifdef PIPES
if (STA(pout) != NULL) {
_pclose(STA(pout));
STA(pout) = NULL;
}
#endif
report:
if (STA(pipdevout) == 2) {
csound->Message(csound,
"%"PRIi32" %d %s%lu%s%s\n",
csound->nrecs, O->outbufsamps, Str("sample blks of "),
(unsigned long)sizeof(MYFLT)*8,Str("-bit floats written to "),
STA(sfoutname));
}
else {
csound->Message(csound, Str("%"PRIi32" %d sample blks of %s written to %s"),
O->outbufsamps, O->outbufsamps * O->sfsampsize,
getstrformat(O->outformat), STA(sfoutname));
if (O->sfheader == 0)
csound->Message(csound, Str(" (raw)\n"));
else
csound->Message(csound, " (%s)\n", type2string(O->filetyp));
}
STA(osfopen) = 0;
}
/* report soundfile write(osfd) error */
/* called after chk of write() bytecnt */
static void sndwrterr(CSOUND *csound, int nret, int nput)
{
csound->ErrorMsg(csound,
Str("soundfile write returned bytecount of %d, not %d"),
nret, nput);
csound->ErrorMsg(csound,
Str("(disk may be full...\n closing the file ...)"));
STA(outbufrem) = csound->oparms->outbufsamps; /* consider buf is flushed */
sfcloseout(csound); /* & try to close the file */
csound->Die(csound, Str("\t... closed\n"));
}
void sfnopenout(CSOUND *csound)
{
alloc_globals(csound);
csound->Message(csound, Str("not writing to sound disk\n"));
/* init counter, though not writing */
STA(outbufrem) = csound->oparms->outbufsamps;
}
static inline void sndfilein_(CSOUND *csound, MYFLT scaleFac)
{
OPARMS *O = csound->oparms;
int i, n, nsmps, bufpos;
nsmps = csound->nspin;
bufpos = (int) O->inbufsamps - (int) STA(inbufrem);
for (i = 0; i<nsmps; i++) {
if ((int) STA(inbufrem) < 1) {
STA(inbufrem) = 0U;
do {
n = ((int) O->inbufsamps - (int) STA(inbufrem)) * (int) sizeof(MYFLT);
n = csound->audrecv(csound, STA(inbuf) + (int) STA(inbufrem), n);
STA(inbufrem) += (unsigned int) (n / (int) sizeof(MYFLT));
} while ((int) STA(inbufrem) < (int) O->inbufsamps);
bufpos = 0;
}
csound->spin[i] = STA(inbuf)[bufpos++] * scaleFac;
STA(inbufrem)--;
}
}
static void sndfilein(CSOUND *csound)
{
sndfilein_(csound, csound->e0dbfs);
}
/* special version of sndfilein for "raw" floating point files */
static void sndfilein_noscale(CSOUND *csound)
{
sndfilein_(csound, FL(1.0));
}
static int audrecv_dummy(CSOUND *csound, MYFLT *buf, int nbytes)
{
(void) csound; (void) buf;
return nbytes;
}
static void audtran_dummy(CSOUND *csound, const MYFLT *buf, int nbytes)
{
(void) csound; (void) buf; (void) nbytes;
}
/* direct recv & tran calls to the right audio formatter */
/* & init its audio_io bufptr */
void iotranset(CSOUND *csound)
{
OPARMS *O;
csound->spinrecv = sndfilein;
csound->spoutran = spoutsf;
if (!csound->enableHostImplementedAudioIO)
return;
alloc_globals(csound);
O = csound->oparms;
csound->audrecv = audrecv_dummy;
csound->audtran = audtran_dummy;
STA(inbufrem) = (unsigned int) O->inbufsamps;
STA(outbufrem) = (unsigned int) O->outbufsamps;
if (!csound->hostRequestedBufferSize) {
O->sfread = 0;
O->sfwrite = 0;
STA(osfopen) = 0;
return;
}
STA(inbufsiz) = (unsigned int) (O->inbufsamps * (int) sizeof(MYFLT));
STA(inbuf) = (MYFLT*) csound->Calloc(csound, STA(inbufsiz));
STA(outbufsiz) = (unsigned int) (O->outbufsamps * (int) sizeof(MYFLT));
STA(outbuf) = (MYFLT*) csound->Calloc(csound, STA(outbufsiz));
STA(outbufp) = STA(outbuf);
O->sfread = 1;
O->sfwrite = 1;
STA(osfopen) = 1;
}
PUBLIC MYFLT *csoundGetInputBuffer(CSOUND *csound)
{
return STA(inbuf);
}
PUBLIC MYFLT *csoundGetOutputBuffer(CSOUND *csound)
{
return STA(outbuf);
}
|