comparison trunk/src/Modules/Output/FileOutputAIMC.cc @ 440:99f9bf0f7798

- AIMC format file output
author tom@acousticscale.org
date Thu, 04 Nov 2010 19:48:53 +0000
parents 69466da9745e
children b5dca605c3d5
comparison
equal deleted inserted replaced
439:2fca84ddd8e3 440:99f9bf0f7798
34 34
35 #include <stdint.h> 35 #include <stdint.h>
36 #include <stdio.h> 36 #include <stdio.h>
37 #include <string.h> 37 #include <string.h>
38 #include <cmath> 38 #include <cmath>
39 #include <string>
39 40
40 #include "Modules/Output/FileOutputAIMC.h" 41 #include "Modules/Output/FileOutputAIMC.h"
41 42
42 namespace aimc { 43 namespace aimc {
43 FileOutputAIMC::FileOutputAIMC(Parameters *params) : Module(params) { 44 FileOutputAIMC::FileOutputAIMC(Parameters *params) : Module(params) {
44 module_description_ = "File output in AIMC format"; 45 module_description_ = "File output in AIMC format";
45 module_identifier_ = "aimc_out"; 46 module_identifier_ = "aimc_out";
46 module_type_ = "output"; 47 module_type_ = "output";
47 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");
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;
52 } 54 }
54 FileOutputAIMC::~FileOutputAIMC() { 56 FileOutputAIMC::~FileOutputAIMC() {
55 if (file_handle_ != NULL) 57 if (file_handle_ != NULL)
56 CloseFile(); 58 CloseFile();
57 } 59 }
58 60
59 bool FileOutputAIMC::OpenFile(const char* filename, float frame_period_ms) { 61 bool FileOutputAIMC::OpenFile(string &filename) {
60 if (file_handle_ != NULL) { 62 if (file_handle_ != NULL) {
61 LOG_ERROR(_T("Couldn't open output file. A file is already open.")); 63 LOG_ERROR(_T("Couldn't open output file. A file is already open."));
62 return false; 64 return false;
63 } 65 }
64 66
65 // Check that the output file exists and is writeable 67 // Check that the output file exists and is writeable
66 if ((file_handle_ = fopen(filename, "wb")) == NULL) { 68 if ((file_handle_ = fopen(filename.c_str(), "wb")) == NULL) {
67 LOG_ERROR(_T("Couldn't open output file '%s' for writing."), filename); 69 LOG_ERROR(_T("Couldn't open output file '%s' for writing."), filename.c_str());
68 return false; 70 return false;
69 } 71 }
72 // Write temporary values for the frame count and frame period.
70 frame_count_ = 0; 73 frame_count_ = 0;
71 frame_period_ms_ = frame_period_ms; 74 frame_period_ms_ = 0.0;
72 header_written_ = false; 75 header_written_ = false;
73 if (initialized_) { 76 if (initialized_) {
74 WriteHeader(); 77 WriteHeader();
75 } 78 }
76 return true; 79 return true;
77 } 80 }
78 81
79 bool FileOutputAIMC::InitializeInternal(const SignalBank &input) { 82 bool FileOutputAIMC::InitializeInternal(const SignalBank &input) {
83 channel_count_ = input.channel_count();
84 buffer_length_ = input.buffer_length();
85 sample_rate_ = input.sample_rate();
86 ResetInternal();
80 if (file_handle_ == NULL) { 87 if (file_handle_ == NULL) {
81 LOG_ERROR(_T("Couldn't initialize file output. " 88 LOG_ERROR(_T("Couldn't initialize file output."));
82 "Please call FileOutputAIMC::OpenFile first"));
83 return false; 89 return false;
84 } 90 }
85 if (header_written_) {
86 LOG_ERROR(_T("A header has already been written on the output file. "
87 "Please call FileOutputAIMC::CloseFile to close that file, "
88 "and FileOutputAIMC::OpenFile to open an new one before "
89 "calling FileOutputAIMC::Initialize again."));
90 return false;
91 }
92 channel_count_ = input.channel_count();
93 buffer_length_ = input.buffer_length();
94 sample_rate_ = input.sample_rate();
95 WriteHeader();
96 return true; 91 return true;
97 } 92 }
98 93
99 void FileOutputAIMC::ResetInternal() { 94 void FileOutputAIMC::ResetInternal() {
100 if (file_handle_ != NULL && !header_written_) { 95 if (file_handle_ != NULL && !header_written_) {
101 WriteHeader(); 96 WriteHeader();
102 } 97 }
103 if (file_handle_ != NULL) 98 if (file_handle_ != NULL)
104 CloseFile(); 99 CloseFile();
100
101 string out_filename;
102 out_filename = global_parameters_->GetString("output_filename_base") + file_suffix_;
103 OpenFile(out_filename);
104
105 } 105 }
106 106
107 void FileOutputAIMC::WriteHeader() { 107 void FileOutputAIMC::WriteHeader() {
108 if (header_written_) 108 if (header_written_)
109 return; 109 return;
167 // with how many samples there are in the file 167 // with how many samples there are in the file
168 fflush(file_handle_); 168 fflush(file_handle_);
169 rewind(file_handle_); 169 rewind(file_handle_);
170 fflush(file_handle_); 170 fflush(file_handle_);
171 uint32_t frame_count = frame_count_; 171 uint32_t frame_count = frame_count_;
172 float sample_period_out = frame_period_ms_;
172 fwrite(&frame_count, sizeof(frame_count), 1, file_handle_); 173 fwrite(&frame_count, sizeof(frame_count), 1, file_handle_);
174 fwrite(&sample_period_out, sizeof(sample_period_out), 1, file_handle_);
173 175
174 // And close the file 176 // And close the file
175 fclose(file_handle_); 177 fclose(file_handle_);
176 file_handle_ = NULL; 178 file_handle_ = NULL;
177 header_written_ = false; 179 header_written_ = false;