CLAM-Development  1.4.0
Mapping.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef _Mapping_
23 #define _Mapping_
24 
25 #include "CLAM_Math.hxx"
26 #include <string>
27 #include <vector>
28 #include "DataTypes.hxx"
29 #include "Array.hxx"
30 
31 namespace CLAM {
32 
33  class MappingFactory;
34 
35  class Mapping
36  {
37  friend class MappingFactory;
38  typedef std::vector<MappingFactory*> MappingFactories;
39 
40  private:
41  static MappingFactories& FactoryInstance();
42  public:
43  virtual TData Map(const TData in) = 0;
44  static Mapping* Create(const std::string& name);
45  virtual void Set(DataArray& arguments) { }
46  virtual ~Mapping();
47  };
48 
50  {
51  friend class Mapping;
52  private:
53  std::string mName;
54  public:
55  MappingFactory(const std::string& name)
56  :mName(name)
57  {
58  }
59 
60  void AddMe(void);
61 
62  virtual Mapping* Create(void) = 0;
63 
64  virtual ~MappingFactory()
65  {
66  }
67  };
68 
69 
71  {
72  private:
73  TData mSemitones ;
74  public:
76  {
77  }
78 
79  void Set( const TData& inSemitones )
80  {
81  mSemitones = inSemitones ;
82  }
83 
84  TData Map( const TData inValue )
85  {
86  return ( TData(pow( 2. , ( ( mSemitones / 12. ) * ( ( inValue - 8192. ) / 8192. ) ) )) ) ;
87  }
88 
89  void Set(DataArray& arguments)
90  {
91  CLAM_ASSERT( arguments.Size() == 1,
92  "Not enough arguments for ValueToRatioMapping" );
93  Set( arguments[0] ) ;
94  }
95  };
96 
98  {
99  public:
102  {
103  AddMe() ;
104  }
105  Mapping* Create(void) { return new ValueToRatioMapping; }
106  };
107 
108 
109 
111  {
112  private:
113  TData mNoteRef ;
114  TData mFrequencyRef ;
115  public:
117  {
118  }
119 
120  void Set(
121  const TData& inNoteRef,
122  const TData& inFrequencyRef )
123  {
124  mNoteRef = inNoteRef ;
125  mFrequencyRef = inFrequencyRef ;
126  }
127 
128  TData Map( const TData inNote )
129  {
130  return ( TData( pow( 2.0 , ( inNote - mNoteRef ) / 12.0 ) ) ) * mFrequencyRef ;
131  }
132 
133  void Set(DataArray& arguments)
134  {
135  if ( arguments.Size() == 0 )
136  {
137  // no arguments have been specified, so use the default values:
138  // reference a 440 Hz at midi note 69
139  Set(69,440);
140  }else
141  {
142  CLAM_ASSERT(arguments.Size() == 2,
143  "Not enough arguments for NoteToFreqMapping" );
144  Set( arguments[0], arguments[1] ) ;
145  }
146  }
147  };
148 
150  {
151  public:
154  {
155  AddMe() ;
156  }
157  Mapping* Create(void) { return new NoteToFreqMapping; }
158  };
159 
160  class LinearMapping:public Mapping
161  {
162  private:
163  TData mInOffset,mOutOffset,mScale;
164  public:
166  {
167  mInOffset = 0;
168  mOutOffset = 0;
169  mScale = 1;
170  }
171 
172  void Set(
173  const TData& inA,const TData& inB,
174  const TData& outA,const TData& outB)
175  {
176  mInOffset = inA;
177  mOutOffset = outA;
178  mScale = (outB-outA)/(inB-inA);
179  }
180  TData Map(const TData in)
181  {
182  return mOutOffset+(in-mInOffset)*mScale;
183  }
184 
185  void Set(DataArray& arguments)
186  {
187  CLAM_ASSERT(arguments.Size()==4,
188  "Not enough arguments for LinearMapping");
189  Set(arguments[0],arguments[1],arguments[2],arguments[3]);
190  }
191 
192  };
193 
195  {
196  public:
199  {
200  AddMe();
201  }
202  Mapping* Create(void) { return new LinearMapping; }
203  };
204 
205 
206 }
207 
208 #endif
209