tomwalters@277: // Copyright 2006-2010, Thomas Walters
tomwalters@277: //
tomwalters@277: // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@277: // http://www.acousticscale.org/AIMC
tomwalters@277: //
tomwalters@277: // This program is free software: you can redistribute it and/or modify
tomwalters@277: // it under the terms of the GNU General Public License as published by
tomwalters@277: // the Free Software Foundation, either version 3 of the License, or
tomwalters@277: // (at your option) any later version.
tomwalters@277: //
tomwalters@277: // This program is distributed in the hope that it will be useful,
tomwalters@277: // but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@277: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@277: // GNU General Public License for more details.
tomwalters@277: //
tomwalters@277: // You should have received a copy of the GNU General Public License
tomwalters@277: // along with this program. If not, see .
tomwalters@277:
tomwalters@277: /*!
tomwalters@277: * \file
tomwalters@277: * \brief File output in the HTK format.
tomwalters@277: *
tomwalters@277: * \author Tom Walters
tomwalters@277: * \author Willem van Engen
tomwalters@277: * \date created 2006/10/30
tomwalters@277: * \version \$Id$
tomwalters@277: */
tomwalters@277:
tomwalters@277: #ifdef _WINDOWS
tomwalters@277: # include // for _mkdir&_rmdir
tomwalters@277: #else
tomwalters@277: # include
tomwalters@277: # include // for opendir&friends
tomwalters@277: #endif
tomwalters@277: #include
tomwalters@277: #include
tomwalters@277: #include
tomwalters@277:
tomwalters@277: #include "Modules/Output/FileOutputHTK.h"
tomwalters@277:
tomwalters@277: namespace aimc {
tomwalters@277: FileOutputHTK::FileOutputHTK(Parameters *params) : Module(params) {
tomwalters@278: module_description_ = "File output in HTK format";
tomwalters@278: module_identifier_ = "htk_out";
tomwalters@278: module_type_ = "output";
tomwalters@278: module_version_ = "$Id$";
tomwalters@278:
tomwalters@277: file_handle_ = NULL;
tomwalters@277: header_written_ = false;
tomwalters@277: filename_[0] = '\0';
tomwalters@277: frame_period_ms_ = 0.0f;
tomwalters@277: }
tomwalters@277:
tomwalters@277: FileOutputHTK::~FileOutputHTK() {
tomwalters@277: if (file_handle_ != NULL)
tomwalters@277: CloseFile();
tomwalters@277: }
tomwalters@277:
tomwalters@277: bool FileOutputHTK::OpenFile(const char* filename, float frame_period_ms) {
tomwalters@277: if (file_handle_ != NULL) {
tomwalters@277: LOG_ERROR(_T("Couldn't open output file. A file is already open."));
tomwalters@277: return false;
tomwalters@277: }
tomwalters@277:
tomwalters@277: // Check that the output file exists and is writeable
tomwalters@277: if ((file_handle_ = fopen(filename, "wb"))==NULL ) {
tomwalters@277: LOG_ERROR(_T("Couldn't open output file '%s' for writing."), filename);
tomwalters@277: return false;
tomwalters@277: }
tomwalters@277: strcpy(filename_, filename);
tomwalters@277: sample_count_ = 0;
tomwalters@277: frame_period_ms_ = frame_period_ms;
tomwalters@277: header_written_ = false;
tomwalters@277: return true;
tomwalters@277: }
tomwalters@277:
tomwalters@277: bool FileOutputHTK::InitializeInternal(const SignalBank &input) {
tomwalters@277: if (file_handle_ == NULL) {
tomwalters@277: LOG_ERROR(_T("Couldn't initialize file output. "
tomwalters@277: "Please call FileOutputHTK::OpenFile first"));
tomwalters@277: return false;
tomwalters@277: }
tomwalters@277: if (header_written_) {
tomwalters@277: LOG_ERROR(_T("A header has already been written on the output file."
tomwalters@277: "Please call FileOutputHTK::CloseFile to close that file, "
tomwalters@277: "and FileOutputHTK::OpenFile to open an new one before "
tomwalters@277: "calling FileOutputHTK::Initialize again."));
tomwalters@277: return false;
tomwalters@277: }
tomwalters@277: channel_count_ = input.channel_count();
tomwalters@277: buffer_length_ = input.buffer_length();
tomwalters@277: WriteHeader(channel_count_ * buffer_length_, frame_period_ms_);
tomwalters@277: return true;
tomwalters@277: }
tomwalters@277:
tomwalters@277: void FileOutputHTK::ResetInternal() {
tomwalters@277: if (file_handle_ != NULL && !header_written_) {
tomwalters@277: WriteHeader(channel_count_ * buffer_length_, frame_period_ms_);
tomwalters@277: }
tomwalters@277: }
tomwalters@277:
tomwalters@277: void FileOutputHTK::WriteHeader(int num_elements, float period_ms) {
tomwalters@277: if (header_written_)
tomwalters@277: return;
tomwalters@277:
tomwalters@277: /* HTK format file: (taken from the HTK book - section 5.10.1)
tomwalters@277: * Header: 12 bytes in total, contains:
tomwalters@277: * sample_count - number of samples in file (4-byte integer)(long)
tomwalters@277: * sample_period - sample period in 100ns units (4-byte integer)(long)
tomwalters@277: * sample_size - number of bytes per sample (2-byte integer)(short)
tomwalters@277: * parameter_kind - a code indicating the sample kind (2-byte integer)(short)
tomwalters@277: */
tomwalters@277:
tomwalters@277: // To be filled in when the file is done
tomwalters@277: int32_t sample_count = 0;
tomwalters@277:
tomwalters@277: int32_t sample_period = floor(1e4 * period_ms);
tomwalters@277: int16_t sample_size = num_elements * sizeof(float);
tomwalters@277:
tomwalters@277: // User-defined coefficients with energy term
tomwalters@277: int16_t parameter_kind = H_USER + H_E;
tomwalters@277:
tomwalters@277: // Fix endianness
tomwalters@277: sample_count = ByteSwap32(sample_count);
tomwalters@277: sample_period = ByteSwap32(sample_period);
tomwalters@277: sample_size = ByteSwap16(sample_size);
tomwalters@277: parameter_kind = ByteSwap16(parameter_kind);
tomwalters@277:
tomwalters@277: // Enter header values. sample_count is a dummy value which is filled in on
tomwalters@277: // file close
tomwalters@277: fwrite(&sample_count, sizeof(sample_count), 1, file_handle_);
tomwalters@277: fwrite(&sample_period, sizeof(sample_period), 1, file_handle_);
tomwalters@277: fwrite(&sample_size, sizeof(sample_size), 1, file_handle_);
tomwalters@277: fwrite(¶meter_kind, sizeof(parameter_kind), 1, file_handle_);
tomwalters@277: fflush(file_handle_);
tomwalters@277:
tomwalters@277: header_written_ = true;
tomwalters@277: }
tomwalters@277:
tomwalters@277:
tomwalters@277: void FileOutputHTK::Process(const SignalBank &input) {
tomwalters@277: if (file_handle_ == NULL) {
tomwalters@277: LOG_ERROR(_T("Couldn't process file output. No file is open."
tomwalters@277: "Please call FileOutputHTK::OpenFile first"));
tomwalters@277: return;
tomwalters@277: }
tomwalters@277:
tomwalters@277: if (!header_written_) {
tomwalters@277: LOG_ERROR(_T("No header has been written on the output file yet. Please"
tomwalters@277: "call FileOutputHTK::Initialize() before calling "
tomwalters@277: "FileOutputHTK::Process()"));
tomwalters@277: return;
tomwalters@277: }
tomwalters@277: float s;
tomwalters@277:
tomwalters@277: for (int ch = 0; ch < input.channel_count(); ch++) {
tomwalters@277: for (int i = 0; i < input.buffer_length(); i++) {
tomwalters@277: s = input.sample(ch, i);
tomwalters@277: s = ByteSwapFloat(s);
tomwalters@277: fwrite(&s, sizeof(float), 1, file_handle_);
tomwalters@277: }
tomwalters@277: }
tomwalters@277: sample_count_++;
tomwalters@277: }
tomwalters@277:
tomwalters@277: bool FileOutputHTK::CloseFile() {
tomwalters@277: if (file_handle_ == NULL)
tomwalters@277: return false;
tomwalters@277:
tomwalters@277: // Write the first 4 bytes of the file
tomwalters@277: // with how many samples there are in the file
tomwalters@277: fflush(file_handle_);
tomwalters@277: rewind(file_handle_);
tomwalters@277: fflush(file_handle_);
tomwalters@277: int32_t samples = sample_count_;
tomwalters@277: samples = ByteSwap32(samples);
tomwalters@277: fwrite(&samples, sizeof(samples), 1, file_handle_);
tomwalters@277:
tomwalters@277: // And close the file
tomwalters@277: fclose(file_handle_);
tomwalters@277: file_handle_ = NULL;
tomwalters@277: return true;
tomwalters@277: }
tomwalters@277:
tomwalters@277: float FileOutputHTK::ByteSwapFloat(float d) {
tomwalters@277: // Endianness fix
tomwalters@277: float a;
tomwalters@277: unsigned char *dst = (unsigned char *)&a;
tomwalters@277: unsigned char *src = (unsigned char *)&d;
tomwalters@277:
tomwalters@277: dst[0] = src[3];
tomwalters@277: dst[1] = src[2];
tomwalters@277: dst[2] = src[1];
tomwalters@277: dst[3] = src[0];
tomwalters@277:
tomwalters@277: return a;
tomwalters@277: }
tomwalters@277: } //namespace aimc
tomwalters@277: