annotate src/Modules/Output/FileOutputAIMC.cc @ 611:0fbaf443ec82

Carfac C++ revision 3, indluding more style improvements. The output structs are now classes again, and have separate storage methods for each output structure along with flags in the Run and RunSegment methods to allow for only storing NAPs if desired.
author alexbrandmeyer
date Fri, 17 May 2013 19:52:45 +0000
parents 087f3b3c36d3
children
rev   line source
tomwalters@46 1 // Copyright 2006-2010, Thomas Walters
tomwalters@46 2 //
tomwalters@46 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@46 4 // http://www.acousticscale.org/AIMC
tomwalters@46 5 //
tomwalters@46 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@46 7 // you may not use this file except in compliance with the License.
tomwalters@46 8 // You may obtain a copy of the License at
tomwalters@46 9 //
tomwalters@46 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@46 11 //
tomwalters@46 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@46 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@46 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@46 15 // See the License for the specific language governing permissions and
tomwalters@46 16 // limitations under the License.
tomwalters@46 17
tomwalters@46 18 /*!
tomwalters@46 19 * \file
tomwalters@46 20 * \brief File output in the HTK format.
tomwalters@46 21 *
tomwalters@46 22 * \author Tom Walters <tom@acousticscale.org>
tomwalters@46 23 * \author Willem van Engen <cnbh@willem.engen.nl>
tomwalters@46 24 * \date created 2007/01/26
tomwalters@46 25 * \version \$Id: $
tomwalters@46 26 */
tomwalters@46 27
tomwalters@603 28 #include "Modules/Output/FileOutputAIMC.h"
tomwalters@603 29
tomwalters@46 30 #ifdef _WINDOWS
tomwalters@46 31 # include <direct.h> // for _mkdir & _rmdir
tomwalters@46 32 #else
tomwalters@46 33 # include <sys/types.h>
tomwalters@46 34 # include <dirent.h> // for opendir & friends
tomwalters@46 35 #endif
tomwalters@46 36
tomwalters@46 37 #include <stdint.h>
tomwalters@46 38 #include <stdio.h>
tomwalters@46 39 #include <string.h>
tomwalters@46 40 #include <cmath>
tom@253 41 #include <string>
tomwalters@46 42
tomwalters@46 43 namespace aimc {
tomwalters@46 44 FileOutputAIMC::FileOutputAIMC(Parameters *params) : Module(params) {
tomwalters@46 45 module_description_ = "File output in AIMC format";
tomwalters@232 46 module_identifier_ = "aimc_out";
tomwalters@46 47 module_type_ = "output";
tomwalters@46 48 module_version_ = "$Id: FileOutputAIMC.cc 51 2010-03-30 22:06:24Z tomwalters $";
tom@253 49 file_suffix_ = parameters_->DefaultString("file_suffix", ".aimc");
tomwalters@603 50 dump_strobes_ = parameters_->DefaultBool("dump_strobes", false);
tomwalters@603 51 strobes_file_suffix_ = parameters_->DefaultString("strobes_file_suffix", ".strobes");
tomwalters@46 52
tomwalters@46 53 file_handle_ = NULL;
tomwalters@603 54 strobes_file_handle_ = NULL;
tomwalters@46 55 header_written_ = false;
tomwalters@46 56 frame_period_ms_ = 0.0f;
tomwalters@46 57 }
tomwalters@46 58
tomwalters@46 59 FileOutputAIMC::~FileOutputAIMC() {
tomwalters@46 60 if (file_handle_ != NULL)
tomwalters@46 61 CloseFile();
tomwalters@603 62 if (strobes_file_handle_ != NULL)
tomwalters@603 63 CloseStrobesFile();
tomwalters@603 64 }
tomwalters@603 65
tomwalters@603 66 bool FileOutputAIMC::OpenStrobesFile(string &filename) {
tomwalters@603 67 if (strobes_file_handle_ != NULL) {
tomwalters@603 68 LOG_ERROR(_T("Couldn't open strobes output file. A file is already open."));
tomwalters@603 69 return false;
tomwalters@603 70 }
tomwalters@603 71 // Check that the output file exists and is writeable
tomwalters@603 72 if ((strobes_file_handle_ = fopen(filename.c_str(), "wb")) == NULL) {
tomwalters@603 73 LOG_ERROR(_T("Couldn't open output file '%s' for writing."), filename.c_str());
tomwalters@603 74 return false;
tomwalters@603 75 }
tomwalters@603 76 return true;
tomwalters@46 77 }
tomwalters@46 78
tom@253 79 bool FileOutputAIMC::OpenFile(string &filename) {
tomwalters@46 80 if (file_handle_ != NULL) {
tomwalters@46 81 LOG_ERROR(_T("Couldn't open output file. A file is already open."));
tomwalters@46 82 return false;
tomwalters@46 83 }
tomwalters@46 84
tomwalters@46 85 // Check that the output file exists and is writeable
tom@253 86 if ((file_handle_ = fopen(filename.c_str(), "wb")) == NULL) {
tom@253 87 LOG_ERROR(_T("Couldn't open output file '%s' for writing."), filename.c_str());
tomwalters@46 88 return false;
tomwalters@46 89 }
tom@253 90 // Write temporary values for the frame count and frame period.
tomwalters@159 91 frame_count_ = 0;
tom@253 92 frame_period_ms_ = 0.0;
tomwalters@46 93 header_written_ = false;
tomwalters@46 94 if (initialized_) {
tomwalters@46 95 WriteHeader();
tomwalters@46 96 }
tomwalters@603 97
tomwalters@46 98 return true;
tomwalters@46 99 }
tomwalters@46 100
tomwalters@46 101 bool FileOutputAIMC::InitializeInternal(const SignalBank &input) {
tom@253 102 channel_count_ = input.channel_count();
tom@253 103 buffer_length_ = input.buffer_length();
tom@253 104 sample_rate_ = input.sample_rate();
tom@253 105 ResetInternal();
tomwalters@46 106 if (file_handle_ == NULL) {
tom@253 107 LOG_ERROR(_T("Couldn't initialize file output."));
tomwalters@46 108 return false;
tomwalters@46 109 }
tomwalters@46 110 return true;
tomwalters@46 111 }
tomwalters@46 112
tomwalters@46 113 void FileOutputAIMC::ResetInternal() {
tomwalters@46 114 if (file_handle_ != NULL && !header_written_) {
tomwalters@46 115 WriteHeader();
tomwalters@46 116 }
tomwalters@46 117 if (file_handle_ != NULL)
tomwalters@46 118 CloseFile();
tom@253 119
tom@253 120 string out_filename;
tom@253 121 out_filename = global_parameters_->GetString("output_filename_base") + file_suffix_;
tom@253 122 OpenFile(out_filename);
tomwalters@603 123 if (dump_strobes_) {
tomwalters@603 124 if (strobes_file_handle_ != NULL) {
tomwalters@603 125 CloseStrobesFile();
tomwalters@603 126 }
tomwalters@603 127 string strobes_filename =
tomwalters@603 128 global_parameters_->GetString("output_filename_base")
tomwalters@603 129 + strobes_file_suffix_;
tomwalters@603 130 OpenStrobesFile(strobes_filename);
tomwalters@603 131 }
tomwalters@46 132 }
tomwalters@46 133
tomwalters@46 134 void FileOutputAIMC::WriteHeader() {
tomwalters@46 135 if (header_written_)
tomwalters@46 136 return;
tomwalters@46 137
tomwalters@46 138 /* File format:
tomwalters@46 139 * Header: Number of frames (uint32),
tomwalters@46 140 * Frame period in milliseconds (float32),
tomwalters@46 141 * Number of channels per frame (uint32),
tomwalters@46 142 * Number of samples per channel of each frame (uint32)
tomwalters@46 143 * Sample rate (float32)
tomwalters@46 144 *
tomwalters@46 145 * Data: Series of floats, by time, then channel, then frame
tomwalters@46 146 * f1c1t1,f1c1t2,f1c1t3...
tomwalters@46 147 */
tomwalters@46 148
tomwalters@159 149 uint32_t frame_count_out = frame_count_;
tomwalters@46 150 float sample_period_out = frame_period_ms_;
tomwalters@46 151 uint32_t channels_out = channel_count_;
tomwalters@46 152 uint32_t samples_out = buffer_length_;
tomwalters@46 153 float sample_rate = sample_rate_;
tomwalters@46 154
tomwalters@159 155 fwrite(&frame_count_out, sizeof(frame_count_out), 1, file_handle_);
tomwalters@46 156 fwrite(&sample_period_out, sizeof(sample_period_out), 1, file_handle_);
tomwalters@46 157 fwrite(&channels_out, sizeof(channels_out), 1, file_handle_);
tomwalters@46 158 fwrite(&samples_out, sizeof(samples_out), 1, file_handle_);
tomwalters@46 159 fwrite(&sample_rate, sizeof(sample_rate), 1, file_handle_);
tomwalters@46 160 fflush(file_handle_);
tomwalters@46 161
tomwalters@46 162 header_written_ = true;
tomwalters@46 163 }
tomwalters@46 164
tomwalters@46 165 void FileOutputAIMC::Process(const SignalBank &input) {
tomwalters@46 166 if (file_handle_ == NULL) {
tomwalters@46 167 LOG_ERROR(_T("Couldn't process file output. No file is open."
tomwalters@46 168 "Please call FileOutputAIMC::OpenFile first"));
tomwalters@46 169 return;
tomwalters@46 170 }
tomwalters@46 171
tomwalters@46 172 if (!header_written_) {
tomwalters@46 173 LOG_ERROR(_T("No header has been written on the output file yet. Please "
tomwalters@46 174 "call FileOutputAIMC::Initialize() before calling "
tomwalters@46 175 "FileOutputAIMC::Process()"));
tomwalters@46 176 return;
tomwalters@46 177 }
tomwalters@46 178 float s;
tomwalters@46 179
tomwalters@46 180 for (int ch = 0; ch < input.channel_count(); ch++) {
tomwalters@46 181 for (int i = 0; i < input.buffer_length(); i++) {
tomwalters@46 182 s = input.sample(ch, i);
tomwalters@46 183 fwrite(&s, sizeof(s), 1, file_handle_);
tomwalters@46 184 }
tomwalters@46 185 }
tomwalters@159 186 frame_count_++;
tomwalters@603 187
tomwalters@603 188 if (dump_strobes_) {
tomwalters@603 189 if (strobes_file_handle_ == NULL) {
tomwalters@603 190 LOG_ERROR(_T("Couldn't process file output for strobes. No srobes file is open."
tomwalters@603 191 "Please call FileOutputAIMC::OpenStrobesFile first"));
tomwalters@603 192 return;
tomwalters@603 193 }
tomwalters@603 194 int strobe;
tomwalters@603 195 for (int ch = 0; ch < input.channel_count(); ch++) {
tomwalters@603 196 const int kStartOfStrobeRow = -65535;
tomwalters@603 197 strobe = kStartOfStrobeRow;
tomwalters@603 198 fwrite(&strobe, sizeof(strobe), 1, strobes_file_handle_);
tomwalters@603 199 for (int i = 0; i < static_cast<int>(input.get_strobes(ch).size());
tomwalters@603 200 i++) {
tomwalters@603 201 strobe = input.get_strobes(ch)[i];
tomwalters@603 202 fwrite(&strobe, sizeof(strobe), 1, strobes_file_handle_);
tomwalters@603 203 }
tomwalters@603 204 }
tomwalters@603 205 }
tomwalters@603 206
tomwalters@603 207 }
tomwalters@603 208
tomwalters@603 209 bool FileOutputAIMC::CloseStrobesFile() {
tomwalters@603 210 if (strobes_file_handle_ == NULL)
tomwalters@603 211 return false;
tomwalters@603 212
tomwalters@603 213 // And close the file
tomwalters@603 214 fclose(strobes_file_handle_);
tomwalters@603 215 strobes_file_handle_ = NULL;
tomwalters@603 216 return true;
tomwalters@46 217 }
tomwalters@46 218
tomwalters@46 219 bool FileOutputAIMC::CloseFile() {
tomwalters@46 220 if (file_handle_ == NULL)
tomwalters@46 221 return false;
tomwalters@46 222
tomwalters@46 223 // Write the first 4 bytes of the file
tomwalters@46 224 // with how many samples there are in the file
tomwalters@46 225 fflush(file_handle_);
tomwalters@46 226 rewind(file_handle_);
tomwalters@46 227 fflush(file_handle_);
tomwalters@159 228 uint32_t frame_count = frame_count_;
tom@253 229 float sample_period_out = frame_period_ms_;
tomwalters@159 230 fwrite(&frame_count, sizeof(frame_count), 1, file_handle_);
tom@253 231 fwrite(&sample_period_out, sizeof(sample_period_out), 1, file_handle_);
tomwalters@46 232
tomwalters@46 233 // And close the file
tomwalters@46 234 fclose(file_handle_);
tomwalters@46 235 file_handle_ = NULL;
tomwalters@46 236 header_written_ = false;
tomwalters@46 237 return true;
tomwalters@46 238 }
tomwalters@46 239 } // namespace aimc
tomwalters@46 240