00001 /*!@file Audio/AudioBuffer.H A buffer of audio data */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Audio/AudioBuffer.H $ 00035 // $Id: AudioBuffer.H 8647 2007-07-30 19:40:34Z abondada $ 00036 // 00037 00038 #ifndef AUDIO_AUDIOBUFFER_H_DEFINED 00039 #define AUDIO_AUDIOBUFFER_H_DEFINED 00040 00041 #include "Image/ArrayData.H" 00042 00043 //! AudioBuffer is a buffer of audio data 00044 /*! AudioBuffer has copy-on-write/ref-counting semantics and automatic 00045 memory management, just like Image does. The template argument 00046 specifies the type of the audio data (typically, byte, or int16). */ 00047 template <class T> 00048 class AudioBuffer { 00049 00050 public: 00051 // ############################################################ 00052 /*! @name Constructors, destructors, assignment */ 00053 //@{ 00054 00055 //! Constructor from raw data 00056 inline AudioBuffer(const T* data, const uint nsampl, const uint nchan, 00057 const float freq); 00058 00059 //! Allocate memory for future data 00060 inline AudioBuffer(const uint nsampl, const uint nchan, const float freq, 00061 InitPolicy init); 00062 00063 //! Copy constructor 00064 inline AudioBuffer(const AudioBuffer<T>& A); 00065 00066 //! Assigment operator 00067 inline AudioBuffer<T>& operator=(const AudioBuffer<T>& A); 00068 00069 //! Empty constructor 00070 inline AudioBuffer(); 00071 00072 //! Destructor 00073 inline ~AudioBuffer(); 00074 00075 //@} 00076 00077 // ############################################################ 00078 /*! @name Access functions */ 00079 //@{ 00080 00081 //! Get number of samples 00082 inline uint nsamples() const; 00083 00084 //! Get number of channels 00085 inline uint nchans() const; 00086 00087 //! Get sampling frequency in samples/s 00088 inline float freq() const; 00089 00090 //! Set sampling frequency in samples/s 00091 inline void setFreq(const float f); 00092 00093 //! Get a sample 00094 inline const T& getVal(const uint sample, const uint channel); 00095 00096 //! Get total data size in number of T's 00097 /*! This is nsamples * nchans */ 00098 inline uint size() const; 00099 00100 //! Get total data size in bytes 00101 /*! This is nsamples * nchans * sizeof(T) */ 00102 inline uint sizeBytes() const; 00103 00104 //! Get a read-only pointer to the raw data 00105 inline const T* getDataPtr() const; 00106 00107 //! Get a read/write pointer to the raw data 00108 inline T* getDataPtr(); 00109 00110 //@} 00111 00112 private: 00113 ArrayHandle<T> itsHdl; // the data proper 00114 float itsFreq; // sampling frequency 00115 }; 00116 00117 // ###################################################################### 00118 // #################### Inlined functions: 00119 // ###################################################################### 00120 00121 template <class T> 00122 inline AudioBuffer<T>::AudioBuffer(const T* data, const uint nsampl, 00123 const uint nchan, const float freq) : 00124 itsHdl(new ArrayData<T>(Dims(nsampl, nchan), data)), 00125 itsFreq(freq) 00126 { } 00127 00128 // ###################################################################### 00129 template <class T> 00130 inline AudioBuffer<T>::AudioBuffer(const uint nsampl, const uint nchan, 00131 const float freq, InitPolicy init) : 00132 itsHdl(new ArrayData<T>(Dims(nsampl, nchan), init)), 00133 itsFreq(freq) 00134 { } 00135 00136 // ###################################################################### 00137 template <class T> 00138 inline AudioBuffer<T>::AudioBuffer(const AudioBuffer<T>& A) : 00139 itsHdl(A.itsHdl), 00140 itsFreq(A.itsFreq) 00141 { } 00142 00143 // ###################################################################### 00144 template <class T> 00145 inline AudioBuffer<T>& AudioBuffer<T>::operator=(const AudioBuffer<T>& A) 00146 { 00147 AudioBuffer<T> A_copy(A); 00148 itsHdl.swap(A_copy.itsHdl); 00149 itsFreq = A.itsFreq; 00150 return *this; 00151 } 00152 00153 // ###################################################################### 00154 template <class T> 00155 inline AudioBuffer<T>::AudioBuffer() : 00156 itsHdl(new ArrayData<T>()), 00157 itsFreq(0.0F) 00158 { } 00159 00160 // ###################################################################### 00161 template <class T> 00162 inline AudioBuffer<T>::~AudioBuffer() 00163 { } 00164 00165 // ###################################################################### 00166 template <class T> 00167 inline uint AudioBuffer<T>::nsamples() const 00168 { return itsHdl.get().w(); } 00169 00170 // ###################################################################### 00171 template <class T> 00172 inline uint AudioBuffer<T>::nchans() const 00173 { return itsHdl.get().h(); } 00174 00175 // ###################################################################### 00176 template <class T> 00177 inline float AudioBuffer<T>::freq() const 00178 { return itsFreq; } 00179 00180 // ###################################################################### 00181 template <class T> 00182 inline void AudioBuffer<T>::setFreq(const float f) 00183 { itsFreq = f; } 00184 00185 // ###################################################################### 00186 template <class T> 00187 inline const T& AudioBuffer<T>::getVal(const uint sample, const uint channel) 00188 { 00189 ASSERT(sample < nsamples() && channel < nchans()); 00190 return itsHdl.get().data()[sample + nsamples() * channel]; 00191 } 00192 00193 // ###################################################################### 00194 template <class T> 00195 inline uint AudioBuffer<T>::size() const 00196 { return nsamples() * nchans(); } 00197 00198 // ###################################################################### 00199 template <class T> 00200 inline uint AudioBuffer<T>::sizeBytes() const 00201 { return nsamples() * nchans() * sizeof(T); } 00202 00203 // ###################################################################### 00204 template <class T> 00205 inline const T* AudioBuffer<T>::getDataPtr() const 00206 { return itsHdl.get().data(); } 00207 00208 // ###################################################################### 00209 template <class T> 00210 inline T* AudioBuffer<T>::getDataPtr() 00211 { return itsHdl.uniq().dataw(); } 00212 00213 00214 // ###################################################################### 00215 /* So things look consistent in everyone's emacs... */ 00216 /* Local Variables: */ 00217 /* mode: c++ */ 00218 /* indent-tabs-mode: nil */ 00219 /* End: */ 00220 00221 #endif // AUDIO_AUDIOBUFFER_H_DEFINED