annotate src/Modules/Input/ModuleFileInput.cc @ 3:decdac21cfc2

- Imported file input using libsndfile from old AIM-C and updated to the new API - Modified the Module base class to propogate Reset() calls down the module chain. - This required changing all Reset() functions in subclasses to ResetInternal() - Removed some unneeded imports from the Gaussians test
author tomwalters
date Tue, 16 Feb 2010 18:00:16 +0000
parents
children 8c859ef1fb75
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@3 6 // This program is free software: you can redistribute it and/or modify
tomwalters@3 7 // it under the terms of the GNU General Public License as published by
tomwalters@3 8 // the Free Software Foundation, either version 3 of the License, or
tomwalters@3 9 // (at your option) any later version.
tomwalters@3 10 //
tomwalters@3 11 // This program is distributed in the hope that it will be useful,
tomwalters@3 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@3 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@3 14 // GNU General Public License for more details.
tomwalters@3 15 //
tomwalters@3 16 // You should have received a copy of the GNU General Public License
tomwalters@3 17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
tomwalters@3 18
tomwalters@3 19 /*! \file
tomwalters@3 20 * \brief Audio file input
tomwalters@3 21 *
tomwalters@3 22 * \author Willem van Engen <cnbh@willem.engen.nl>
tomwalters@3 23 * \author Thomas Walters <tom@acousticscale.org>
tomwalters@3 24 * \date created 2006/09/21
tomwalters@3 25 * \version \$Id$
tomwalters@3 26 */
tomwalters@3 27
tomwalters@3 28 #include <vector>
tomwalters@3 29
tomwalters@3 30 #include "Modules/Input/ModuleFileInput.h"
tomwalters@3 31
tomwalters@3 32 namespace aimc {
tomwalters@3 33 ModuleFileInput::ModuleFileInput(Parameters *params) : Module(params) {
tomwalters@3 34 file_handle_ = NULL;
tomwalters@3 35 buffer_length_ = parameters_->DefaultInt("input.buffersize", 1024);
tomwalters@3 36
tomwalters@3 37 file_position_samples_ = 0;
tomwalters@3 38 file_loaded_ = false;
tomwalters@3 39 audio_channels_ = 0;
tomwalters@3 40 buffer_length_ = 0;
tomwalters@3 41 sample_rate_ = 0.0f;
tomwalters@3 42 }
tomwalters@3 43
tomwalters@3 44 ModuleFileInput::~ModuleFileInput() {
tomwalters@3 45 if (file_handle_)
tomwalters@3 46 sf_close(file_handle_);
tomwalters@3 47 }
tomwalters@3 48
tomwalters@3 49 void ModuleFileInput::ResetInternal() {
tomwalters@3 50 output_.Initialize(audio_channels_, buffer_length_, sample_rate_);
tomwalters@3 51 output_.set_start_time(0);
tomwalters@3 52 }
tomwalters@3 53
tomwalters@3 54 bool ModuleFileInput::LoadFile(const char* filename) {
tomwalters@3 55 // Open the file
tomwalters@3 56 SF_INFO sfinfo;
tomwalters@3 57 memset((void*)&sfinfo, 0, sizeof(SF_INFO));
tomwalters@3 58 file_handle_ = sf_open(filename, SFM_READ, &sfinfo);
tomwalters@3 59
tomwalters@3 60 if (file_handle_ == NULL) {
tomwalters@3 61 //! \todo Also display error reason
tomwalters@3 62 LOG_ERROR(_T("Couldn't read audio file '%s'"), filename);
tomwalters@3 63 return false;
tomwalters@3 64 }
tomwalters@3 65
tomwalters@3 66 file_loaded_ = true;
tomwalters@3 67 audio_channels_ = sfinfo.channels;
tomwalters@3 68 sample_rate_ = sfinfo.samplerate;
tomwalters@3 69 file_position_samples_ = 0;
tomwalters@3 70
tomwalters@3 71 // A dummy signal bank to be passed to the Initialize() function.
tomwalters@3 72 SignalBank s;
tomwalters@3 73 s.Initialize(1, 1, 1);
tomwalters@3 74
tomwalters@3 75 // Self-initialize by calling Module::Initialize() explicitly.
tomwalters@3 76 // The Initialize() call in this subclass is overloaded to prevent it from
tomwalters@3 77 // being called drectly.
tomwalters@3 78 return Module::Initialize(s);
tomwalters@3 79 }
tomwalters@3 80
tomwalters@3 81 bool ModuleFileInput::Initialize(const SignalBank& input) {
tomwalters@3 82 LOG_ERROR(_T("Do not call Initialize() on ModuleFileInput directly "
tomwalters@3 83 "instead call LoadFile() with a filename to load. "
tomwalters@3 84 "This will automatically initialize the module."));
tomwalters@3 85 return false;
tomwalters@3 86 }
tomwalters@3 87
tomwalters@3 88 void ModuleFileInput::Process(const SignalBank& input) {
tomwalters@3 89 LOG_ERROR(_T("Call Process() on ModuleFileInput instead of passing in "
tomwalters@3 90 "a SignalBank"));
tomwalters@3 91 }
tomwalters@3 92
tomwalters@3 93 bool ModuleFileInput::InitializeInternal(const SignalBank& input) {
tomwalters@3 94 if (!file_loaded_)
tomwalters@3 95 return false;
tomwalters@3 96 ResetInternal();
tomwalters@3 97 return true;
tomwalters@3 98 }
tomwalters@3 99
tomwalters@3 100 void ModuleFileInput::Process() {
tomwalters@3 101 sf_count_t read;
tomwalters@3 102 vector<float> buffer;
tomwalters@3 103 buffer.resize(buffer_length_ * audio_channels_);
tomwalters@3 104
tomwalters@3 105 while (true) {
tomwalters@3 106 // Read buffersize bytes into buffer
tomwalters@3 107 read = sf_readf_float(file_handle_, &buffer[0], buffer_length_);
tomwalters@3 108
tomwalters@3 109 // Place the contents of the buffer into the signal bank
tomwalters@3 110 int counter = 0;
tomwalters@3 111 for (int c = 0; c < audio_channels_; ++c) {
tomwalters@3 112 for (int i = 0; i < read; ++i) {
tomwalters@3 113 output_.set_sample(c, i, buffer[counter]);
tomwalters@3 114 ++counter;
tomwalters@3 115 }
tomwalters@3 116 }
tomwalters@3 117
tomwalters@3 118 // If the number of saples read is less than the buffer length, the end
tomwalters@3 119 // of the file has been reached.
tomwalters@3 120 if (read < buffer_length_) {
tomwalters@3 121 // Zero samples at end
tomwalters@3 122 for (int c = 0; c < audio_channels_; ++c) {
tomwalters@3 123 for (int i = read; i < buffer_length_; ++i) {
tomwalters@3 124 output_.set_sample(c, i, 0.0f);
tomwalters@3 125 }
tomwalters@3 126 }
tomwalters@3 127 // When we're past the end of the buffer, stop looping.
tomwalters@3 128 if (read == 0)
tomwalters@3 129 break;
tomwalters@3 130 }
tomwalters@3 131
tomwalters@3 132 // Update time
tomwalters@3 133 output_.set_start_time(file_position_samples_);
tomwalters@3 134 file_position_samples_ += read;
tomwalters@3 135
tomwalters@3 136 PushOutput();
tomwalters@3 137 }
tomwalters@3 138 }
tomwalters@3 139 } // namespace aimc