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
|
/*
Audio File Library
Copyright (C) 2004, Michael Pruett <michael@68k.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307 USA.
*/
/*
IFF.cpp
This file contains routines for reading and writing IFF/8SVX
sound files.
*/
#include "config.h"
#include "IFF.h"
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "File.h"
#include "Marker.h"
#include "Setup.h"
#include "Tag.h"
#include "Track.h"
#include "afinternal.h"
#include "audiofile.h"
#include "byteorder.h"
#include "util.h"
static _AFfilesetup iff_default_filesetup =
{
_AF_VALID_FILESETUP, /* valid */
AF_FILE_IFF_8SVX, /* fileFormat */
true, /* trackSet */
true, /* instrumentSet */
true, /* miscellaneousSet */
1, /* trackCount */
NULL, /* tracks */
0, /* instrumentCount */
NULL, /* instruments */
0, /* miscellaneousCount */
NULL /* miscellaneous */
};
bool IFFFile::recognize(File *fh)
{
uint8_t buffer[8];
fh->seek(0, File::SeekFromBeginning);
if (fh->read(buffer, 8) != 8 || memcmp(buffer, "FORM", 4) != 0)
return false;
if (fh->read(buffer, 4) != 4 || memcmp(buffer, "8SVX", 4) != 0)
return false;
return true;
}
IFFFile::IFFFile()
{
setFormatByteOrder(AF_BYTEORDER_BIGENDIAN);
miscellaneousPosition = 0;
VHDR_offset = 0;
BODY_offset = 0;
}
/*
Parse miscellaneous data chunks such as name, author, copyright,
and annotation chunks.
*/
status IFFFile::parseMiscellaneous(const Tag &type, size_t size)
{
int misctype = AF_MISC_UNRECOGNIZED;
assert(type == "NAME" || type == "AUTH" ||
type == "(c) " || type == "ANNO");
/* Skip zero-length miscellaneous chunks. */
if (size == 0)
return AF_FAIL;
miscellaneousCount++;
miscellaneous = (Miscellaneous *) _af_realloc(miscellaneous,
miscellaneousCount * sizeof (Miscellaneous));
if (type == "NAME")
misctype = AF_MISC_NAME;
else if (type == "AUTH")
misctype = AF_MISC_AUTH;
else if (type == "(c) ")
misctype = AF_MISC_COPY;
else if (type == "ANNO")
misctype = AF_MISC_ANNO;
miscellaneous[miscellaneousCount - 1].id = miscellaneousCount;
miscellaneous[miscellaneousCount - 1].type = misctype;
miscellaneous[miscellaneousCount - 1].size = size;
miscellaneous[miscellaneousCount - 1].position = 0;
miscellaneous[miscellaneousCount - 1].buffer = _af_malloc(size);
fh->read(miscellaneous[miscellaneousCount - 1].buffer, size);
return AF_SUCCEED;
}
/*
Parse voice header chunk.
*/
status IFFFile::parseVHDR(const Tag &type, size_t size)
{
assert(type == "VHDR");
Track *track = getTrack();
uint32_t oneShotSamples, repeatSamples, samplesPerRepeat;
uint16_t sampleRate;
uint8_t octaves, compression;
uint32_t volume;
readU32(&oneShotSamples);
readU32(&repeatSamples);
readU32(&samplesPerRepeat);
readU16(&sampleRate);
readU8(&octaves);
readU8(&compression);
readU32(&volume);
track->f.sampleWidth = 8;
track->f.sampleRate = sampleRate;
track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
track->f.compressionType = AF_COMPRESSION_NONE;
track->f.byteOrder = AF_BYTEORDER_BIGENDIAN;
track->f.channelCount = 1;
_af_set_sample_format(&track->f, track->f.sampleFormat, track->f.sampleWidth);
return AF_SUCCEED;
}
status IFFFile::parseBODY(const Tag &type, size_t size)
{
Track *track = getTrack();
/*
IFF/8SVX files have only one audio channel with one
byte per sample, so the number of frames is equal to
the number of bytes.
*/
track->totalfframes = size;
track->data_size = size;
/* Sound data follows. */
track->fpos_first_frame = fh->tell();
return AF_SUCCEED;
}
status IFFFile::readInit(AFfilesetup setup)
{
fh->seek(0, File::SeekFromBeginning);
Tag type;
uint32_t size;
Tag formtype;
readTag(&type);
readU32(&size);
readTag(&formtype);
if (type != "FORM" || formtype != "8SVX")
return AF_FAIL;
/* IFF/8SVX files have only one track. */
Track *track = allocateTrack();
/* Set the index to include the form type ('8SVX' in this case). */
size_t index = 4;
while (index < size)
{
Tag chunkid;
uint32_t chunksize = 0;
status result = AF_SUCCEED;
readTag(&chunkid);
readU32(&chunksize);
if (chunkid == "VHDR")
{
result = parseVHDR(chunkid, chunksize);
}
else if (chunkid == "BODY")
{
result = parseBODY(chunkid, chunksize);
}
else if (chunkid == "NAME" ||
chunkid == "AUTH" ||
chunkid == "(c) " ||
chunkid == "ANNO")
{
parseMiscellaneous(chunkid, chunksize);
}
if (result == AF_FAIL)
return AF_FAIL;
/*
Increment the index by the size of the chunk
plus the size of the chunk header.
*/
index += chunksize + 8;
/* All chunks must be aligned on an even number of bytes. */
if ((index % 2) != 0)
index++;
/* Set the seek position to the beginning of the next chunk. */
fh->seek(index + 8, File::SeekFromBeginning);
}
/* The file has been successfully parsed. */
return AF_SUCCEED;
}
AFfilesetup IFFFile::completeSetup(AFfilesetup setup)
{
if (setup->trackSet && setup->trackCount != 1)
{
_af_error(AF_BAD_NUMTRACKS, "IFF/8SVX file must have 1 track");
return AF_NULL_FILESETUP;
}
TrackSetup *track = &setup->tracks[0];
if (track->sampleFormatSet &&
track->f.sampleFormat != AF_SAMPFMT_TWOSCOMP)
{
_af_error(AF_BAD_SAMPFMT,
"IFF/8SVX format supports only two's complement integer data");
return AF_NULL_FILESETUP;
}
if (track->sampleFormatSet && track->f.sampleWidth != 8)
{
_af_error(AF_BAD_WIDTH,
"IFF/8SVX file allows only 8 bits per sample "
"(%d bits requested)", track->f.sampleWidth);
return AF_NULL_FILESETUP;
}
if (track->channelCountSet && track->f.channelCount != 1)
{
_af_error(AF_BAD_CHANNELS,
"invalid channel count (%d) for IFF/8SVX format "
"(only 1 channel supported)",
track->f.channelCount);
return AF_NULL_FILESETUP;
}
if (track->f.compressionType != AF_COMPRESSION_NONE)
{
_af_error(AF_BAD_COMPRESSION,
"IFF/8SVX does not support compression");
return AF_NULL_FILESETUP;
}
/* Ignore requested byte order since samples are only one byte. */
track->f.byteOrder = AF_BYTEORDER_BIGENDIAN;
/* Either one channel was requested or no request was made. */
track->f.channelCount = 1;
_af_set_sample_format(&track->f, AF_SAMPFMT_TWOSCOMP, 8);
if (track->markersSet && track->markerCount != 0)
{
_af_error(AF_BAD_NUMMARKS,
"IFF/8SVX format does not support markers");
return AF_NULL_FILESETUP;
}
if (setup->instrumentSet && setup->instrumentCount != 0)
{
_af_error(AF_BAD_NUMINSTS,
"IFF/8SVX format does not support instruments");
return AF_NULL_FILESETUP;
}
return _af_filesetup_copy(setup, &iff_default_filesetup, true);
}
status IFFFile::writeInit(AFfilesetup setup)
{
uint32_t fileSize = 0;
if (_af_filesetup_make_handle(setup, this) == AF_FAIL)
return AF_FAIL;
fh->write("FORM", 4);
writeU32(&fileSize);
fh->write("8SVX", 4);
writeVHDR();
writeMiscellaneous();
writeBODY();
return AF_SUCCEED;
}
status IFFFile::update()
{
uint32_t length;
writeVHDR();
writeMiscellaneous();
writeBODY();
/* Get the length of the file. */
length = fh->length();
length -= 8;
/* Set the length of the FORM chunk. */
fh->seek(4, File::SeekFromBeginning);
writeU32(&length);
return AF_SUCCEED;
}
status IFFFile::writeVHDR()
{
uint32_t chunkSize;
uint32_t oneShotSamples, repeatSamples, samplesPerRepeat;
uint16_t sampleRate;
uint8_t octaves, compression;
uint32_t volume;
/*
If VHDR_offset hasn't been set yet, set it to the
current offset.
*/
if (VHDR_offset == 0)
VHDR_offset = fh->tell();
else
fh->seek(VHDR_offset, File::SeekFromBeginning);
Track *track = getTrack();
fh->write("VHDR", 4);
chunkSize = 20;
writeU32(&chunkSize);
/*
IFF/8SVX files have only one audio channel, so the
number of samples is equal to the number of frames.
*/
oneShotSamples = track->totalfframes;
writeU32(&oneShotSamples);
repeatSamples = 0;
writeU32(&repeatSamples);
samplesPerRepeat = 0;
writeU32(&samplesPerRepeat);
sampleRate = track->f.sampleRate;
writeU16(&sampleRate);
octaves = 0;
compression = 0;
writeU8(&octaves);
writeU8(&compression);
/* Volume is in fixed-point notation; 65536 means gain of 1.0. */
volume = 65536;
writeU32(&volume);
return AF_SUCCEED;
}
status IFFFile::writeBODY()
{
uint32_t chunkSize;
Track *track = getTrack();
if (BODY_offset == 0)
BODY_offset = fh->tell();
else
fh->seek(BODY_offset, File::SeekFromBeginning);
fh->write("BODY", 4);
/*
IFF/8SVX supports only one channel, so the number of
frames is equal to the number of samples, and each
sample is one byte.
*/
chunkSize = track->totalfframes;
writeU32(&chunkSize);
if (track->fpos_first_frame == 0)
track->fpos_first_frame = fh->tell();
/* Add a pad byte to the end of the chunk if the chunk size is odd. */
if ((chunkSize % 2) == 1)
{
uint8_t zero = 0;
fh->seek(BODY_offset + 8 + chunkSize, File::SeekFromBeginning);
writeU8(&zero);
}
return AF_SUCCEED;
}
/*
WriteMiscellaneous writes all the miscellaneous data chunks in a
file handle structure to an IFF/8SVX file.
*/
status IFFFile::writeMiscellaneous()
{
if (miscellaneousPosition == 0)
miscellaneousPosition = fh->tell();
else
fh->seek(miscellaneousPosition, File::SeekFromBeginning);
for (int i=0; i<miscellaneousCount; i++)
{
Miscellaneous *misc = &miscellaneous[i];
Tag chunkType;
uint32_t chunkSize;
uint8_t padByte = 0;
switch (misc->type)
{
case AF_MISC_NAME:
chunkType = "NAME"; break;
case AF_MISC_AUTH:
chunkType = "AUTH"; break;
case AF_MISC_COPY:
chunkType = "(c) "; break;
case AF_MISC_ANNO:
chunkType = "ANNO"; break;
}
writeTag(&chunkType);
chunkSize = misc->size;
writeU32(&chunkSize);
/*
Write the miscellaneous buffer and then a pad byte
if necessary. If the buffer is null, skip the space
for now.
*/
if (misc->buffer != NULL)
fh->write(misc->buffer, misc->size);
else
fh->seek(misc->size, File::SeekFromCurrent);
if (misc->size % 2 != 0)
writeU8(&padByte);
}
return AF_SUCCEED;
}
|