status WAVEFile::writeMiscellaneous() { if (miscellaneousCount != 0) { uint32_t miscellaneousBytes; uint32_t chunkSize; /* Start at 12 to account for 'LIST', size, and 'INFO'. */ miscellaneousBytes = 12; /* Then calculate the size of the whole INFO chunk. */ for (int i=0; i<miscellaneousCount; i++) { Tag miscid; // Skip miscellaneous data of an unsupported type. if (!misc_type_to_wave(miscellaneous[i].type, &miscid)) continue; // Account for miscellaneous type and size. miscellaneousBytes += 8; miscellaneousBytes += miscellaneous[i].size; // Add a pad byte if necessary. if (miscellaneous[i].size % 2 != 0) miscellaneousBytes++; assert(miscellaneousBytes % 2 == 0); } if (miscellaneousStartOffset == 0) miscellaneousStartOffset = fh->tell(); else fh->seek(miscellaneousStartOffset, File::SeekFromBeginning); totalMiscellaneousSize = miscellaneousBytes; /* Write the data. On the first call to this function (from _af_wave_write_init), the data won't be available, fh->seek is used to reserve space until the data has been provided. On subseuent calls to this function (from _af_wave_update), the data will really be written. */ /* Write 'LIST'. */ fh->write("LIST", 4); /* Write the size of the following chunk. */ chunkSize = miscellaneousBytes-8; writeU32(&chunkSize); /* Write 'INFO'. */ fh->write("INFO", 4); /* Write each miscellaneous chunk. */ for (int i=0; i<miscellaneousCount; i++) { uint32_t miscsize = miscellaneous[i].size; Tag miscid; // Skip miscellaneous data of an unsupported type. if (!misc_type_to_wave(miscellaneous[i].type, &miscid)) continue; writeTag(&miscid); writeU32(&miscsize); if (miscellaneous[i].buffer != NULL) { uint8_t zero = 0; fh->write(miscellaneous[i].buffer, miscellaneous[i].size); // Pad if necessary. if ((miscellaneous[i].size%2) != 0) writeU8(&zero); } else { int size; size = miscellaneous[i].size; // Pad if necessary. if ((size % 2) != 0) size++; fh->seek(size, File::SeekFromCurrent); } } } return AF_SUCCEED; }
status WriteMiscellaneous (AFfilehandle filehandle) { _WAVEInfo *wave = (_WAVEInfo *) filehandle->formatSpecific; if (filehandle->miscellaneousCount != 0) { int i; uint32_t miscellaneousBytes; uint32_t chunkSize; /* Start at 12 to account for 'LIST', size, and 'INFO'. */ miscellaneousBytes = 12; /* Then calculate the size of the whole INFO chunk. */ for (i=0; i<filehandle->miscellaneousCount; i++) { uint32_t miscid; /* Skip miscellaneous data of an unsupported type. */ if (misc_type_to_wave(filehandle->miscellaneous[i].type, &miscid) == AF_FAIL) continue; /* Account for miscellaneous type and size. */ miscellaneousBytes += 8; miscellaneousBytes += filehandle->miscellaneous[i].size; /* Add a pad byte if necessary. */ if (filehandle->miscellaneous[i].size % 2 != 0) miscellaneousBytes++; assert(miscellaneousBytes % 2 == 0); } if (wave->miscellaneousStartOffset == 0) wave->miscellaneousStartOffset = af_ftell(filehandle->fh); else af_fseek(filehandle->fh, wave->miscellaneousStartOffset, SEEK_SET); wave->totalMiscellaneousSize = miscellaneousBytes; /* Write the data. On the first call to this function (from _af_wave_write_init), the data won't be available, af_fseek is used to reserve space until the data has been provided. On subseuent calls to this function (from _af_wave_update), the data will really be written. */ /* Write 'LIST'. */ af_fwrite("LIST", 4, 1, filehandle->fh); /* Write the size of the following chunk. */ chunkSize = miscellaneousBytes-8; af_write_uint32_le(&chunkSize, filehandle->fh); /* Write 'INFO'. */ af_fwrite("INFO", 4, 1, filehandle->fh); /* Write each miscellaneous chunk. */ for (i=0; i<filehandle->miscellaneousCount; i++) { uint32_t miscsize = filehandle->miscellaneous[i].size; uint32_t miscid = 0; /* Skip miscellaneous data of an unsupported type. */ if (misc_type_to_wave(filehandle->miscellaneous[i].type, &miscid) == AF_FAIL) continue; af_fwrite(&miscid, 4, 1, filehandle->fh); af_write_uint32_le(&miscsize, filehandle->fh); if (filehandle->miscellaneous[i].buffer != NULL) { uint8_t zero = 0; af_fwrite(filehandle->miscellaneous[i].buffer, filehandle->miscellaneous[i].size, 1, filehandle->fh); /* Pad if necessary. */ if ((filehandle->miscellaneous[i].size%2) != 0) af_write_uint8(&zero, filehandle->fh); } else { int size; size = filehandle->miscellaneous[i].size; /* Pad if necessary. */ if ((size % 2) != 0) size++; af_fseek(filehandle->fh, size, SEEK_CUR); } } } return AF_SUCCEED; }