EMIPLIB

miphrirbase.h

Go to the documentation of this file.
00001 /*
00002     
00003   This file is a part of EMIPLIB, the EDM Media over IP Library.
00004   
00005   Copyright (C) 2006-2011  Hasselt University - Expertise Centre for
00006                       Digital Media (EDM) (http://www.edm.uhasselt.be)
00007 
00008   This library is free software; you can redistribute it and/or
00009   modify it under the terms of the GNU Lesser General Public
00010   License as published by the Free Software Foundation; either
00011   version 2.1 of the License, or (at your option) any later version.
00012 
00013   This library is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016   Lesser General Public License for more details.
00017 
00018   You should have received a copy of the GNU Lesser General Public
00019   License along with this library; if not, write to the Free Software
00020   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
00021   USA
00022 
00023 */
00024 
00029 #ifndef MIPHRIRBASE_H
00030 
00031 #define MIPHRIRBASE_H
00032 
00033 #include "mipconfig.h"
00034 #include "mipaudio3dbase.h"
00035 #include <cmath>
00036 #include <list>
00037 
00039 class EMIPLIB_IMPORTEXPORT MIPHRIRBase : public MIPAudio3DBase
00040 {
00041 protected:
00043         MIPHRIRBase(const std::string &compName) : MIPAudio3DBase(compName)                     { }
00044 public:
00045         ~MIPHRIRBase()                                                                          { }
00046 protected:
00047         class HRIRData
00048         {
00049         public:
00050                 HRIRData(real_t radius, int azimuth, int elevation, float *pLeftChannel, int numLeft,
00051                          float *pRightChannel, int numRight)
00052                 {
00053                         m_radius = radius;
00054                         m_azimuth = azimuth;
00055                         m_elevation = elevation;
00056                         m_pLeftChannel = pLeftChannel;
00057                         m_pRightChannel = pRightChannel;
00058                         m_numLeft = numLeft;
00059                         m_numRight = numRight;
00060                         m_azimuthRad = (((real_t)azimuth)/180.0)*MIPAUDIO3DBASE_CONST_PI;
00061                         m_elevationRad = (((real_t)elevation)/180.0)*MIPAUDIO3DBASE_CONST_PI;
00062                 }
00063                 ~HRIRData()
00064                 {
00065                         delete [] m_pLeftChannel;
00066                         delete [] m_pRightChannel;
00067                 }
00068 
00069                 real_t getRadius() const                                                        { return m_radius; }
00070                 int getAzimuth() const                                                          { return m_azimuth; }
00071                 int getElevation() const                                                        { return m_elevation; }
00072                 real_t getAzimuthRadians() const                                                { return m_azimuthRad; }
00073                 real_t getElevationRadians() const                                              { return m_elevationRad; }
00074                 float *getLeftChannel() const                                                   { return m_pLeftChannel; }
00075                 float *getRightChannel() const                                                  { return m_pRightChannel; }
00076                 int getNumberOfLeftSamples() const                                              { return m_numLeft; }
00077                 int getNumberOfRightSamples() const                                             { return m_numRight; }
00078         private:
00079                 real_t m_radius, m_azimuthRad, m_elevationRad;
00080                 int m_azimuth, m_elevation;
00081                 int m_numLeft, m_numRight;
00082                 float *m_pLeftChannel, *m_pRightChannel;
00083         };
00084         
00085         class HRIRInfo
00086         {
00087         public:
00088                 HRIRInfo(int subjectNumber)                                                     { m_subjectNumber = subjectNumber; }
00089                 ~HRIRInfo()
00090                 {
00091                         std::list<HRIRData *>::iterator it;
00092 
00093                         for (it = m_HRIRSet.begin() ; it != m_HRIRSet.end() ; it++)
00094                                 delete (*it);
00095                 }
00096                 
00097                 int getSubjectNumber() const                                                    { return m_subjectNumber; }
00098 
00099                 // radius in meters, azimuth in degrees, elevation in degrees
00100                 bool addHRIRData(real_t radius, int azimuth, int elevation, float *pLeftChannel, 
00101                                  int numLeft, float *pRightChannel, int numRight)
00102                 {
00103                         std::list<HRIRData *>::const_iterator it;
00104                         bool found = false;
00105                         
00106                         for (it = m_HRIRSet.begin() ; !found && it != m_HRIRSet.end() ; it++)
00107                         {
00108                                 if (azimuth == (*it)->getAzimuth() && elevation == (*it)->getElevation())
00109                                         found = true;
00110                         }
00111 
00112                         if (found)
00113                                 return false;
00114                         
00115                         m_HRIRSet.push_back(new HRIRData(radius, azimuth, elevation, pLeftChannel, numLeft, pRightChannel, numRight));
00116                         return true;
00117                 }
00118 
00119                 // both azimuth and elevation in radians
00120                 HRIRData *findBestMatch(real_t azimuth, real_t elevation)
00121                 {
00122                         std::list<HRIRData *>::const_iterator it;
00123                         real_t minDist = 2.0*MIPAUDIO3DBASE_CONST_PI;
00124                         HRIRData *bestData = 0;
00125                         
00126                         for (it = m_HRIRSet.begin() ; it != m_HRIRSet.end() ; it++)
00127                         {
00128                                 real_t A2 = (*it)->getAzimuthRadians();
00129                                 real_t E2 = (*it)->getElevationRadians();
00130 
00131                                 real_t b = MIPAUDIO3DBASE_CONST_PI/2.0-elevation;
00132                                 real_t c = MIPAUDIO3DBASE_CONST_PI/2.0-E2;
00133                                         
00134                                 real_t cosDist = cos(b)*cos(c) + sin(b)*sin(c)*cos(A2-azimuth);
00135                                 real_t dist = acos(cosDist);
00136 
00137                                 if (dist < minDist)
00138                                 {
00139                                         minDist = dist;
00140                                         bestData = (*it);
00141                                 }
00142                         }
00143                         return bestData;
00144                 }
00145         private:
00146                 int m_subjectNumber;
00147                 std::list<HRIRData *> m_HRIRSet;
00148         };
00149 };
00150 
00151 #endif // MIPHRIRBASE_H
00152