/* Parse marker chunks, which contain the positions and names of loop markers. */ static status ParseMARK (AFfilehandle file, AFvirtualfile *fh, u_int32_t type, size_t size) { _Track *track; int i; u_int16_t numMarkers; assert(!memcmp(&type, "MARK", 4)); track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK); af_fread(&numMarkers, sizeof (u_int16_t), 1, fh); numMarkers = BENDIAN_TO_HOST_INT16(numMarkers); track->markerCount = numMarkers; if (numMarkers) track->markers = _af_marker_new(numMarkers); for (i=0; i<numMarkers; i++) { u_int16_t markerID = 0; u_int32_t markerPosition = 0; u_int8_t sizeByte = 0; char *markerName = NULL; af_fread(&markerID, sizeof (u_int16_t), 1, fh); markerID = BENDIAN_TO_HOST_INT16(markerID); af_fread(&markerPosition, sizeof (u_int32_t), 1, fh); markerPosition = BENDIAN_TO_HOST_INT32(markerPosition); af_fread(&sizeByte, sizeof (unsigned char), 1, fh); markerName = _af_malloc(sizeByte + 1); af_fread(markerName, sizeof (unsigned char), sizeByte, fh); markerName[sizeByte] = '\0'; #ifdef DEBUG printf("marker id: %d, position: %d, name: %s\n", markerID, markerPosition, markerName); printf("size byte: %d\n", sizeByte); #endif /* If sizeByte is even, then 1+sizeByte (the length of the string) is odd. Skip an extra byte to make it even. */ if ((sizeByte % 2) == 0) af_fseek(fh, 1, SEEK_CUR); track->markers[i].id = markerID; track->markers[i].position = markerPosition; track->markers[i].name = markerName; track->markers[i].comment = _af_strdup(""); } return AF_SUCCEED; }
status af_read_uint16_be (uint16_t *value, AFvirtualfile *vf) { uint16_t v; if (af_fread(&v, sizeof (v), 1, vf) != 1) return AF_FAIL; *value = BENDIAN_TO_HOST_INT16(v); return AF_SUCCEED; }
/* Parse common data chunks, which contain information regarding the sampling rate, the number of sample frames, and the number of sound channels. */ static status ParseCOMM (AFfilehandle file, AFvirtualfile *fh, u_int32_t type, size_t size) { _Track *track; u_int16_t numChannels; u_int32_t numSampleFrames; u_int16_t sampleSize; unsigned char sampleRate[10]; assert(!memcmp(&type, "COMM", 4)); track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK); af_fread(&numChannels, sizeof (u_int16_t), 1, fh); track->f.channelCount = BENDIAN_TO_HOST_INT16(numChannels); af_fread(&numSampleFrames, sizeof (u_int32_t), 1, fh); track->totalfframes = BENDIAN_TO_HOST_INT32(numSampleFrames); af_fread(&sampleSize, sizeof (u_int16_t), 1, fh); track->f.sampleWidth = BENDIAN_TO_HOST_INT16(sampleSize); af_fread(sampleRate, 10, 1, fh); track->f.sampleRate = _af_convert_from_ieee_extended(sampleRate); track->f.compressionType = AF_COMPRESSION_NONE; track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP; track->f.byteOrder = AF_BYTEORDER_BIGENDIAN; if (file->fileFormat == AF_FILE_AIFFC) { u_int8_t compressionID[4]; /* Pascal strings are at most 255 bytes long. */ unsigned char compressionName[256]; unsigned char compressionNameLength; af_fread(compressionID, 4, 1, fh); /* Read the Pascal-style string containing the name. */ af_fread(&compressionNameLength, 1, 1, fh); af_fread(compressionName, compressionNameLength, 1, fh); compressionName[compressionNameLength] = '\0'; if (!memcmp(compressionID, "NONE", 4)) track->f.compressionType = AF_COMPRESSION_NONE; else if (!memcmp(compressionID, "ACE2", 4) || !memcmp(compressionID, "ACE8", 4) || !memcmp(compressionID, "MAC3", 4) || !memcmp(compressionID, "MAC6", 4)) { _af_error(AF_BAD_NOT_IMPLEMENTED, "AIFF-C format does not support Apple's proprietary %s compression format", compressionName); return AF_FAIL; } else if (!memcmp(compressionID, "ulaw", 4) || !memcmp(compressionID, "ULAW", 4)) { track->f.compressionType = AF_COMPRESSION_G711_ULAW; } else if (!memcmp(compressionID, "alaw", 4) || !memcmp(compressionID, "ALAW", 4)) { track->f.compressionType = AF_COMPRESSION_G711_ALAW; } else if (!memcmp(compressionID, "fl32", 4) || !memcmp(compressionID, "FL32", 4)) { track->f.sampleFormat = AF_SAMPFMT_FLOAT; track->f.sampleWidth = 32; track->f.compressionType = AF_COMPRESSION_NONE; } else if (!memcmp(compressionID, "fl64", 4) || !memcmp(compressionID, "FL64", 4)) { track->f.sampleFormat = AF_SAMPFMT_DOUBLE; track->f.sampleWidth = 64; track->f.compressionType = AF_COMPRESSION_NONE; } else if (!memcmp(compressionID, "sowt", 4)) { track->f.compressionType = AF_COMPRESSION_NONE; track->f.byteOrder = AF_BYTEORDER_LITTLEENDIAN; } else { _af_error(AF_BAD_NOT_IMPLEMENTED, "AIFF-C compression type '%c%c%c%c' not currently supported", compressionID[0], compressionID[1], compressionID[2], compressionID[3]); return AF_FAIL; } } _af_set_sample_format(&track->f, track->f.sampleFormat, track->f.sampleWidth); return AF_SUCCEED; }
/* Parse instrument chunks, which contain information about using sound data as a sampled instrument. */ static status ParseINST (AFfilehandle file, AFvirtualfile *fh, u_int32_t type, size_t size) { _Instrument *instrument; u_int8_t baseNote; int8_t detune; u_int8_t lowNote, highNote, lowVelocity, highVelocity; int16_t gain; u_int16_t sustainLoopPlayMode, sustainLoopBegin, sustainLoopEnd; u_int16_t releaseLoopPlayMode, releaseLoopBegin, releaseLoopEnd; assert(!memcmp(&type, "INST", 4)); instrument = _af_calloc(1, sizeof (_Instrument)); instrument->id = AF_DEFAULT_INST; instrument->values = _af_calloc(_AF_AIFF_NUM_INSTPARAMS, sizeof (AFPVu)); instrument->loopCount = 2; instrument->loops = _af_calloc(2, sizeof (_Loop)); file->instrumentCount = 1; file->instruments = instrument; af_fread(&baseNote, 1, 1, fh); af_fread(&detune, 1, 1, fh); af_fread(&lowNote, 1, 1, fh); af_fread(&highNote, 1, 1, fh); af_fread(&lowVelocity, 1, 1, fh); af_fread(&highVelocity, 1, 1, fh); af_fread(&gain, 2, 1, fh); gain = BENDIAN_TO_HOST_INT16(gain); #ifdef DEBUG printf("baseNote/detune/lowNote/highNote/lowVelocity/highVelocity/gain:" " %d %d %d %d %d %d %d\n", baseNote, detune, lowNote, highNote, lowVelocity, highVelocity, gain); #endif instrument->values[0].l = baseNote; instrument->values[1].l = detune; instrument->values[2].l = lowVelocity; instrument->values[3].l = highVelocity; instrument->values[4].l = lowNote; instrument->values[5].l = highNote; instrument->values[6].l = gain; instrument->values[7].l = 1; /* sustain loop id */ instrument->values[8].l = 2; /* release loop id */ af_fread(&sustainLoopPlayMode, sizeof (u_int16_t), 1, fh); sustainLoopPlayMode = BENDIAN_TO_HOST_INT16(sustainLoopPlayMode); af_fread(&sustainLoopBegin, sizeof (u_int16_t), 1, fh); sustainLoopBegin = BENDIAN_TO_HOST_INT16(sustainLoopBegin); af_fread(&sustainLoopEnd, sizeof (u_int16_t), 1, fh); sustainLoopEnd = BENDIAN_TO_HOST_INT16(sustainLoopEnd); af_fread(&releaseLoopPlayMode, sizeof (u_int16_t), 1, fh); releaseLoopPlayMode = BENDIAN_TO_HOST_INT16(releaseLoopPlayMode); af_fread(&releaseLoopBegin, sizeof (u_int16_t), 1, fh); releaseLoopBegin = BENDIAN_TO_HOST_INT16(releaseLoopBegin); af_fread(&releaseLoopEnd, sizeof (u_int16_t), 1, fh); releaseLoopEnd = BENDIAN_TO_HOST_INT16(releaseLoopEnd); #ifdef DEBUG printf("sustain loop: mode %d, begin %d, end %d\n", sustainLoopPlayMode, sustainLoopBegin, sustainLoopEnd); printf("release loop: mode %d, begin %d, end %d\n", releaseLoopPlayMode, releaseLoopBegin, releaseLoopEnd); #endif instrument->loops[0].id = 1; instrument->loops[0].mode = sustainLoopPlayMode; instrument->loops[0].beginMarker = sustainLoopBegin; instrument->loops[0].endMarker = sustainLoopEnd; instrument->loops[1].id = 2; instrument->loops[1].mode = releaseLoopPlayMode; instrument->loops[1].beginMarker = releaseLoopBegin; instrument->loops[1].endMarker = releaseLoopEnd; return AF_SUCCEED; }