diff trunk/src/Modules/Input/ModuleFileInput.cc @ 402:69466da9745e

- Massive refactoring to make module tree stuff work. In theory we now support configuration files again. The graphics stuff is untested as yet.
author tomwalters
date Mon, 18 Oct 2010 04:42:28 +0000
parents 30dde71d0230
children 733a11a65f3d
line wrap: on
line diff
--- a/trunk/src/Modules/Input/ModuleFileInput.cc	Sun Oct 17 02:42:12 2010 +0000
+++ b/trunk/src/Modules/Input/ModuleFileInput.cc	Mon Oct 18 04:42:28 2010 +0000
@@ -52,11 +52,14 @@
 }
 
 void ModuleFileInput::ResetInternal() {
-  output_.Initialize(audio_channels_, buffer_length_, sample_rate_);
-  output_.set_start_time(0);
+  // If there's a file open, rewind to the beginning.
+  if (file_handle_ != NULL) {
+    sf_seek(file_handle_, 0, SEEK_SET);
+    file_position_samples_ = 0;
+  }
 }
 
-bool ModuleFileInput::LoadFile(const char* filename) {
+bool ModuleFileInput::InitializeInternal(const SignalBank& input) {
   // If there's a file open. Close it.
   if (file_handle_ != NULL) {
     sf_close(file_handle_);
@@ -66,98 +69,74 @@
   SF_INFO sfinfo;
   memset(reinterpret_cast<void*>(&sfinfo), 0, sizeof(SF_INFO));
 
-  file_handle_ = sf_open(filename, SFM_READ, &sfinfo);
+  file_loaded_ = false;
+  file_handle_ = sf_open(global_parameters_->GetString("input_filename"),
+                         SFM_READ,
+                         &sfinfo);
 
   if (file_handle_ == NULL) {
     /*! \todo Also display error reason
      */
-    LOG_ERROR(_T("Couldn't read audio file '%s'"), filename);
+    LOG_ERROR(_T("Couldn't read audio file '%s'"),
+              global_parameters_->GetString("input_filename"));
     return false;
   }
-
+  
   file_loaded_ = true;
+  done_ = false;
   audio_channels_ = sfinfo.channels;
   sample_rate_ = sfinfo.samplerate;
   file_position_samples_ = 0;
 
-  // A dummy signal bank to be passed to the Initialize() function.
-  SignalBank s;
-  s.Initialize(1, 1, 1);
+  if (audio_channels_ < 1 || buffer_length_ < 1 || sample_rate_ < 0.0f) {
+    LOG_ERROR(_T("Problem with file: audio_channels = %d, buffer_length_ = %d, sample_rate = %f"), audio_channels_, buffer_length_, sample_rate_);
+    return false;
+  }
 
-  // Self-initialize by calling Module::Initialize() explicitly.
-  // The Initialize() call in this subclass is overloaded to prevent it from
-  // being called drectly.
-  return Module::Initialize(s);
-}
-
-
-/* Do not call Initialize() on ModuleFileInput directly
- * instead call LoadFile() with a filename to load.
- * This will automatically initialize the module.
- */
-bool ModuleFileInput::Initialize(const SignalBank& input) {
-  LOG_ERROR(_T("Do not call Initialize() on ModuleFileInput directly "
-               "instead call LoadFile() with a filename to load. "
-               "This will automatically initialize the module."));
-  return false;
+  output_.Initialize(audio_channels_, buffer_length_, sample_rate_);
+  output_.set_start_time(0);
+  
+  return true;
 }
 
 void ModuleFileInput::Process(const SignalBank& input) {
-  LOG_ERROR(_T("Call Process() on ModuleFileInput instead of passing in "
-               "a SignalBank"));
-}
-
-bool ModuleFileInput::InitializeInternal(const SignalBank& input) {
-  if (!file_loaded_) {
-    LOG_ERROR(_T("No file loaded in FileOutputHTK"));
-    return false;
-  }
-  if (audio_channels_ < 1 || buffer_length_ < 1 || sample_rate_ < 0.0f) {
-    LOG_ERROR(_T("audio_channels, buffer_length_ or sample_rate too small"));
-    return false;
-  }
-  ResetInternal();
-  return true;
-}
-
-void ModuleFileInput::Process() {
   if (!file_loaded_)
     return;
   sf_count_t read;
   vector<float> buffer;
   buffer.resize(buffer_length_ * audio_channels_);
 
-  while (true) {
-    // Read buffersize bytes into buffer
-    read = sf_readf_float(file_handle_, &buffer[0], buffer_length_);
+  // Read buffersize bytes into buffer
+  read = sf_readf_float(file_handle_, &buffer[0], buffer_length_);
+    
+  // Place the contents of the buffer into the signal bank
+  int counter = 0;
+  for (int c = 0; c < audio_channels_; ++c) {
+    for (int i = 0; i < read; ++i) {
+      output_.set_sample(c, i, buffer[counter]);
+      ++counter;
+    }
+  }
 
-    // Place the contents of the buffer into the signal bank
-    int counter = 0;
+  // If the number of saples read is less than the buffer length, the end
+  // of the file has been reached.
+  if (read < buffer_length_) {
+    // Zero samples at end
     for (int c = 0; c < audio_channels_; ++c) {
-      for (int i = 0; i < read; ++i) {
-        output_.set_sample(c, i, buffer[counter]);
-        ++counter;
+      for (int i = read; i < buffer_length_; ++i) {
+        output_.set_sample(c, i, 0.0f);
       }
     }
+    // When we're past the end of the buffer, set the
+    // module state to 'done' and exit.
+    if (read == 0)
+      done_ = true;
+      return;
+  }
 
-    // If the number of saples read is less than the buffer length, the end
-    // of the file has been reached.
-    if (read < buffer_length_) {
-      // Zero samples at end
-      for (int c = 0; c < audio_channels_; ++c) {
-        for (int i = read; i < buffer_length_; ++i) {
-          output_.set_sample(c, i, 0.0f);
-        }
-      }
-      // When we're past the end of the buffer, stop looping.
-      if (read == 0)
-        break;
-    }
-
-    // Update time
-    output_.set_start_time(file_position_samples_);
-    file_position_samples_ += read;
-    PushOutput();
-  }
+  // Update time.
+  output_.set_start_time(file_position_samples_);
+  file_position_samples_ += read;
+  PushOutput();
 }
 }  // namespace aimc