Mozzi  version v2.0
sound synthesis library for Arduino
PDResonant.h
1 /*
2  * PDResonant.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2013-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 #include <mozzi_midi.h>
13 #include <ADSR.h>
14 #include <Oscil.h>
15 #include <Phasor.h>
16 // wavetable for oscillator:
17 #include <tables/sin2048_int8.h>
18 
32 {
33 
34 public:
35 
39  PDM_SCALE(0.05)
40  {
41  aOsc.setTable(SIN2048_DATA);
42  aAmpEnv.setADLevels(255, 255);
43  aAmpEnv.setTimes(50, 300, 60000, 1000);
44  kResonantFreqEnv.setADLevels(255,100);
45  }
46 
52  void noteOn(byte channel, byte pitch, byte velocity)
53  {
54  kResonantFreqEnv.noteOn();
55  aAmpEnv.noteOn();
56  freq = mtof(pitch);
57  aBaseCounter.setFreq(freq); // gets modulated in updateControl()
58  aResonanceFreqCounter.setFreq(freq);
59  }
60 
61 
67  void noteOff(byte channel, byte pitch, byte velocity)
68  {
69  aAmpEnv.noteOff();
70  kResonantFreqEnv.noteOff();
71  }
72 
73 
78  void setPDEnv(int attack, int decay)
79  {
80  // sustain and release timesare hardcoded here but don't need to be
81  kResonantFreqEnv.setTimes(attack, decay, 60000, 1000);
82  kResonantFreqEnv.update();
83 
84  float resonance_freq = freq + ((float)freq * ((float)kResonantFreqEnv.next()*PDM_SCALE));
85  aResonanceFreqCounter.setFreq(resonance_freq);
86  }
87 
88 
91  void update()
92  {
93  aAmpEnv.update();
94  kResonantFreqEnv.update();
95  // change freq of resonant freq counter, following the envelope
96  float resonance_freq = freq + ((float)freq * ((float)kResonantFreqEnv.next()*PDM_SCALE));
97  aResonanceFreqCounter.setFreq(resonance_freq);
98  }
99 
102  int next()
103  {
104  static byte previous_base_counter;
105  byte base_counter = aBaseCounter.next()>>24;
106 
107  // reset resonance counter (wiki b.)
108  if (base_counter<previous_base_counter) aResonanceFreqCounter.set(0);
109  previous_base_counter= base_counter;
110 
111  // index (phase) needs to end up as 11bit to match 2048 wavetable size
112  unsigned int index = aResonanceFreqCounter.next()>>21; // 11 bits fits 2048 cell sin table
113 
114  // amp ramp smooths the jump when aResonanceFreqCounter is reset (wiki d.)
115  byte amp_ramp = 255-base_counter;
116 
117  // wiki e., with amp envelope added
118  return ((long)aAmpEnv.next() * amp_ramp * aOsc.atIndex(index))>>16;
119 
120  // return ((index>>3)*amp_ramp)>>8; // this also sounds good - squelchy sawtooth
121  }
122 
123 
124 private:
125  const float PDM_SCALE;
126  byte amp;
127  int freq;
128 
129  Phasor <MOZZI_AUDIO_RATE> aBaseCounter;
130  Phasor <MOZZI_AUDIO_RATE> aResonanceFreqCounter;
131 
135 
136 };
void update()
Updates the internal controls of the ADSR.
Definition: ADSR.h:128
void noteOff()
Start the release phase of the ADSR.
Definition: ADSR.h:188
void setADLevels(byte attack, byte decay)
Set the attack and decay levels of the ADSR.
Definition: ADSR.h:249
void setTimes(unsigned int attack_ms, unsigned int decay_ms, unsigned int sustain_ms, unsigned int release_ms)
Set the attack, decay and release times of the ADSR in milliseconds.
Definition: ADSR.h:347
unsigned char next()
Advances one audio step along the ADSR and returns the level.
Definition: ADSR.h:161
void noteOn(bool reset=false)
Start the attack phase of the ADSR.
Definition: ADSR.h:176
void setTable(const int8_t *TABLE_NAME)
Change the sound table which will be played by the Oscil.
Definition: Oscil.h:99
int8_t atIndex(unsigned int index)
Returns the sample at the given table index.
Definition: Oscil.h:333
PDResonant is a simple midi instrument using Phase distortion used to simulate resonant filter,...
Definition: PDResonant.h:32
void noteOff(byte channel, byte pitch, byte velocity)
Stop a note in response to midi input.
Definition: PDResonant.h:67
PDResonant()
Constructor.
Definition: PDResonant.h:38
void setPDEnv(int attack, int decay)
Set the resonant filter sweep parameters.
Definition: PDResonant.h:78
void noteOn(byte channel, byte pitch, byte velocity)
Play a note in response to midi input.
Definition: PDResonant.h:52
int next()
Produce the audio output.
Definition: PDResonant.h:102
void update()
Update the filter sweep.
Definition: PDResonant.h:91
uint32_t next()
Increments one step along the phase.
Definition: Phasor.h:46
void set(uint32_t value)
Set the current value of the phasor.
Definition: Phasor.h:56
void setFreq(int frequency)
Set the Phasor frequency with an unsigned int.
Definition: Phasor.h:68
float mtof(float midival)
Converts midi note number to frequency.
Definition: mozzi_midi.h:75