CLAM-Development
1.1
|
00001 /* 00002 * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG) 00003 * UNIVERSITAT POMPEU FABRA 00004 * 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 * 00020 */ 00021 00022 #include "AudioFileMemoryLoader.hxx" 00023 #include <CLAM/ProcessingFactory.hxx> 00024 00025 #include <cmath> 00026 00027 namespace CLAM 00028 { 00029 00030 namespace Hidden 00031 { 00032 static const char * metadata[] = { 00033 "key", "AudioFileMemoryLoader", 00034 "category", "Audio File I/O", 00035 "description", "AudioFileMemoryLoader", 00036 0 00037 }; 00038 static FactoryRegistrator<ProcessingFactory, AudioFileMemoryLoader> reg = metadata; 00039 } 00040 00041 00042 AudioFileMemoryLoader::AudioFileMemoryLoader( const ProcessingConfig& cfg ) 00043 : _output( "Samples Read", this ), 00044 _timeOutput( "Current Time Position", this ), 00045 _positionInput( "Current Time Position (%)", this ), 00046 _lastPosition( 0 ), 00047 _position( 0 ) 00048 { 00049 Configure( cfg ); 00050 _positionInput.SetBounds( 0.0, 1.0 ); 00051 } 00052 00053 AudioFileMemoryLoader::~AudioFileMemoryLoader() 00054 { 00055 } 00056 00057 // TODO: move it to the header 00058 const char* AudioFileMemoryLoader::GetClassName() const 00059 { 00060 return "AudioFileMemoryLoader"; 00061 } 00062 00063 const ProcessingConfig& AudioFileMemoryLoader::GetConfig() const 00064 { 00065 return _config; 00066 } 00067 00068 bool AudioFileMemoryLoader::ConcreteConfigure( const ProcessingConfig& cfgObject ) 00069 { 00070 CopyAsConcreteConfig( _config, cfgObject ); 00071 00072 MonoAudioFileReader reader(_config); 00073 00074 if ( !reader.IsConfigured() ) 00075 { 00076 AddConfigErrorMessage(reader.GetConfigErrorMessage()); 00077 return false; 00078 } 00079 00080 reader.Start(); 00081 _samples.SetSize(reader.GetHeader().GetSamples()); 00082 reader.Do(_samples); 00083 00084 _sampleRate = reader.GetHeader().GetSampleRate(); 00085 _delta = 0.9 / reader.GetHeader().GetSamples(); 00086 _position = 0; 00087 _timeOutput.SendControl(0); 00088 00089 return true; 00090 } 00091 00092 bool AudioFileMemoryLoader::ConcreteStart() 00093 { 00094 _position = 0; 00095 _timeOutput.SendControl(0); 00096 return true; 00097 } 00098 00099 bool AudioFileMemoryLoader::Do() 00100 { 00101 bool result = Do( _output.GetAudio() ); 00102 _output.Produce(); 00103 00104 return result; 00105 } 00106 00107 bool AudioFileMemoryLoader::Do( Audio & outputSamples ) 00108 { 00109 CLAM::TData * samplesArray = &_samples.GetBuffer()[0]; 00110 CLAM::TData * outputArray = &outputSamples.GetBuffer()[0]; 00111 00112 CLAM::TControlData currentPosition = _positionInput.GetLastValue(); 00113 if (std::fabs (currentPosition - _lastPosition) > _delta) 00114 { 00115 _lastPosition = currentPosition; 00116 _position = long(currentPosition * TControlData(_samples.GetSize())); 00117 } 00118 00119 long lastSample = _samples.GetSize() - 1; 00120 long length = outputSamples.GetSize(); 00121 bool loop = _config.GetLoop(); 00122 long i = 0; 00123 while (i < length) 00124 { 00125 long samplesLeft = lastSample - _position; 00126 if (samplesLeft > length - i) 00127 samplesLeft = length - i; 00128 00129 for (; samplesLeft > 0; samplesLeft--) 00130 { 00131 outputArray[i] = samplesArray[_position]; 00132 i++; 00133 _position++; 00134 } 00135 00136 if (!loop) 00137 break; 00138 00139 if (_position >= lastSample) 00140 _position = 0; 00141 } 00142 00143 for (; i < length; i++) 00144 { 00145 outputArray[i] = 0.0; 00146 } 00147 00148 _timeOutput.SendControl(float(_position) / _samples.GetSize()); 00149 00150 return true; 00151 } 00152 00153 unsigned long AudioFileMemoryLoader::GetSamples() 00154 { 00155 return _samples.GetSize(); 00156 } 00157 00158 } 00159