AudioCodecs_Stream.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 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 "AudioCodecs_Stream.hxx"
00023 
00024 namespace CLAM
00025 {
00026 
00027 namespace AudioCodecs
00028 {
00029         Stream::Stream()
00030                 : mStrictStreaming( true ), mFramesLastRead( 0 )
00031         {
00032         }
00033 
00034         Stream::~Stream()
00035         {
00036         }
00037 
00038         bool Stream::AllChannelsProduced()
00039         {
00040 
00041                 std::vector<bool>::iterator i = mChannelsProduced.begin();
00042 
00043                 while ( i != mChannelsProduced.end() && *i )
00044                         i++;
00045 
00046                 return i == mChannelsProduced.end();
00047         }
00048 
00049         void Stream::MarkAllChannelsAsProduced()
00050         {
00051 
00052                 for ( std::vector<bool>::iterator i = mChannelsProduced.begin();
00053                       i != mChannelsProduced.end();
00054                       i++ )
00055                         *i = true;
00056 
00057         }
00058 
00059         void Stream::ResetProducedChannels()
00060         {
00061                 for ( std::vector<bool>::iterator i = mChannelsProduced.begin();
00062                       i != mChannelsProduced.end();
00063                       i++ )
00064                         *i = false;
00065         }
00066 
00067         bool Stream::AllChannelsConsumed()
00068         {
00069                 std::vector<bool>::iterator i = mChannelsConsumed.begin();
00070 
00071                 while ( i != mChannelsConsumed.end() && *i )
00072                         i++;
00073 
00074                 return i == mChannelsConsumed.end();
00075 
00076         }
00077 
00078         void Stream::MarkAllChannelsAsConsumed()
00079         {
00080                 for ( std::vector<bool>::iterator i = mChannelsConsumed.begin();
00081                       i != mChannelsConsumed.end();
00082                       i++ )
00083                         *i = true;
00084         }
00085 
00086         void Stream::ResetConsumedChannels()
00087         {
00088                 for ( std::vector<bool>::iterator i = mChannelsConsumed.begin();
00089                       i != mChannelsConsumed.end();
00090                       i++ )
00091                         *i = false;
00092         }
00093 
00094         bool Stream::HandleReAllocation( DataArray& buffer, TSize newSize )
00095         {
00096                 if ( newSize  > buffer.Size() )
00097                 {
00098                         buffer.Resize( newSize );
00099                         buffer.SetSize( newSize );
00100                         return true;
00101                 }
00102                 else
00103                 {
00104                         buffer.SetSize( newSize );              
00105                         return false;
00106                 }
00107         }
00108 
00109         void Stream::CheckForFileReading( TSize howmany )
00110         {
00111                 if ( !StrictStreaming() || AllChannelsConsumed() )
00112                 {
00113                         ResetConsumedChannels();
00114                         
00115                         if ( HandleReAllocation( mInterleavedData, howmany*mChannels ) )
00116                                 mFramesToRead = howmany;
00117 
00118                         // Acquire samples from file 
00119 
00120                         DiskToMemoryTransfer();
00121                 }
00122         }
00123 
00124         bool Stream::ReadData( int channel, TData* ptr, TSize howmany )
00125         {
00126                 CheckForFileReading( howmany );
00127 
00128                 if ( mFramesLastRead != 0 )
00129                 {
00130                         
00131                         
00132                         // Actual data reading
00133                         int channelCount = mChannels;
00134                         
00135                         const TData* end = mInterleavedData.GetPtr() + mInterleavedData.Size();
00136                         const int stride = channelCount;
00137                         
00138                         for ( TData* i = mInterleavedData.GetPtr() + channel;
00139                               i < end; i+=stride, ptr++ )
00140                                 *ptr = *i;
00141                         
00142                         mChannelsConsumed[ channel ] = true;
00143                         
00144                 }
00145                 
00146                 return mEOFReached;
00147                        
00148         }
00149 
00150         bool Stream::ReadData( int* channels, int nchannels,
00151                                TData** samples, TSize howmany )
00152         {
00153                 CheckForFileReading( howmany );
00154 
00155                 if ( mFramesLastRead != 0 )
00156                 {
00157                         
00158                         // Actual data reading
00159                         int channelCount = mChannels;
00160                         
00161                         const TData*  end = mInterleavedData.GetPtr() + mInterleavedData.Size();
00162                         //Unused variable TData** const samplesEnd = samples + nchannels;
00163                         const int* endChannels = channels + nchannels;
00164                         std::vector<bool>::iterator cIt = mChannelsConsumed.begin();
00165                         
00166                         for( int* currentChannel = channels;
00167                              currentChannel != endChannels;
00168                              currentChannel++ )
00169                         {
00170                                 const int channelIndex = *currentChannel;
00171                                 // mark channel as consumed
00172                                 *(cIt + channelIndex ) = true;
00173                                 const int stride = channelCount;
00174                                 TData* pSamples = *(samples + channelIndex);
00175                                 
00176                                 for ( const TData* i = mInterleavedData.GetPtr() + channelIndex;
00177                                       i<end;
00178                                       i+=stride, pSamples++ )
00179                                 {
00180                                         *pSamples = *i;
00181                                         
00182                                 }
00183                                 
00184                         }
00185                 }
00186 
00187 
00188                 return mEOFReached;
00189                 
00190         }
00191 
00192         void Stream::PrepareFileWriting( TSize howmany )
00193         {
00194                 if ( AllChannelsProduced() )
00195                 {
00196                         ResetProducedChannels();
00197                         
00198                         if ( HandleReAllocation( mInterleavedDataOut, howmany * mChannels ) )
00199                                 mFramesToWrite = howmany;
00200 
00201                 }
00202         }
00203 
00204         void Stream::WriteData( int channel, const TData* ptr, TSize howmany )
00205         {
00206                 PrepareFileWriting( howmany );
00207 
00208                 int channelCount = mChannels;
00209 
00210                 const TData* endData = mInterleavedDataOut.GetPtr()+mInterleavedDataOut.Size();
00211 
00212                 const int stride = channelCount;
00213 
00214                 for ( TData* data = mInterleavedDataOut.GetPtr() + channel;
00215                       data < endData;
00216                       data += stride, ptr++ )
00217                         *data = *ptr;
00218 
00219                 mChannelsProduced[channel] = true;
00220 
00221                 if ( AllChannelsProduced() )
00222                         MemoryToDiskTransfer();
00223 
00224         }
00225 
00226         void Stream::WriteData( int* channels, int nchannels,
00227                                 TData** const samples, TSize howmany )
00228         {
00229                 PrepareFileWriting( howmany );
00230         
00231                 int channelCount = mChannels;
00232 
00233                 const TData* end = mInterleavedDataOut.GetPtr() + mInterleavedDataOut.Size();
00234                 const int* endChannels = channels + nchannels;
00235                 //Unused variable TData** const samplesEnd = samples + nchannels;
00236                 std::vector<bool>::iterator cIt = mChannelsProduced.begin();
00237 
00238 
00239 
00240                 for( int* currentChannel = channels;
00241                      currentChannel != endChannels;
00242                      currentChannel++ )
00243                 {
00244                         const int channelIndex = *currentChannel;
00245                         // mark channel as consumed
00246                         *(cIt + channelIndex ) = true;
00247                         const int stride = channelCount;
00248                         const TData* pSamples = *(samples + channelIndex);
00249 
00250                         for ( TData* i = mInterleavedDataOut.GetPtr() + channelIndex;
00251                               i<end;
00252                               i+=stride, pSamples++ )
00253                         {
00254                                 *i = *pSamples; 
00255 
00256                         }
00257 
00258                 }
00259 
00260                 if ( AllChannelsProduced() )
00261                         MemoryToDiskTransfer();
00262 
00263         }
00264 
00265         void Stream::SetChannels( TSize nChannels )
00266         {
00267                 mChannels = nChannels;
00268                 mChannelsConsumed.resize( nChannels );
00269                 mChannelsProduced.resize( nChannels );
00270         }
00271 
00272 }
00273 
00274 }
00275 

Generated on Tue Aug 12 22:33:41 2008 for CLAM by  doxygen 1.5.5