Mozzi  version v2.0
sound synthesis library for Arduino
MetaOscil.h
1 /*
2  * MetaOscil.h
3  *
4  * A wrap-up to swap between different oscillators seemlessly, allowing to produce non-aliased sounds by automatically switching between oscillators.
5  *
6  * This file is part of Mozzi.
7  *
8  * Copyright 2021-2024 T. Combriat and the Mozzi Team
9  *
10  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
11  *
12  */
13 
14 
15 #ifndef META_OSCIL_H
16 #define META_OSCIL_H
17 
18 
19 #include <Arduino.h>
20 
21 #include "Oscil.h"
22 #include "mozzi_fixmath.h"
23 #include <FixMath.h>
24 
25 
31 template<uint16_t NUM_TABLE_CELLS, uint16_t UPDATE_RATE, byte N_OSCIL>
32  class MetaOscil
33 
34 {
35  public:
39  template<class... T> MetaOscil(Oscil<NUM_TABLE_CELLS, UPDATE_RATE>* first, T*... elements):oscillators{first, elements...} {
40  current_osc=oscillators[0];};
41 
42  MetaOscil(){};
43 
44  /* Add one oscil to the MetaOscil.
45  @param osc is a pointer toward an Oscil
46  @param cutoff_freq is the cutoff frequency of this Oscil
47  void addOscil(Oscil<NUM_TABLE_CELLS, UPDATE_RATE>* osc, int cutoff_freq)
48  {
49  oscillators[current_rank] = osc;
50  cutoff_freqs[current_rank] = cutoff_freq;
51  if (current_rank == 0) current_osc=oscillators[0];
52  current_rank += 1;
53  }*/
54 
55 
58  template<typename ... T > void setOscils(Oscil<NUM_TABLE_CELLS, UPDATE_RATE>* first,T... elements)
59  {
60  oscillators[current_rank]=first;
61  if (current_rank == 0) current_osc=oscillators[0];
62  current_rank+=1;
63  setOscils(elements...);
64  current_rank = 0;
65  }
66 
67  void setOscils(){};
68 
69 
72  template<typename ... T > void setCutoffFreqs(int first,T... elements)
73  {
74  cutoff_freqs[current_rank]=first;
75  current_rank+=1;
76  setCutoffFreqs(elements...);
77  current_rank = 0;
78  }
79 
80  void setCutoffFreqs() {};
81 
85  void setCutoffFreq(int freq, byte rank)
86  {
87  cutoff_freqs[rank] = freq;
88  }
89 
93  inline
94  int8_t next() {return current_osc->next();}
95 
99  void setTable(const int8_t * TABLE_NAME, byte rank) {oscillators[rank]->setTable(TABLE_NAME);}
100 
101 
104  void setPhase(unsigned int phase) {current_osc->setPhase(phase);}
105 
106 
109  void setPhaseFractional(unsigned long phase) {current_osc->setPhaseFractional(phase);}
110 
111 
115  unsigned long getPhaseFractional() {return current_osc->getPhaseFractional();}
116 
117 
118 
125  inline
126  int8_t phMod(Q15n16 phmod_proportion) {return current_osc->phMod(phmod_proportion);}
127 
128 
135  template <int8_t NI, int8_t NF, uint8_t RANGE>
136  inline
137  int8_t phMod(SFix<NI,NF,RANGE> phmod_proportion)
138  {
139  return current_osc->phMod(phmod_proportion);
140  }
141 
142 
143 
150  inline
151  int8_t phMod(SFix<15,16> phmod_proportion)
152  {
153  return current_osc->phMod(phmod_proportion);
154  }
155 
156 
159  inline
160  void setFreq(int frequency, bool apply = true)
161  {
162  if (frequency < cutoff_freqs[0]) //getting out the extreme cases
163  {
164  oscillators[0]->setPhaseFractional(current_osc->getPhaseFractional());
165  current_osc = oscillators[0];
166  current_osc->setFreq(frequency);
167  }
168 
169  else if (frequency > cutoff_freqs[N_OSCIL-1])
170  {
171  oscillators[N_OSCIL-1]->setPhaseFractional(current_osc->getPhaseFractional());
172  current_osc = oscillators[N_OSCIL-1];
173  current_osc->setFreq(frequency);
174  }
175  else // dichotomic search
176  {
177  byte low_point = 0, high_point = N_OSCIL-1, mid_point = (N_OSCIL-1)>>1;
178  while(low_point != high_point)
179  {
180  if (frequency > cutoff_freqs[mid_point]) low_point = mid_point+1;
181  else if (frequency < cutoff_freqs[mid_point]) high_point = mid_point;
182  else
183  {
184  break;
185  }
186  mid_point = (low_point + high_point)>>1;
187  }
188  oscillators[mid_point]->setPhaseFractional(current_osc->getPhaseFractional());
189  current_osc = oscillators[mid_point];
190  if (apply) current_osc->setFreq(frequency);
191  }
192 
193  }
194 
195 
198  inline
199  void setFreq(float frequency)
200  {
201  setFreq((int) frequency, false);
202  current_osc->setFreq(frequency);
203  }
204 
205 
209  template <int8_t NI, int8_t NF, uint64_t RANGE>
210  inline
211  void setFreq(UFix<NI,NF,RANGE> frequency)
212  {
213  setFreq(frequency.asInt(), false);
214  current_osc->setFreq(frequency);
215  }
216 
217 
218 
221  inline
222  void setFreq_Q24n8(Q24n8 frequency)
223  {
224  setFreq((int) (frequency>>8), false);
225  current_osc->setFreq_Q24n8(frequency);
226  }
227 
228 
229 
232  inline
233  void setFreq_Q16n16(Q16n16 frequency)
234  {
235  setFreq((int) (frequency>>16), false);
236  current_osc->setFreq_Q16n16(frequency);
237  }
238 
239 
245  inline
246  int8_t atIndex(unsigned int index) {return current_osc->atIndex(index);}
247 
248 
252  inline
253  unsigned long phaseIncFromFreq(int frequency) {return current_osc->phaseIncFromFreq(frequency);}
254 
258  inline
259  void setPhaseInc(unsigned long phaseinc_fractional) {current_osc->setPhaseInc(phaseinc_fractional);}
260 
261 
262 
263  private:
264  Oscil<NUM_TABLE_CELLS, UPDATE_RATE> * oscillators[N_OSCIL];
265  Oscil<NUM_TABLE_CELLS, UPDATE_RATE> * current_osc = NULL;
266  int cutoff_freqs[N_OSCIL];
267  byte current_rank = 0;
268 
269 };
270 
276 #endif /* META_OSCIL_H */
MetaOscil is a wrapper for several Oscil.
Definition: MetaOscil.h:34
MetaOscil(Oscil< NUM_TABLE_CELLS, UPDATE_RATE > *first, T *... elements)
Constructor Declare a MetaOscil containing any number of Oscil pointers.
Definition: MetaOscil.h:39
void setFreq(int frequency, bool apply=true)
Set the MetaOsc frequency with an unsigned int.
Definition: MetaOscil.h:160
unsigned long getPhaseFractional()
Get the phase of the currently playin Oscil in fractional format.
Definition: MetaOscil.h:115
void setOscils(Oscil< NUM_TABLE_CELLS, UPDATE_RATE > *first, T... elements)
Set all Oscil of a MetaOscil.
Definition: MetaOscil.h:58
void setFreq(float frequency)
Set the MetaOsc frequency with a float.
Definition: MetaOscil.h:199
void setPhaseInc(unsigned long phaseinc_fractional)
Set a specific phase increment.
Definition: MetaOscil.h:259
void setCutoffFreqs(int first, T... elements)
Set all the cutoff frequencies for changing between Oscil.
Definition: MetaOscil.h:72
void setPhase(unsigned int phase)
Set the phase of the currently playing Oscil.
Definition: MetaOscil.h:104
int8_t phMod(SFix< NI, NF, RANGE > phmod_proportion)
Returns the next sample given a phase modulation value.
Definition: MetaOscil.h:137
void setFreq_Q16n16(Q16n16 frequency)
Set the MetaOsc frequency with a Q16n16 fixed-point number format.
Definition: MetaOscil.h:233
int8_t next()
Updates the phase according to the current frequency and returns the sample at the new phase position...
Definition: MetaOscil.h:94
void setFreq(UFix< NI, NF, RANGE > frequency)
Set the MetaOsc frequency with a UFix<NI,NF> fixed-point number format.
Definition: MetaOscil.h:211
void setPhaseFractional(unsigned long phase)
Set the phase of the currently playing Oscil in fractional format.
Definition: MetaOscil.h:109
int8_t atIndex(unsigned int index)
Returns the sample at the given table index of the current Oscil.
Definition: MetaOscil.h:246
int8_t phMod(Q15n16 phmod_proportion)
Returns the next sample given a phase modulation value.
Definition: MetaOscil.h:126
void setCutoffFreq(int freq, byte rank)
Set or change the cutoff frequency of one Oscil.
Definition: MetaOscil.h:85
int8_t phMod(SFix< 15, 16 > phmod_proportion)
Returns the next sample given a phase modulation value.
Definition: MetaOscil.h:151
void setFreq_Q24n8(Q24n8 frequency)
Set the MetaOsc frequency with a Q24n8 fixed-point number format.
Definition: MetaOscil.h:222
unsigned long phaseIncFromFreq(int frequency)
phaseIncFromFreq() and setPhaseInc() are for saving processor time when sliding between frequencies.
Definition: MetaOscil.h:253
void setTable(const int8_t *TABLE_NAME, byte rank)
Change the sound table which will be played by the Oscil of rank.
Definition: MetaOscil.h:99
Oscil plays a wavetable, cycling through the table to generate an audio or control signal.
Definition: Oscil.h:62
int32_t Q15n16
signed fractional number using 15 integer bits and 16 fractional bits, represents -32767....
Definition: mozzi_fixmath.h:46
uint32_t Q24n8
unsigned fractional number using 24 integer bits and 8 fractional bits, represents 0 to 16777215
Definition: mozzi_fixmath.h:51
uint32_t Q16n16
unsigned fractional number using 16 integer bits and 16 fractional bits, represents 0 to 65535....
Definition: mozzi_fixmath.h:52