Mozzi  version v2.0
sound synthesis library for Arduino
IntMap.h
1 /*
2  * IntMap.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2012-2024 Tim Barrass and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10  */
11 
12 
13 #ifndef INTMAP_H_
14 #define INTMAP_H_
15 
16 /** A faster version of Arduino's map() function.
17 This uses ints instead of longs internally and does some of the maths at compile time.
18 */
19 class IntMap {
20 
21 public:
22  /** Constructor.
23  @param in_min the minimum of the input range.
24  @param in_max the maximum of the input range.
25  @param out_min the minimum of the output range.
26  @param out_max the maximum of the output range.
27  */
28  IntMap(int in_min, int in_max, int out_min, int out_max)
29  : _IN_MIN(in_min),_IN_MAX(in_max),_OUT_MIN(out_min),_OUT_MAX(out_max),
30  _MULTIPLIER((256L * (out_max-out_min)) / (in_max-in_min))
31  {
32  ;
33  }
34 
35  /** Process the next input value.
36  @param n the next integer to process.
37  @return the input integer mapped to the output range.
38  */
39  int operator()(int n) const {
40  return (int) (((_MULTIPLIER*(n-_IN_MIN))>>8) + _OUT_MIN);
41  }
42 
43 
44 private:
45  const int _IN_MIN, _IN_MAX, _OUT_MIN, _OUT_MAX;
46  const long _MULTIPLIER;
47 };
48 
49 #endif /* INTMAP_H_ */