tomwalters@277
|
1 // Copyright 2006-2010, Thomas Walters
|
tomwalters@277
|
2 //
|
tomwalters@277
|
3 // AIM-C: A C++ implementation of the Auditory Image Model
|
tomwalters@277
|
4 // http://www.acousticscale.org/AIMC
|
tomwalters@277
|
5 //
|
tomwalters@277
|
6 // This program is free software: you can redistribute it and/or modify
|
tomwalters@277
|
7 // it under the terms of the GNU General Public License as published by
|
tomwalters@277
|
8 // the Free Software Foundation, either version 3 of the License, or
|
tomwalters@277
|
9 // (at your option) any later version.
|
tomwalters@277
|
10 //
|
tomwalters@277
|
11 // This program is distributed in the hope that it will be useful,
|
tomwalters@277
|
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
tomwalters@277
|
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
tomwalters@277
|
14 // GNU General Public License for more details.
|
tomwalters@277
|
15 //
|
tomwalters@277
|
16 // You should have received a copy of the GNU General Public License
|
tomwalters@277
|
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
|
tomwalters@277
|
18
|
tomwalters@277
|
19 /*!
|
tomwalters@277
|
20 * \file
|
tomwalters@277
|
21 * \brief File output in the HTK format.
|
tomwalters@277
|
22 *
|
tomwalters@277
|
23 * \author Tom Walters <tom@acousticscale.org>
|
tomwalters@277
|
24 * \author Willem van Engen <cnbh@willem.engen.nl>
|
tomwalters@277
|
25 * \date created 2006/10/30
|
tomwalters@277
|
26 * \version \$Id$
|
tomwalters@277
|
27 */
|
tomwalters@277
|
28
|
tomwalters@277
|
29 #ifdef _WINDOWS
|
tomwalters@277
|
30 # include <direct.h> // for _mkdir&_rmdir
|
tomwalters@277
|
31 #else
|
tomwalters@277
|
32 # include <sys/types.h>
|
tomwalters@277
|
33 # include <dirent.h> // for opendir&friends
|
tomwalters@277
|
34 #endif
|
tomwalters@277
|
35 #include <stdio.h>
|
tomwalters@277
|
36 #include <string.h>
|
tomwalters@277
|
37 #include <cmath>
|
tomwalters@277
|
38
|
tomwalters@277
|
39 #include "Modules/Output/FileOutputHTK.h"
|
tomwalters@277
|
40
|
tomwalters@277
|
41 namespace aimc {
|
tomwalters@277
|
42 FileOutputHTK::FileOutputHTK(Parameters *params) : Module(params) {
|
tomwalters@277
|
43 file_handle_ = NULL;
|
tomwalters@277
|
44 header_written_ = false;
|
tomwalters@277
|
45 filename_[0] = '\0';
|
tomwalters@277
|
46 frame_period_ms_ = 0.0f;
|
tomwalters@277
|
47 }
|
tomwalters@277
|
48
|
tomwalters@277
|
49 FileOutputHTK::~FileOutputHTK() {
|
tomwalters@277
|
50 if (file_handle_ != NULL)
|
tomwalters@277
|
51 CloseFile();
|
tomwalters@277
|
52 }
|
tomwalters@277
|
53
|
tomwalters@277
|
54 bool FileOutputHTK::OpenFile(const char* filename, float frame_period_ms) {
|
tomwalters@277
|
55 if (file_handle_ != NULL) {
|
tomwalters@277
|
56 LOG_ERROR(_T("Couldn't open output file. A file is already open."));
|
tomwalters@277
|
57 return false;
|
tomwalters@277
|
58 }
|
tomwalters@277
|
59
|
tomwalters@277
|
60 // Check that the output file exists and is writeable
|
tomwalters@277
|
61 if ((file_handle_ = fopen(filename, "wb"))==NULL ) {
|
tomwalters@277
|
62 LOG_ERROR(_T("Couldn't open output file '%s' for writing."), filename);
|
tomwalters@277
|
63 return false;
|
tomwalters@277
|
64 }
|
tomwalters@277
|
65 strcpy(filename_, filename);
|
tomwalters@277
|
66 sample_count_ = 0;
|
tomwalters@277
|
67 frame_period_ms_ = frame_period_ms;
|
tomwalters@277
|
68 header_written_ = false;
|
tomwalters@277
|
69 return true;
|
tomwalters@277
|
70 }
|
tomwalters@277
|
71
|
tomwalters@277
|
72 bool FileOutputHTK::InitializeInternal(const SignalBank &input) {
|
tomwalters@277
|
73 if (file_handle_ == NULL) {
|
tomwalters@277
|
74 LOG_ERROR(_T("Couldn't initialize file output. "
|
tomwalters@277
|
75 "Please call FileOutputHTK::OpenFile first"));
|
tomwalters@277
|
76 return false;
|
tomwalters@277
|
77 }
|
tomwalters@277
|
78 if (header_written_) {
|
tomwalters@277
|
79 LOG_ERROR(_T("A header has already been written on the output file."
|
tomwalters@277
|
80 "Please call FileOutputHTK::CloseFile to close that file, "
|
tomwalters@277
|
81 "and FileOutputHTK::OpenFile to open an new one before "
|
tomwalters@277
|
82 "calling FileOutputHTK::Initialize again."));
|
tomwalters@277
|
83 return false;
|
tomwalters@277
|
84 }
|
tomwalters@277
|
85 channel_count_ = input.channel_count();
|
tomwalters@277
|
86 buffer_length_ = input.buffer_length();
|
tomwalters@277
|
87 WriteHeader(channel_count_ * buffer_length_, frame_period_ms_);
|
tomwalters@277
|
88 return true;
|
tomwalters@277
|
89 }
|
tomwalters@277
|
90
|
tomwalters@277
|
91 void FileOutputHTK::ResetInternal() {
|
tomwalters@277
|
92 if (file_handle_ != NULL && !header_written_) {
|
tomwalters@277
|
93 WriteHeader(channel_count_ * buffer_length_, frame_period_ms_);
|
tomwalters@277
|
94 }
|
tomwalters@277
|
95 }
|
tomwalters@277
|
96
|
tomwalters@277
|
97 void FileOutputHTK::WriteHeader(int num_elements, float period_ms) {
|
tomwalters@277
|
98 if (header_written_)
|
tomwalters@277
|
99 return;
|
tomwalters@277
|
100
|
tomwalters@277
|
101 /* HTK format file: (taken from the HTK book - section 5.10.1)
|
tomwalters@277
|
102 * Header: 12 bytes in total, contains:
|
tomwalters@277
|
103 * sample_count - number of samples in file (4-byte integer)(long)
|
tomwalters@277
|
104 * sample_period - sample period in 100ns units (4-byte integer)(long)
|
tomwalters@277
|
105 * sample_size - number of bytes per sample (2-byte integer)(short)
|
tomwalters@277
|
106 * parameter_kind - a code indicating the sample kind (2-byte integer)(short)
|
tomwalters@277
|
107 */
|
tomwalters@277
|
108
|
tomwalters@277
|
109 // To be filled in when the file is done
|
tomwalters@277
|
110 int32_t sample_count = 0;
|
tomwalters@277
|
111
|
tomwalters@277
|
112 int32_t sample_period = floor(1e4 * period_ms);
|
tomwalters@277
|
113 int16_t sample_size = num_elements * sizeof(float);
|
tomwalters@277
|
114
|
tomwalters@277
|
115 // User-defined coefficients with energy term
|
tomwalters@277
|
116 int16_t parameter_kind = H_USER + H_E;
|
tomwalters@277
|
117
|
tomwalters@277
|
118 // Fix endianness
|
tomwalters@277
|
119 sample_count = ByteSwap32(sample_count);
|
tomwalters@277
|
120 sample_period = ByteSwap32(sample_period);
|
tomwalters@277
|
121 sample_size = ByteSwap16(sample_size);
|
tomwalters@277
|
122 parameter_kind = ByteSwap16(parameter_kind);
|
tomwalters@277
|
123
|
tomwalters@277
|
124 // Enter header values. sample_count is a dummy value which is filled in on
|
tomwalters@277
|
125 // file close
|
tomwalters@277
|
126 fwrite(&sample_count, sizeof(sample_count), 1, file_handle_);
|
tomwalters@277
|
127 fwrite(&sample_period, sizeof(sample_period), 1, file_handle_);
|
tomwalters@277
|
128 fwrite(&sample_size, sizeof(sample_size), 1, file_handle_);
|
tomwalters@277
|
129 fwrite(¶meter_kind, sizeof(parameter_kind), 1, file_handle_);
|
tomwalters@277
|
130 fflush(file_handle_);
|
tomwalters@277
|
131
|
tomwalters@277
|
132 header_written_ = true;
|
tomwalters@277
|
133 }
|
tomwalters@277
|
134
|
tomwalters@277
|
135
|
tomwalters@277
|
136 void FileOutputHTK::Process(const SignalBank &input) {
|
tomwalters@277
|
137 if (file_handle_ == NULL) {
|
tomwalters@277
|
138 LOG_ERROR(_T("Couldn't process file output. No file is open."
|
tomwalters@277
|
139 "Please call FileOutputHTK::OpenFile first"));
|
tomwalters@277
|
140 return;
|
tomwalters@277
|
141 }
|
tomwalters@277
|
142
|
tomwalters@277
|
143 if (!header_written_) {
|
tomwalters@277
|
144 LOG_ERROR(_T("No header has been written on the output file yet. Please"
|
tomwalters@277
|
145 "call FileOutputHTK::Initialize() before calling "
|
tomwalters@277
|
146 "FileOutputHTK::Process()"));
|
tomwalters@277
|
147 return;
|
tomwalters@277
|
148 }
|
tomwalters@277
|
149 float s;
|
tomwalters@277
|
150
|
tomwalters@277
|
151 for (int ch = 0; ch < input.channel_count(); ch++) {
|
tomwalters@277
|
152 for (int i = 0; i < input.buffer_length(); i++) {
|
tomwalters@277
|
153 s = input.sample(ch, i);
|
tomwalters@277
|
154 s = ByteSwapFloat(s);
|
tomwalters@277
|
155 fwrite(&s, sizeof(float), 1, file_handle_);
|
tomwalters@277
|
156 }
|
tomwalters@277
|
157 }
|
tomwalters@277
|
158 sample_count_++;
|
tomwalters@277
|
159 }
|
tomwalters@277
|
160
|
tomwalters@277
|
161 bool FileOutputHTK::CloseFile() {
|
tomwalters@277
|
162 if (file_handle_ == NULL)
|
tomwalters@277
|
163 return false;
|
tomwalters@277
|
164
|
tomwalters@277
|
165 // Write the first 4 bytes of the file
|
tomwalters@277
|
166 // with how many samples there are in the file
|
tomwalters@277
|
167 fflush(file_handle_);
|
tomwalters@277
|
168 rewind(file_handle_);
|
tomwalters@277
|
169 fflush(file_handle_);
|
tomwalters@277
|
170 int32_t samples = sample_count_;
|
tomwalters@277
|
171 samples = ByteSwap32(samples);
|
tomwalters@277
|
172 fwrite(&samples, sizeof(samples), 1, file_handle_);
|
tomwalters@277
|
173
|
tomwalters@277
|
174 // And close the file
|
tomwalters@277
|
175 fclose(file_handle_);
|
tomwalters@277
|
176 file_handle_ = NULL;
|
tomwalters@277
|
177 return true;
|
tomwalters@277
|
178 }
|
tomwalters@277
|
179
|
tomwalters@277
|
180 float FileOutputHTK::ByteSwapFloat(float d) {
|
tomwalters@277
|
181 // Endianness fix
|
tomwalters@277
|
182 float a;
|
tomwalters@277
|
183 unsigned char *dst = (unsigned char *)&a;
|
tomwalters@277
|
184 unsigned char *src = (unsigned char *)&d;
|
tomwalters@277
|
185
|
tomwalters@277
|
186 dst[0] = src[3];
|
tomwalters@277
|
187 dst[1] = src[2];
|
tomwalters@277
|
188 dst[2] = src[1];
|
tomwalters@277
|
189 dst[3] = src[0];
|
tomwalters@277
|
190
|
tomwalters@277
|
191 return a;
|
tomwalters@277
|
192 }
|
tomwalters@277
|
193 } //namespace aimc
|
tomwalters@277
|
194
|