|
CLAM-Development
1.3
|
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 "MonoAudioFileReader.hxx" 00023 #include "AudioCodecs_Stream.hxx" 00024 #include "ProcessingFactory.hxx" 00025 00026 00027 namespace CLAM 00028 { 00029 00030 namespace Hidden 00031 { 00032 static const char * metadata[] = { 00033 "key", "MonoAudioFileReader", 00034 "category", "Audio File I/O", 00035 "description", "MonoAudioFileReader", 00036 0 00037 }; 00038 static FactoryRegistrator<ProcessingFactory, MonoAudioFileReader> reg = metadata; 00039 } 00040 00041 00042 MonoAudioFileReader::MonoAudioFileReader( const ProcessingConfig& cfg ) 00043 : mOutput( "Samples Read", this ) 00044 , mSeekControl( "Seek", this) 00045 , mTimeOutput( "Current Time Position", this) 00046 , mFramePositionOutput( "Current Frame Position", this) 00047 , mProgressOutput( "Progress", this) 00048 , mNativeStream( NULL ) 00049 { 00050 Configure( cfg ); 00051 mSeekControl.SetBounds(0.,1.); 00052 } 00053 00054 MonoAudioFileReader::~MonoAudioFileReader() 00055 { 00056 if ( mNativeStream ) 00057 delete mNativeStream; 00058 } 00059 00060 const char* MonoAudioFileReader::GetClassName() const 00061 { 00062 return "MonoAudioFileReader"; 00063 } 00064 00065 const ProcessingConfig& MonoAudioFileReader::GetConfig() const 00066 { 00067 return mConfig; 00068 } 00069 00070 bool MonoAudioFileReader::ConcreteConfigure( const ProcessingConfig& cfgObject ) 00071 { 00072 CopyAsConcreteConfig( mConfig, cfgObject ); 00073 00074 if ( !mConfig.HasSourceFile() ) 00075 { 00076 AddConfigErrorMessage("The provided config object lacked the field 'SourceFile'"); 00077 return false; 00078 } 00079 00080 const std::string & location = mConfig.GetSourceFile(); 00081 if ( location == "") 00082 { 00083 AddConfigErrorMessage("No file selected"); 00084 return false; 00085 } 00086 mAudioFile.OpenExisting(location); 00087 // Check that the given file can be opened 00088 if ( ! mAudioFile.IsReadable() ) 00089 { 00090 AddConfigErrorMessage("The audio file '" + location + "' could not be opened"); 00091 return false; 00092 } 00093 00094 00095 if ( mConfig.GetSelectedChannel() < 0 00096 || mConfig.GetSelectedChannel() >= mAudioFile.GetHeader().GetChannels() ) 00097 { 00098 AddConfigErrorMessage("The channel selected for reading does not exist"); 00099 return false; 00100 } 00101 00102 mNativeStream = mAudioFile.GetStream(); 00103 return true; 00104 } 00105 00106 bool MonoAudioFileReader::ConcreteStart() 00107 { 00108 if ( not mNativeStream ) 00109 mNativeStream = mAudioFile.GetStream(); 00110 00111 mNativeStream->PrepareReading(); 00112 mEOFReached = false; 00113 00114 return true; 00115 } 00116 00117 bool MonoAudioFileReader::ConcreteStop() 00118 { 00119 mNativeStream->Dispose(); 00120 delete mNativeStream; 00121 mNativeStream = NULL; 00122 00123 return true; 00124 } 00125 00126 bool MonoAudioFileReader::Do() 00127 { 00128 bool result = Do( mOutput.GetAudio() ); 00129 mOutput.Produce(); 00130 00131 return result; 00132 } 00133 00134 00135 bool MonoAudioFileReader::Do( Audio & output ) 00136 { 00137 TData * outputBuffer = output.GetBuffer().GetPtr(); 00138 const unsigned outputSize = output.GetSize(); 00139 00140 if (not mSeekControl.HasBeenRead()) 00141 { 00142 mNativeStream->SeekTo(GetHeader().GetSamples()*mSeekControl.GetLastValue()); 00143 mEOFReached=false; 00144 } 00145 00146 const unsigned long framePosition = mNativeStream->GetFramePosition(); 00147 const TTime secondsPosition = 00148 framePosition / mAudioFile.GetHeader().GetSampleRate(); 00149 00150 if (not mEOFReached) 00151 { 00152 mEOFReached = mNativeStream->ReadData( 00153 mConfig.GetSelectedChannel(), outputBuffer, outputSize); 00154 } 00155 else 00156 { 00157 memset (outputBuffer, 0, outputSize*sizeof(TData)); 00158 } 00159 output.SetBeginTime( secondsPosition*1000 ); 00160 output.SetSampleRate( mAudioFile.GetHeader().GetSampleRate() ); 00161 00162 mTimeOutput.SendControl( secondsPosition*1000.); 00163 mFramePositionOutput.SendControl( framePosition ); 00164 mProgressOutput.SendControl( float(framePosition)/GetHeader().GetSamples()); 00165 00166 if ( not mEOFReached ) return true; // Still in the middle 00167 if ( not mConfig.GetLoop() ) return false; // End reached, not looping 00168 00169 mNativeStream->SeekTo(0); 00170 mEOFReached=false; 00171 return true; 00172 } 00173 } 00174
1.7.6.1