Audio.cxx

Go to the documentation of this file.
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 "Audio.hxx"
00023 #include "CLAM_Math.hxx"
00024 
00025 using namespace CLAM;
00026 
00027 
00028 void Audio::DefaultInit(void)
00029 {
00030         // Attribute instantiation.
00031         AddSampleRate();
00032         AddBeginTime();
00033         AddBuffer();
00034         UpdateData();
00035 
00036         // Attribute initialization (Default values).
00037         SetSampleRate(44100);
00038         SetBeginTime(0);//By default, audio starts at 0
00039 }
00040 
00041 void Audio::SetEndTime(TTime time)
00042 {
00043         CLAM_WARNING(false, "Audio::SetEndTime is about to be deprecated. Please use Audio::ResizeToEndTime instead");
00044         ResizeToEndTime(time);
00045 }
00046 
00047 void Audio::ResizeToEndTime(TTime time)
00048 {
00049         const int newsizeRound = Round((time-GetBeginTime())/1000.0*GetSampleRate());
00050         SetSize(newsizeRound);
00051 }
00052 
00053 void Audio::SetSize(int s)
00054 {
00055         int oldSize=GetSize();
00056         if (s==oldSize) return;
00057 
00058         CLAM_ASSERT(s>=0,"Audio::SetSize(): Negative size specified");
00059         if (HasBuffer()) {
00060                 GetBuffer().Resize(s);
00061                 GetBuffer().SetSize(s);
00062         }
00063         if(s>oldSize)
00064                 memset(GetBuffer().GetPtr()+oldSize,0,(s-oldSize)*sizeof(TData));
00065 }
00066 
00067 void Audio::SetDuration(TTime duration)
00068 {
00069         CLAM_WARNING(false, "Audio::SetDuration is about to be deprecated. Please use Audio::ResizeToDuration instead");
00070         ResizeToDuration(GetIndexFromTime(duration));
00071 }
00072 
00073 void Audio::ResizeToDuration(TTime duration)
00074 {
00075         SetSize(GetIndexFromTime(duration));
00076 }
00077 
00078 TTime Audio::GetTimeFromIndex(TIndex index) const
00079 {
00080         return (TTime)((TTime)index / GetSampleRate()*1000.0  );
00081 }
00082 
00083 TIndex Audio::GetIndexFromTime(TTime time) const
00084 {
00085         return Round(time*((TData)GetSampleRate()/1000.0));
00086 }
00087 
00088 void Audio::GetAudioChunk(TTime beginTime, TTime endTime,Audio& chunk, bool configureChunk) const
00089 {
00090         GetAudioChunk(GetIndexFromTime(beginTime),GetIndexFromTime(endTime),chunk, configureChunk);
00091 }
00092 
00093 void Audio::GetAudioSlice(TTime beginTime, TTime endTime,Audio& slice, bool configureSlice) const
00094 {
00095         GetAudioSlice(GetIndexFromTime(beginTime),GetIndexFromTime(endTime),slice, configureSlice);
00096 }
00097 
00098 
00099 
00100 void Audio::GetAudioSlice( TIndex beginIndex, TIndex endIndex, Audio& slice, bool configureChunk ) const
00101 {
00102         CLAM_ASSERT( beginIndex >=0, "Negative indexes are not allowed for audio slices" );
00103         CLAM_ASSERT( endIndex <= GetSize(), "Slices are not allowed to surpass audio size" );
00104 
00105 
00106         TIndex size=endIndex-beginIndex;
00107 
00108         DataArray tmpArray;
00109         tmpArray.SetPtr( GetBuffer().GetPtr() + beginIndex );
00110         tmpArray.SetSize( size );
00111         slice.SetBuffer( tmpArray );
00112 
00113         if(configureChunk)
00114         {
00115                 slice.SetBeginTime(GetTimeFromIndex(beginIndex));
00116                 slice.SetSampleRate( GetSampleRate() );
00117                 slice.GetBuffer().SetSize(size);
00118         }
00119         CLAM_ASSERT(HasBuffer(),"Audio::GetAudioChunk: Buffer not initialized");
00120         
00121 }
00122 
00123 void Audio::GetAudioChunk(TIndex beginIndex,TIndex endIndex,Audio& chunk, bool configureChunk) const
00124 {
00125         
00126         /*Note that begin index is allowed to be less than zero and the end index to be beyond the end*/
00127         CLAM_ASSERT(endIndex>beginIndex,
00128                    "Audio::GetAudioChunk: Incorrect index boundaries for audio chunk");
00129         TSize nBytesToCopy,offset=0;
00130         
00131         if(beginIndex>=GetSize()){
00132                 TIndex size=endIndex-beginIndex;
00133                 if(configureChunk) chunk.SetSize(size);
00134                 //make sure that 0's are set in non written part of audio
00135                 memset(chunk.GetBuffer().GetPtr(),0,size*sizeof(TData));
00136                 return;
00137         }
00138         
00139         chunk.SetBeginTime(GetTimeFromIndex(beginIndex));
00140 
00141         if(configureChunk)
00142         {
00143                 chunk.SetSampleRate( GetSampleRate() );
00144                 TIndex size=endIndex-beginIndex;
00145                 chunk.SetSize(size);
00146                 chunk.SetSampleRate( GetSampleRate() );
00147                 chunk.SetBeginTime( GetTimeFromIndex(beginIndex) );
00148         }
00149         
00150                 
00151         CLAM_ASSERT(HasBuffer(),"Audio::GetAudioChunk: Buffer not initialized");
00152         
00153         /*Whenever trying to copy samples before the beginning or after end of 
00154           actual audio, zeros will be added at the beginning or end of chunk*/
00155 
00156         if(beginIndex<0)
00157         {
00158                 offset=-beginIndex;
00159                 beginIndex=0;
00160                 //make sure that 0's are set in non written part of audio
00161                 memset(chunk.GetBuffer().GetPtr(),0,offset*sizeof(TData));
00162         }
00163         if(endIndex>=GetSize())
00164         { 
00165                 TSize ending=endIndex-GetSize();
00166                 memset(chunk.GetBuffer().GetPtr()+GetSize()-beginIndex ,0,ending*sizeof(TData));
00167                 endIndex=GetSize();
00168         }
00169 
00170 
00171         nBytesToCopy=(endIndex-beginIndex)*sizeof(TData);
00172         
00173         CLAM_ASSERT(
00174                 nBytesToCopy>=0
00175                 && beginIndex>=0
00176                 && int(nBytesToCopy+beginIndex*sizeof(TData))<=GetBuffer().SizeInBytes(),
00177                 "Error");
00178 
00179         memcpy(chunk.GetBuffer().GetPtr()+offset,GetBuffer().GetPtr()+beginIndex,nBytesToCopy);
00180 }
00181 
00182 void Audio::SetAudioChunk(TTime beginTime,const Audio& chunk)
00183 {
00184         SetAudioChunk(GetIndexFromTime(beginTime),chunk);
00185 }
00186 
00187 void Audio::SetAudioChunk(TIndex beginIndex,const Audio& chunk)
00188 {
00189         CLAM_ASSERT(beginIndex<GetSize(),"Audio::SetAudioChunk: Incorrect begin index");
00190         TSize nBytesToCopy,offset=0;
00191         TIndex endIndex=beginIndex+chunk.GetSize();
00192         if(endIndex>GetSize()) endIndex=GetSize();
00193         if(beginIndex<0){ 
00194                 offset=-beginIndex;
00195                 beginIndex=0;}
00196         
00197         CLAM_ASSERT(chunk.HasBuffer()&&HasBuffer(),"Audio::SetAudioChunk: one of the buffers is not initialized") ;
00198         nBytesToCopy=(endIndex-beginIndex)*sizeof(TData);
00199         memcpy(GetBuffer().GetPtr()+beginIndex,chunk.GetBuffer().GetPtr()+offset,nBytesToCopy);
00200 }
00201 

Generated on Tue Jun 19 20:34:52 2007 for CLAM-Development by  doxygen 1.5.2