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
|
/*
* April 15, 1992
* Copyright 1992 Rick Richardson
* Copyright 1991 Lance Norskog And Sundry Contributors
* This source code is freely redistributable and may be used for
* any purpose. This copyright notice must be maintained.
* Lance Norskog And Sundry Contributors are not responsible for
* the consequences of using this software.
*/
/*
* Windows 3.0 .wav format driver
*/
/*
* Fixed by various contributors:
* 1) Little-endian handling
* 2) Skip other kinds of file data
* 3) Handle 16-bit formats correctly
* 4) Not go into infinite loop
*/
/*
* User options should overide file
* header :- we assumed user knows what
* they are doing if they specify options.
* Enhancements and clean up
* by Graeme W. Gill, 93/5/17
*/
#include <string.h>
#include "st.h"
#include "wav.h"
extern void sciprint __PARAMS((char *fmt, ...));
/* Private data for .wav file */
typedef struct wavstuff
{
long samples;
int second_header; /* non-zero on second header write */
} *wav_t;
static char *wav_format_str(unsigned int wFormatTag);
/*
* Do anything required before you start reading samples.
* Read file header.
* Find out sampling rate,
* size and style of samples,
* mono/stereo/quad.
*/
void wavstartread(ft_t ft, WavInfo *Wi, int flag)
{
wav_t wav = (wav_t) ft->priv;
char magic[4];
unsigned len;
int littlendian = 1;
char *endptr;
/* wave file characteristics */
endptr = (char *) &littlendian;
if (!*endptr) ft->swap = 1;
/** sciprint("Read swap status %d\r\n",ft->swap); **/
/* If you need to seek around the input file. */
if (0 && ! ft->seekable)
{
sciprint("Sorry, .wav input file must be a file, not a pipe\r\n");
ft->ierr=1;return;
}
if ( fread(magic, 1, 4, ft->fp) != 4
|| strncmp("RIFF", magic, 4))
{
sciprint("Sorry, not a RIFF file\r\n");
ft->ierr=1;return;
}
len = rllong(ft);
if ( fread(magic, 1, 4, ft->fp) != 4
|| strncmp("WAVE", magic, 4))
{
sciprint("Sorry, not a WAVE file\r\n");
ft->ierr=1;return;
}
/* Now look for the format chunk */
for (;;)
{
if ( fread(magic, 1, 4, ft->fp) != 4 )
{
sciprint("Sorry, missing fmt spec\r\n");
ft->ierr=1;return;
}
len = rllong(ft);
if (strncmp("fmt ", magic, 4) == 0)
break; /* Found the format chunk */
while (len > 0 && !feof(ft->fp)) /* skip to next chunk */
{
getc(ft->fp);
len--;
}
}
if ( len < 16 )
{
sciprint("Sorry, fmt chunk is too short\r\n");
ft->ierr=1;return;
}
Wi->wFormatTag = rlshort(ft);
switch ( Wi->wFormatTag)
{
case WAVE_FORMAT_UNKNOWN:
{
sciprint("Sorry, this WAV file is in Microsoft Official Unknown format.\r\n");
ft->ierr=1;return;
}
case WAVE_FORMAT_PCM_SCI: /* this one, at least, I can handle */
if (ft->info.style != -1 && ft->info.style != UNSIGNED && ft->info.style != SIGN2)
sciprint("User options overiding style read in .wav header\r\n");
break;
case WAVE_FORMAT_ADPCM:
{
sciprint("Sorry, this WAV file is in Microsoft ADPCM format.\r\n");
ft->ierr=1;return;
}
case WAVE_FORMAT_ALAW: /* Think I can handle this */
if (ft->info.style == -1 || ft->info.style == ALAW)
ft->info.style = ALAW;
else
sciprint("User options overiding style read in .wav header\r\n");
break;
case WAVE_FORMAT_MULAW: /* Think I can handle this */
if (ft->info.style == -1 || ft->info.style == ULAW)
ft->info.style = ULAW;
else
sciprint("User options overiding style read in .wav header\r\n");
break;
case WAVE_FORMAT_OKI_ADPCM:
{
sciprint("Sorry, this WAV file is in OKI ADPCM format.\r\n");
ft->ierr=1;return;
}
case WAVE_FORMAT_DIGISTD:
{
sciprint("Sorry, this WAV file is in Digistd format.\r\n");
ft->ierr=1;return;
}
case WAVE_FORMAT_DIGIFIX:
{
sciprint("Sorry, this WAV file is in Digifix format.\r\n");
ft->ierr=1;return;
}
case IBM_FORMAT_MULAW:
{
sciprint("Sorry, this WAV file is in IBM U-law format.\r\n");
ft->ierr=1;return;
}
case IBM_FORMAT_ALAW:
{
sciprint("Sorry, this WAV file is in IBM A-law format.\r\n");
ft->ierr=1;return;
}
case IBM_FORMAT_ADPCM:
{
sciprint("Sorry, this WAV file is in IBM ADPCM format.\r\n");
ft->ierr=1;return;
}
default:
{
sciprint("Sorry, don't understand format\r\n");
ft->ierr=1;return;
}
}
Wi->wChannels = rlshort(ft);
/* User options take precedence */
if (ft->info.channels == -1 || ft->info.channels == Wi->wChannels)
ft->info.channels = Wi->wChannels;
else
sciprint("User options overiding channels read in .wav header\r\n");
Wi->wSamplesPerSecond = rllong(ft);
if (ft->info.rate == (unsigned int)0 || ft->info.rate == Wi->wSamplesPerSecond)
ft->info.rate = Wi->wSamplesPerSecond;
else
sciprint("User options overiding rate read in .wav header\r\n");
Wi->wAvgBytesPerSec = rllong(ft); /* Average bytes/second */
Wi->wBlockAlign = rlshort(ft); /* Block align */
Wi->wBitsPerSample = rlshort(ft); /* bits per sample per channel */
Wi->bytespersample = (Wi->wBitsPerSample + 7)/8;
switch (Wi->bytespersample)
{
case 1:
/* User options take precedence */
if (ft->info.size == -1 || ft->info.size == BYTESCI)
ft->info.size = BYTESCI;
else
sciprint("User options overiding size read in .wav header\r\n");
if (ft->info.style == -1 || ft->info.style == UNSIGNED)
ft->info.style = UNSIGNED;
else if (ft->info.style != ALAW && ft->info.style != ULAW)
sciprint("User options overiding style read in .wav header\r\n");
break;
case 2:
if (ft->info.size == -1 || ft->info.size == WORDSCI)
ft->info.size = WORDSCI;
else
sciprint("User options overiding size read in .wav header\r\n");
if (ft->info.style == -1 || ft->info.style == SIGN2)
ft->info.style = SIGN2;
else
sciprint("User options overiding style read in .wav header\r\n");
break;
case 4:
if (ft->info.size == -1 || ft->info.size == LONGSCI)
ft->info.size = LONGSCI;
else
sciprint("User options overiding size read in .wav header\r\n");
if (ft->info.style == -1 || ft->info.style == SIGN2)
ft->info.style = SIGN2;
else
sciprint("User options overiding style read in .wav header\r\n");
break;
default:
{
sciprint("Sorry, don't understand .wav size\r\n");
ft->ierr=1;return;
}
}
len -= 16;
while (len > 0 && !feof(ft->fp))
{
getc(ft->fp);
len--;
}
/* Now look for the wave data chunk */
for (;;)
{
if ( fread(magic, 1, 4, ft->fp) != 4 )
{
sciprint("Sorry, missing data chunk\r\n");
ft->ierr=1;return;
}
len = rllong(ft);
if (strncmp("data", magic, 4) == 0)
break; /* Found the data chunk */
while (len > 0 && !feof(ft->fp)) /* skip to next chunk */
{
getc(ft->fp);
len--;
}
}
Wi->data_length = len;
wav->samples = Wi->data_length/ft->info.size; /* total samples */
if ( flag == 1 )
{
sciprint("Reading Wave file: %s format, %d channel%s, %d samp/sec\r\n",
wav_format_str(Wi->wFormatTag), Wi->wChannels,
Wi->wChannels == 1 ? "" : "s", Wi->wSamplesPerSecond);
sciprint(" %d byte/sec, %d block align, %d bits/samp, %u data bytes\r\n",
Wi->wAvgBytesPerSec, Wi->wBlockAlign,Wi->wBitsPerSample, Wi->data_length);
}
Wi->wav_format = wav_format_str(Wi->wFormatTag);
}
/*
* Read up to len samples from file.
* Convert to signed longs.
* Place in buf[].
* Return number of samples read.
*/
#if defined(__alpha)|defined(__ia64__)
int wavread(ft_t ft,int *buf, long int len)
#else
int wavread(ft_t ft, long int *buf, long int len)
#endif
{
wav_t wav = (wav_t) ft->priv;
int done;
if (len > wav->samples) len = wav->samples;
if (len == 0)
return 0;
done = rawread(ft, buf, len);
if (done == 0)
sciprint("Premature EOF on .wav input file\r\n");
wav->samples -= done;
return done;
}
/*
* Do anything required when you stop reading samples.
* Don't close input file!
*/
void wavstopread(ft_t ft)
{
}
void wavstartwrite(ft_t ft)
{
wav_t wav = (wav_t) ft->priv;
int littlendian = 1;
char *endptr;
endptr = (char *) &littlendian;
if (!*endptr) ft->swap = 1;
wav->samples = 0;
wav->second_header = 0;
if (! ft->seekable)
sciprint("Length in output .wav header will wrong since can't seek to fix it\r\n");
wavwritehdr(ft);
}
void wavwritehdr(ft_t ft)
{
wav_t wav = (wav_t) ft->priv;
/* wave file characteristics */
unsigned short wFormatTag; /* data format */
unsigned short wChannels; /* number of channels */
unsigned long wSamplesPerSecond; /* samples per second per channel */
unsigned long wAvgBytesPerSec; /* estimate of bytes per second needed */
unsigned short wBlockAlign; /* byte alignment of a basic sample block */
unsigned short wBitsPerSample; /* bits per sample */
unsigned long data_length; /* length of sound data in bytes */
unsigned long bytespersample; /* bytes per sample (per channel) */
switch (ft->info.size)
{
case BYTESCI:
wBitsPerSample = 8;
if (ft->info.style == -1 || ft->info.style == UNSIGNED)
ft->info.style = UNSIGNED;
else if (!wav->second_header && ft->info.style != ALAW && ft->info.style != ULAW)
sciprint("User options overiding style written to .wav header\r\n");
break;
case WORDSCI:
wBitsPerSample = 16;
if (ft->info.style == -1 || ft->info.style == SIGN2)
ft->info.style = SIGN2;
else if (!wav->second_header)
sciprint("User options overiding style written to .wav header\r\n");
break;
case LONGSCI:
wBitsPerSample = 32;
if (ft->info.style == -1 || ft->info.style == SIGN2)
ft->info.style = SIGN2;
else if (!wav->second_header)
sciprint("User options overiding style written to .wav header\r\n");
break;
default:
wBitsPerSample = 32;
if (ft->info.style == -1)
ft->info.style = SIGN2;
if (!wav->second_header)
sciprint("Sciprinting - writing bad .wav file using %s\r\n",sizes[ft->info.size]);
break;
}
switch (ft->info.style)
{
case UNSIGNED:
wFormatTag = WAVE_FORMAT_PCM_SCI;
if (wBitsPerSample != 8 && !wav->second_header)
sciprint("Sciprinting - writing bad .wav file using unsigned data and %d bits/sample\r\n",wBitsPerSample);
break;
case SIGN2:
wFormatTag = WAVE_FORMAT_PCM_SCI;
if (wBitsPerSample == 8 && !wav->second_header)
sciprint("Sciprinting - writing bad .wav file using signed data and %d bits/sample\r\n",wBitsPerSample);
break;
case ALAW:
wFormatTag = WAVE_FORMAT_ALAW;
if (wBitsPerSample != 8 && !wav->second_header)
sciprint("Sciprinting - writing bad .wav file using A-law data and %d bits/sample\r\n",wBitsPerSample);
break;
case ULAW:
wFormatTag = WAVE_FORMAT_MULAW;
if (wBitsPerSample != 8 && !wav->second_header)
sciprint("Sciprinting - writing bad .wav file using U-law data and %d bits/sample\r\n",wBitsPerSample);
break;
}
wSamplesPerSecond = ft->info.rate;
bytespersample = (wBitsPerSample + 7)/8;
wAvgBytesPerSec = ft->info.rate * ft->info.channels * bytespersample;
wChannels = ft->info.channels;
wBlockAlign = (unsigned short) (ft->info.channels * bytespersample);
if (!wav->second_header) /* use max length value first time */
data_length = 0x7fffffff - (8+16+12);
else /* fixup with real length */
data_length = bytespersample * wav->samples;
/* figured out header info, so write it */
fputs("RIFF", ft->fp);
wllong(ft, data_length + 8+16+12); /* Waveform chunk size: FIXUP(4) */
fputs("WAVE", ft->fp);
fputs("fmt ", ft->fp);
wllong(ft, (long)16); /* fmt chunk size */
wlshort(ft, wFormatTag);
wlshort(ft, wChannels);
wllong(ft, wSamplesPerSecond);
wllong(ft, wAvgBytesPerSec);
wlshort(ft, wBlockAlign);
wlshort(ft, wBitsPerSample);
fputs("data", ft->fp);
wllong(ft, data_length); /* data chunk size: FIXUP(40) */
if (!wav->second_header)
{
sciprint("Writing Wave file: %s format, %d channel%s, %d samp/sec\r\n",
wav_format_str(wFormatTag), wChannels,
wChannels == 1 ? "" : "s", wSamplesPerSecond);
sciprint(" %d byte/sec, %d block align, %d bits/samp\r\n",
wAvgBytesPerSec, wBlockAlign, wBitsPerSample);
}
else
sciprint("Finished writing Wave file, %u data bytes\r\n",data_length);
}
void wavwrite(ft_t ft, long int *buf, long int len)
{
wav_t wav = (wav_t) ft->priv;
wav->samples += len;
rawwrite(ft, buf, len);
}
void wavstopwrite(ft_t ft)
{
/* All samples are already written out. */
/* If file header needs fixing up, for example it needs the */
/* the number of samples in a field, seek back and write them here. */
if (!ft->seekable)
return;
if (fseek(ft->fp, 0L, 0) != 0)
{
sciprint("Sorry, can't rewind output file to rewrite .wav header.\r\n");
ft->ierr=1;return;
}
((wav_t) ft->priv)->second_header = 1;
wavwritehdr(ft);
}
/*
* Return a string corresponding to the wave format type.
*/
static char *
wav_format_str(unsigned int wFormatTag)
{
switch (wFormatTag)
{
case WAVE_FORMAT_UNKNOWN:
return "Microsoft Official Unknown";
case WAVE_FORMAT_PCM_SCI:
return "Microsoft PCM";
case WAVE_FORMAT_ADPCM:
return "Microsoft ADPCM";
case WAVE_FORMAT_ALAW:
return "Microsoft A-law";
case WAVE_FORMAT_MULAW:
return "Microsoft U-law";
case WAVE_FORMAT_OKI_ADPCM:
return "OKI ADPCM format.";
case WAVE_FORMAT_DIGISTD:
return "Digistd format.";
case WAVE_FORMAT_DIGIFIX:
return "Digifix format.";
case IBM_FORMAT_MULAW:
return "IBM U-law format.";
case IBM_FORMAT_ALAW:
return "IBM A-law";
case IBM_FORMAT_ADPCM:
return "IBM ADPCM";
default:
return "Unknown";
}
}
|