annotate src/Modules/Output/FileOutputHTK.cc @ 49:0cd20c748308

- AIMCopy v5 for just smooth NAP features - Reporting of all modules and versions
author tomwalters
date Sun, 11 Jul 2010 03:46:06 +0000
parents c8024714e13e
children 3cdaa81c3aca
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@45 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@45 7 // you may not use this file except in compliance with the License.
tomwalters@45 8 // You may obtain a copy of the License at
tomwalters@5 9 //
tomwalters@45 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@5 11 //
tomwalters@45 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@45 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@45 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@45 15 // See the License for the specific language governing permissions and
tomwalters@45 16 // limitations under the License.
tomwalters@5 17
tomwalters@5 18 /*!
tomwalters@5 19 * \file
tomwalters@7 20 * \brief File output in the HTK format.
tomwalters@5 21 *
tomwalters@7 22 * \author Tom Walters <tom@acousticscale.org>
tomwalters@5 23 * \author Willem van Engen <cnbh@willem.engen.nl>
tomwalters@5 24 * \date created 2006/10/30
tomwalters@5 25 * \version \$Id$
tomwalters@5 26 */
tomwalters@5 27
tomwalters@5 28 #ifdef _WINDOWS
tomwalters@46 29 # include <direct.h> // for _mkdir & _rmdir
tomwalters@5 30 #else
tomwalters@7 31 # include <sys/types.h>
tomwalters@46 32 # include <dirent.h> // for opendir & friends
tomwalters@5 33 #endif
tomwalters@21 34
tomwalters@21 35 #include <stdint.h>
tomwalters@5 36 #include <stdio.h>
tomwalters@5 37 #include <string.h>
tomwalters@5 38 #include <cmath>
tomwalters@5 39
tomwalters@5 40 #include "Modules/Output/FileOutputHTK.h"
tomwalters@5 41
tomwalters@5 42 namespace aimc {
tomwalters@5 43 FileOutputHTK::FileOutputHTK(Parameters *params) : Module(params) {
tomwalters@6 44 module_description_ = "File output in HTK format";
tomwalters@6 45 module_identifier_ = "htk_out";
tomwalters@6 46 module_type_ = "output";
tomwalters@6 47 module_version_ = "$Id$";
tomwalters@6 48
tomwalters@7 49 file_handle_ = NULL;
tomwalters@7 50 header_written_ = false;
tomwalters@5 51 frame_period_ms_ = 0.0f;
tomwalters@5 52 }
tomwalters@5 53
tomwalters@5 54 FileOutputHTK::~FileOutputHTK() {
tomwalters@7 55 if (file_handle_ != NULL)
tomwalters@7 56 CloseFile();
tomwalters@5 57 }
tomwalters@5 58
tomwalters@5 59 bool FileOutputHTK::OpenFile(const char* filename, float frame_period_ms) {
tomwalters@7 60 if (file_handle_ != NULL) {
tomwalters@7 61 LOG_ERROR(_T("Couldn't open output file. A file is already open."));
tomwalters@7 62 return false;
tomwalters@7 63 }
tomwalters@5 64
tomwalters@7 65 // Check that the output file exists and is writeable
tomwalters@8 66 if ((file_handle_ = fopen(filename, "wb")) == NULL) {
tomwalters@7 67 LOG_ERROR(_T("Couldn't open output file '%s' for writing."), filename);
tomwalters@7 68 return false;
tomwalters@7 69 }
tomwalters@7 70 sample_count_ = 0;
tomwalters@5 71 frame_period_ms_ = frame_period_ms;
tomwalters@5 72 header_written_ = false;
tomwalters@20 73 if (initialized_) {
tomwalters@20 74 WriteHeader(channel_count_ * buffer_length_, frame_period_ms_);
tomwalters@20 75 }
tomwalters@7 76 return true;
tomwalters@5 77 }
tomwalters@5 78
tomwalters@5 79 bool FileOutputHTK::InitializeInternal(const SignalBank &input) {
tomwalters@5 80 if (file_handle_ == NULL) {
tomwalters@5 81 LOG_ERROR(_T("Couldn't initialize file output. "
tomwalters@5 82 "Please call FileOutputHTK::OpenFile first"));
tomwalters@7 83 return false;
tomwalters@5 84 }
tomwalters@5 85 if (header_written_) {
tomwalters@20 86 LOG_ERROR(_T("A header has already been written on the output file. "
tomwalters@5 87 "Please call FileOutputHTK::CloseFile to close that file, "
tomwalters@5 88 "and FileOutputHTK::OpenFile to open an new one before "
tomwalters@5 89 "calling FileOutputHTK::Initialize again."));
tomwalters@7 90 return false;
tomwalters@5 91 }
tomwalters@5 92 channel_count_ = input.channel_count();
tomwalters@5 93 buffer_length_ = input.buffer_length();
tomwalters@5 94 WriteHeader(channel_count_ * buffer_length_, frame_period_ms_);
tomwalters@5 95 return true;
tomwalters@5 96 }
tomwalters@5 97
tomwalters@5 98 void FileOutputHTK::ResetInternal() {
tomwalters@5 99 if (file_handle_ != NULL && !header_written_) {
tomwalters@5 100 WriteHeader(channel_count_ * buffer_length_, frame_period_ms_);
tomwalters@5 101 }
tomwalters@20 102 if (file_handle_ != NULL)
tomwalters@20 103 CloseFile();
tomwalters@5 104 }
tomwalters@5 105
tomwalters@5 106 void FileOutputHTK::WriteHeader(int num_elements, float period_ms) {
tomwalters@7 107 if (header_written_)
tomwalters@7 108 return;
tomwalters@5 109
tomwalters@7 110 /* HTK format file: (taken from the HTK book - section 5.10.1)
tomwalters@7 111 * Header: 12 bytes in total, contains:
tomwalters@7 112 * sample_count - number of samples in file (4-byte integer)(long)
tomwalters@7 113 * sample_period - sample period in 100ns units (4-byte integer)(long)
tomwalters@7 114 * sample_size - number of bytes per sample (2-byte integer)(short)
tomwalters@7 115 * parameter_kind - a code indicating the sample kind (2-byte integer)(short)
tomwalters@7 116 */
tomwalters@5 117
tomwalters@7 118 // To be filled in when the file is done
tomwalters@7 119 int32_t sample_count = 0;
tomwalters@5 120
tomwalters@5 121 int32_t sample_period = floor(1e4 * period_ms);
tomwalters@17 122 int16_t sample_size = num_elements * sizeof(float); // NOLINT
tomwalters@5 123
tomwalters@5 124 // User-defined coefficients with energy term
tomwalters@7 125 int16_t parameter_kind = H_USER + H_E;
tomwalters@5 126
tomwalters@7 127 // Fix endianness
tomwalters@7 128 sample_count = ByteSwap32(sample_count);
tomwalters@7 129 sample_period = ByteSwap32(sample_period);
tomwalters@7 130 sample_size = ByteSwap16(sample_size);
tomwalters@7 131 parameter_kind = ByteSwap16(parameter_kind);
tomwalters@5 132
tomwalters@5 133 // Enter header values. sample_count is a dummy value which is filled in on
tomwalters@5 134 // file close
tomwalters@7 135 fwrite(&sample_count, sizeof(sample_count), 1, file_handle_);
tomwalters@7 136 fwrite(&sample_period, sizeof(sample_period), 1, file_handle_);
tomwalters@7 137 fwrite(&sample_size, sizeof(sample_size), 1, file_handle_);
tomwalters@7 138 fwrite(&parameter_kind, sizeof(parameter_kind), 1, file_handle_);
tomwalters@7 139 fflush(file_handle_);
tomwalters@5 140
tomwalters@7 141 header_written_ = true;
tomwalters@5 142 }
tomwalters@5 143
tomwalters@5 144 void FileOutputHTK::Process(const SignalBank &input) {
tomwalters@5 145 if (file_handle_ == NULL) {
tomwalters@5 146 LOG_ERROR(_T("Couldn't process file output. No file is open."
tomwalters@5 147 "Please call FileOutputHTK::OpenFile first"));
tomwalters@7 148 return;
tomwalters@5 149 }
tomwalters@5 150
tomwalters@5 151 if (!header_written_) {
tomwalters@20 152 LOG_ERROR(_T("No header has been written on the output file yet. Please "
tomwalters@5 153 "call FileOutputHTK::Initialize() before calling "
tomwalters@5 154 "FileOutputHTK::Process()"));
tomwalters@7 155 return;
tomwalters@5 156 }
tomwalters@7 157 float s;
tomwalters@5 158
tomwalters@7 159 for (int ch = 0; ch < input.channel_count(); ch++) {
tomwalters@5 160 for (int i = 0; i < input.buffer_length(); i++) {
tomwalters@5 161 s = input.sample(ch, i);
tomwalters@5 162 s = ByteSwapFloat(s);
tomwalters@8 163 fwrite(&s, sizeof(s), 1, file_handle_);
tomwalters@5 164 }
tomwalters@5 165 }
tomwalters@7 166 sample_count_++;
tomwalters@5 167 }
tomwalters@5 168
tomwalters@5 169 bool FileOutputHTK::CloseFile() {
tomwalters@7 170 if (file_handle_ == NULL)
tomwalters@5 171 return false;
tomwalters@5 172
tomwalters@7 173 // Write the first 4 bytes of the file
tomwalters@7 174 // with how many samples there are in the file
tomwalters@7 175 fflush(file_handle_);
tomwalters@7 176 rewind(file_handle_);
tomwalters@7 177 fflush(file_handle_);
tomwalters@5 178 int32_t samples = sample_count_;
tomwalters@7 179 samples = ByteSwap32(samples);
tomwalters@7 180 fwrite(&samples, sizeof(samples), 1, file_handle_);
tomwalters@5 181
tomwalters@7 182 // And close the file
tomwalters@7 183 fclose(file_handle_);
tomwalters@7 184 file_handle_ = NULL;
tomwalters@20 185 header_written_ = false;
tomwalters@7 186 return true;
tomwalters@5 187 }
tomwalters@5 188
tomwalters@5 189 float FileOutputHTK::ByteSwapFloat(float d) {
tomwalters@5 190 // Endianness fix
tomwalters@5 191 float a;
tomwalters@5 192 unsigned char *dst = (unsigned char *)&a;
tomwalters@5 193 unsigned char *src = (unsigned char *)&d;
tomwalters@5 194
tomwalters@5 195 dst[0] = src[3];
tomwalters@5 196 dst[1] = src[2];
tomwalters@5 197 dst[2] = src[1];
tomwalters@5 198 dst[3] = src[0];
tomwalters@5 199
tomwalters@5 200 return a;
tomwalters@5 201 }
tomwalters@8 202 } // namespace aimc
tomwalters@5 203