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 "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 mTimeOutput( "Current Time Position", this), 00045 mNativeStream( NULL ) 00046 { 00047 Configure( cfg ); 00048 } 00049 00050 MonoAudioFileReader::~MonoAudioFileReader() 00051 { 00052 if ( mNativeStream ) 00053 delete mNativeStream; 00054 } 00055 00056 const char* MonoAudioFileReader::GetClassName() const 00057 { 00058 return "MonoAudioFileReader"; 00059 } 00060 00061 const ProcessingConfig& MonoAudioFileReader::GetConfig() const 00062 { 00063 return mConfig; 00064 } 00065 00066 bool MonoAudioFileReader::ConcreteConfigure( const ProcessingConfig& cfgObject ) 00067 { 00068 CopyAsConcreteConfig( mConfig, cfgObject ); 00069 00070 if ( !mConfig.HasSourceFile() ) 00071 { 00072 AddConfigErrorMessage("The provided config object lacked the field 'SourceFile'"); 00073 return false; 00074 } 00075 00076 const std::string & location = mConfig.GetSourceFile(); 00077 if ( location == "") 00078 { 00079 AddConfigErrorMessage("No file selected"); 00080 return false; 00081 } 00082 mAudioFile.OpenExisting(location); 00083 // Check that the given file can be opened 00084 if ( ! mAudioFile.IsReadable() ) 00085 { 00086 AddConfigErrorMessage("The audio file '" + location + "' could not be opened"); 00087 return false; 00088 } 00089 00090 00091 if ( mConfig.GetSelectedChannel() < 0 00092 || mConfig.GetSelectedChannel() >= mAudioFile.GetHeader().GetChannels() ) 00093 { 00094 AddConfigErrorMessage("The channel selected for reading does not exist"); 00095 return false; 00096 } 00097 00098 mNativeStream = mAudioFile.GetStream(); 00099 00100 mNativeStream->DeactivateStrictStreaming(); 00101 00102 return true; 00103 } 00104 00105 bool MonoAudioFileReader::ConcreteStart() 00106 { 00107 if ( mNativeStream == NULL ) 00108 { 00109 mNativeStream = mAudioFile.GetStream(); 00110 mNativeStream->DeactivateStrictStreaming(); 00111 } 00112 00113 mNativeStream->PrepareReading(); 00114 mCurrentBeginTime = 0.0; 00115 mEOFReached = false; 00116 mIsPaused = false; 00117 00118 return true; 00119 } 00120 00121 bool MonoAudioFileReader::ConcreteStop() 00122 { 00123 mNativeStream->Dispose(); 00124 delete mNativeStream; 00125 mNativeStream = NULL; 00126 00127 return true; 00128 } 00129 00130 bool MonoAudioFileReader::Do() 00131 { 00132 bool result = Do( mOutput.GetAudio() ); 00133 mOutput.Produce(); 00134 00135 return result; 00136 } 00137 00138 00139 00140 bool MonoAudioFileReader::Do( Audio & outputSamples ) 00141 { 00142 if ( !AbleToExecute() ) 00143 return false; 00144 00145 if ( !mEOFReached && !mIsPaused ) 00146 { 00147 mEOFReached = mNativeStream->ReadData( mConfig.GetSelectedChannel(), 00148 outputSamples.GetBuffer().GetPtr(), 00149 outputSamples.GetSize() ); 00150 } 00151 else 00152 { 00153 if ( mEOFReached ) mCurrentBeginTime = GetHeader().GetLength();// /1000; 00154 memset ((void *)outputSamples.GetBuffer().GetPtr(), 0, outputSamples.GetSize()*sizeof(TData)); 00155 } 00156 00157 outputSamples.SetBeginTime( mCurrentBeginTime ); 00158 if ( !mEOFReached && !mIsPaused ) 00159 { 00160 mDeltaTime = outputSamples.GetSize() / mAudioFile.GetHeader().GetSampleRate()*1000; 00161 mCurrentBeginTime += mDeltaTime; 00162 } 00163 mTimeOutput.SendControl( mCurrentBeginTime / 1000 ); 00164 outputSamples.SetSampleRate( mAudioFile.GetHeader().GetSampleRate() ); 00165 00166 if ( ! mEOFReached ) return true; 00167 if ( ! mConfig.GetLoop() ) return false; 00168 00169 // Reseting the playback to the begining 00170 ConcreteStop(); 00171 mNativeStream = mAudioFile.GetStream(); 00172 mNativeStream->DeactivateStrictStreaming(); 00173 ConcreteStart(); 00174 return true; 00175 } 00176 00177 } 00178