CLAM-Development  1.4.0
AudioFileMemoryLoader.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
23 #include <CLAM/ProcessingFactory.hxx>
24 
25 #include <cmath>
26 
27 namespace CLAM
28 {
29 
30 namespace Hidden
31 {
32  static const char * metadata[] = {
33  "key", "AudioFileMemoryLoader",
34  "category", "Audio File I/O",
35  "description", "AudioFileMemoryLoader",
36  0
37  };
39 }
40 
41 
43  : _output( "Samples Read", this )
44  , _timeOutput( "Current Time Position", this )
45  , _positionInput( "Current Time Position (%)", this )
46  , _lastPosition( 0 )
47  , _position( 0 )
48  {
49  Configure( cfg );
50  _positionInput.SetBounds( 0.0, 1.0 );
51  }
52 
54  {
55  }
56 
57  // TODO: move it to the header
59  {
60  return "AudioFileMemoryLoader";
61  }
62 
64  {
65  return _config;
66  }
67 
69  {
70  CopyAsConcreteConfig( _config, cfgObject );
71 
73 
74  if ( !reader.IsConfigured() )
75  {
77  return false;
78  }
79 
80  reader.Start();
81  _samples.SetSize(reader.GetHeader().GetSamples());
82  reader.Do(_samples);
83 
84  _sampleRate = reader.GetHeader().GetSampleRate();
85  _delta = 0.9 / reader.GetHeader().GetSamples();
86  _position = 0;
88 
89  return true;
90  }
91 
93  {
94  _position = 0;
96  return true;
97  }
98 
100  {
101  bool result = Do( _output.GetAudio() );
102  _output.Produce();
103 
104  return result;
105  }
106 
107  bool AudioFileMemoryLoader::Do( Audio & outputSamples )
108  {
109  CLAM::TData * samplesArray = &_samples.GetBuffer()[0];
110  CLAM::TData * outputArray = &outputSamples.GetBuffer()[0];
111 
112  CLAM::TControlData currentPosition = _positionInput.GetLastValue();
113  if (std::fabs (currentPosition - _lastPosition) > _delta)
114  {
115  _lastPosition = currentPosition;
116  _position = long(currentPosition * TControlData(_samples.GetSize()));
117  }
118 
119  long lastSample = _samples.GetSize() - 1;
120  long length = outputSamples.GetSize();
121  bool loop = _config.GetLoop();
122  long i = 0;
123  while (i < length)
124  {
125  long samplesLeft = lastSample - _position;
126  if (samplesLeft > length - i)
127  samplesLeft = length - i;
128 
129  for (; samplesLeft > 0; samplesLeft--)
130  {
131  outputArray[i] = samplesArray[_position];
132  i++;
133  _position++;
134  }
135 
136  if (!loop)
137  break;
138 
139  if (_position >= lastSample)
140  _position = 0;
141  }
142 
143  for (; i < length; i++)
144  {
145  outputArray[i] = 0.0;
146  }
147 
149 
150  return true;
151  }
152 
154  {
155  return _samples.GetSize();
156  }
157 
158 }
159