tomwalters@5: // Copyright 2006-2010, Thomas Walters tomwalters@5: // tomwalters@5: // AIM-C: A C++ implementation of the Auditory Image Model tomwalters@5: // http://www.acousticscale.org/AIMC tomwalters@5: // tomwalters@45: // Licensed under the Apache License, Version 2.0 (the "License"); tomwalters@45: // you may not use this file except in compliance with the License. tomwalters@45: // You may obtain a copy of the License at tomwalters@5: // tomwalters@45: // http://www.apache.org/licenses/LICENSE-2.0 tomwalters@5: // tomwalters@45: // Unless required by applicable law or agreed to in writing, software tomwalters@45: // distributed under the License is distributed on an "AS IS" BASIS, tomwalters@45: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tomwalters@45: // See the License for the specific language governing permissions and tomwalters@45: // limitations under the License. tomwalters@5: tomwalters@5: /*! tomwalters@5: * \file tomwalters@7: * \brief File output in the HTK format. tomwalters@5: * tomwalters@7: * \author Tom Walters tomwalters@5: * \author Willem van Engen tomwalters@5: * \date created 2006/10/30 tomwalters@5: * \version \$Id$ tomwalters@5: */ tomwalters@5: tomwalters@5: #ifdef _WINDOWS tomwalters@46: # include // for _mkdir & _rmdir tomwalters@5: #else tomwalters@7: # include tomwalters@46: # include // for opendir & friends tomwalters@5: #endif tomwalters@21: tomwalters@21: #include tomwalters@5: #include tomwalters@5: #include tomwalters@5: #include tomwalters@5: tomwalters@5: #include "Modules/Output/FileOutputHTK.h" tomwalters@5: tomwalters@5: namespace aimc { tomwalters@5: FileOutputHTK::FileOutputHTK(Parameters *params) : Module(params) { tomwalters@6: module_description_ = "File output in HTK format"; tomwalters@6: module_identifier_ = "htk_out"; tomwalters@6: module_type_ = "output"; tomwalters@6: module_version_ = "$Id$"; tomwalters@232: tomwalters@232: file_suffix_ = parameters_->DefaultString("htk_out.file_suffix", ".htk"); tomwalters@6: tomwalters@7: file_handle_ = NULL; tomwalters@7: header_written_ = false; tomwalters@5: frame_period_ms_ = 0.0f; tomwalters@232: previous_start_time_ = 0; tomwalters@5: } tomwalters@5: tomwalters@5: FileOutputHTK::~FileOutputHTK() { tomwalters@7: if (file_handle_ != NULL) tomwalters@7: CloseFile(); tomwalters@5: } tomwalters@5: tomwalters@232: bool FileOutputHTK::InitializeInternal(const SignalBank &input) { tomwalters@232: channel_count_ = input.channel_count(); tomwalters@232: buffer_length_ = input.buffer_length(); tomwalters@232: ResetInternal(); tomwalters@232: if (file_handle_ == NULL) { tomwalters@232: LOG_ERROR(_T("Couldn't initialize file output.")); tomwalters@157: return false; tomwalters@157: } tomwalters@232: if (!header_written_) { tomwalters@157: WriteHeader(channel_count_ * buffer_length_, frame_period_ms_); tomwalters@157: } tomwalters@232: tomwalters@5: return true; tomwalters@5: } tomwalters@5: tomwalters@5: void FileOutputHTK::ResetInternal() { tomwalters@232: // Finalize and close the open file, if there is one. tomwalters@5: if (file_handle_ != NULL && !header_written_) { tomwalters@157: WriteHeader(channel_count_ * buffer_length_, frame_period_ms_); tomwalters@5: } tomwalters@20: if (file_handle_ != NULL) tomwalters@20: CloseFile(); tomwalters@232: tomwalters@232: // Now open and set up the new file. tomwalters@232: // Check that the output file exists and is writeable. tomwalters@232: string out_filename; tomwalters@232: out_filename = global_parameters_->GetString("output_filename_base") + file_suffix_; tomwalters@232: if ((file_handle_ = fopen(out_filename.c_str(), tomwalters@232: "wb")) == NULL) { tomwalters@232: LOG_ERROR(_T("Couldn't open output file '%s' for writing."), tomwalters@232: out_filename.c_str()); tomwalters@232: return; tomwalters@232: } tomwalters@232: sample_count_ = 0; tomwalters@232: header_written_ = false; tomwalters@232: WriteHeader(channel_count_ * buffer_length_, frame_period_ms_); tomwalters@5: } tomwalters@5: tomwalters@157: void FileOutputHTK::WriteHeader(int num_elements, float period_ms) { tomwalters@7: if (header_written_) tomwalters@7: return; tomwalters@5: tomwalters@7: /* HTK format file: (taken from the HTK book - section 5.10.1) tomwalters@7: * Header: 12 bytes in total, contains: tomwalters@7: * sample_count - number of samples in file (4-byte integer)(long) tomwalters@7: * sample_period - sample period in 100ns units (4-byte integer)(long) tomwalters@7: * sample_size - number of bytes per sample (2-byte integer)(short) tomwalters@7: * parameter_kind - a code indicating the sample kind (2-byte integer)(short) tomwalters@7: */ tomwalters@5: tomwalters@7: // To be filled in when the file is done tomwalters@7: int32_t sample_count = 0; tomwalters@5: tomwalters@157: int32_t sample_period = floor(1e4 * period_ms); tomwalters@17: int16_t sample_size = num_elements * sizeof(float); // NOLINT tomwalters@5: tomwalters@5: // User-defined coefficients with energy term tomwalters@7: int16_t parameter_kind = H_USER + H_E; tomwalters@5: tomwalters@7: // Fix endianness tomwalters@7: sample_count = ByteSwap32(sample_count); tomwalters@7: sample_period = ByteSwap32(sample_period); tomwalters@7: sample_size = ByteSwap16(sample_size); tomwalters@7: parameter_kind = ByteSwap16(parameter_kind); tomwalters@5: tomwalters@5: // Enter header values. sample_count is a dummy value which is filled in on tomwalters@5: // file close tomwalters@7: fwrite(&sample_count, sizeof(sample_count), 1, file_handle_); tomwalters@7: fwrite(&sample_period, sizeof(sample_period), 1, file_handle_); tomwalters@7: fwrite(&sample_size, sizeof(sample_size), 1, file_handle_); tomwalters@7: fwrite(¶meter_kind, sizeof(parameter_kind), 1, file_handle_); tomwalters@7: fflush(file_handle_); tomwalters@5: tomwalters@7: header_written_ = true; tomwalters@5: } tomwalters@5: tomwalters@5: void FileOutputHTK::Process(const SignalBank &input) { tomwalters@5: if (file_handle_ == NULL) { tomwalters@5: LOG_ERROR(_T("Couldn't process file output. No file is open." tomwalters@5: "Please call FileOutputHTK::OpenFile first")); tomwalters@7: return; tomwalters@5: } tomwalters@5: tomwalters@5: if (!header_written_) { tomwalters@20: LOG_ERROR(_T("No header has been written on the output file yet. Please " tomwalters@232: "call FileOutputHTK::Initialize() or FileOutputHTK::Reset() " tomwalters@232: " before calling FileOutputHTK::Process()")); tomwalters@7: return; tomwalters@5: } tomwalters@7: float s; tomwalters@5: tomwalters@7: for (int ch = 0; ch < input.channel_count(); ch++) { tomwalters@5: for (int i = 0; i < input.buffer_length(); i++) { tomwalters@5: s = input.sample(ch, i); tomwalters@5: s = ByteSwapFloat(s); tomwalters@8: fwrite(&s, sizeof(s), 1, file_handle_); tomwalters@5: } tomwalters@5: } tomwalters@7: sample_count_++; tomwalters@232: frame_period_ms_ = 1000.0 tomwalters@232: * (input.start_time() - previous_start_time_) tomwalters@232: / input.sample_rate(); tomwalters@232: previous_start_time_ = input.start_time(); tomwalters@5: } tomwalters@5: tomwalters@5: bool FileOutputHTK::CloseFile() { tomwalters@7: if (file_handle_ == NULL) tomwalters@5: return false; tomwalters@5: tomwalters@7: // Write the first 4 bytes of the file tomwalters@7: // with how many samples there are in the file tomwalters@7: fflush(file_handle_); tomwalters@7: rewind(file_handle_); tomwalters@7: fflush(file_handle_); tomwalters@5: int32_t samples = sample_count_; tomwalters@7: samples = ByteSwap32(samples); tomwalters@7: fwrite(&samples, sizeof(samples), 1, file_handle_); tomwalters@5: tomwalters@7: // And close the file tomwalters@7: fclose(file_handle_); tomwalters@7: file_handle_ = NULL; tomwalters@20: header_written_ = false; tomwalters@7: return true; tomwalters@5: } tomwalters@5: tomwalters@5: float FileOutputHTK::ByteSwapFloat(float d) { tomwalters@5: // Endianness fix tomwalters@5: float a; tomwalters@5: unsigned char *dst = (unsigned char *)&a; tomwalters@5: unsigned char *src = (unsigned char *)&d; tomwalters@5: tomwalters@5: dst[0] = src[3]; tomwalters@5: dst[1] = src[2]; tomwalters@5: dst[2] = src[1]; tomwalters@5: dst[3] = src[0]; tomwalters@5: tomwalters@5: return a; tomwalters@5: } tomwalters@8: } // namespace aimc tomwalters@5: