Mozzi  version v2.0
sound synthesis library for Arduino
WaveShaper.h
1 /*
2  * WaveShaper.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 #ifndef WAVESHAPER_H_
13 #define WAVESHAPER_H_
14 
15 #include "Arduino.h"
16 
17 /** WaveShaper maps values from its input to values in a table, which are returned as output.
18 @tparam T the type of numbers being input to be shaped, chosen to match the table.
19 */
20 
21 template <class T>
23  {}
24 ;
25 
26 
27 /** int8_t specialisation of WaveShaper template*/
28 template <>
29 class WaveShaper <char>
30 {
31 
32 public:
33  /** Constructor. Use the template parameter to set type of numbers being mapped. For
34  example, WaveShaper <int> myWaveShaper; makes a WaveShaper which uses ints.
35  @tparam T the type of numbers being input to be shaped, chosen to match the table.
36  @param TABLE_NAME the name of the table being used, which can be found in the
37  ".h" file containing the table. */
38  WaveShaper(const int8_t * TABLE_NAME):table(TABLE_NAME)
39  {
40  ;
41  }
42 
43 
44  /** Maps input to output, transforming it according to the table being used.
45  @param in the input signal. For flexibility, it's up to you to give the correct offset
46  to your input signal. So if you're mapping a signed 8-bit signal (such as the output of
47  an Oscil) into a 256 cell table centred around cell 128, add 128 to offset the
48  input value.
49  @return the shaped signal.
50  */
51  inline
53  {
54  return FLASH_OR_RAM_READ<const int8_t>(table + in);
55  }
56 
57 private:
58  const int8_t * table;
59 };
60 
61 
62 
63 /** int specialisation of WaveShaper template*/
64 template <>
65 class WaveShaper <int>
66 {
67 
68 public:
69  /** Constructor. Use the template parameter to set type of numbers being mapped. For
70  example, WaveShaper <int> myWaveShaper; makes a WaveShaper which uses ints.
71  @tparam T the type of numbers being input to be shaped, chosen to match the table.
72  @param TABLE_NAME the name of the table being used, which can be found in the
73  ".h" file containing the table. */
74  WaveShaper(const int16_t * TABLE_NAME):table(TABLE_NAME)
75  {
76  ;
77  }
78 
79 
80  /** Maps input to output, transforming it according to the table being used.
81  @param in the input signal. For flexibility, it's up to you to give the
82  correct offset to your input signal. So if you're mapping a signed 9-bit signal
83  (such as the sum of 2 8-bit Oscils) into a 512 cell table centred around
84  cell 256, add 256 to offset the input value. With a sigmoid table, this
85  may be useful for compressing a bigger signal into the -244 to 243
86  output range of Mozzi, rather than dividing the signal and returning a
87  int8_t from updateAudio().
88  @return the shaped signal.
89  */
90  inline
91  int next(int in)
92  {
93  return FLASH_OR_RAM_READ<const int16_t>(table + in);
94  }
95 
96 private:
97  const int16_t * table;
98 };
99 
100 /** @example 06.Synthesis/WaveShaper/WaveShaper.ino
101 This is an example of how to use the WaveShaper class.
102 */
103 #endif /* WAVESHAPER_H_ */