1
+ /* Example applying an ADSR envelope to an audio filter
2
+ with Mozzi sonification library. This shows
3
+ how to use an ADSR which updates at MOZZI_AUDIO_RATE, in updateAudio(),
4
+ and output using next() at MOZZI_AUDIO_RATE in updateAudio().
5
+
6
+ Demonstrates a simple ADSR object being controlled with
7
+ noteOn() and controlling the cutoff frequency of a LowPassFilter.
8
+
9
+ Mozzi documentation/API
10
+ https://sensorium.github.io/Mozzi/doc/html/index.html
11
+
12
+ Mozzi help/discussion/announcements:
13
+ https://groups.google.com/forum/#!forum/mozzi-users
14
+
15
+ Copyright 2024 Tim Barrass and the Mozzi Team
16
+
17
+ Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
18
+ */
19
+
20
+ #include < Mozzi.h>
21
+ #include < Oscil.h>
22
+ #include < EventDelay.h>
23
+ #include < ADSR.h>
24
+ #include < ResonantFilter.h>
25
+ #include < tables/triangle2048_int8.h>
26
+ #include < mozzi_rand.h>
27
+ #include < mozzi_midi.h>
28
+
29
+ Oscil<TRIANGLE2048_NUM_CELLS, MOZZI_AUDIO_RATE> aOscil (TRIANGLE2048_DATA);
30
+
31
+
32
+ // for triggering the envelope
33
+ EventDelay noteDelay;
34
+
35
+ ADSR<MOZZI_AUDIO_RATE, MOZZI_AUDIO_RATE> envelope;
36
+
37
+ // LowPass filter controlled by the envelope
38
+ LowPassFilter lpf;
39
+
40
+
41
+ void setup () {
42
+ // Serial.begin(9600); // for Teensy 3.1, beware printout can cause glitches
43
+ Serial.begin (115200 );
44
+ randSeed (); // fresh random
45
+ envelope.setADLevels (128 , 64 );
46
+ envelope.setTimes (2000 , 1500 , 250 , 1250 ); // always the same envelope times
47
+ noteDelay.set (5000 ); // 5 second countdown
48
+ lpf.setResonance (230 );
49
+ startMozzi ();
50
+ }
51
+
52
+
53
+ unsigned int duration, attack, decay, sustain, release_ms;
54
+
55
+ void updateControl () {
56
+ if (noteDelay.ready ()) {
57
+ envelope.noteOn (true );
58
+ byte midi_note = rand (50 ) + 20 ;
59
+ aOscil.setFreq ((int )mtof (midi_note));
60
+ noteDelay.start (5000 );
61
+ }
62
+ }
63
+
64
+
65
+ AudioOutput updateAudio () {
66
+ envelope.update ();
67
+ // change the cutoff frequency with the envelope.
68
+ lpf.setCutoffFreq (envelope.next ());
69
+ // the signal returned by aOscil is 8 bits, but with this much resonance the filtered signal goes higher,
70
+ // so we let 1 bit of headroom, and clip in case it goes beyond...
71
+ return MonoOutput::fromNBit (9 , (int )(lpf.next (aOscil.next ()))).clip ();
72
+ }
73
+
74
+
75
+ void loop () {
76
+ audioHook (); // required here
77
+ }
0 commit comments