CLAM-Development  1.4.0
AudioFile.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-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 #include "AudioFile.hxx"
23 #include "Assert.hxx"
24 #include "PCMCodec.hxx"
25 #if USE_OGGVORBIS == 1
26 # include "OggVorbisCodec.hxx"
27 #endif
28 
29 #if USE_MAD == 1
30 # include "MpegCodec.hxx"
31 #endif
32 
33 namespace CLAM
34 {
36  : mCodec( EAudioFileCodec::eUnknown )
37  , mActiveCodec( NULL )
38  {
39  }
40 
42  {
43  mLocation = obj.mLocation;
44  mActiveCodec = obj.mActiveCodec;
45  mCodec = obj.mCodec;
46  mHeaderData = obj.mHeaderData;
47  mTextDescriptors = obj.mTextDescriptors;
48  }
49 
51  {
52  mLocation = obj.mLocation;
53  mActiveCodec = obj.mActiveCodec;
54  mCodec = obj.mCodec;
55  mHeaderData = obj.mHeaderData;
56  mTextDescriptors = obj.mTextDescriptors;
57  return *this;
58  }
59 
61  {
62  }
63 
65  {
66  mActiveCodec = NULL;
67 
68  const std::string &location = mLocation;
69  if ( !AudioCodecs::Codec::FileExists( location ) )
70  {
72  return;
73  }
74 
76  {
78  mActiveCodec = &AudioCodecs::PCMCodec::Instantiate();
79  }
80 #if USE_OGGVORBIS == 1
82  {
85  }
86 #endif
87 #if USE_MAD == 1
88  else if ( AudioCodecs::MpegCodec::Instantiate().IsReadable( location ) )
89  {
91  mActiveCodec = &AudioCodecs::MpegCodec::Instantiate();
92  }
93 #endif
94  else
96  }
97 
98  bool AudioFile::SetHeader( const AudioFileHeader& newHeader )
99  {
100  mHeaderData.AddAll();
101  mHeaderData.UpdateData();
102 
103  if ( !newHeader.HasSampleRate() )
104  return false;
105  else
106  mHeaderData.SetSampleRate( newHeader.GetSampleRate() );
107 
108  if ( !newHeader.HasChannels() )
109  mHeaderData.SetChannels( 1 );
110  else
111  mHeaderData.SetChannels( newHeader.GetChannels() );
112 
113  if ( !newHeader.HasFormat() )
114  return false;
115 
116  if ( newHeader.HasEncoding() )
117  mHeaderData.SetEncoding( newHeader.GetEncoding() );
118 
119 
120  if ( newHeader.GetFormat() < EAudioFileFormat::eVorbisMk1 )
121  {
123  mActiveCodec = &AudioCodecs::PCMCodec::Instantiate();
124  }
125  else if ( newHeader.GetFormat() == EAudioFileFormat::eVorbisMk1 )
126  {
128 #if USE_OGGVORBIS==1
130 #endif
131  }
132  else
133  CLAM_ASSERT( false, "The Enum contained and invalid value!" );
134 
135  mHeaderData.SetFormat( newHeader.GetFormat() );
136 
137  if ( !newHeader.HasEndianess() )
138  mHeaderData.SetEndianess( EAudioFileEndianess::eDefault );
139  else
140  mHeaderData.SetEndianess( newHeader.GetEndianess() );
141 
142  return true;
143  }
144 
146  {
147  return mCodec;
148  }
149 
150  bool AudioFile::IsValid() const
151  {
152  return mActiveCodec == NULL;
153  }
154 
156  {
157  return mActiveCodec && mActiveCodec->IsReadable(mLocation);
158  }
159 
161  {
162  return mActiveCodec && mActiveCodec->IsWritable(mLocation, mHeaderData );
163  }
164 
166  {
167  if ( !mActiveCodec ) return 0;
168  return mActiveCodec->GetStreamFor( *this );
169  }
170 
172  {
173  if (!mActiveCodec) return;
174  mActiveCodec->RetrieveHeaderData( mLocation, mHeaderData );
175  mActiveCodec->RetrieveTextDescriptors( mLocation, mTextDescriptors );
176  }
177 
179  {
180  mHeaderData.RemoveAll();
181  mHeaderData.UpdateData();
182  }
183 
184  /* =============================================================== */
185 
186  void AudioFileSource::OpenExisting(const std::string &location)
187  {
188  mLocation = location;
189  ResolveCodec();
190  ActivateCodec();
192  ResetHeaderData();
193  }
194 
195  /* =============================================================== */
196 
197  bool AudioFileTarget::CreateNew(const std::string &location,
198  const AudioFileHeader &header)
199  {
200  mLocation = location;
201  return SetHeader(header);
202  }
203 
204 }
205