comparison trunk/src/Modules/Input/ModuleFileInput.cc @ 279:f469d936337f

- Replacing tabs with spaces for indentation
author tomwalters
date Thu, 18 Feb 2010 20:04:04 +0000
parents 5b8b9ea1218a
children e55d0c225a57
comparison
equal deleted inserted replaced
278:5b8b9ea1218a 279:f469d936337f
34 module_description_ = "File input using libsndfile"; 34 module_description_ = "File input using libsndfile";
35 module_identifier_ = "file_input"; 35 module_identifier_ = "file_input";
36 module_type_ = "input"; 36 module_type_ = "input";
37 module_version_ = "$Id$"; 37 module_version_ = "$Id$";
38 38
39 file_handle_ = NULL; 39 file_handle_ = NULL;
40 buffer_length_ = parameters_->DefaultInt("input.buffersize", 1024); 40 buffer_length_ = parameters_->DefaultInt("input.buffersize", 1024);
41 41
42 file_position_samples_ = 0; 42 file_position_samples_ = 0;
43 file_loaded_ = false; 43 file_loaded_ = false;
44 audio_channels_ = 0; 44 audio_channels_ = 0;
45 sample_rate_ = 0.0f; 45 sample_rate_ = 0.0f;
46 } 46 }
47 47
48 ModuleFileInput::~ModuleFileInput() { 48 ModuleFileInput::~ModuleFileInput() {
49 if (file_handle_) 49 if (file_handle_)
50 sf_close(file_handle_); 50 sf_close(file_handle_);
51 } 51 }
52 52
53 void ModuleFileInput::ResetInternal() { 53 void ModuleFileInput::ResetInternal() {
54 output_.Initialize(audio_channels_, buffer_length_, sample_rate_); 54 output_.Initialize(audio_channels_, buffer_length_, sample_rate_);
55 output_.set_start_time(0); 55 output_.set_start_time(0);
56 } 56 }
57 57
58 bool ModuleFileInput::LoadFile(const char* filename) { 58 bool ModuleFileInput::LoadFile(const char* filename) {
59 // Open the file 59 // Open the file
60 SF_INFO sfinfo; 60 SF_INFO sfinfo;
61 memset((void*)&sfinfo, 0, sizeof(SF_INFO)); 61 memset((void*)&sfinfo, 0, sizeof(SF_INFO));
62 file_handle_ = sf_open(filename, SFM_READ, &sfinfo); 62 file_handle_ = sf_open(filename, SFM_READ, &sfinfo);
63 63
64 if (file_handle_ == NULL) { 64 if (file_handle_ == NULL) {
65 //! \todo Also display error reason 65 //! \todo Also display error reason
66 LOG_ERROR(_T("Couldn't read audio file '%s'"), filename); 66 LOG_ERROR(_T("Couldn't read audio file '%s'"), filename);
67 return false; 67 return false;
68 } 68 }
69 69
70 file_loaded_ = true; 70 file_loaded_ = true;
71 audio_channels_ = sfinfo.channels; 71 audio_channels_ = sfinfo.channels;
72 sample_rate_ = sfinfo.samplerate; 72 sample_rate_ = sfinfo.samplerate;
73 file_position_samples_ = 0; 73 file_position_samples_ = 0;
74 74
75 // A dummy signal bank to be passed to the Initialize() function. 75 // A dummy signal bank to be passed to the Initialize() function.
76 SignalBank s; 76 SignalBank s;
77 s.Initialize(1, 1, 1); 77 s.Initialize(1, 1, 1);
78 78
79 // Self-initialize by calling Module::Initialize() explicitly. 79 // Self-initialize by calling Module::Initialize() explicitly.
80 // The Initialize() call in this subclass is overloaded to prevent it from 80 // The Initialize() call in this subclass is overloaded to prevent it from
81 // being called drectly. 81 // being called drectly.
82 return Module::Initialize(s); 82 return Module::Initialize(s);
83 } 83 }
84 84
85 85
86 /* Do not call Initialize() on ModuleFileInput directly 86 /* Do not call Initialize() on ModuleFileInput directly
107 if (audio_channels_ < 1 || buffer_length_ < 1 || sample_rate_ < 0.0f) { 107 if (audio_channels_ < 1 || buffer_length_ < 1 || sample_rate_ < 0.0f) {
108 LOG_ERROR(_T("audio_channels, buffer_length_ or sample_rate too small")); 108 LOG_ERROR(_T("audio_channels, buffer_length_ or sample_rate too small"));
109 return false; 109 return false;
110 } 110 }
111 ResetInternal(); 111 ResetInternal();
112 return true; 112 return true;
113 } 113 }
114 114
115 void ModuleFileInput::Process() { 115 void ModuleFileInput::Process() {
116 if (!file_loaded_) 116 if (!file_loaded_)
117 return; 117 return;
118 sf_count_t read; 118 sf_count_t read;
119 vector<float> buffer; 119 vector<float> buffer;
120 buffer.resize(buffer_length_ * audio_channels_); 120 buffer.resize(buffer_length_ * audio_channels_);
121 121
122 while (true) { 122 while (true) {
123 // Read buffersize bytes into buffer 123 // Read buffersize bytes into buffer
124 read = sf_readf_float(file_handle_, &buffer[0], buffer_length_); 124 read = sf_readf_float(file_handle_, &buffer[0], buffer_length_);
125 125
126 // Place the contents of the buffer into the signal bank 126 // Place the contents of the buffer into the signal bank
127 int counter = 0; 127 int counter = 0;
128 for (int c = 0; c < audio_channels_; ++c) { 128 for (int c = 0; c < audio_channels_; ++c) {
129 for (int i = 0; i < read; ++i) { 129 for (int i = 0; i < read; ++i) {
130 output_.set_sample(c, i, buffer[counter]); 130 output_.set_sample(c, i, buffer[counter]);
131 ++counter; 131 ++counter;
132 } 132 }
133 } 133 }
134 134
135 // If the number of saples read is less than the buffer length, the end 135 // If the number of saples read is less than the buffer length, the end
136 // of the file has been reached. 136 // of the file has been reached.
137 if (read < buffer_length_) { 137 if (read < buffer_length_) {
138 // Zero samples at end 138 // Zero samples at end
139 for (int c = 0; c < audio_channels_; ++c) { 139 for (int c = 0; c < audio_channels_; ++c) {
140 for (int i = read; i < buffer_length_; ++i) { 140 for (int i = read; i < buffer_length_; ++i) {
141 output_.set_sample(c, i, 0.0f); 141 output_.set_sample(c, i, 0.0f);
142 } 142 }
143 } 143 }
144 // When we're past the end of the buffer, stop looping. 144 // When we're past the end of the buffer, stop looping.
145 if (read == 0) 145 if (read == 0)
146 break; 146 break;
147 } 147 }
148 148
149 // Update time 149 // Update time
150 output_.set_start_time(file_position_samples_); 150 output_.set_start_time(file_position_samples_);
151 file_position_samples_ += read; 151 file_position_samples_ += read;
152
153 PushOutput(); 152 PushOutput();
154 } 153 }
155 } 154 }
156 } // namespace aimc 155 } // namespace aimc