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                 : mFramesLastRead(0)
00031                 , mFramePosition(0)
00032         {
00033         }
00034 
00035         Stream::~Stream()
00036         {
00037         }
00038 
00039         bool Stream::ReadData( int channel, TData* buffer, unsigned nFrames )
00040         {
00041                 AdjustInterleavedBuffer( nFrames );
00042                 DiskToMemoryTransfer();
00043                 if ( mFramesLastRead == 0 ) return mEOFReached;
00044 
00045                 // Actual data reading
00046                 const TData* begin = &mInterleavedData[0];
00047                 const TData* end = begin + mInterleavedData.size();
00048                 for (const TData* i = begin + channel; i < end; i+=mChannels, buffer++ )
00049                         *buffer = *i;
00050                 
00051                 return mEOFReached;
00052                        
00053         }
00054         bool Stream::ReadData(TData** buffers, unsigned nFrames )
00055         {
00056                 AdjustInterleavedBuffer( nFrames );
00057                 DiskToMemoryTransfer();
00058                 if ( mFramesLastRead == 0 ) return mEOFReached;
00059 
00060                 const TData * interleaved = &mInterleavedData[0];
00061                 for (unsigned iFrame=0; iFrame<nFrames; iFrame++)
00062                         for (unsigned iChannel=0; iChannel<mChannels; iChannel++)
00063                                 buffers[iChannel][iFrame] = *interleaved++;
00064 
00065                 return mEOFReached;
00066         }
00067 
00068         bool Stream::ReadData( int* channels, int nchannels, TData** buffers, unsigned nFrames )
00069         {
00070                 AdjustInterleavedBuffer( nFrames );
00071                 DiskToMemoryTransfer();
00072                 if ( mFramesLastRead == 0 ) return mEOFReached;
00073 
00074                 // Actual data reading
00075                 const TData* begin = &mInterleavedData[0];
00076                 const TData* end = begin + mInterleavedData.size();
00077                 const int* endChannels = channels + nchannels;
00078                 
00079                 for( int* currentChannel = channels;
00080                          currentChannel != endChannels;
00081                          currentChannel++ )
00082                 {
00083                         const int channelIndex = *currentChannel;
00084                         TData* pSamples = buffers[channelIndex];
00085                         for ( const TData* i = begin + channelIndex; i<end; i+=mChannels)
00086                         {
00087                                 *pSamples++ = *i;
00088                         }
00089                 }
00090                 return mEOFReached;
00091         }
00092 
00093         void Stream::AdjustInterleavedBuffer( unsigned nFrames )
00094         {
00095                 unsigned newSize = nFrames * mChannels;
00096                 if (newSize == mInterleavedData.size()) return;
00097                 mInterleavedData.resize( nFrames * mChannels );
00098         }
00099 
00100         void Stream::WriteData( int channel, const TData* buffer, unsigned nFrames )
00101         {
00102                 AdjustInterleavedBuffer( nFrames );
00103                 TData* beginData = &mInterleavedData[0];
00104                 TData* endData = beginData + mInterleavedData.size();
00105                 for (TData* data = beginData+channel; data < endData; data += mChannels)
00106                         *data = *buffer++;
00107 
00108                 MemoryToDiskTransfer();
00109         }
00110 
00111         void Stream::WriteData(TData ** const buffers, unsigned nFrames)
00112         {
00113                 AdjustInterleavedBuffer( nFrames );
00114 
00115                 TData * interleaved = &mInterleavedData[0];
00116                 for (unsigned iFrame=0; iFrame<nFrames; iFrame++)
00117                         for (unsigned iChannel=0; iChannel<mChannels; iChannel++)
00118                                 *interleaved++ = buffers[iChannel][iFrame];
00119 
00120                 MemoryToDiskTransfer();
00121         }
00122 
00123         void Stream::WriteData( int* channels, int nchannels,
00124                                 TData** const samples, unsigned nFrames )
00125         {
00126                 AdjustInterleavedBuffer( nFrames );
00127 
00128                 TData* begin = &mInterleavedData[0];
00129                 TData* end = begin + mInterleavedData.size();
00130                 const int* endChannels = channels + nchannels;
00131 
00132                 for( int* currentChannel = channels;
00133                      currentChannel != endChannels;
00134                      currentChannel++ )
00135                 {
00136                         const int channelIndex = *currentChannel;
00137                         const TData* pSamples = *(samples + channelIndex);
00138                         for ( TData* i = begin + channelIndex; i<end; i+=mChannels, pSamples++ )
00139                         {
00140                                 *i = *pSamples; 
00141                         }
00142                 }
00143 
00144                 MemoryToDiskTransfer();
00145         }
00146 
00147         void Stream::SetChannels( unsigned nChannels )
00148         {
00149                 mChannels = nChannels;
00150         }
00151 
00152 }
00153 
00154 }
00155 
Generated by  doxygen 1.6.3