annotate src/Modules/Output/FileOutputHTK.cc @ 25:e361baf69120

-Added a bit of junk to try and build a bianry for OS X 10.4 in SConstruct -Fixes to the FileList to build on linux - hopefully
author tomwalters
date Tue, 23 Feb 2010 21:51:11 +0000
parents d386805133f1
children c5f5e9569863
rev   line source
tomwalters@5 1 // Copyright 2006-2010, Thomas Walters
tomwalters@5 2 //
tomwalters@5 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@5 4 // http://www.acousticscale.org/AIMC
tomwalters@5 5 //
tomwalters@5 6 // This program is free software: you can redistribute it and/or modify
tomwalters@5 7 // it under the terms of the GNU General Public License as published by
tomwalters@5 8 // the Free Software Foundation, either version 3 of the License, or
tomwalters@5 9 // (at your option) any later version.
tomwalters@5 10 //
tomwalters@5 11 // This program is distributed in the hope that it will be useful,
tomwalters@5 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@5 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@5 14 // GNU General Public License for more details.
tomwalters@5 15 //
tomwalters@5 16 // You should have received a copy of the GNU General Public License
tomwalters@5 17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
tomwalters@5 18
tomwalters@5 19 /*!
tomwalters@5 20 * \file
tomwalters@7 21 * \brief File output in the HTK format.
tomwalters@5 22 *
tomwalters@7 23 * \author Tom Walters <tom@acousticscale.org>
tomwalters@5 24 * \author Willem van Engen <cnbh@willem.engen.nl>
tomwalters@5 25 * \date created 2006/10/30
tomwalters@5 26 * \version \$Id$
tomwalters@5 27 */
tomwalters@5 28
tomwalters@5 29 #ifdef _WINDOWS
tomwalters@8 30 # include <direct.h> // for _mkdir&_rmdir
tomwalters@5 31 #else
tomwalters@7 32 # include <sys/types.h>
tomwalters@8 33 # include <dirent.h> // for opendir&friends
tomwalters@5 34 #endif
tomwalters@21 35
tomwalters@21 36 #include <stdint.h>
tomwalters@5 37 #include <stdio.h>
tomwalters@5 38 #include <string.h>
tomwalters@5 39 #include <cmath>
tomwalters@5 40
tomwalters@5 41 #include "Modules/Output/FileOutputHTK.h"
tomwalters@5 42
tomwalters@5 43 namespace aimc {
tomwalters@5 44 FileOutputHTK::FileOutputHTK(Parameters *params) : Module(params) {
tomwalters@6 45 module_description_ = "File output in HTK format";
tomwalters@6 46 module_identifier_ = "htk_out";
tomwalters@6 47 module_type_ = "output";
tomwalters@6 48 module_version_ = "$Id$";
tomwalters@6 49
tomwalters@7 50 file_handle_ = NULL;
tomwalters@7 51 header_written_ = false;
tomwalters@5 52 frame_period_ms_ = 0.0f;
tomwalters@5 53 }
tomwalters@5 54
tomwalters@5 55 FileOutputHTK::~FileOutputHTK() {
tomwalters@7 56 if (file_handle_ != NULL)
tomwalters@7 57 CloseFile();
tomwalters@5 58 }
tomwalters@5 59
tomwalters@5 60 bool FileOutputHTK::OpenFile(const char* filename, float frame_period_ms) {
tomwalters@7 61 if (file_handle_ != NULL) {
tomwalters@7 62 LOG_ERROR(_T("Couldn't open output file. A file is already open."));
tomwalters@7 63 return false;
tomwalters@7 64 }
tomwalters@5 65
tomwalters@7 66 // Check that the output file exists and is writeable
tomwalters@8 67 if ((file_handle_ = fopen(filename, "wb")) == NULL) {
tomwalters@7 68 LOG_ERROR(_T("Couldn't open output file '%s' for writing."), filename);
tomwalters@7 69 return false;
tomwalters@7 70 }
tomwalters@7 71 sample_count_ = 0;
tomwalters@5 72 frame_period_ms_ = frame_period_ms;
tomwalters@5 73 header_written_ = false;
tomwalters@20 74 if (initialized_) {
tomwalters@20 75 WriteHeader(channel_count_ * buffer_length_, frame_period_ms_);
tomwalters@20 76 }
tomwalters@7 77 return true;
tomwalters@5 78 }
tomwalters@5 79
tomwalters@5 80 bool FileOutputHTK::InitializeInternal(const SignalBank &input) {
tomwalters@5 81 if (file_handle_ == NULL) {
tomwalters@5 82 LOG_ERROR(_T("Couldn't initialize file output. "
tomwalters@5 83 "Please call FileOutputHTK::OpenFile first"));
tomwalters@7 84 return false;
tomwalters@5 85 }
tomwalters@5 86 if (header_written_) {
tomwalters@20 87 LOG_ERROR(_T("A header has already been written on the output file. "
tomwalters@5 88 "Please call FileOutputHTK::CloseFile to close that file, "
tomwalters@5 89 "and FileOutputHTK::OpenFile to open an new one before "
tomwalters@5 90 "calling FileOutputHTK::Initialize again."));
tomwalters@7 91 return false;
tomwalters@5 92 }
tomwalters@5 93 channel_count_ = input.channel_count();
tomwalters@5 94 buffer_length_ = input.buffer_length();
tomwalters@5 95 WriteHeader(channel_count_ * buffer_length_, frame_period_ms_);
tomwalters@5 96 return true;
tomwalters@5 97 }
tomwalters@5 98
tomwalters@5 99 void FileOutputHTK::ResetInternal() {
tomwalters@5 100 if (file_handle_ != NULL && !header_written_) {
tomwalters@5 101 WriteHeader(channel_count_ * buffer_length_, frame_period_ms_);
tomwalters@5 102 }
tomwalters@20 103 if (file_handle_ != NULL)
tomwalters@20 104 CloseFile();
tomwalters@5 105 }
tomwalters@5 106
tomwalters@5 107 void FileOutputHTK::WriteHeader(int num_elements, float period_ms) {
tomwalters@7 108 if (header_written_)
tomwalters@7 109 return;
tomwalters@5 110
tomwalters@7 111 /* HTK format file: (taken from the HTK book - section 5.10.1)
tomwalters@7 112 * Header: 12 bytes in total, contains:
tomwalters@7 113 * sample_count - number of samples in file (4-byte integer)(long)
tomwalters@7 114 * sample_period - sample period in 100ns units (4-byte integer)(long)
tomwalters@7 115 * sample_size - number of bytes per sample (2-byte integer)(short)
tomwalters@7 116 * parameter_kind - a code indicating the sample kind (2-byte integer)(short)
tomwalters@7 117 */
tomwalters@5 118
tomwalters@7 119 // To be filled in when the file is done
tomwalters@7 120 int32_t sample_count = 0;
tomwalters@5 121
tomwalters@5 122 int32_t sample_period = floor(1e4 * period_ms);
tomwalters@17 123 int16_t sample_size = num_elements * sizeof(float); // NOLINT
tomwalters@5 124
tomwalters@5 125 // User-defined coefficients with energy term
tomwalters@7 126 int16_t parameter_kind = H_USER + H_E;
tomwalters@5 127
tomwalters@7 128 // Fix endianness
tomwalters@7 129 sample_count = ByteSwap32(sample_count);
tomwalters@7 130 sample_period = ByteSwap32(sample_period);
tomwalters@7 131 sample_size = ByteSwap16(sample_size);
tomwalters@7 132 parameter_kind = ByteSwap16(parameter_kind);
tomwalters@5 133
tomwalters@5 134 // Enter header values. sample_count is a dummy value which is filled in on
tomwalters@5 135 // file close
tomwalters@7 136 fwrite(&sample_count, sizeof(sample_count), 1, file_handle_);
tomwalters@7 137 fwrite(&sample_period, sizeof(sample_period), 1, file_handle_);
tomwalters@7 138 fwrite(&sample_size, sizeof(sample_size), 1, file_handle_);
tomwalters@7 139 fwrite(&parameter_kind, sizeof(parameter_kind), 1, file_handle_);
tomwalters@7 140 fflush(file_handle_);
tomwalters@5 141
tomwalters@7 142 header_written_ = true;
tomwalters@5 143 }
tomwalters@5 144
tomwalters@5 145
tomwalters@5 146 void FileOutputHTK::Process(const SignalBank &input) {
tomwalters@5 147 if (file_handle_ == NULL) {
tomwalters@5 148 LOG_ERROR(_T("Couldn't process file output. No file is open."
tomwalters@5 149 "Please call FileOutputHTK::OpenFile first"));
tomwalters@7 150 return;
tomwalters@5 151 }
tomwalters@5 152
tomwalters@5 153 if (!header_written_) {
tomwalters@20 154 LOG_ERROR(_T("No header has been written on the output file yet. Please "
tomwalters@5 155 "call FileOutputHTK::Initialize() before calling "
tomwalters@5 156 "FileOutputHTK::Process()"));
tomwalters@7 157 return;
tomwalters@5 158 }
tomwalters@7 159 float s;
tomwalters@5 160
tomwalters@7 161 for (int ch = 0; ch < input.channel_count(); ch++) {
tomwalters@5 162 for (int i = 0; i < input.buffer_length(); i++) {
tomwalters@5 163 s = input.sample(ch, i);
tomwalters@5 164 s = ByteSwapFloat(s);
tomwalters@8 165 fwrite(&s, sizeof(s), 1, file_handle_);
tomwalters@5 166 }
tomwalters@5 167 }
tomwalters@7 168 sample_count_++;
tomwalters@5 169 }
tomwalters@5 170
tomwalters@5 171 bool FileOutputHTK::CloseFile() {
tomwalters@7 172 if (file_handle_ == NULL)
tomwalters@5 173 return false;
tomwalters@5 174
tomwalters@7 175 // Write the first 4 bytes of the file
tomwalters@7 176 // with how many samples there are in the file
tomwalters@7 177 fflush(file_handle_);
tomwalters@7 178 rewind(file_handle_);
tomwalters@7 179 fflush(file_handle_);
tomwalters@5 180 int32_t samples = sample_count_;
tomwalters@7 181 samples = ByteSwap32(samples);
tomwalters@7 182 fwrite(&samples, sizeof(samples), 1, file_handle_);
tomwalters@5 183
tomwalters@7 184 // And close the file
tomwalters@7 185 fclose(file_handle_);
tomwalters@7 186 file_handle_ = NULL;
tomwalters@20 187 header_written_ = false;
tomwalters@7 188 return true;
tomwalters@5 189 }
tomwalters@5 190
tomwalters@5 191 float FileOutputHTK::ByteSwapFloat(float d) {
tomwalters@5 192 // Endianness fix
tomwalters@5 193 float a;
tomwalters@5 194 unsigned char *dst = (unsigned char *)&a;
tomwalters@5 195 unsigned char *src = (unsigned char *)&d;
tomwalters@5 196
tomwalters@5 197 dst[0] = src[3];
tomwalters@5 198 dst[1] = src[2];
tomwalters@5 199 dst[2] = src[1];
tomwalters@5 200 dst[3] = src[0];
tomwalters@5 201
tomwalters@5 202 return a;
tomwalters@5 203 }
tomwalters@8 204 } // namespace aimc
tomwalters@5 205