annotate src/Modules/Output/FileOutputJSON.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 49eef19e4f1d
children
rev   line source
sness@607 1 // Copyright 2006-2010, Thomas Walters
sness@607 2 //
sness@607 3 // AIM-C: A C++ implementation of the Auditory Image Model
sness@607 4 // http://www.acousticscale.org/AIMC
sness@607 5 //
sness@607 6 // Licensed under the Apache License, Version 2.0 (the "License");
sness@607 7 // you may not use this file except in compliance with the License.
sness@607 8 // You may obtain a copy of the License at
sness@607 9 //
sness@607 10 // http://www.apache.org/licenses/LICENSE-2.0
sness@607 11 //
sness@607 12 // Unless required by applicable law or agreed to in writing, software
sness@607 13 // distributed under the License is distributed on an "AS IS" BASIS,
sness@607 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sness@607 15 // See the License for the specific language governing permissions and
sness@607 16 // limitations under the License.
sness@607 17
sness@607 18 /*!
sness@607 19 * \file
sness@607 20 * \brief File output in the HTK format.
sness@607 21 *
sness@607 22 * \author Tom Walters <tom@acousticscale.org>
sness@607 23 * \author Willem van Engen <cnbh@willem.engen.nl>
sness@607 24 * \date created 2007/01/26
sness@607 25 * \version \$Id: $
sness@607 26 */
sness@607 27
sness@607 28
sness@607 29 #include <iostream>
sness@607 30 #include <fstream>
sness@607 31 #include <iomanip>
sness@607 32
sness@607 33 #include "Modules/Output/FileOutputJSON.h"
sness@607 34
sness@607 35 #ifdef _WINDOWS
sness@607 36 # include <direct.h> // for _mkdir & _rmdir
sness@607 37 #else
sness@607 38 # include <sys/types.h>
sness@607 39 # include <dirent.h> // for opendir & friends
sness@607 40 #endif
sness@607 41
sness@607 42 #include <stdint.h>
sness@607 43 #include <stdio.h>
sness@607 44 #include <string.h>
sness@607 45 #include <cmath>
sness@607 46 #include <string>
sness@607 47
sness@607 48 namespace aimc {
sness@607 49 FileOutputJSON::FileOutputJSON(Parameters *params) : Module(params) {
sness@607 50 module_description_ = "File output in JSON format";
sness@607 51 module_identifier_ = "json_out";
sness@607 52 module_type_ = "output";
sness@607 53 module_version_ = "$Id: FileOutputJSON.cc 51 2010-03-30 22:06:24Z tomwalters $";
sness@607 54 file_suffix_ = parameters_->DefaultString("file_suffix", ".json");
sness@607 55
sness@607 56 header_written_ = false;
sness@607 57 frame_period_ms_ = 0.0f;
sness@607 58 }
sness@607 59
sness@607 60 FileOutputJSON::~FileOutputJSON() {
sness@607 61 if (file_handle_)
sness@607 62 CloseFile();
sness@607 63 }
sness@607 64
sness@607 65 bool FileOutputJSON::OpenFile(string &filename) {
sness@607 66 file_handle_.open(filename.c_str());
sness@607 67
sness@607 68 frame_count_ = 0;
sness@607 69 frame_period_ms_ = 0.0;
sness@607 70 header_written_ = false;
sness@607 71
sness@607 72 if (initialized_) {
sness@607 73 WriteHeader();
sness@607 74 }
sness@607 75
sness@607 76 return true;
sness@607 77 }
sness@607 78
sness@607 79 bool FileOutputJSON::InitializeInternal(const SignalBank &input) {
sness@607 80 channel_count_ = input.channel_count();
sness@607 81 buffer_length_ = input.buffer_length();
sness@607 82 sample_rate_ = input.sample_rate();
sness@607 83 ResetInternal();
sness@607 84 if (file_handle_ == NULL) {
sness@607 85 LOG_ERROR(_T("Couldn't initialize file output."));
sness@607 86 return false;
sness@607 87 }
sness@607 88 return true;
sness@607 89 }
sness@607 90
sness@607 91 void FileOutputJSON::ResetInternal() {
sness@607 92 if (file_handle_ != NULL && !header_written_) {
sness@607 93 WriteHeader();
sness@607 94 }
sness@607 95 if (file_handle_ != NULL)
sness@607 96 CloseFile();
sness@607 97
sness@607 98 string out_filename;
sness@607 99 out_filename = global_parameters_->GetString("output_filename_base") + file_suffix_;
sness@607 100 OpenFile(out_filename);
sness@607 101 }
sness@607 102
sness@607 103 void FileOutputJSON::WriteHeader() {
sness@607 104 if (header_written_)
sness@607 105 return;
sness@607 106
sness@607 107 uint32_t channels_out = channel_count_;
sness@607 108 uint32_t samples_out = buffer_length_;
sness@607 109 float sample_rate = sample_rate_;
sness@607 110
sness@607 111 file_handle_ << std::setprecision(3);
sness@607 112 file_handle_ << "{" << std::endl;
sness@607 113 file_handle_ << "\"channels\" : " << channels_out << "," << std::endl;
sness@607 114 file_handle_ << "\"samples\" : " << sizeof(samples_out) << "," << std::endl;
sness@607 115 file_handle_ << "\"sample_rate\" : " << sizeof(sample_rate) << "," << std::endl;
sness@607 116
sness@607 117 header_written_ = true;
sness@607 118 }
sness@607 119
sness@607 120 void FileOutputJSON::Process(const SignalBank &input) {
sness@607 121 if (file_handle_ == NULL) {
sness@607 122 LOG_ERROR(_T("Couldn't process file output. No file is open."
sness@607 123 "Please call FileOutputJSON::OpenFile first"));
sness@607 124 return;
sness@607 125 }
sness@607 126
sness@607 127 if (!header_written_) {
sness@607 128 LOG_ERROR(_T("No header has been written on the output file yet. Please "
sness@607 129 "call FileOutputJSON::Initialize() before calling "
sness@607 130 "FileOutputJSON::Process()"));
sness@607 131 return;
sness@607 132 }
sness@607 133 float s;
sness@607 134
sness@607 135 for (int ch = 0; ch < input.channel_count(); ch++) {
sness@607 136 file_handle_ << "\"channel\" : [";
sness@607 137 for (int i = 0; i < input.buffer_length(); i++) {
sness@607 138 s = input.sample(ch, i);
sness@607 139 file_handle_ << s;
sness@607 140 if (i < input.buffer_length() - 1) {
sness@607 141 file_handle_ << ",";
sness@607 142 }
sness@607 143 }
sness@607 144 file_handle_ << "]" << std::endl;
sness@607 145 if (ch < input.channel_count() - 1) {
sness@607 146 file_handle_ << ",";
sness@607 147 }
sness@607 148 }
sness@607 149
sness@607 150 frame_count_++;
sness@607 151
sness@607 152 }
sness@607 153
sness@607 154 bool FileOutputJSON::CloseFile() {
sness@607 155 if (file_handle_ == NULL)
sness@607 156 return false;
sness@607 157
sness@607 158 uint32_t frame_count = frame_count_;
sness@607 159 float sample_period_out = frame_period_ms_;
sness@607 160
sness@607 161 file_handle_ << "\"frame_count\" : " << sizeof(frame_count) << std::endl;
sness@607 162 file_handle_ << "\"samples\" : " << sizeof(sample_period_out) << std::endl;
sness@607 163 file_handle_ << "}" << std::endl;
sness@607 164
sness@607 165
sness@607 166 file_handle_.close();
sness@607 167 header_written_ = false;
sness@607 168 return true;
sness@607 169 }
sness@607 170 } // namespace aimc
sness@607 171