annotate src/Modules/Input/ModuleFileInput.cc @ 45:c5f5e9569863

- Modified licence from GPL 3 to Apache v2
author tomwalters
date Tue, 30 Mar 2010 22:06:24 +0000
parents fff25824d1d1
children 3cdaa81c3aca
rev   line source
tomwalters@3 1 // Copyright 2006-2010, Willem van Engen, Thomas Walters
tomwalters@3 2 //
tomwalters@3 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@3 4 // http://www.acousticscale.org/AIMC
tomwalters@3 5 //
tomwalters@45 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@45 7 // you may not use this file except in compliance with the License.
tomwalters@45 8 // You may obtain a copy of the License at
tomwalters@3 9 //
tomwalters@45 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@3 11 //
tomwalters@45 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@45 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@45 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@45 15 // See the License for the specific language governing permissions and
tomwalters@45 16 // limitations under the License.
tomwalters@3 17
tomwalters@3 18 /*! \file
tomwalters@3 19 * \brief Audio file input
tomwalters@3 20 *
tomwalters@3 21 * \author Willem van Engen <cnbh@willem.engen.nl>
tomwalters@3 22 * \author Thomas Walters <tom@acousticscale.org>
tomwalters@3 23 * \date created 2006/09/21
tomwalters@3 24 * \version \$Id$
tomwalters@3 25 */
tomwalters@3 26
tomwalters@3 27 #include <vector>
tomwalters@3 28
tomwalters@3 29 #include "Modules/Input/ModuleFileInput.h"
tomwalters@3 30
tomwalters@3 31 namespace aimc {
tomwalters@3 32 ModuleFileInput::ModuleFileInput(Parameters *params) : Module(params) {
tomwalters@6 33 module_description_ = "File input using libsndfile";
tomwalters@6 34 module_identifier_ = "file_input";
tomwalters@6 35 module_type_ = "input";
tomwalters@6 36 module_version_ = "$Id$";
tomwalters@6 37
tomwalters@7 38 file_handle_ = NULL;
tomwalters@7 39 buffer_length_ = parameters_->DefaultInt("input.buffersize", 1024);
tomwalters@3 40
tomwalters@3 41 file_position_samples_ = 0;
tomwalters@3 42 file_loaded_ = false;
tomwalters@3 43 audio_channels_ = 0;
tomwalters@3 44 sample_rate_ = 0.0f;
tomwalters@3 45 }
tomwalters@3 46
tomwalters@3 47 ModuleFileInput::~ModuleFileInput() {
tomwalters@20 48 if (file_handle_ != NULL) {
tomwalters@7 49 sf_close(file_handle_);
tomwalters@20 50 file_handle_ = NULL;
tomwalters@20 51 }
tomwalters@3 52 }
tomwalters@3 53
tomwalters@3 54 void ModuleFileInput::ResetInternal() {
tomwalters@3 55 output_.Initialize(audio_channels_, buffer_length_, sample_rate_);
tomwalters@3 56 output_.set_start_time(0);
tomwalters@3 57 }
tomwalters@3 58
tomwalters@3 59 bool ModuleFileInput::LoadFile(const char* filename) {
tomwalters@20 60 // If there's a file open. Close it.
tomwalters@20 61 if (file_handle_ != NULL) {
tomwalters@20 62 sf_close(file_handle_);
tomwalters@20 63 file_handle_ = NULL;
tomwalters@20 64 }
tomwalters@7 65 // Open the file
tomwalters@7 66 SF_INFO sfinfo;
tomwalters@8 67 memset(reinterpret_cast<void*>(&sfinfo), 0, sizeof(SF_INFO));
tomwalters@20 68
tomwalters@7 69 file_handle_ = sf_open(filename, SFM_READ, &sfinfo);
tomwalters@3 70
tomwalters@7 71 if (file_handle_ == NULL) {
tomwalters@8 72 /*! \todo Also display error reason
tomwalters@8 73 */
tomwalters@7 74 LOG_ERROR(_T("Couldn't read audio file '%s'"), filename);
tomwalters@7 75 return false;
tomwalters@7 76 }
tomwalters@3 77
tomwalters@3 78 file_loaded_ = true;
tomwalters@3 79 audio_channels_ = sfinfo.channels;
tomwalters@3 80 sample_rate_ = sfinfo.samplerate;
tomwalters@7 81 file_position_samples_ = 0;
tomwalters@3 82
tomwalters@3 83 // A dummy signal bank to be passed to the Initialize() function.
tomwalters@3 84 SignalBank s;
tomwalters@3 85 s.Initialize(1, 1, 1);
tomwalters@3 86
tomwalters@7 87 // Self-initialize by calling Module::Initialize() explicitly.
tomwalters@7 88 // The Initialize() call in this subclass is overloaded to prevent it from
tomwalters@7 89 // being called drectly.
tomwalters@3 90 return Module::Initialize(s);
tomwalters@3 91 }
tomwalters@3 92
tomwalters@6 93
tomwalters@6 94 /* Do not call Initialize() on ModuleFileInput directly
tomwalters@6 95 * instead call LoadFile() with a filename to load.
tomwalters@6 96 * This will automatically initialize the module.
tomwalters@6 97 */
tomwalters@3 98 bool ModuleFileInput::Initialize(const SignalBank& input) {
tomwalters@3 99 LOG_ERROR(_T("Do not call Initialize() on ModuleFileInput directly "
tomwalters@3 100 "instead call LoadFile() with a filename to load. "
tomwalters@3 101 "This will automatically initialize the module."));
tomwalters@3 102 return false;
tomwalters@3 103 }
tomwalters@3 104
tomwalters@3 105 void ModuleFileInput::Process(const SignalBank& input) {
tomwalters@3 106 LOG_ERROR(_T("Call Process() on ModuleFileInput instead of passing in "
tomwalters@3 107 "a SignalBank"));
tomwalters@3 108 }
tomwalters@3 109
tomwalters@3 110 bool ModuleFileInput::InitializeInternal(const SignalBank& input) {
tomwalters@6 111 if (!file_loaded_) {
tomwalters@6 112 LOG_ERROR(_T("No file loaded in FileOutputHTK"));
tomwalters@3 113 return false;
tomwalters@6 114 }
tomwalters@6 115 if (audio_channels_ < 1 || buffer_length_ < 1 || sample_rate_ < 0.0f) {
tomwalters@6 116 LOG_ERROR(_T("audio_channels, buffer_length_ or sample_rate too small"));
tomwalters@6 117 return false;
tomwalters@6 118 }
tomwalters@3 119 ResetInternal();
tomwalters@7 120 return true;
tomwalters@3 121 }
tomwalters@3 122
tomwalters@3 123 void ModuleFileInput::Process() {
tomwalters@6 124 if (!file_loaded_)
tomwalters@6 125 return;
tomwalters@7 126 sf_count_t read;
tomwalters@3 127 vector<float> buffer;
tomwalters@3 128 buffer.resize(buffer_length_ * audio_channels_);
tomwalters@3 129
tomwalters@3 130 while (true) {
tomwalters@7 131 // Read buffersize bytes into buffer
tomwalters@7 132 read = sf_readf_float(file_handle_, &buffer[0], buffer_length_);
tomwalters@3 133
tomwalters@3 134 // Place the contents of the buffer into the signal bank
tomwalters@3 135 int counter = 0;
tomwalters@3 136 for (int c = 0; c < audio_channels_; ++c) {
tomwalters@3 137 for (int i = 0; i < read; ++i) {
tomwalters@3 138 output_.set_sample(c, i, buffer[counter]);
tomwalters@3 139 ++counter;
tomwalters@3 140 }
tomwalters@3 141 }
tomwalters@3 142
tomwalters@7 143 // If the number of saples read is less than the buffer length, the end
tomwalters@7 144 // of the file has been reached.
tomwalters@7 145 if (read < buffer_length_) {
tomwalters@7 146 // Zero samples at end
tomwalters@7 147 for (int c = 0; c < audio_channels_; ++c) {
tomwalters@3 148 for (int i = read; i < buffer_length_; ++i) {
tomwalters@3 149 output_.set_sample(c, i, 0.0f);
tomwalters@3 150 }
tomwalters@3 151 }
tomwalters@7 152 // When we're past the end of the buffer, stop looping.
tomwalters@7 153 if (read == 0)
tomwalters@3 154 break;
tomwalters@7 155 }
tomwalters@3 156
tomwalters@7 157 // Update time
tomwalters@7 158 output_.set_start_time(file_position_samples_);
tomwalters@7 159 file_position_samples_ += read;
tomwalters@3 160 PushOutput();
tomwalters@3 161 }
tomwalters@3 162 }
tomwalters@3 163 } // namespace aimc