PCMAudioStream.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 "PCMAudioStream.hxx"
00023 #include "AudioFile.hxx"
00024 #include "Assert.hxx"
00025 
00026 #ifdef CLAM_DOUBLE
00027 #define CLAM_sf_read sf_readf_double
00028 #define CLAM_sf_write sf_writef_double
00029 #else
00030 #define CLAM_sf_read sf_readf_float
00031 #define CLAM_sf_write sf_writef_float
00032 #endif
00033 
00034 namespace CLAM
00035 {
00036 
00037 namespace AudioCodecs
00038 {
00039         PCMAudioStream::PCMAudioStream()
00040                 : mFileHandle( NULL )
00041         {
00042                 mNativeFileParams.format = 0;
00043         }
00044 
00045         PCMAudioStream::PCMAudioStream( const AudioFile& file )
00046                 : mFileHandle( NULL )
00047         {
00048                 SetFOI( file );
00049 
00050         }
00051 
00052         PCMAudioStream::~PCMAudioStream()
00053         {
00054                 if ( mFileHandle )
00055                 {
00056                         sf_close( mFileHandle );
00057                 }
00058         }
00059 
00060         void PCMAudioStream::AudioFileToNative( const AudioFile& file)
00061         {
00062                 mName = file.GetLocation();
00063                 mNativeFileParams.channels = file.GetHeader().GetChannels();
00064                 mNativeFileParams.samplerate = (int) file.GetHeader().GetSampleRate();
00065                 mNativeFileParams.format = file.GetHeader().GetFormat() | file.GetHeader().GetEncoding() | file.GetHeader().GetEndianess();
00066         }
00067 
00068         void PCMAudioStream::SetFOI( const AudioFile& file )
00069         {
00070                 Dispose();
00071                 AudioFileToNative(file);
00072                 SetChannels( mNativeFileParams.channels );
00073         }
00074 
00075         void PCMAudioStream::PrepareReading()
00076         {
00077                 mFileHandle = sf_open( mName.c_str(), 
00078                                        SFM_READ, 
00079                                        &mNativeFileParams );
00080 
00081                 CLAM_ASSERT( mFileHandle != NULL,
00082                              "Cannot open file for reading!!!" );
00083                 MarkAllChannelsAsConsumed();
00084                 mEOFReached = false;
00085         }
00086 
00087         void PCMAudioStream::PrepareWriting()
00088         {
00089                 
00090                 mFileHandle = sf_open( mName.c_str(),
00091                                        SFM_WRITE,
00092                                        &mNativeFileParams );
00093 
00094                 CLAM_ASSERT( mFileHandle != NULL,
00095                              "Cannot open file for writing!!!" );
00096 
00097                 MarkAllChannelsAsProduced();
00098         }
00099 
00100         void PCMAudioStream::PrepareReadWrite()
00101         {
00102                 mFileHandle = sf_open( mName.c_str(),
00103                                        SFM_RDWR,
00104                                        &mNativeFileParams );
00105 
00106                 CLAM_ASSERT( mFileHandle != NULL,
00107                              "Cannot open file for reading/writing!!!" );
00108 
00109                 MarkAllChannelsAsConsumed();
00110                 MarkAllChannelsAsProduced();
00111                 mEOFReached = false;
00112         }
00113 
00114         void PCMAudioStream::Dispose()
00115         {
00116                 if ( mFileHandle )
00117                 {
00118                         sf_close( mFileHandle );
00119                         mFileHandle = NULL;
00120                 }
00121         }
00122 
00123         void PCMAudioStream::DiskToMemoryTransfer()
00124         {
00125                 int channelCount = mNativeFileParams.channels;                  
00126                 
00127                 sf_count_t framesRead = CLAM_sf_read( mFileHandle, 
00128                                                        mInterleavedData.GetPtr(),
00129                                                        mFramesToRead );
00130 
00131                 mFramesLastRead = (TSize)framesRead;
00132                 
00133                 if ( framesRead == 0 ) // No more data to read - EOF reached
00134                 {
00135                         mEOFReached = true;
00136                         return;
00137                 }
00138                 
00139                 if ( framesRead < mFramesToRead ) // EOF reached
00140                 {
00141                         // We set the remainder to zero
00142 
00143                         const TData* end = mInterleavedData.GetPtr() + mInterleavedData.Size();
00144 
00145                         for ( TData* i = mInterleavedData.GetPtr() + ( framesRead * channelCount );
00146                               i != end;
00147                               i++ )
00148                                 *i = 0.0;
00149                         
00150                         mEOFReached = true;
00151                 }               
00152         }
00153 
00154         
00155         void PCMAudioStream::MemoryToDiskTransfer()
00156         {
00157                 sf_count_t samplesWritten = CLAM_sf_write( mFileHandle,
00158                                                            mInterleavedDataOut.GetPtr(),
00159                                                            mFramesToWrite );
00160 
00161                 CLAM_DEBUG_ASSERT( samplesWritten == mFramesToWrite,
00162                              "Could not write all samples to disk!" );
00163         }
00164 
00165 }
00166 }
00167 

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