tomwalters@275
|
1 // Copyright 2006-2010, Willem van Engen, Thomas Walters
|
tomwalters@275
|
2 //
|
tomwalters@275
|
3 // AIM-C: A C++ implementation of the Auditory Image Model
|
tomwalters@275
|
4 // http://www.acousticscale.org/AIMC
|
tomwalters@275
|
5 //
|
tomwalters@318
|
6 // Licensed under the Apache License, Version 2.0 (the "License");
|
tomwalters@318
|
7 // you may not use this file except in compliance with the License.
|
tomwalters@318
|
8 // You may obtain a copy of the License at
|
tomwalters@275
|
9 //
|
tomwalters@318
|
10 // http://www.apache.org/licenses/LICENSE-2.0
|
tomwalters@275
|
11 //
|
tomwalters@318
|
12 // Unless required by applicable law or agreed to in writing, software
|
tomwalters@318
|
13 // distributed under the License is distributed on an "AS IS" BASIS,
|
tomwalters@318
|
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
tomwalters@318
|
15 // See the License for the specific language governing permissions and
|
tomwalters@318
|
16 // limitations under the License.
|
tomwalters@275
|
17
|
tomwalters@275
|
18 /*! \file
|
tomwalters@275
|
19 * \brief Audio file input
|
tomwalters@275
|
20 *
|
tomwalters@275
|
21 * \author Willem van Engen <cnbh@willem.engen.nl>
|
tomwalters@275
|
22 * \author Thomas Walters <tom@acousticscale.org>
|
tomwalters@275
|
23 * \date created 2006/09/21
|
tomwalters@275
|
24 * \version \$Id$
|
tomwalters@275
|
25 */
|
tomwalters@275
|
26
|
tomwalters@275
|
27 #include <vector>
|
tomwalters@275
|
28
|
tomwalters@275
|
29 #include "Modules/Input/ModuleFileInput.h"
|
tomwalters@275
|
30
|
tomwalters@275
|
31 namespace aimc {
|
tomwalters@275
|
32 ModuleFileInput::ModuleFileInput(Parameters *params) : Module(params) {
|
tomwalters@278
|
33 module_description_ = "File input using libsndfile";
|
tomwalters@278
|
34 module_identifier_ = "file_input";
|
tomwalters@278
|
35 module_type_ = "input";
|
tomwalters@278
|
36 module_version_ = "$Id$";
|
tomwalters@278
|
37
|
tomwalters@279
|
38 file_handle_ = NULL;
|
tomwalters@279
|
39 buffer_length_ = parameters_->DefaultInt("input.buffersize", 1024);
|
tomwalters@275
|
40
|
tomwalters@275
|
41 file_position_samples_ = 0;
|
tomwalters@275
|
42 file_loaded_ = false;
|
tomwalters@275
|
43 audio_channels_ = 0;
|
tomwalters@275
|
44 sample_rate_ = 0.0f;
|
tomwalters@275
|
45 }
|
tomwalters@275
|
46
|
tomwalters@275
|
47 ModuleFileInput::~ModuleFileInput() {
|
tomwalters@292
|
48 if (file_handle_ != NULL) {
|
tomwalters@279
|
49 sf_close(file_handle_);
|
tomwalters@292
|
50 file_handle_ = NULL;
|
tomwalters@292
|
51 }
|
tomwalters@275
|
52 }
|
tomwalters@275
|
53
|
tomwalters@275
|
54 void ModuleFileInput::ResetInternal() {
|
tomwalters@292
|
55 // If there's a file open. Close it.
|
tomwalters@292
|
56 if (file_handle_ != NULL) {
|
tomwalters@292
|
57 sf_close(file_handle_);
|
tomwalters@292
|
58 file_handle_ = NULL;
|
tomwalters@292
|
59 }
|
tom@420
|
60 // Open a file
|
tomwalters@279
|
61 SF_INFO sfinfo;
|
tomwalters@280
|
62 memset(reinterpret_cast<void*>(&sfinfo), 0, sizeof(SF_INFO));
|
tomwalters@292
|
63
|
tomwalters@402
|
64 file_loaded_ = false;
|
tomwalters@402
|
65 file_handle_ = sf_open(global_parameters_->GetString("input_filename"),
|
tomwalters@402
|
66 SFM_READ,
|
tomwalters@402
|
67 &sfinfo);
|
tomwalters@275
|
68
|
tomwalters@279
|
69 if (file_handle_ == NULL) {
|
tomwalters@280
|
70 /*! \todo Also display error reason
|
tomwalters@280
|
71 */
|
tomwalters@402
|
72 LOG_ERROR(_T("Couldn't read audio file '%s'"),
|
tomwalters@402
|
73 global_parameters_->GetString("input_filename"));
|
tom@420
|
74 return;
|
tomwalters@279
|
75 }
|
tomwalters@402
|
76
|
tomwalters@275
|
77 file_loaded_ = true;
|
tomwalters@402
|
78 done_ = false;
|
tomwalters@275
|
79 audio_channels_ = sfinfo.channels;
|
tomwalters@275
|
80 sample_rate_ = sfinfo.samplerate;
|
tomwalters@279
|
81 file_position_samples_ = 0;
|
tomwalters@275
|
82
|
tomwalters@402
|
83 if (audio_channels_ < 1 || buffer_length_ < 1 || sample_rate_ < 0.0f) {
|
tomwalters@402
|
84 LOG_ERROR(_T("Problem with file: audio_channels = %d, buffer_length_ = %d, sample_rate = %f"), audio_channels_, buffer_length_, sample_rate_);
|
tom@420
|
85 return;
|
tomwalters@402
|
86 }
|
tomwalters@275
|
87
|
tomwalters@402
|
88 output_.Initialize(audio_channels_, buffer_length_, sample_rate_);
|
tomwalters@402
|
89 output_.set_start_time(0);
|
tom@420
|
90 }
|
tom@420
|
91
|
tom@420
|
92 bool ModuleFileInput::InitializeInternal(const SignalBank& input) {
|
tom@420
|
93 ResetInternal();
|
tomwalters@402
|
94 return true;
|
tomwalters@275
|
95 }
|
tomwalters@275
|
96
|
tomwalters@275
|
97 void ModuleFileInput::Process(const SignalBank& input) {
|
tomwalters@278
|
98 if (!file_loaded_)
|
tomwalters@278
|
99 return;
|
tomwalters@279
|
100 sf_count_t read;
|
tomwalters@275
|
101 vector<float> buffer;
|
tomwalters@275
|
102 buffer.resize(buffer_length_ * audio_channels_);
|
tomwalters@275
|
103
|
tomwalters@402
|
104 // Read buffersize bytes into buffer
|
tomwalters@402
|
105 read = sf_readf_float(file_handle_, &buffer[0], buffer_length_);
|
hamel@449
|
106
|
tomwalters@402
|
107 // Place the contents of the buffer into the signal bank
|
tomwalters@402
|
108 int counter = 0;
|
hamel@448
|
109 for (int i = 0; i < read; ++i) {
|
hamel@448
|
110 for (int c = 0; c < audio_channels_; ++c) {
|
tomwalters@402
|
111 output_.set_sample(c, i, buffer[counter]);
|
tomwalters@402
|
112 ++counter;
|
tomwalters@402
|
113 }
|
tomwalters@402
|
114 }
|
tomwalters@275
|
115
|
tom@442
|
116 // If the number of samples read is less than the buffer length, the end
|
tomwalters@402
|
117 // of the file has been reached.
|
tomwalters@402
|
118 if (read < buffer_length_) {
|
tomwalters@402
|
119 // Zero samples at end
|
hamel@448
|
120 for (int i = read; i < buffer_length_; ++i) {
|
hamel@448
|
121 for (int c = 0; c < audio_channels_; ++c) {
|
tomwalters@402
|
122 output_.set_sample(c, i, 0.0f);
|
tomwalters@275
|
123 }
|
tomwalters@275
|
124 }
|
tomwalters@402
|
125 // When we're past the end of the buffer, set the
|
tomwalters@402
|
126 // module state to 'done' and exit.
|
tomwalters@402
|
127 if (read == 0)
|
tomwalters@402
|
128 done_ = true;
|
tomwalters@402
|
129 return;
|
tomwalters@402
|
130 }
|
tomwalters@275
|
131
|
tomwalters@402
|
132 // Update time.
|
tomwalters@402
|
133 output_.set_start_time(file_position_samples_);
|
tomwalters@402
|
134 file_position_samples_ += read;
|
tomwalters@402
|
135 PushOutput();
|
tomwalters@275
|
136 }
|
tomwalters@275
|
137 } // namespace aimc
|