From c2e6b73722f0d631bc33d7fcf2305d8b14ba6087 Mon Sep 17 00:00:00 2001 From: Tomas Mudrunka Date: Mon, 1 Feb 2021 23:54:06 +0100 Subject: [PATCH] Mozzi ADSR --- .../MIDIUSB_Mozzi_adsr/MIDIUSB_Mozzi_adsr.ino | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 arduino/MIDIUSB_Mozzi_adsr/MIDIUSB_Mozzi_adsr.ino diff --git a/arduino/MIDIUSB_Mozzi_adsr/MIDIUSB_Mozzi_adsr.ino b/arduino/MIDIUSB_Mozzi_adsr/MIDIUSB_Mozzi_adsr.ino new file mode 100644 index 0000000..43a5288 --- /dev/null +++ b/arduino/MIDIUSB_Mozzi_adsr/MIDIUSB_Mozzi_adsr.ino @@ -0,0 +1,122 @@ +/* + * MIDIUSB_buzzer.ino + * + * Author: Paulo Costa + */ + +#include +#include +#include +#include // oscillator template +#include // sine table for oscillator +#include +#include + +// use: Oscil oscilName (wavetable), look in .h file of table #included above +Oscil aSin(SIN2048_DATA); + +ADSR envelope; + +const char* pitch_name(byte pitch) { + static const char* names[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"}; + return names[pitch % 12]; +} + +int pitch_octave(byte pitch) { + return (pitch / 12) - 1; +} + +void noteOn(byte channel, byte pitch, byte velocity) { + aSin.setFreq(pitchFrequency[pitch]); + envelope.noteOn(); + + Serial.print("Note On: "); + Serial.print(pitch_name(pitch)); + Serial.print(pitch_octave(pitch)); + Serial.print(", channel="); + Serial.print(channel); + Serial.print(", velocity="); + Serial.println(velocity); +} + +void noteOff(byte channel, byte pitch, byte velocity) { + //aSin.setFreq(0); + envelope.noteOff(); + + Serial.print("Note Off: "); + Serial.print(pitch_name(pitch)); + Serial.print(pitch_octave(pitch)); + Serial.print(", channel="); + Serial.print(channel); + Serial.print(", velocity="); + Serial.println(velocity); +} + +void controlChange(byte channel, byte control, byte value) { + Serial.print("Control change: control="); + Serial.print(control); + Serial.print(", value="); + Serial.print(value); + Serial.print(", channel="); + Serial.println(channel); +} + +void setup() { + Serial.begin(115200); + envelope.setADLevels(255,48); + envelope.setTimes(150,200,700,200); // 700 is so the note will sustain 0.7 seconds unless a noteOff comes + + aSin.setFreq(440); // default frequency + startMozzi(CONTROL_RATE); +} +void updateControl(){ + envelope.update(); +} + + +AudioOutput_t updateAudio(){ + return MonoOutput::from16Bit(envelope.next() * aSin.next()); // return an int signal centred around 0 +} + +void loop() { + audioHook(); + midiEventPacket_t rx = MidiUSB.read(); + switch (rx.header) { + case 0: + break; //No pending events + + case 0x9: + noteOn( + rx.byte1 & 0xF, //channel + rx.byte2, //pitch + rx.byte3 //velocity + ); + break; + + case 0x8: + noteOff( + rx.byte1 & 0xF, //channel + rx.byte2, //pitch + rx.byte3 //velocity + ); + break; + + case 0xB: + controlChange( + rx.byte1 & 0xF, //channel + rx.byte2, //control + rx.byte3 //value + ); + break; + + default: + Serial.print("Unhandled MIDI message: "); + Serial.print(rx.header, HEX); + Serial.print("-"); + Serial.print(rx.byte1, HEX); + Serial.print("-"); + Serial.print(rx.byte2, HEX); + Serial.print("-"); + Serial.println(rx.byte3, HEX); + } +} -- 2.30.2