|
| 1 | +#include <Mozzi.h> |
| 2 | +#include <Oscil.h> |
| 3 | +#include <EventDelay.h> |
| 4 | +#include <FixMath.h> |
| 5 | +#include <mozzi_rand.h> |
| 6 | +#include <mozzi_midi.h> |
| 7 | +#include <tables/sin2048_int8.h> |
| 8 | + |
| 9 | + |
| 10 | +// use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above |
| 11 | +Oscil<SIN2048_NUM_CELLS, AUDIO_RATE> aSin1(SIN2048_DATA); // sine wave sound source |
| 12 | +Oscil<SIN2048_NUM_CELLS, AUDIO_RATE> aSin2(SIN2048_DATA); // sine wave sound source |
| 13 | +Oscil<SIN2048_NUM_CELLS, AUDIO_RATE> aGain(SIN2048_DATA); // to fade audio signal in and out before waveshaping |
| 14 | + |
| 15 | +// for scheduling note changes |
| 16 | +EventDelay kChangeNoteDelay; |
| 17 | + |
| 18 | +const UFix<8, 0> freq1 = 184; |
| 19 | +const auto harmonic_step = freq1 * UFix<8, 0>(12).invAccurate(); // harmonic_step = freq1/12; |
| 20 | + |
| 21 | +void setup() { |
| 22 | + Serial.begin(115200); |
| 23 | + aSin1.setFreq(freq1); |
| 24 | + aGain.setFreq(0.2f); // use a float for low frequencies, in setup it doesn't need to be fast |
| 25 | + kChangeNoteDelay.set(2000); // note duration ms, within resolution of CONTROL_RATE |
| 26 | + startMozzi(); // :) |
| 27 | +} |
| 28 | + |
| 29 | +void updateControl() { |
| 30 | + if (kChangeNoteDelay.ready()) { |
| 31 | + UFix<4, 0> harmonic = rand((byte)12); |
| 32 | + auto shimmer = toUFraction(rand((byte)255)); // Creates a UFix<0,8> |
| 33 | + auto freq2difference = (harmonic * harmonic_step) + (harmonic_step * shimmer).sR<3>(); |
| 34 | + auto freq2 = (freq1 - freq2difference).asUFix(); |
| 35 | + aSin2.setFreq((freq2)); |
| 36 | + kChangeNoteDelay.start(); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +AudioOutput_t updateAudio() { |
| 41 | + auto asig = (toSInt(aSin1.next()) + toSInt(aSin2.next())) * (toSFraction(aGain.next()) + UFix<1, 7>(1.2)); // this is a SFix<9,9> in the end |
| 42 | + return MonoOutput::fromAlmostNBit(11, asig.asRaw()).clip(); // TODO, implement smart MonoOutput |
| 43 | +} |
| 44 | + |
| 45 | +void loop() { |
| 46 | + audioHook(); // required here |
| 47 | +} |
0 commit comments