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
|
/* sndheader.c -- nice new empty */
/* functions to write or delete
references from nyqsrc/sndread.c/snd_make_read()
snd_seek()
snd_bytes_per_frame()
cvt_from_8
cvt_from_16
cvt_from_24
cvt_from_32
cvt_from_unknown
references from nyqsrc/sndwritepa.c/find_cvt_to_fn
cvt_from_8
cvt_from_16
cvt_from_24
cvt_from_32
cvt_from_unknown
snd/audiooss.o: In function `audio_poll':
snd_bytes_per_frame()
snd/snd.o: In function `file_close':
write_sndheader_finish()
snd/snd.o: In function `snd_read':
snd_bytes_per_frame()
snd/snd.o: In function `snd_write':
snd_bytes_per_frame()
snd/snd.o:
snd_open_file() in the file_dictionary snd_fns_node.
*/
/* sndheader.c -- low-level sound I/O
*
*
* Judy Hawkins
* Feb 2008
*
* Rewritten to use libsndfile 1.0.17.
*
* Based on sndheader.c from Nyquist 3.01, which had
* a long history courtesy of Jim Zelenka, CMU/ITC, 9 Jun 1992
* and Roger Dannenberg, CMU, Mar 1993
*
*
*/
/* Standard includes */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "snd.h" /* probably going to modify this one */
/* jlh leaving a whole bunch of includes out; as needed I'll put them
back in. */
#ifdef LINUX
# ifdef WIN32
# error LINUX and WIN32 both?
# endif
#endif
/* jlh leaving out a bunch of Macintosh specific stuff on the theory
that libsndfile does all that now. */
/* jlh leaving out some AIFF marker and instrument structure
definitions on the theory that if anyone is using them I'll figure
out how to use libsndfile to access them, and write structures as
needed to support libsndfile. */
/* jlh leaving out convert functions; libsndfile handles all that. */
/* jlh leaving out a bunch of little bitty file and data handling
support functions; libsndfile... */
/* Local buffer for constructing useful messages */
#define STRBUFF_LEN 1024
static char strBuffer [STRBUFF_LEN] ;
/* =====
* lifted verbatim from sndheader.c v301
*/
void snd_open_fail(snd_type snd)
{
/* char msg[250]; */
snd->device = SND_DEVICE_NONE;
snd->dictionary = &snd_none_dictionary;
/* It's not fatal if the file does not exist...
sprintf(msg, "snd_open: could not open file %s\n",
snd->u.file.filename);
snd_fail(msg); */
return;
}
/*=====================
** Convert libsndfile's minor format to nyquist's SND_MODE specifier.
*/
int get_ny_mode (SF_INFO *sfinfo)
{
int mode;
mode = 0;
switch (sfinfo->format & SF_FORMAT_SUBMASK )
{
case SF_FORMAT_IMA_ADPCM:
case SF_FORMAT_MS_ADPCM:
case SF_FORMAT_VOX_ADPCM:
case SF_FORMAT_G721_32:
case SF_FORMAT_G723_24:
case SF_FORMAT_G723_40:
mode = SND_MODE_ADPCM;
break;
case SF_FORMAT_RAW:
case SF_FORMAT_PCM_S8:
case SF_FORMAT_PCM_16:
case SF_FORMAT_PCM_24:
case SF_FORMAT_PCM_32:
case SF_FORMAT_DPCM_8: /* jlh or are these two the UPCM, see below? */
case SF_FORMAT_DPCM_16:
mode = SND_MODE_PCM;
break;
case SF_FORMAT_ULAW:
mode = SND_MODE_ULAW;
break;
case SF_FORMAT_ALAW:
mode = SND_MODE_ALAW;
break;
case SF_FORMAT_FLOAT:
mode = SND_MODE_FLOAT;
break;
/* jlh libsndfile doesn't do UPCM? or is that the same as DPCM,
see above? And what about the special case in the
sndheader.c-orig, about how 8 bit PCM WAV files are actually
UPCM? JLH */
case SF_FORMAT_PCM_U8: /* jlh looks more like UPCM than anything else */
mode = SND_MODE_UPCM;
break;
default:
#ifdef DEBUG_LSF
printf(" at the default case in get_ny_mode -- check mask...\n");
#endif
mode = SND_MODE_UNKNOWN;
break;
}
return mode;
}
/*==============
* Convert the libsndfile extension to what nyquist needs.
*/
int get_ny_head ( SF_INFO *sfinfo )
{
int head;
head = 0;
/* this would already have been put in by snd_open_fail if the file
open failed: SND_HEAD_NONE */
switch (sfinfo->format & SF_FORMAT_TYPEMASK)
{
case SF_FORMAT_AIFF:
head = SND_HEAD_AIFF;
break;
case SF_FORMAT_IRCAM:
head = SND_HEAD_IRCAM;
break;
case SF_FORMAT_AU:
head = SND_HEAD_NEXT;
break;
case SF_FORMAT_WAV:
head = SND_HEAD_WAVE;
break;
default:
head = SND_HEAD_NONE;
break;
}
return head;
}
int get_ny_bits(SF_INFO *sfinfo)
{
int bits;
bits = 0;
/* does libsdnfile give me any help with this? making a lot of
guesses I'll have to verify later... jlh*/
switch (sfinfo->format & SF_FORMAT_SUBMASK )
{
/* 8 bit */
case SF_FORMAT_PCM_S8:
case SF_FORMAT_DPCM_8:
case SF_FORMAT_PCM_U8:
case SF_FORMAT_RAW:
bits = 8;
break;
/* 16 bit */
case SF_FORMAT_IMA_ADPCM:
case SF_FORMAT_MS_ADPCM:
case SF_FORMAT_VOX_ADPCM:
case SF_FORMAT_PCM_16:
case SF_FORMAT_DPCM_16:
bits = 16;
break;
/* 24 bit */
case SF_FORMAT_G723_24:
case SF_FORMAT_PCM_24:
bits = 24;
break;
/* 32 bit */
case SF_FORMAT_G721_32:
case SF_FORMAT_PCM_32:
bits = 32;
break;
/* 40 bit */
case SF_FORMAT_G723_40:
bits = 40;
break;
/* wild guess at 4 bytes = 32 bits */
case SF_FORMAT_ULAW:
case SF_FORMAT_ALAW:
case SF_FORMAT_FLOAT:
bits = 32;
break;
default:
#ifdef DEBUG_LSF
printf(" at the default case in get_ny_bits -- check mask...\n");
#endif
bits = 8;
break;
}
return bits;
}
/*==================
* Fill in the SF_INFO structure for opening a file for write or read/write.
*
* I am attempting to make this do double duty for SND_WRITE and
* SND_OVERWRITE. Wish me luck.
*/
int make_sfinfo(snd_type snd)
{
int format;
int subformat;
subformat = format = 0;
/* Translate nyquist/snd header, mode and bits into libsndfile values. */
switch(snd->u.file.header)
{
case SND_HEAD_AIFF:
format = SF_FORMAT_AIFF;
break;
case SND_HEAD_IRCAM:
format = SF_FORMAT_IRCAM;
break;
case SND_HEAD_NEXT:
format = SF_FORMAT_AU;
break;
case SND_HEAD_WAVE:
format = SF_FORMAT_WAV;
break;
default:
format = SF_FORMAT_WAV;
break;
}
switch(snd->format.mode)
{
case SND_MODE_ADPCM:
subformat = SF_FORMAT_IMA_ADPCM;
break;
case SND_MODE_PCM:
switch(snd->format.bits)
{
case 8:
subformat = SF_FORMAT_PCM_S8;
break;
case 16:
subformat = SF_FORMAT_PCM_16;
break;
case 24:
subformat = SF_FORMAT_PCM_24;
break;
case 32:
subformat = SF_FORMAT_PCM_32;
break;
default:
subformat = SF_FORMAT_PCM_16;
break;
}
break;
case SND_MODE_ULAW:
subformat = SF_FORMAT_ULAW;
break;
case SND_MODE_ALAW:
subformat = SF_FORMAT_ALAW;
break;
case SND_MODE_FLOAT:
subformat = SF_FORMAT_FLOAT;
break;
case SND_MODE_UPCM:
subformat = SF_FORMAT_PCM_U8;
break;
default:
/* I think for simplicity in this here not very limited first
cut, I'll go for a default of WAV, 16 bit PCM. I could get a
lot more complicated, but that can happen later. jlh.*/
format = SF_FORMAT_WAV;
subformat = SF_FORMAT_PCM_16;
break;
}
snd->u.file.sfinfo.format = format | subformat;
if (snd->format.srate > 0)
snd->u.file.sfinfo.samplerate = snd->format.srate;
else
snd->u.file.sfinfo.samplerate = 41000;
if (snd->format.channels > 0)
snd->u.file.sfinfo.channels = snd->format.channels;
else
snd->u.file.sfinfo.channels = 1;
snd->u.file.sfinfo.frames = 0;
return sf_format_check(&snd->u.file.sfinfo);
}
/*==============================================================
* The beating heart of this libsndfile project.
*
* Code from the original sndheader.c, v301, is mostly gone; I tried
* to keep error handling the same.
*
* Bits and pieces from libsndfile examples/
*/
int snd_open_file ( snd_type snd, long *flags)
{
SNDFILE *sffile;
(*flags) = 0; /* No file has successfully opened; at the end, when
all checks have passed, flags will get set to
values that say the file open worked and fields
have valid values. */
if (snd->write_flag == SND_READ)
{
snd->u.file.loop_info = FALSE;
/* jlh -- somewhere previous to here I have to make sure format
is 0, except if this is a RAW file, in which case I have to
set samplerate, channels and format fields. */
snd->u.file.sfinfo.format = 0;
/* if raw... */
if ((sffile = sf_open (snd->u.file.filename, SFM_READ, &snd->u.file.sfinfo)) == NULL)
{
printf ("Error : Not able to open input file %s.\n",
snd->u.file.filename) ;
fflush (stdout) ;
memset (strBuffer, 0, sizeof (strBuffer)) ;
sf_command (sffile, SFC_GET_LOG_INFO, strBuffer, STRBUFF_LEN) ;
puts (strBuffer) ;
puts (sf_strerror (NULL)) ;
snd_open_fail(snd);
return SND_FILE_FAILURE;
}
/* snd_read_header OUTTA DA WINDOW!!! (sound of splintering code
fragments) jlh */
/* jlh -- instead, I have put sfinfo into snd. Now I will make
sure it is a good happy little structure. */
if ((sf_format_check(&snd->u.file.sfinfo)) != TRUE)
{
printf ("Error : sfinfo not ok on file open: %s.\n",
snd->u.file.filename) ;
fflush (stdout) ;
memset (strBuffer, 0, sizeof (strBuffer)) ;
sf_command (sffile, SFC_GET_LOG_INFO, strBuffer, STRBUFF_LEN) ;
puts (strBuffer) ;
puts (sf_strerror (NULL)) ;
snd_open_fail(snd);
return SND_FILE_FAILURE;
}
/* YEAH. cooking with hydrogen. */
/* Fill out the nyquist format structure. */
snd->format.mode = get_ny_mode(&snd->u.file.sfinfo);
snd->format.channels = snd->u.file.sfinfo.channels;
snd->format.srate = snd->u.file.sfinfo.samplerate;
snd->format.bits = get_ny_bits(&snd->u.file.sfinfo);
/* nyquist header value, used in lisp code (I believe. jlh )*/
snd->u.file.header = get_ny_head(&snd->u.file.sfinfo);
} /* if (snd->write_flag == SND_READ) */
else if (snd->write_flag == SND_WRITE)
{
if (snd->u.file.header < 0 || snd->u.file.header >= SND_NUM_HEADS)
{
sprintf(strBuffer,
"snd_open_file: can't write file %s: invalid snd header value\n",
snd->u.file.filename);
snd_fail (strBuffer);
snd_open_fail(snd);
return !SND_SUCCESS;
}
/* Writing a new file: clear the libsndfile structures for action */
memset (&snd->u.file.sfinfo, 0, sizeof(snd->u.file.sfinfo));
snd->u.file.sffile = NULL;
if ( make_sfinfo(snd) == SF_FALSE )
{
sprintf(strBuffer,
"snd_open_file: Header information invalid for writing %s\n",
snd->u.file.filename);
snd_fail (strBuffer);
snd_open_fail(snd);
return !SND_SUCCESS;
}
/* Headers and info structures all filled out; now create the
file */
if ((sffile = sf_open (snd->u.file.filename,
SFM_WRITE,
&snd->u.file.sfinfo)) == NULL)
{
sprintf(strBuffer,
"snd_open_file: failed to open \"%s\" for write;\nLIBSNDFILE error: %s\n",
snd->u.file.filename,
sf_strerror(sffile));
snd_fail (strBuffer);
snd_open_fail(snd);
return !SND_SUCCESS;
}
snd->u.file.file = 0; /* Ok, this is going to make for noise,
but I do want to know when .file is
being accessed so I can fix it. jlh */
snd->u.file.sffile = sffile; /* prob redundant... but I may
get rid of file.file. jlh */
/* JLH gee... is this all I need to do? that was easy. */
} /* if snd->write_flag == SND_WRITE */
else if (snd->write_flag == SND_OVERWRITE)
{
if (snd->u.file.header < 0 || snd->u.file.header >= SND_NUM_HEADS)
{
sprintf(strBuffer,
"snd_open_file: can't write file %s: invalid snd header value\n",
snd->u.file.filename);
snd_fail (strBuffer);
snd_open_fail(snd);
return !SND_SUCCESS;
}
/* If sfinfo is valid, keep it; if not, make a new one. */
if (sf_format_check(&snd->u.file.sfinfo) == SF_FALSE)
{
memset (&snd->u.file.sfinfo, 0, sizeof(snd->u.file.sfinfo));
snd->u.file.sffile = NULL;
if ( make_sfinfo(snd) == SF_FALSE )
{
sprintf(strBuffer,
"snd_open_file: Header information invalid for writing %s\n",
snd->u.file.filename);
snd_fail (strBuffer);
snd_open_fail(snd);
return !SND_SUCCESS;
}
}
/* Headers and info structures all filled out; now create the
file */
if ((sffile = sf_open (snd->u.file.filename,
SFM_RDWR,
&snd->u.file.sfinfo)) == NULL)
{
sprintf(strBuffer,
"snd_open_file: failed to open \"%s\" for read/write;\nLIBSNDFILE error: %s\n",
snd->u.file.filename,
sf_strerror(sffile));
snd_fail (strBuffer);
snd_open_fail(snd);
return !SND_SUCCESS;
}
/* Don't need to find the beginning of the data, libsndfile
handles that. */
}
/* JLH I'm not doing anything with swap, I'm assuming libsndfile
handles all that. */
snd->u.file.file = 0; /* jlh and fix problems as they occur */
snd->u.file.sffile = sffile; /* prob redundant... but I may
get rid of file.file. jlh */
/* jlh The file must have opened successfully; make flags reflect that,
using old nyquist/snd values; lots of things use these??? */
(*flags) = SND_HEAD_SRATE | SND_HEAD_CHANNELS | SND_HEAD_BITS | SND_HEAD_MODE | SND_HEAD_LEN;
return SND_SUCCESS;
}
/*==================
* used once, in sndread.c
*
*/
/* FIX -- need to test this. I think offset is in seconds, so this
function is not going to be compatible with previous one. */
int snd_seek(snd_type snd, double offset)
{
sf_count_t frames;
sf_count_t tsk;
int whence;
frames = offset; /* Once again, the question of what units offset
is in.... I'm going to assume frames until
proven wrong. jlh */
whence = (int)snd->u.file.current_offset; /* jlh and what if.... */
tsk = sf_seek( snd->u.file.sffile, frames, whence);
if (tsk < 0)
{
return !SND_SUCCESS;
}
else
{
snd->u.file.current_offset = (long)tsk;
}
return SND_SUCCESS;
}
|