comparison trunk/src/Modules/Output/FileOutputHTK.cc @ 402:69466da9745e

- Massive refactoring to make module tree stuff work. In theory we now support configuration files again. The graphics stuff is untested as yet.
author tomwalters
date Mon, 18 Oct 2010 04:42:28 +0000
parents c74acd46121b
children 3b22559cd848
comparison
equal deleted inserted replaced
401:b71ec2cbe55b 402:69466da9745e
43 FileOutputHTK::FileOutputHTK(Parameters *params) : Module(params) { 43 FileOutputHTK::FileOutputHTK(Parameters *params) : Module(params) {
44 module_description_ = "File output in HTK format"; 44 module_description_ = "File output in HTK format";
45 module_identifier_ = "htk_out"; 45 module_identifier_ = "htk_out";
46 module_type_ = "output"; 46 module_type_ = "output";
47 module_version_ = "$Id$"; 47 module_version_ = "$Id$";
48
49 file_suffix_ = parameters_->DefaultString("htk_out.file_suffix", ".htk");
48 50
49 file_handle_ = NULL; 51 file_handle_ = NULL;
50 header_written_ = false; 52 header_written_ = false;
51 frame_period_ms_ = 0.0f; 53 frame_period_ms_ = 0.0f;
54 previous_start_time_ = 0;
52 } 55 }
53 56
54 FileOutputHTK::~FileOutputHTK() { 57 FileOutputHTK::~FileOutputHTK() {
55 if (file_handle_ != NULL) 58 if (file_handle_ != NULL)
56 CloseFile(); 59 CloseFile();
57 } 60 }
58 61
59 bool FileOutputHTK::OpenFile(const char* filename, float frame_period_ms) {
60 if (file_handle_ != NULL) {
61 LOG_ERROR(_T("Couldn't open output file. A file is already open."));
62 return false;
63 }
64
65 // Check that the output file exists and is writeable
66 if ((file_handle_ = fopen(filename, "wb")) == NULL) {
67 LOG_ERROR(_T("Couldn't open output file '%s' for writing."), filename);
68 return false;
69 }
70 sample_count_ = 0;
71 frame_period_ms_ = frame_period_ms;
72 header_written_ = false;
73 if (initialized_) {
74 WriteHeader(channel_count_ * buffer_length_, frame_period_ms_);
75 }
76 return true;
77 }
78
79 bool FileOutputHTK::InitializeInternal(const SignalBank &input) { 62 bool FileOutputHTK::InitializeInternal(const SignalBank &input) {
80 if (file_handle_ == NULL) {
81 LOG_ERROR(_T("Couldn't initialize file output. "
82 "Please call FileOutputHTK::OpenFile first"));
83 return false;
84 }
85 if (header_written_) {
86 LOG_ERROR(_T("A header has already been written on the output file. "
87 "Please call FileOutputHTK::CloseFile to close that file, "
88 "and FileOutputHTK::OpenFile to open an new one before "
89 "calling FileOutputHTK::Initialize again."));
90 return false;
91 }
92 channel_count_ = input.channel_count(); 63 channel_count_ = input.channel_count();
93 buffer_length_ = input.buffer_length(); 64 buffer_length_ = input.buffer_length();
94 WriteHeader(channel_count_ * buffer_length_, frame_period_ms_); 65 ResetInternal();
66 if (file_handle_ == NULL) {
67 LOG_ERROR(_T("Couldn't initialize file output."));
68 return false;
69 }
70 if (!header_written_) {
71 WriteHeader(channel_count_ * buffer_length_, frame_period_ms_);
72 }
73
95 return true; 74 return true;
96 } 75 }
97 76
98 void FileOutputHTK::ResetInternal() { 77 void FileOutputHTK::ResetInternal() {
78 // Finalize and close the open file, if there is one.
99 if (file_handle_ != NULL && !header_written_) { 79 if (file_handle_ != NULL && !header_written_) {
100 WriteHeader(channel_count_ * buffer_length_, frame_period_ms_); 80 WriteHeader(channel_count_ * buffer_length_, frame_period_ms_);
101 } 81 }
102 if (file_handle_ != NULL) 82 if (file_handle_ != NULL)
103 CloseFile(); 83 CloseFile();
84
85 // Now open and set up the new file.
86 // Check that the output file exists and is writeable.
87 string out_filename;
88 out_filename = global_parameters_->GetString("output_filename_base") + file_suffix_;
89 if ((file_handle_ = fopen(out_filename.c_str(),
90 "wb")) == NULL) {
91 LOG_ERROR(_T("Couldn't open output file '%s' for writing."),
92 out_filename.c_str());
93 return;
94 }
95 sample_count_ = 0;
96 header_written_ = false;
97 WriteHeader(channel_count_ * buffer_length_, frame_period_ms_);
104 } 98 }
105 99
106 void FileOutputHTK::WriteHeader(int num_elements, float period_ms) { 100 void FileOutputHTK::WriteHeader(int num_elements, float period_ms) {
107 if (header_written_) 101 if (header_written_)
108 return; 102 return;
148 return; 142 return;
149 } 143 }
150 144
151 if (!header_written_) { 145 if (!header_written_) {
152 LOG_ERROR(_T("No header has been written on the output file yet. Please " 146 LOG_ERROR(_T("No header has been written on the output file yet. Please "
153 "call FileOutputHTK::Initialize() before calling " 147 "call FileOutputHTK::Initialize() or FileOutputHTK::Reset() "
154 "FileOutputHTK::Process()")); 148 " before calling FileOutputHTK::Process()"));
155 return; 149 return;
156 } 150 }
157 float s; 151 float s;
158 152
159 for (int ch = 0; ch < input.channel_count(); ch++) { 153 for (int ch = 0; ch < input.channel_count(); ch++) {
162 s = ByteSwapFloat(s); 156 s = ByteSwapFloat(s);
163 fwrite(&s, sizeof(s), 1, file_handle_); 157 fwrite(&s, sizeof(s), 1, file_handle_);
164 } 158 }
165 } 159 }
166 sample_count_++; 160 sample_count_++;
161 frame_period_ms_ = 1000.0
162 * (input.start_time() - previous_start_time_)
163 / input.sample_rate();
164 previous_start_time_ = input.start_time();
167 } 165 }
168 166
169 bool FileOutputHTK::CloseFile() { 167 bool FileOutputHTK::CloseFile() {
170 if (file_handle_ == NULL) 168 if (file_handle_ == NULL)
171 return false; 169 return false;