PCMCodec.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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