CLAM-Development  1.4.0
PCMCodec.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #if USE_SNDFILE != 1
23 #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
24 #endif
25 
26 #include "PCMCodec.hxx"
27 #include "AudioFileFormats.hxx"
28 #include "AudioFile.hxx"
29 #include "AudioFileHeader.hxx"
30 #include "DataTypes.hxx"
31 #include <sndfile.h>
32 #include "PCMAudioStream.hxx"
33 
34 namespace CLAM
35 {
36 
37 namespace AudioCodecs
38 {
39  PCMCodec::PCMCodec()
40  {
41  }
42 
44  {
45  }
46 
48  {
49  static PCMCodec theInstance;
50 
51  return theInstance;
52  }
53 
54  bool PCMCodec::IsReadable( std::string uri ) const
55  {
56  SNDFILE* fileHandle = NULL;
57  SF_INFO fileHeaderInfo;
58  // MRJ: Done as libsndfile doc says
59  fileHeaderInfo.format = 0;
60 
61  fileHandle = sf_open( uri.c_str(), SFM_READ, &fileHeaderInfo );
62 
63  if ( fileHandle != NULL )
64  {
65  sf_close( fileHandle );
66  return true;
67  }
68 
69  // TODO: An exception should be thrown
70  return false;
71 
72  }
73 
74  bool PCMCodec::IsWritable( std::string uri, const AudioFileHeader& header ) const
75  {
76  if ( !header.HasSampleRate() )
77  return false;
78  if ( !header.HasChannels() )
79  return false;
80  if ( !header.HasFormat() )
81  return false;
82  if ( !header.HasEncoding() )
83  return false;
84  if ( !header.HasEndianess() )
85  return false;
86 
87  SF_INFO fileInfo;
88  fileInfo.samplerate = (int)header.GetSampleRate();
89  fileInfo.channels = (int)header.GetChannels();
90  fileInfo.format = header.GetFormat() | header.GetEncoding() | header.GetEndianess();
91 
92  return sf_format_check( &fileInfo ) == 1;
93  }
94 
96  {
97  return new PCMAudioStream(file);
98  }
99 
100  void PCMCodec::RetrieveHeaderData( std::string uri, AudioFileHeader& hdr )
101  {
102  SNDFILE* fileHandle = NULL;
103  SF_INFO fileHeaderInfo;
104  fileHeaderInfo.format = 0;
105 
106  fileHandle = sf_open( uri.c_str(), SFM_READ, &fileHeaderInfo );
107 
108  if ( fileHandle != NULL )
109  {
110  hdr.AddSampleRate();
111  hdr.AddSamples();
112  hdr.AddChannels();
113  hdr.AddFormat();
114  hdr.AddEncoding();
115  hdr.AddEndianess();
116  hdr.AddLength();
117  hdr.UpdateData();
118 
119  hdr.SetSampleRate( (TData)fileHeaderInfo.samplerate );
120  hdr.SetSamples( (TSize)fileHeaderInfo.frames );
121  hdr.SetChannels( (TSize)fileHeaderInfo.channels );
122  hdr.SetFormat( fileHeaderInfo.format & SF_FORMAT_TYPEMASK );
123  hdr.SetEncoding( fileHeaderInfo.format & SF_FORMAT_SUBMASK );
124  hdr.SetEndianess( fileHeaderInfo.format & SF_FORMAT_ENDMASK );
125  hdr.SetLength( TTime(fileHeaderInfo.frames) / hdr.GetSampleRate() );
126  hdr.SetLength( hdr.GetLength() * 1000. );
127  sf_close(fileHandle);
128  }
129 
130  }
131 }
132 
133 }
134