comparison trunk/src/Modules/Output/FileOutputAIMC.cc @ 614:b5dca605c3d5

Add option to dump strobes
author tomwalters@google.com
date Tue, 07 May 2013 14:24:33 +0000
parents 99f9bf0f7798
children
comparison
equal deleted inserted replaced
613:c14c83f3d5d6 614:b5dca605c3d5
23 * \author Willem van Engen <cnbh@willem.engen.nl> 23 * \author Willem van Engen <cnbh@willem.engen.nl>
24 * \date created 2007/01/26 24 * \date created 2007/01/26
25 * \version \$Id: $ 25 * \version \$Id: $
26 */ 26 */
27 27
28 #include "Modules/Output/FileOutputAIMC.h"
29
28 #ifdef _WINDOWS 30 #ifdef _WINDOWS
29 # include <direct.h> // for _mkdir & _rmdir 31 # include <direct.h> // for _mkdir & _rmdir
30 #else 32 #else
31 # include <sys/types.h> 33 # include <sys/types.h>
32 # include <dirent.h> // for opendir & friends 34 # include <dirent.h> // for opendir & friends
35 #include <stdint.h> 37 #include <stdint.h>
36 #include <stdio.h> 38 #include <stdio.h>
37 #include <string.h> 39 #include <string.h>
38 #include <cmath> 40 #include <cmath>
39 #include <string> 41 #include <string>
40
41 #include "Modules/Output/FileOutputAIMC.h"
42 42
43 namespace aimc { 43 namespace aimc {
44 FileOutputAIMC::FileOutputAIMC(Parameters *params) : Module(params) { 44 FileOutputAIMC::FileOutputAIMC(Parameters *params) : Module(params) {
45 module_description_ = "File output in AIMC format"; 45 module_description_ = "File output in AIMC format";
46 module_identifier_ = "aimc_out"; 46 module_identifier_ = "aimc_out";
47 module_type_ = "output"; 47 module_type_ = "output";
48 module_version_ = "$Id: FileOutputAIMC.cc 51 2010-03-30 22:06:24Z tomwalters $"; 48 module_version_ = "$Id: FileOutputAIMC.cc 51 2010-03-30 22:06:24Z tomwalters $";
49 file_suffix_ = parameters_->DefaultString("file_suffix", ".aimc"); 49 file_suffix_ = parameters_->DefaultString("file_suffix", ".aimc");
50 dump_strobes_ = parameters_->DefaultBool("dump_strobes", false);
51 strobes_file_suffix_ = parameters_->DefaultString("strobes_file_suffix", ".strobes");
50 52
51 file_handle_ = NULL; 53 file_handle_ = NULL;
54 strobes_file_handle_ = NULL;
52 header_written_ = false; 55 header_written_ = false;
53 frame_period_ms_ = 0.0f; 56 frame_period_ms_ = 0.0f;
54 } 57 }
55 58
56 FileOutputAIMC::~FileOutputAIMC() { 59 FileOutputAIMC::~FileOutputAIMC() {
57 if (file_handle_ != NULL) 60 if (file_handle_ != NULL)
58 CloseFile(); 61 CloseFile();
62 if (strobes_file_handle_ != NULL)
63 CloseStrobesFile();
64 }
65
66 bool FileOutputAIMC::OpenStrobesFile(string &filename) {
67 if (strobes_file_handle_ != NULL) {
68 LOG_ERROR(_T("Couldn't open strobes output file. A file is already open."));
69 return false;
70 }
71 // Check that the output file exists and is writeable
72 if ((strobes_file_handle_ = fopen(filename.c_str(), "wb")) == NULL) {
73 LOG_ERROR(_T("Couldn't open output file '%s' for writing."), filename.c_str());
74 return false;
75 }
76 return true;
59 } 77 }
60 78
61 bool FileOutputAIMC::OpenFile(string &filename) { 79 bool FileOutputAIMC::OpenFile(string &filename) {
62 if (file_handle_ != NULL) { 80 if (file_handle_ != NULL) {
63 LOG_ERROR(_T("Couldn't open output file. A file is already open.")); 81 LOG_ERROR(_T("Couldn't open output file. A file is already open."));
74 frame_period_ms_ = 0.0; 92 frame_period_ms_ = 0.0;
75 header_written_ = false; 93 header_written_ = false;
76 if (initialized_) { 94 if (initialized_) {
77 WriteHeader(); 95 WriteHeader();
78 } 96 }
97
79 return true; 98 return true;
80 } 99 }
81 100
82 bool FileOutputAIMC::InitializeInternal(const SignalBank &input) { 101 bool FileOutputAIMC::InitializeInternal(const SignalBank &input) {
83 channel_count_ = input.channel_count(); 102 channel_count_ = input.channel_count();
99 CloseFile(); 118 CloseFile();
100 119
101 string out_filename; 120 string out_filename;
102 out_filename = global_parameters_->GetString("output_filename_base") + file_suffix_; 121 out_filename = global_parameters_->GetString("output_filename_base") + file_suffix_;
103 OpenFile(out_filename); 122 OpenFile(out_filename);
104 123 if (dump_strobes_) {
124 if (strobes_file_handle_ != NULL) {
125 CloseStrobesFile();
126 }
127 string strobes_filename =
128 global_parameters_->GetString("output_filename_base")
129 + strobes_file_suffix_;
130 OpenStrobesFile(strobes_filename);
131 }
105 } 132 }
106 133
107 void FileOutputAIMC::WriteHeader() { 134 void FileOutputAIMC::WriteHeader() {
108 if (header_written_) 135 if (header_written_)
109 return; 136 return;
155 s = input.sample(ch, i); 182 s = input.sample(ch, i);
156 fwrite(&s, sizeof(s), 1, file_handle_); 183 fwrite(&s, sizeof(s), 1, file_handle_);
157 } 184 }
158 } 185 }
159 frame_count_++; 186 frame_count_++;
187
188 if (dump_strobes_) {
189 if (strobes_file_handle_ == NULL) {
190 LOG_ERROR(_T("Couldn't process file output for strobes. No srobes file is open."
191 "Please call FileOutputAIMC::OpenStrobesFile first"));
192 return;
193 }
194 int strobe;
195 for (int ch = 0; ch < input.channel_count(); ch++) {
196 const int kStartOfStrobeRow = -65535;
197 strobe = kStartOfStrobeRow;
198 fwrite(&strobe, sizeof(strobe), 1, strobes_file_handle_);
199 for (int i = 0; i < static_cast<int>(input.get_strobes(ch).size());
200 i++) {
201 strobe = input.get_strobes(ch)[i];
202 fwrite(&strobe, sizeof(strobe), 1, strobes_file_handle_);
203 }
204 }
205 }
206
207 }
208
209 bool FileOutputAIMC::CloseStrobesFile() {
210 if (strobes_file_handle_ == NULL)
211 return false;
212
213 // And close the file
214 fclose(strobes_file_handle_);
215 strobes_file_handle_ = NULL;
216 return true;
160 } 217 }
161 218
162 bool FileOutputAIMC::CloseFile() { 219 bool FileOutputAIMC::CloseFile() {
163 if (file_handle_ == NULL) 220 if (file_handle_ == NULL)
164 return false; 221 return false;