Mercurial > hg > aimc
changeset 157:ce97ae23c66b
- Added support for a very basic AIM-C file format
author | tomwalters@google.com |
---|---|
date | Thu, 27 May 2010 07:25:03 +0000 |
parents | c257ef1919f3 |
children | eeeb5dceb60e |
files | SConstruct matlab/AIMCread.m src/Main/aimc.cc src/Modules/Output/FileOutputAIMC.cc src/Modules/Output/FileOutputAIMC.h src/Modules/Output/FileOutputHTK.cc |
diffstat | 6 files changed, 255 insertions(+), 305 deletions(-) [+] |
line wrap: on
line diff
--- a/SConstruct Thu Jan 13 18:58:15 2011 +0000 +++ b/SConstruct Thu May 27 07:25:03 2010 +0000 @@ -1,228 +1,179 @@ -# Copyright 2006-2010, Willem van Engen, Thomas Walters -# -# AIM-C: A C++ implementation of the Auditory Image Model -# http://www.acousticscale.org/AIMC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -## @author Thomas Walters <tom@acousticscale.org> -# @author Willem van Engen <cnbh@willem.engen.nl> -# @date created 2010/02/02 -# @version \$Id$ - -"""@package SConstruct -SConstruct file for the aimc project - -""" - -import os -import shutil - -# Location of libraries / headers on Windows -windows_libsndfile_location = "C:\\Program Files\\Mega-Nerd\\libsndfile\\" -windows_cairo_location = "C:\\Program Files\\cairo\\" - -# Sources common to every version -common_sources = ['Support/Common.cc', - 'Support/FileList.cc', - 'Support/SignalBank.cc', - 'Support/Parameters.cc', - 'Support/Module.cc', - 'Support/ModuleFactory.cc', - 'Support/ModuleTree.cc', - 'Modules/Input/ModuleFileInput.cc', - 'Modules/BMM/ModuleGammatone.cc', - 'Modules/BMM/ModulePZFC.cc', - 'Modules/NAP/ModuleHCL.cc', - 'Modules/Strobes/ModuleParabola.cc', - 'Modules/Strobes/ModuleLocalMax.cc', - 'Modules/SAI/ModuleSAI.cc', - 'Modules/SSI/ModuleSSI.cc', - 'Modules/Profile/ModuleSlice.cc', - 'Modules/Profile/ModuleScaler.cc', - 'Modules/Output/FileOutputHTK.cc', - 'Modules/Output/FileOutputAIMC.cc', - 'Modules/Features/ModuleGaussians.cc', - 'Modules/Features/ModuleBoxes.cc',] - #'Modules/Features/ModuleDCT.cc' ] - -graphics_sources = [ 'Modules/Output/Graphics/GraphAxisSpec.cc', - 'Modules/Output/Graphics/GraphicsView.cc', - 'Modules/Output/Graphics/GraphicsViewTime.cc', - 'Modules/Output/Graphics/Scale/Scale.cc', - 'Modules/Output/Graphics/Devices/GraphicsOutputDevice.cc', - 'Modules/Output/Graphics/Devices/GraphicsOutputDeviceCairo.cc', - 'Modules/Output/Graphics/Devices/GraphicsOutputDeviceMovie.cc',] - #'Modules/Output/Graphics/Devices/GraphicsOutputDeviceMovieDirect.cc' ] -graphics_libraries = [ 'cairo', - ] - -# List of currently incative source files which we may want to add back in -sources_disabled = ['Modules/SNR/ModuleNoise.cc', - ] - -# File which contains main() -#sources = common_sources + graphics_sources + ['Main/AIMCopy_SSI_Features_v3.cc'] -#sources = common_sources + ['Main/AIMCopy_SSI_Features_v4_PZFC.cc'] -sources = common_sources + graphics_sources + ['Main/AIMCopy.cc'] -#sources = common_sources + ['Main/aimc.cc'] - -# Test sources -test_sources = ['Modules/Profile/ModuleSlice_unittest.cc'] -test_sources += common_sources - -# Define the command-line options for running scons -options = Variables() -options.Add(BoolVariable('mingw', - 'Compile on Windows using mingw rather than msvc', - False)) -options.Add(BoolVariable('symbols', - 'Add debuging symbols when compiling on gcc', - False)) - -# Environment variables -env = Environment(options = options, ENV = os.environ) -if env['mingw']: - # SCons Defaults to MSVC if installed on Windows. - env = Environment(options = opts, ENV = os.environ, tools = ['mingw']) - -# Platform -build_platform = env['PLATFORM'] -target_platform = build_platform - -# Build products location and executable name -build_dir = os.path.join('build', target_platform + '-release') -#target_executable = 'aimc' -target_executable = 'AIMCopy' -test_executable = 'aimc_tests' - -# Create build products directory if necessary -if not os.path.exists(build_dir): - os.makedirs(build_dir) -env.SConsignFile(os.path.join(build_dir,'.sconsign')) - -# Set any platform-specific environment variables and options -if target_platform == 'win32': - env.AppendUnique(CPPDEFINES = ['_WINDOWS', 'WIN32', - 'WINVER=0x0400', '_CONSOLE']) -elif target_platform == 'darwin': - env.AppendUnique(CPPDEFINES = ['_MACOSX']) - -# Compiler selection based on platform -# compiler can be one of: gcc msvc -compiler = 'gcc' -if (build_platform == 'win32' - and target_platform == 'win32' - and not env['mingw']): - compiler = 'msvc' - -# Compiler-specific options: -# Microsoft visual studio -if compiler == 'msvc': - env.AppendUnique(CPPFLAGS = ['/arch:SSE2', '/nologo', '/W3', '/EHsc']) - env.AppendUnique(CPPDEFINES = ['_CRT_SECURE_NO_DEPRECATE', - '_RINT_REQUIRED']) - env.AppendUnique(CPPFLAGS = ['/Ox']) - env.AppendUnique(CPPDEFINES = ['NDEBUG', '_ATL_MIN_CRT']) - -# GNU compiler collection -elif compiler == 'gcc': - env['STRIP'] = 'strip' - env.AppendUnique(CPPFLAGS = ['-Wall']) - env.AppendUnique(CPPFLAGS = ['-O1',])# '-fomit-frame-pointer']) - if env['symbols']: - env.AppendUnique(CPPFLAGS = ['-g']) - if env['mingw']: - if not env['PLATFORM'] == 'win32': - print('Cross-compilation for Windows is not supported') - Exit(1) -else: - print('Unsupported compiler: ' + compiler) - Exit(1) - -# To make a statically-linked version for os 10.4 and up... -#if build_platform == 'darwin': -# env.AppendUnique(CPPFLAGS = ['-arch', 'i386']) -# env.AppendUnique(LINKFLAGS = ['-arch', 'i386']) -# env.AppendUnique(LINKFLAGS = ['-Wl']) -# env.AppendUnique(LINKFLAGS = ['-search_paths_first']) -# env.AppendUnique(MACOSX_DEPLOYMENT_TARGET = ['10.4']) -# env.AppendUnique(GCC_VERSION = ['4.0']) -# env.Replace(CC = ['gcc-4.0']) -# env.Replace(CXX = ['gcc-4.0']) -# env.AppendUnique(CPPFLAGS = ['-fno-stack-protector','-isysroot', '/Developer/SDKs/MacOSX10.5.sdk', '-mmacosx-version-min=10.4']) -# deplibs = ['sndfile', 'flac', 'vorbis', 'vorbisenc', 'ogg'] -if not target_platform == 'win32': - # On windows, utf support is builtin for SimpleIni - # but not on other platforms - sources += ['Support/ConvertUTF.c'] - -# Place the build products in the corect place -env.VariantDir('#' + build_dir, '#', duplicate = 0) - -# Look for the sources in the correct place -env.Append(CPPPATH = ['#src']) - -# Dependencies -deplibs = ['sndfile'] -deplibs += graphics_libraries - -if target_platform != 'win32': - for depname in deplibs: - env.ParseConfig('pkg-config --cflags --libs ' + depname) -else: - #env.AppendUnique(LIBS = ['wsock32', 'winmm']) - if 'sndfile' in deplibs: - ###### libsndfile ######################################## - # This one is only valid for win32 and already precompiled - # Only need to create .lib file from .def - shutil.copyfile(windows_libsndfile_location + '/libsndfile-1.dll', - build_dir+'/libsndfile-1.dll') - if compiler=='msvc': - shutil.copyfile(windows_libsndfile_location + '/libsndfile-1.def', - build_dir+'/libsndfile-1.def') - env.Command(build_dir + '/libsndfile-1.lib', build_dir + '/libsndfile-1.def', - env['AR'] + ' /nologo /machine:i386 /def:$SOURCE /out:$TARGET') - env.Append(CPPPATH = [windows_libsndfile_location + '/include/']) - env.AppendUnique(LIBPATH = [build_dir]) - # Replace 'sndfile' with 'sndfile-1' - deplibs.remove('sndfile') - deplibs.append('libsndfile-1') - if 'cairo' in deplibs: - shutil.copyfile(windows_cairo_location + '/bin/libcairo-2.dll', - build_dir+'/libcairo-2.dll') - env.Append(CPPPATH = [windows_cairo_location + '/include/cairo/']) - env.AppendUnique(LIBPATH = [windows_cairo_location + '/lib/']) -env.AppendUnique(LIBS = deplibs) - - - -# Builder for the main program -program = env.Program(target = os.path.join(build_dir, target_executable), - source = map(lambda x: '#' + build_dir + '/src/' + x, - sources)) -env.Alias(target_executable, os.path.join(build_dir, target_executable)) -env.Default(program) - -#test_env = env.Clone() -#test_libs = ['gtest', 'gtest_main'] -##for depname in test_libs: -## test_env.ParseConfig('pkg-config --cflags --libs ' + depname) -#test_env.AppendUnique(LIBPATH = ['/usr/local/lib'], -# CPPPATH = ['/usr/local/lib'], -# LIBS = test_libs) -# -#test = test_env.Program(target = os.path.join(build_dir, test_executable), -# source = map(lambda x: '#' + build_dir + '/src/' + x, -# test_sources)) -#env.Alias('test', os.path.join(build_dir, test_executable)) +# Copyright 2006-2010, Willem van Engen, Thomas Walters +# +# AIM-C: A C++ implementation of the Auditory Image Model +# http://www.acousticscale.org/AIMC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +## @author Thomas Walters <tom@acousticscale.org> +# @author Willem van Engen <cnbh@willem.engen.nl> +# @date created 2010/02/02 +# @version \$Id$ + +"""@package SConstruct +SConstruct file for the aimc project + +""" + +import os +import shutil + +# Sources common to every version +common_sources = ['Support/Common.cc', + 'Support/FileList.cc', + 'Support/SignalBank.cc', + 'Support/Parameters.cc', + 'Support/Module.cc', + 'Support/ModuleFactory.cc', + 'Modules/Input/ModuleFileInput.cc', + 'Modules/BMM/ModuleGammatone.cc', + 'Modules/BMM/ModulePZFC.cc', + 'Modules/NAP/ModuleHCL.cc', + 'Modules/Strobes/ModuleParabola.cc', + 'Modules/Strobes/ModuleLocalMax.cc', + 'Modules/SAI/ModuleSAI.cc', + 'Modules/SNR/ModuleNoise.cc', + 'Modules/SSI/ModuleSSI.cc', + 'Modules/Profile/ModuleSlice.cc', + 'Modules/Profile/ModuleScaler.cc', + 'Modules/Features/ModuleGaussians.cc', + 'Modules/Output/FileOutputHTK.cc', + 'Modules/Output/FileOutputAIMC.cc'] + +# File which contains main() +#sources = common_sources + ['Main/AIMCopy_SSI_Features_v4_PZFC.cc'] +sources = common_sources + ['Main/aimc.cc'] + +# Test sources +test_sources = ['Modules/Profile/ModuleSlice_unittest.cc'] +test_sources += common_sources + +# Define the command-line options for running scons +options = Variables() +options.Add(BoolVariable('mingw', + 'Compile on Windows using mingw rather than msvc', + False)) +options.Add(BoolVariable('symbols', + 'Add debuging symbols when compiling on gcc', + False)) + +# Environment variables +env = Environment(options = options, ENV = os.environ) +if env['mingw']: + # SCons Defaults to MSVC if installed on Windows. + env = Environment(options = opts, ENV = os.environ, tools = ['mingw']) + +# Platform +build_platform = env['PLATFORM'] +target_platform = build_platform + +# Build products location and executable name +build_dir = os.path.join('build', target_platform + '-release') +target_executable = 'aimc' +test_executable = 'aimc_tests' + +# Create build products directory if necessary +if not os.path.exists(build_dir): + os.makedirs(build_dir) +env.SConsignFile(os.path.join(build_dir,'.sconsign')) + +# Set any platform-specific environment variables and options +if target_platform == 'win32': + env.AppendUnique(CPPDEFINES = ['_WINDOWS', 'WIN32', + 'WINVER=0x0400', '_CONSOLE']) +elif target_platform == 'darwin': + env.AppendUnique(CPPDEFINES = ['_MACOSX']) + +# Compiler selection based on platform +# compiler can be one of: gcc msvc +compiler = 'gcc' +if (build_platform == 'win32' + and target_platform == 'win32' + and not env['mingw']): + compiler = 'msvc' + +# Compiler-specific options: +# Microsoft visual studio +if compiler == 'msvc': + env.AppendUnique(CPPFLAGS = ['/arch:SSE2', '/nologo', '/W3', '/EHsc']) + env.AppendUnique(CPPDEFINES = ['_CRT_SECURE_NO_DEPRECATE', + '_RINT_REQUIRED']) + env.AppendUnique(CPPFLAGS = ['/Ox']) + env.AppendUnique(CPPDEFINES = ['NDEBUG', '_ATL_MIN_CRT']) + +# GNU compiler collection +elif compiler == 'gcc': + env['STRIP'] = 'strip' + env.AppendUnique(CPPFLAGS = ['-Wall']) + env.AppendUnique(CPPFLAGS = ['-O3', '-fomit-frame-pointer']) + if env['symbols']: + env.AppendUnique(CPPFLAGS = ['-g']) + if env['mingw']: + if not env['PLATFORM'] == 'win32': + print('Cross-compilation for Windows is not supported') + Exit(1) +else: + print('Unsupported compiler: ' + compiler) + Exit(1) + +# To make a statically-linked version for os 10.4 and up... +#if build_platform == 'darwin': +# env.AppendUnique(CPPFLAGS = ['-arch', 'i386']) +# env.AppendUnique(LINKFLAGS = ['-arch', 'i386']) +# env.AppendUnique(LINKFLAGS = ['-Wl']) +# env.AppendUnique(LINKFLAGS = ['-search_paths_first']) +# env.AppendUnique(MACOSX_DEPLOYMENT_TARGET = ['10.4']) +# env.AppendUnique(GCC_VERSION = ['4.0']) +# env.Replace(CC = ['gcc-4.0']) +# env.Replace(CXX = ['gcc-4.0']) +# env.AppendUnique(CPPFLAGS = ['-fno-stack-protector','-isysroot', '/Developer/SDKs/MacOSX10.5.sdk', '-mmacosx-version-min=10.4']) +# deplibs = ['sndfile', 'flac', 'vorbis', 'vorbisenc', 'ogg'] +if not target_platform == 'win32': + # On windows, utf support is builtin for SimpleIni + # but not on other platforms + sources += ['Support/ConvertUTF.c'] + +# Place the build products in the corect place +env.VariantDir('#' + build_dir, '#', duplicate = 0) + +# Look for the sources in the correct place +env.Append(CPPPATH = ['#src']) + +# Dependencies +deplibs = ['sndfile'] + +for depname in deplibs: + env.ParseConfig('pkg-config --cflags --libs ' + depname) + +env.AppendUnique(LIBS = deplibs) + + +# Builder for the main program +program = env.Program(target = os.path.join(build_dir, target_executable), + source = map(lambda x: '#' + build_dir + '/src/' + x, + sources)) +env.Alias(target_executable, os.path.join(build_dir, target_executable)) +env.Default(program) + +#test_env = env.Clone() +#test_libs = ['gtest', 'gtest_main'] +##for depname in test_libs: +## test_env.ParseConfig('pkg-config --cflags --libs ' + depname) +#test_env.AppendUnique(LIBPATH = ['/usr/local/lib'], +# CPPPATH = ['/usr/local/lib'], +# LIBS = test_libs) +# +#test = test_env.Program(target = os.path.join(build_dir, test_executable), +# source = map(lambda x: '#' + build_dir + '/src/' + x, +# test_sources)) +#env.Alias('test', os.path.join(build_dir, test_executable))
--- a/matlab/AIMCread.m Thu Jan 13 18:58:15 2011 +0000 +++ b/matlab/AIMCread.m Thu May 27 07:25:03 2010 +0000 @@ -12,10 +12,10 @@ debug = 0; -nFrames = fread( fid, 1, 'uint32'); +nFrames = fread( fid, 1, 'int32'); period = fread( fid, 1, 'float32'); % Frame period in ms -nChannels = fread( fid, 1, 'uint32'); % vertical axis of an AI -nSamples = fread( fid, 1, 'uint32'); % horizontal axis of an AI +nChannels = fread( fid, 1, 'int32'); % vertical axis of an AI +nSamples = fread( fid, 1, 'int32'); % horizontal axis of an AI sample_rate = fread(fid, 1, 'float32'); % sample rate of each channel in Hz if nChannels == 1 % temporal profiles
--- a/src/Main/aimc.cc Thu Jan 13 18:58:15 2011 +0000 +++ b/src/Main/aimc.cc Thu May 27 07:25:03 2010 +0000 @@ -39,14 +39,20 @@ params.SetInt("input.buffersize", buffer_length); aimc::ModuleFileInput input(¶ms); - aimc::ModulePZFC bmm(¶ms); + aimc::ModuleGammatone bmm(¶ms); + aimc::ModuleHCL nap(¶ms); + aimc::ModuleLocalMax strobes(¶ms); + aimc::ModuleSAI sai(¶ms); aimc::FileOutputAIMC output(¶ms); std::string parameters_string = params.WriteString(); printf("%s", parameters_string.c_str()); input.AddTarget(&bmm); - bmm.AddTarget(&output); + bmm.AddTarget(&nap); + nap.AddTarget(&strobes); + strobes.AddTarget(&sai); + sai.AddTarget(&output); output.OpenFile("test_output.aimc", params.GetFloat("sai.frame_period_ms")); if (input.LoadFile("test.wav")) {
--- a/src/Modules/Output/FileOutputAIMC.cc Thu Jan 13 18:58:15 2011 +0000 +++ b/src/Modules/Output/FileOutputAIMC.cc Thu May 27 07:25:03 2010 +0000 @@ -36,17 +36,15 @@ #include <stdio.h> #include <string.h> #include <cmath> -#include <string> #include "Modules/Output/FileOutputAIMC.h" namespace aimc { FileOutputAIMC::FileOutputAIMC(Parameters *params) : Module(params) { module_description_ = "File output in AIMC format"; - module_identifier_ = "aimc_out"; + module_identifier_ = "htk_out"; module_type_ = "output"; module_version_ = "$Id: FileOutputAIMC.cc 51 2010-03-30 22:06:24Z tomwalters $"; - file_suffix_ = parameters_->DefaultString("file_suffix", ".aimc"); file_handle_ = NULL; header_written_ = false; @@ -58,20 +56,19 @@ CloseFile(); } -bool FileOutputAIMC::OpenFile(string &filename) { +bool FileOutputAIMC::OpenFile(const char* filename, float frame_period_ms) { if (file_handle_ != NULL) { LOG_ERROR(_T("Couldn't open output file. A file is already open.")); return false; } // Check that the output file exists and is writeable - if ((file_handle_ = fopen(filename.c_str(), "wb")) == NULL) { - LOG_ERROR(_T("Couldn't open output file '%s' for writing."), filename.c_str()); + if ((file_handle_ = fopen(filename, "wb")) == NULL) { + LOG_ERROR(_T("Couldn't open output file '%s' for writing."), filename); return false; } - // Write temporary values for the frame count and frame period. - frame_count_ = 0; - frame_period_ms_ = 0.0; + sample_count_ = 0; + frame_period_ms_ = frame_period_ms; header_written_ = false; if (initialized_) { WriteHeader(); @@ -80,14 +77,22 @@ } bool FileOutputAIMC::InitializeInternal(const SignalBank &input) { - channel_count_ = input.channel_count(); - buffer_length_ = input.buffer_length(); - sample_rate_ = input.sample_rate(); - ResetInternal(); if (file_handle_ == NULL) { - LOG_ERROR(_T("Couldn't initialize file output.")); + LOG_ERROR(_T("Couldn't initialize file output. " + "Please call FileOutputAIMC::OpenFile first")); return false; } + if (header_written_) { + LOG_ERROR(_T("A header has already been written on the output file. " + "Please call FileOutputAIMC::CloseFile to close that file, " + "and FileOutputAIMC::OpenFile to open an new one before " + "calling FileOutputAIMC::Initialize again.")); + return false; + } + channel_count_ = input.channel_count(); + buffer_length_ = input.buffer_length(); + sample_rate_ = input.sample_rate(); + WriteHeader(); return true; } @@ -97,11 +102,6 @@ } if (file_handle_ != NULL) CloseFile(); - - string out_filename; - out_filename = global_parameters_->GetString("output_filename_base") + file_suffix_; - OpenFile(out_filename); - } void FileOutputAIMC::WriteHeader() { @@ -119,13 +119,13 @@ * f1c1t1,f1c1t2,f1c1t3... */ - uint32_t frame_count_out = frame_count_; + uint32_t sample_count_out = sample_count_; float sample_period_out = frame_period_ms_; uint32_t channels_out = channel_count_; uint32_t samples_out = buffer_length_; float sample_rate = sample_rate_; - fwrite(&frame_count_out, sizeof(frame_count_out), 1, file_handle_); + fwrite(&sample_count_out, sizeof(sample_count_out), 1, file_handle_); fwrite(&sample_period_out, sizeof(sample_period_out), 1, file_handle_); fwrite(&channels_out, sizeof(channels_out), 1, file_handle_); fwrite(&samples_out, sizeof(samples_out), 1, file_handle_); @@ -156,7 +156,7 @@ fwrite(&s, sizeof(s), 1, file_handle_); } } - frame_count_++; + sample_count_++; } bool FileOutputAIMC::CloseFile() { @@ -168,10 +168,8 @@ fflush(file_handle_); rewind(file_handle_); fflush(file_handle_); - uint32_t frame_count = frame_count_; - float sample_period_out = frame_period_ms_; - fwrite(&frame_count, sizeof(frame_count), 1, file_handle_); - fwrite(&sample_period_out, sizeof(sample_period_out), 1, file_handle_); + uint32_t samples = sample_count_; + fwrite(&samples, sizeof(samples), 1, file_handle_); // And close the file fclose(file_handle_);
--- a/src/Modules/Output/FileOutputAIMC.h Thu Jan 13 18:58:15 2011 +0000 +++ b/src/Modules/Output/FileOutputAIMC.h Thu May 27 07:25:03 2010 +0000 @@ -28,8 +28,6 @@ #ifndef AIMC_MODULES_OUTPUT_AIMC_H_ #define AIMC_MODULES_OUTPUT_AIMC_H_ -#include <string> - #include "Support/Module.h" #include "Support/SignalBank.h" @@ -46,7 +44,7 @@ * If the file exists it will be overwritten * \return Returns true on success of initialization. */ - bool OpenFile(string &filename); + bool OpenFile(const char *filename, float frame_period_ms); bool CloseFile(); virtual void Process(const SignalBank &input); private: @@ -65,13 +63,12 @@ /*! \brief Count of the number of samples in the file, written on close */ - int frame_count_; + int sample_count_; int channel_count_; int buffer_length_; - float sample_rate_; + float sample_rate_; float frame_period_ms_; - string file_suffix_; }; } // namespace aimc
--- a/src/Modules/Output/FileOutputHTK.cc Thu Jan 13 18:58:15 2011 +0000 +++ b/src/Modules/Output/FileOutputHTK.cc Thu May 27 07:25:03 2010 +0000 @@ -45,13 +45,10 @@ module_identifier_ = "htk_out"; module_type_ = "output"; module_version_ = "$Id$"; - - file_suffix_ = parameters_->DefaultString("htk_out.file_suffix", ".htk"); file_handle_ = NULL; header_written_ = false; frame_period_ms_ = 0.0f; - previous_start_time_ = 0; } FileOutputHTK::~FileOutputHTK() { @@ -59,45 +56,54 @@ CloseFile(); } +bool FileOutputHTK::OpenFile(const char* filename, float frame_period_ms) { + if (file_handle_ != NULL) { + LOG_ERROR(_T("Couldn't open output file. A file is already open.")); + return false; + } + + // Check that the output file exists and is writeable + if ((file_handle_ = fopen(filename, "wb")) == NULL) { + LOG_ERROR(_T("Couldn't open output file '%s' for writing."), filename); + return false; + } + sample_count_ = 0; + frame_period_ms_ = frame_period_ms; + header_written_ = false; + if (initialized_) { + WriteHeader(channel_count_ * buffer_length_, frame_period_ms_); + } + return true; +} + bool FileOutputHTK::InitializeInternal(const SignalBank &input) { + if (file_handle_ == NULL) { + LOG_ERROR(_T("Couldn't initialize file output. " + "Please call FileOutputHTK::OpenFile first")); + return false; + } + if (header_written_) { + LOG_ERROR(_T("A header has already been written on the output file. " + "Please call FileOutputHTK::CloseFile to close that file, " + "and FileOutputHTK::OpenFile to open an new one before " + "calling FileOutputHTK::Initialize again.")); + return false; + } channel_count_ = input.channel_count(); buffer_length_ = input.buffer_length(); - ResetInternal(); - if (file_handle_ == NULL) { - LOG_ERROR(_T("Couldn't initialize file output.")); - return false; - } - if (!header_written_) { - WriteHeader(channel_count_ * buffer_length_); - } - + WriteHeader(channel_count_ * buffer_length_, frame_period_ms_); return true; } void FileOutputHTK::ResetInternal() { - // Finalize and close the open file, if there is one. if (file_handle_ != NULL && !header_written_) { - WriteHeader(channel_count_ * buffer_length_); + WriteHeader(channel_count_ * buffer_length_, frame_period_ms_); } if (file_handle_ != NULL) CloseFile(); - - // Now open and set up the new file. - // Check that the output file exists and is writeable. - string out_filename; - out_filename = global_parameters_->GetString("output_filename_base") + file_suffix_; - if ((file_handle_ = fopen(out_filename.c_str(), - "wb")) == NULL) { - LOG_ERROR(_T("Couldn't open output file '%s' for writing."), - out_filename.c_str()); - return; - } - sample_count_ = 0; - header_written_ = false; - WriteHeader(channel_count_ * buffer_length_); } -void FileOutputHTK::WriteHeader(int num_elements) { +void FileOutputHTK::WriteHeader(int num_elements, float period_ms) { if (header_written_) return; @@ -112,7 +118,7 @@ // To be filled in when the file is done int32_t sample_count = 0; - int32_t sample_period = floor(1e4 * frame_period_ms_); + int32_t sample_period = floor(1e4 * period_ms); int16_t sample_size = num_elements * sizeof(float); // NOLINT // User-defined coefficients with energy term @@ -144,8 +150,8 @@ if (!header_written_) { LOG_ERROR(_T("No header has been written on the output file yet. Please " - "call FileOutputHTK::Initialize() or FileOutputHTK::Reset() " - "before calling FileOutputHTK::Process()")); + "call FileOutputHTK::Initialize() before calling " + "FileOutputHTK::Process()")); return; } float s; @@ -158,10 +164,6 @@ } } sample_count_++; - frame_period_ms_ = 1000.0 - * (input.start_time() - previous_start_time_) - / input.sample_rate(); - previous_start_time_ = input.start_time(); } bool FileOutputHTK::CloseFile() { @@ -170,16 +172,12 @@ // Write the first 4 bytes of the file // with how many samples there are in the file - // and the next 4 bytes with the frame period. fflush(file_handle_); rewind(file_handle_); fflush(file_handle_); int32_t samples = sample_count_; samples = ByteSwap32(samples); - int32_t sample_period = floor(1e4 * frame_period_ms_); - sample_period = ByteSwap32(sample_period); fwrite(&samples, sizeof(samples), 1, file_handle_); - fwrite(&sample_period, sizeof(sample_period), 1, file_handle_); // And close the file fclose(file_handle_);