// file: $isip/class/mmedia/AudioFile/adf_04.cc // version: $Id: adf_04.cc 9266 2003-07-15 19:33:59Z parihar $ // // isip include files // #include "AudioFile.h" #include #include #include #include // method: write // // arguments: // Sof& sof: (input) sof file object // int32 tag: (input) sof object instance tag // const String& name: (input) sof object instance name // // return: a bool8 value indicating status // // this method has the object read itself from an Sof file // bool8 AudioFile::write(Sof& sof_a, int32 tag_a, const String& name_a) const { int32 obj_size; if (sof_a.isText()) { obj_size = Sof::ANY_SIZE; } else { // AudioFile object have VectorFloat type data field // but the File format is VectorLong, VectorShort, VectorByte // int32 len = sampled_data_d.length(); Vector temp1(len); Vector temp2(len); Vector temp3(len); int32 size = 0; if (len > 0) { if (sample_num_bytes_d == (Long)sizeof(byte8)) { size = temp1.sofSize(); } else if (sample_num_bytes_d == (Long)sizeof(int16)) { size = temp2.sofSize(); } else if (sample_num_bytes_d == (Long)sizeof(int32)) { size = temp3.sofSize(); } } obj_size = sofSize() + size; } // put the object into the sof file's index // if (!sof_a.put(name_a, tag_a, obj_size)) { return false; } // exit gracefully // return writeData(sof_a); } // method: writeData // // arguments: // Sof& sof: (input) sof file object // const String& pname: (input) parameter name // // return: a bool8 value indicating status // // this method writes the object to the Sof file. it assumes that the // Sof file is already positioned correctly. // bool8 AudioFile::writeData(Sof& sof_a, const String& pname_a) const { // write a start string if necessary // sof_a.writeLabelPrefix(pname_a); // write the configuration to the audiofile (sof format) // if (!writeSofConfig(sof_a)) { return Error::handle(name(), L"writeData", Error::IO, __FILE__, __LINE__, Error::WARNING); } if (sampled_data_d.length() > 0) { // write the data // writeSofData(sof_a); } // put an end string if necessary // sof_a.writeLabelSuffix(pname_a); // exit gracefully // return true; } // method: writeSofStart // // arguments: // Sof& sof: (input) sof file object // int32 tag: (input) sof object instance tag // const String& name: (input) sof object instance name // // return: a bool8 value indicating status // // this method has the object read itself from an Sof file // bool8 AudioFile::writeSofStart(Sof& sof_a, int32 tag_a, const String& name_a) const { int32 obj_size; if (sof_a.isText()) { obj_size = Sof::ANY_SIZE; } else { obj_size = sofSize(); } // put the object into the sof file's index // if (!sof_a.put(name_a, tag_a, obj_size)) { return false; } // exit gracefully // return writeSofStartData(sof_a); } // method: writeSofStartData // // arguments: // Sof& sof: (input) sof file object // const String& pname: (input) textual parameter name // // return: a bool8 value indicating status // // this method is required for the partial write // bool8 AudioFile::writeSofStartData(Sof& sof_a, const String& pname_a) const { // write a start string if necessary // sof_a.writeLabelPrefix(pname_a); // write the configuration to the audiofile (sof format) // if (!writeSofConfig(sof_a)) { return Error::handle(name(), L"writeSofStartData", Error::IO, __FILE__, __LINE__, Error::WARNING); } // exit gracefully // return true; } // method: writeSofConfig // // arguments: // Sof& sof: (input) sof file object // // return: a bool8 value indicating status // // this method has the object write its configuration to the Sof // file. it assumes that the Sof file is already positioned correctly. // bool8 AudioFile::writeSofConfig(Sof& sof_a) const { FILE_TYPE_MAP.writeElementData(sof_a, PARAM_FILE_TYPE, (uint16)file_type_d); FILE_FORMAT_MAP.writeElementData(sof_a, PARAM_FILE_FORMAT, (uint16)file_format_d); COMP_TYPE_MAP.writeElementData(sof_a, PARAM_COMP_TYPE, (uint16)compression_type_d); amplitude_range_d.writeData(sof_a, PARAM_RANGE); sample_freq_d.writeData(sof_a, PARAM_SAMPLE_FREQUENCY); sample_num_bytes_d.writeData(sof_a, PARAM_SAMPLE_NUM_BYTES); SAMPLE_PRECISION_MAP.writeElementData(sof_a, PARAM_SAMPLE_PRECISION, (uint16)sample_precision_d); BMODE_MAP.writeElementData(sof_a, PARAM_BYTE_ORDER, (uint16)byte_mode_d); num_channels_d.writeData(sof_a, PARAM_NUM_CHANNELS); tag_d.writeData(sof_a, PARAM_TAG); block_size_d.writeData(sof_a, PARAM_BLOCK_SIZE); buf_size_d.writeData(sof_a, PARAM_BUF_SIZE); id_d.writeData(sof_a, PARAM_ID); ma_coeff_d.writeData(sof_a, PARAM_MA_COEFF); ar_coeff_d.writeData(sof_a, PARAM_AR_COEFF); // exit gracefully // return true; } // method: writeSofData // // arguments: // Sof& sof: (input) sof file object // // return: a bool8 value indicating status // // this method writes the audio data to raw audio file // int32 AudioFile::writeSofData(Sof& sof_a) const { // combine the multi-channel data to a single Vector // VectorLong whole_data; Vector< VectorFloat> temp; temp.assign(sampled_data_d); revertData(whole_data, temp, CHANNEL_TAG_ALL); int32 num_write = whole_data.length(); if (sample_num_bytes_d == (Long)sizeof(byte8)) { VectorByte buffer(num_write); for (int32 i = 0; i < num_write; i++) { buffer(i).assign(whole_data(i)); } // call the partial write method // num_write = buffer.writeData(sof_a, PARAM_DATA); } else if (sample_num_bytes_d == (Long)sizeof(int16)) { VectorShort buffer(num_write); for (int32 i = 0; i < num_write; i++) { buffer(i).assign(whole_data(i)); } // call the partial write method // num_write = buffer.writeData(sof_a, PARAM_DATA); } else if (sample_num_bytes_d == (Long)sizeof(int32)) { VectorLong buffer(num_write); for (int32 i = 0; i < num_write; i++) { buffer(i).assign(whole_data(i)); } // call the partial write method // num_write = buffer.writeData(sof_a, PARAM_DATA); } else { return Error::handle(name(), L"writeSofData", ERR, __FILE__, __LINE__); } // exit gracefully // return num_write; }