EMIPLIB

mipaudio3dbase.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 MIPAUDIO3DBASE_H
00030 
00031 #define MIPAUDIO3DBASE_H
00032 
00033 #include "mipconfig.h"
00034 #include "mipcomponent.h"
00035 #include "miptime.h"
00036 #include <string.h>
00037 #if defined(WIN32) || defined(_WIN32_WCE)
00038         #include <hash_map>
00039 #else
00040         #include <ext/hash_map>
00041 #endif // Win32
00042 
00043 #define MIPAUDIO3DBASE_CONST_PI 3.14159265
00044 
00051 class EMIPLIB_IMPORTEXPORT MIPAudio3DBase : public MIPComponent
00052 {
00053 protected:
00055         MIPAudio3DBase(const std::string &compName);
00056 public:
00057         ~MIPAudio3DBase();
00058 
00063         void setRightHanded(bool f = true)                                                      { m_rightHanded = f; }
00064         
00070         bool setSourcePosition(uint64_t sourceID, real_t pos[3]);
00071 
00077         bool setOwnPosition(real_t pos[3], real_t frontDirection[3], real_t upDirection[3]);
00078 
00083         void setDistanceFactor(real_t distFact)                                                 { m_distanceFactor = distFact; }
00084 protected:
00086         void cleanUp();
00087 
00092         void expirePositionalInfo();
00093 
00100         bool getPositionalInfo(uint64_t sourceID, real_t *azimuth, real_t *elevation, real_t *distance);
00101 
00114         void convolve(float *pDestStereo, int numDestFrames, float scale, const float *pSrcMono, 
00115                       int numSrcFrames, const float *pLeftFilter, int numLeftFrames, 
00116                       const float *pRightFilter, int numRightFrames);
00117 private:
00118         class PositionalInfo
00119         {
00120         public:
00121                 PositionalInfo(real_t x = 0, real_t y = 0, real_t z = 0, MIPTime updateTime = MIPTime(0))
00122                 {
00123                         m_x = x;
00124                         m_y = y;
00125                         m_z = z;
00126                         m_updateTime = updateTime;
00127                 }
00128                 real_t getX() const                                                             { return m_x; }
00129                 real_t getY() const                                                             { return m_y; }
00130                 real_t getZ() const                                                             { return m_z; }
00131                 MIPTime getLastUpdateTime() const                                               { return m_updateTime; }
00132         private:
00133                 real_t m_x, m_y, m_z;
00134                 MIPTime m_updateTime;
00135         };
00136 
00137 #if defined(WIN32) || defined(_WIN32_WCE)
00138         stdext::hash_map<uint64_t, PositionalInfo> m_posInfo;
00139 #else
00140         __gnu_cxx::hash_map<uint64_t, PositionalInfo, __gnu_cxx::hash<uint32_t> > m_posInfo;
00141 #endif // Win32
00142 
00143         real_t m_ownPos[3];
00144         real_t m_xDir[3]; // front direction
00145         real_t m_yDir[3]; // left ear direction
00146         real_t m_zDir[3]; // above head direction
00147         real_t m_distanceFactor;
00148         bool m_rightHanded;
00149 
00150         MIPTime m_lastExpireTime;
00151 };
00152 
00153 #ifndef MIPCONFIG_SUPPORT_INTELIPP
00154 
00155 inline void MIPAudio3DBase::convolve(float *pDestStereo, int numDestFrames, float scale,
00156                                      const float *pSrcMono, int numSrcFrames, 
00157                                      const float *pLeftFilter, int numLeftFrames, 
00158                                      const float *pRightFilter, int numRightFrames)
00159 {
00160         int i,j,k,l;
00161 
00162         memset(pDestStereo,0,numDestFrames*2*sizeof(float));            
00163                 
00164         for (i = 0, j = 0 ; i < numSrcFrames ; i++, j+= 2)
00165         {
00166                 float val = scale*pSrcMono[i];
00167                 for (k = 0,l = 0 ; k < numLeftFrames ; k++, l += 2)
00168                         pDestStereo[j+l] += val*pLeftFilter[k];
00169                 for (k = 0,l = 0 ; k < numRightFrames ; k++, l += 2)
00170                         pDestStereo[j+l+1] += val*pRightFilter[k];
00171         }
00172 }
00173 
00174 #else
00175 
00176 #include "ipp.h"
00177 #include "ipps.h"
00178 
00179 inline void MIPAudio3DBase::convolve(float *pDestStereo, int numDestFrames, float scale,
00180                                      const float *pSrcMono, int numSrcFrames, 
00181                                      const float *pLeftFilter, int numLeftFrames, 
00182                                      const float *pRightFilter, int numRightFrames)
00183 {
00184         float tmp[16384];
00185         float tmpLeft[16384];
00186         float tmpRight[16384];
00187 
00188         if (numDestFrames > 16384 || numSrcFrames > 16384)
00189         {
00190                 ippsZero_32f(pDestStereo,numDestFrames*2);
00191                 return;
00192         }
00193         
00194         ippsMulC_32f(pSrcMono, scale, tmp, numSrcFrames);
00195         ippsConv_32f(tmp, numSrcFrames, pLeftFilter, numLeftFrames, tmpLeft);
00196         ippsZero_32f(tmpLeft+numSrcFrames+numLeftFrames-1, (numDestFrames-(numSrcFrames+numLeftFrames-1)));
00197         ippsConv_32f(tmp, numSrcFrames, pRightFilter, numRightFrames, tmpRight);
00198         ippsZero_32f(tmpRight+numSrcFrames+numRightFrames-1, (numDestFrames-(numSrcFrames+numRightFrames-1)));
00199         for (int i = 0, j = 0 ; i < numDestFrames ; i++, j += 2)
00200         {
00201                 pDestStereo[j] = tmpLeft[i];
00202                 pDestStereo[j+1] = tmpRight[i];
00203         }
00204 }
00205 
00206 #endif // MIPCONFIG_SUPPORT_INTELIPP
00207 
00208 #endif // MIPAUDIO3DBASE_H
00209