TAudioFile.hpp

Go to the documentation of this file.
00001 #ifndef __TAudioFile__
00002 #define __TAudioFile__
00003 
00004 #include <Basics/CCountedObject.hpp>
00005 #include "TWaveFileFormat.hpp"
00006 #include "TAiffFileFormat.hpp"
00007 #include "TAudioBuffer.hpp"
00008 using Exponent::Audio::TAudioBuffer;
00009 using Exponent::Audio::TWaveFileFormat;
00010 using Exponent::Audio::TAiffFileFormat;
00011 using Exponent::Basics::CCountedObject;
00012 
00013 namespace Exponent
00014 {
00015     namespace Audio
00016     {
00034         template <class TypeName> class TAudioFile : public CCountedObject
00035         {
00037             EXPONENT_CLASS_DECLARATION;
00040 //  ===========================================================================
00041 
00042         public:
00043 
00044 //  ===========================================================================
00045 
00049             TAudioFile()
00050             {
00051                 EXPONENT_CLASS_CONSTRUCTION(TAudioFile<TypeName>);
00052             }
00053 
00057             virtual ~TAudioFile()
00058             {
00059                 EXPONENT_CLASS_DESTRUCTION(TAudioFile<TypeName>);
00060             }
00061 
00062 //  ===========================================================================
00063 
00068             const TAudioBuffer<TypeName> *getAudioBuffer() const { return &m_audioBuffer; }
00069 
00074             TAudioBuffer<TypeName> *getMutableAudioBuffer() { return &m_audioBuffer; }
00075 
00076 //  ===========================================================================
00077 
00083             bool readFile(const CSystemString &filename)
00084             {
00085                 TWaveFileFormat<TypeName> waveFileFormat;
00086                 if (waveFileFormat.isValidFormat(filename))
00087                 {
00088                     if (!waveFileFormat.openFile(CFileStream::e_input, filename))
00089                     {
00090                         return false;
00091                     }
00092                     
00093                     // Construct the file format
00094                     typename TWaveFileFormat<TypeName>::SAudioFileFormat format;
00095                     if (!waveFileFormat.readHeader(format))
00096                     {
00097                         return false;
00098                     }
00099 
00100                     m_audioBuffer.initialise(format.m_numberOfSamples, format.m_numberOfChannels);
00101 
00102                     // Now read in the audio data, in range  -1 to +1\n
00103                     if (!waveFileFormat.readData(m_audioBuffer))
00104                     {
00105                         return false;
00106                     }
00107 
00108                     return true;
00109                 }
00110                 else
00111                 {
00112                     TAiffFileFormat<TypeName> aiffFileFormat;
00113                     if (!aiffFileFormat.isValidFormat(filename))
00114                     {
00115                         return false;
00116                     }
00117 
00118                     if (!aiffFileFormat.openFile(CFileStream::e_input, filename))
00119                     {
00120                         return false;
00121                     }
00122                     
00123                     typename IAudioFileFormat< TypeName >::SAudioFileFormat format;
00124                     if (!aiffFileFormat.readHeader(format))
00125                     {
00126                         return false;
00127                     }
00128 
00129                     m_audioBuffer.initialise(format.m_numberOfSamples, format.m_numberOfChannels);
00130 
00131                     // Now read in the audio data, in range  -1 to +1\n
00132                     if (!aiffFileFormat.readData(m_audioBuffer))
00133                     {
00134                         return false;
00135                     }
00136 
00137                     return true;
00138                 }
00139                 return false;
00140             }
00141 
00150             bool writeFile(const CSystemString &filename, const unsigned long bitDepth, const unsigned long sampleRate)
00151             {
00152                 return this->writeFile(filename, bitDepth, sampleRate, m_audioBuffer);
00153             }
00154 
00164             bool writeFile(const CSystemString &filename, const unsigned long bitDepth, const unsigned long sampleRate, TAudioBuffer<TypeName> &buffer)
00165             {
00166                 if (!(bitDepth == 8  ||
00167                       bitDepth == 16 ||
00168                       bitDepth == 24))
00169                 {
00170                     return false;
00171                 }
00172 
00173                 TWaveFileFormat<TypeName> waveFileFormat;
00174 
00175                 if (!waveFileFormat.openFile(CFileStream::e_output, filename))
00176                 {
00177                     return false;
00178                 }
00179 
00180                 // Now create the format to write to
00181                 typename TWaveFileFormat<TypeName>::SAudioFileFormat format;
00182                 format.m_bitDepth         = bitDepth;   
00183                 format.m_sampleRate       = sampleRate; 
00184                 format.m_numberOfChannels = buffer.getNumberOfChannels();   
00185                 format.m_numberOfSamples  = buffer.getNumberOfSamples(); 
00186 
00187                 if (!waveFileFormat.writeHeader(format))
00188                 {
00189                     return false;
00190                 }
00191 
00192                 if (!waveFileFormat.writeData(buffer))
00193                 {
00194                     return false;
00195                 }
00196 
00197                 return true;
00198             }
00199 
00200 
00201 //  ===========================================================================
00202 
00203         protected:
00204 
00205 //  ===========================================================================
00206     
00207             TAudioBuffer<TypeName> m_audioBuffer;               
00208         };
00209 
00211         EXPONENT_TEMPLATE_CLASS_IMPLEMENTATION(TAudioFile<TypeName>, TypeName, CCountedObject);
00213     }
00214 }
00215 #endif  // End of TAudioFile.hpp

Infinity API - TAudioFile.hpp Source File generated on 7 Mar 2007