PCMCodec.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 #if USE_SNDFILE != 1
00023 #error USE_SNDFILE was not set to 1 in your settings.cfg file, but you are including files that require this. Please fix your settings.cfg
00024 #endif
00025 
00026 #include "PCMCodec.hxx"
00027 #include "AudioFileFormats.hxx"
00028 #include "AudioFile.hxx"
00029 #include "AudioFileHeader.hxx"
00030 #include "DataTypes.hxx"
00031 #include <sndfile.h>
00032 #include "PCMAudioStream.hxx"
00033 
00034 namespace CLAM
00035 {
00036 
00037 namespace AudioCodecs
00038 {
00039         PCMCodec::PCMCodec()
00040         {
00041         }
00042 
00043         PCMCodec::~PCMCodec()
00044         {
00045         }
00046 
00047         PCMCodec& PCMCodec::Instantiate()
00048         {
00049                 static PCMCodec theInstance;
00050 
00051                 return theInstance;
00052         }
00053 
00054         bool PCMCodec::IsReadable( std::string uri ) const
00055         {
00056                 SNDFILE*   fileHandle = NULL;
00057                 SF_INFO   fileHeaderInfo;
00058                 // MRJ: Done as libsndfile doc says
00059                 fileHeaderInfo.format = 0;
00060 
00061                 fileHandle = sf_open( uri.c_str(), SFM_READ, &fileHeaderInfo );
00062 
00063                 if ( fileHandle != NULL )
00064                 {
00065                         sf_close( fileHandle );
00066                         return true;
00067                 }
00068 
00069                 // TODO: An exception should be thrown
00070                 return false;
00071 
00072         }
00073 
00074         bool PCMCodec::IsWritable( std::string uri, const AudioFileHeader& header ) const
00075         {
00076                 if ( !header.HasSampleRate() )
00077                         return false;
00078                 if ( !header.HasChannels() )
00079                         return false;
00080                 if ( !header.HasFormat() )
00081                         return false;
00082                 if ( !header.HasEncoding() )
00083                         return false;
00084                 if ( !header.HasEndianess() )
00085                         return false;
00086 
00087                 SF_INFO fileInfo;
00088                 fileInfo.samplerate = (int)header.GetSampleRate();
00089                 fileInfo.channels = (int)header.GetChannels();
00090                 fileInfo.format = header.GetFormat() | header.GetEncoding() | header.GetEndianess();
00091 
00092                 return sf_format_check( &fileInfo ) == 1;
00093         }
00094 
00095         Stream* PCMCodec::GetStreamFor( const AudioFile& file )
00096         {
00097                 return new PCMAudioStream(file);
00098         }
00099 
00100         void PCMCodec::RetrieveHeaderData( std::string uri, AudioFileHeader& hdr )
00101         {
00102                 SNDFILE* fileHandle = NULL;
00103                 SF_INFO  fileHeaderInfo;
00104                 fileHeaderInfo.format = 0;
00105                 
00106                 fileHandle = sf_open( uri.c_str(), SFM_READ, &fileHeaderInfo );
00107                 
00108                 if ( fileHandle != NULL )
00109                 {
00110                         hdr.AddSampleRate();
00111                         hdr.AddSamples();
00112                         hdr.AddChannels();
00113                         hdr.AddFormat();
00114                         hdr.AddEncoding();
00115                         hdr.AddEndianess();
00116                         hdr.AddLength();
00117                         hdr.UpdateData();
00118                                                 
00119                         hdr.SetSampleRate( (TData)fileHeaderInfo.samplerate );
00120                         hdr.SetSamples( (TSize)fileHeaderInfo.frames );
00121                         hdr.SetChannels( (TSize)fileHeaderInfo.channels );
00122                         hdr.SetFormat( fileHeaderInfo.format & SF_FORMAT_TYPEMASK );
00123                         hdr.SetEncoding( fileHeaderInfo.format & SF_FORMAT_SUBMASK );
00124                         hdr.SetEndianess( fileHeaderInfo.format & SF_FORMAT_ENDMASK );
00125                         hdr.SetLength( TTime(fileHeaderInfo.frames) / hdr.GetSampleRate() );
00126                         hdr.SetLength( hdr.GetLength() * 1000. );
00127                         sf_close(fileHandle);
00128                 }
00129 
00130         }
00131 }
00132 
00133 }
00134 
Generated by  doxygen 1.6.3