comparison trunk/src/Modules/Output/FileOutputJSON.cc @ 618:e01caec17186

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