Mapping.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef _Mapping_
00023 #define _Mapping_
00024 
00025 #include "CLAM_Math.hxx"
00026 #include <string>
00027 #include <vector>
00028 #include "DataTypes.hxx"
00029 #include "Array.hxx"
00030 
00031 namespace CLAM {
00032 
00033         class MappingFactory;
00034 
00035         class Mapping
00036         {
00037         friend class MappingFactory;
00038         typedef std::vector<MappingFactory*> MappingFactories;
00039         
00040         private:
00041                 static MappingFactories& FactoryInstance();
00042         public:
00043                 virtual TData Map(const TData in) = 0;
00044                 static Mapping* Create(const std::string& name);
00045                 virtual void Set(DataArray& arguments) { }
00046                 virtual ~Mapping();
00047         };
00048 
00049         class MappingFactory
00050         {
00051         friend class Mapping;
00052         private:
00053                 std::string mName;
00054         public:
00055                 MappingFactory(const std::string& name)
00056                 :mName(name)
00057                 {
00058                 }
00059 
00060                 void AddMe(void);
00061 
00062                 virtual Mapping* Create(void) = 0;
00063 
00064                 virtual ~MappingFactory()
00065                 {
00066                 }
00067         };
00068 
00069 
00070         class ValueToRatioMapping:public Mapping
00071         {
00072         private:
00073                 TData mSemitones ;
00074         public:
00075                 ValueToRatioMapping()
00076                 {
00077                 }
00078 
00079                 void Set( const TData& inSemitones ) 
00080                 {
00081                         mSemitones = inSemitones ;
00082                 }
00083 
00084                 TData Map( const TData inValue )
00085                 {
00086                         return ( TData(pow( 2. , ( ( mSemitones / 12. ) * ( ( inValue - 8192. ) / 8192. ) ) )) ) ;
00087                 }
00088 
00089                 void Set(DataArray& arguments)
00090                 {
00091                         CLAM_ASSERT( arguments.Size() == 1,
00092                                 "Not enough arguments for ValueToRatioMapping" );
00093                         Set( arguments[0] ) ;
00094                 }
00095         };
00096 
00097         class ValueToRatioMappingFactory:public MappingFactory
00098         {
00099         public:
00100                 static ValueToRatioMappingFactory sSingleton ;
00101                 ValueToRatioMappingFactory():MappingFactory( "ValueToRatio" )
00102                 {
00103                         AddMe() ;
00104                 }
00105                 Mapping* Create(void) { return new ValueToRatioMapping; }
00106         };
00107 
00108 
00109 
00110         class NoteToFreqMapping:public Mapping
00111         {
00112         private:
00113                 TData mNoteRef ;
00114                 TData mFrequencyRef ;
00115         public:
00116                 NoteToFreqMapping()
00117                 {
00118                 }
00119 
00120                 void Set( 
00121                         const TData& inNoteRef,
00122                         const TData& inFrequencyRef    )
00123                 {
00124                         mNoteRef = inNoteRef ;
00125                         mFrequencyRef = inFrequencyRef ;
00126                 }
00127 
00128                 TData Map( const TData inNote )
00129                 {
00130                         return ( TData( pow( 2.0 , ( inNote - mNoteRef ) / 12.0 ) ) ) * mFrequencyRef ;
00131                 }
00132 
00133                 void Set(DataArray& arguments)
00134                 {
00135                         if ( arguments.Size() == 0 )
00136                         {
00137                                 // no arguments have been specified, so use the default values:
00138                                 // reference a 440 Hz at midi note 69
00139                                 Set(69,440);
00140                         }else
00141                         {
00142                                 CLAM_ASSERT(arguments.Size() == 2,
00143                                         "Not enough arguments for NoteToFreqMapping" );
00144                                 Set( arguments[0], arguments[1] ) ;
00145                         }
00146                 }
00147         };
00148 
00149         class NoteToFreqMappingFactory:public MappingFactory
00150         {
00151         public:
00152                 static NoteToFreqMappingFactory sSingleton ;
00153                 NoteToFreqMappingFactory():MappingFactory( "NoteToFreq" )
00154                 {
00155                         AddMe() ;
00156                 }
00157                 Mapping* Create(void) { return new NoteToFreqMapping; }
00158         };
00159 
00160         class LinearMapping:public Mapping
00161         {
00162         private:
00163                 TData mInOffset,mOutOffset,mScale;
00164         public:
00165                 LinearMapping()
00166                 {
00167                         mInOffset = 0;
00168                         mOutOffset = 0;
00169                         mScale = 1;
00170                 }
00171 
00172                 void Set(
00173                         const TData& inA,const TData& inB,
00174                         const TData& outA,const TData& outB)
00175                 {
00176                         mInOffset = inA;
00177                         mOutOffset = outA;
00178                         mScale = (outB-outA)/(inB-inA);
00179                 }
00180                 TData Map(const TData in)
00181                 {
00182                         return mOutOffset+(in-mInOffset)*mScale;
00183                 }
00184 
00185                 void Set(DataArray& arguments)
00186                 {
00187                         CLAM_ASSERT(arguments.Size()==4,
00188                                 "Not enough arguments for LinearMapping");
00189                         Set(arguments[0],arguments[1],arguments[2],arguments[3]);
00190                 }
00191 
00192         };
00193 
00194         class LinearMappingFactory:public MappingFactory
00195         {
00196         public:
00197                 static LinearMappingFactory sSingleton;
00198                 LinearMappingFactory():MappingFactory("linear")
00199                 {
00200                         AddMe();
00201                 }
00202                 Mapping* Create(void) { return new LinearMapping; }
00203         };
00204 
00205 
00206 }
00207 
00208 #endif
00209 
Generated by  doxygen 1.6.3