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_readf sf_readf_double
00028 #define CLAM_sf_writef sf_writef_double
00029 #else
00030 #define CLAM_sf_readf sf_readf_float
00031 #define CLAM_sf_writef sf_writef_float
00032 #endif
00033 
00034 namespace CLAM
00035 {
00036 
00037 namespace AudioCodecs
00038 {
00039         PCMAudioStream::PCMAudioStream( const AudioFile& file )
00040                 : mFileHandle( NULL )
00041         {
00042                 mName = file.GetLocation();
00043                 mNativeFileParams.channels = file.GetHeader().GetChannels();
00044                 mNativeFileParams.samplerate = (int) file.GetHeader().GetSampleRate();
00045                 mNativeFileParams.format = file.GetHeader().GetFormat() | file.GetHeader().GetEncoding() | file.GetHeader().GetEndianess();
00046                 SetChannels( mNativeFileParams.channels );
00047         }
00048 
00049         PCMAudioStream::~PCMAudioStream()
00050         {
00051                 Dispose();
00052         }
00053 
00054         void PCMAudioStream::PrepareReading()
00055         {
00056                 mFileHandle = sf_open( mName.c_str(), 
00057                                        SFM_READ, 
00058                                        &mNativeFileParams );
00059 
00060                 CLAM_ASSERT( mFileHandle != NULL,
00061                         "Cannot open file for reading!!!" );
00062                 mEOFReached = false;
00063                 mFramePosition = 0;
00064         }
00065 
00066         void PCMAudioStream::PrepareWriting()
00067         {
00068                 mFileHandle = sf_open( mName.c_str(),
00069                                        SFM_WRITE,
00070                                        &mNativeFileParams );
00071 
00072                 CLAM_ASSERT( mFileHandle != NULL,
00073                         "Cannot open file for writing!!!" );
00074         }
00075 
00076         void PCMAudioStream::Dispose()
00077         {
00078                 if ( mFileHandle )
00079                 {
00080                         sf_close( mFileHandle );
00081                         mFileHandle = NULL;
00082                 }
00083         }
00084 
00085         void PCMAudioStream::DiskToMemoryTransfer()
00086         {
00087                 int nChannels = mNativeFileParams.channels;
00088                 unsigned nFrames = mInterleavedData.size()/nChannels;
00089 
00090                 TData* begin = &mInterleavedData[0];
00091                 const TData* end = begin + mInterleavedData.size();
00092 
00093                 sf_count_t framesRead = CLAM_sf_readf( mFileHandle, begin, nFrames );
00094                 mFramesLastRead = (TSize)framesRead;
00095                 mFramePosition += mFramesLastRead;
00096 
00097                 if (not framesRead) // No more data to read - EOF reached
00098                 {
00099                         mEOFReached = true;
00100                         return;
00101                 }
00102                 if (framesRead<nFrames) // EOF reached
00103                 {
00104                         mEOFReached = true;
00105                         for (TData * p=begin+framesRead*nChannels; p!=end; p++)
00106                                 *p= 0.0;
00107                 }
00108         }
00109 
00110         void PCMAudioStream::MemoryToDiskTransfer()
00111         {
00112                 unsigned nFrames = mInterleavedData.size()/mChannels;
00113                 const TData* begin = &mInterleavedData[0];
00114                 sf_count_t samplesWritten = CLAM_sf_writef( mFileHandle,
00115                                                            begin,
00116                                                            nFrames );
00117 
00118                 CLAM_DEBUG_ASSERT( samplesWritten == nFrames,
00119                              "Could not write all samples to disk!" );
00120         }
00121 
00122         void PCMAudioStream::SeekTo(long unsigned framePosition)
00123         {
00124                 mFramePosition = sf_seek(mFileHandle, framePosition, SEEK_SET);
00125                 mEOFReached = false;
00126         }
00127 }
00128 }
00129 
Generated by  doxygen 1.6.3