Advertisement
retrokits

MOZZI USBMIDI Synth

Aug 5th, 2019
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  Example of a sound being triggered by MIDI input.
  2.  
  3.     Demonstrates playing notes with Mozzi in response to MIDI input,
  4.     using  Arduino MIDI library v4.2
  5.     (https://github.com/FortySevenEffects/arduino_midi_library/releases/tag/4.2)
  6.  
  7.     Circuit:
  8.       MIDI input circuit as per http://arduino.cc/en/Tutorial/Midi
  9.       Note: midi input on rx pin, not tx as in the illustration on the above page.
  10.       Midi has to be disconnected from rx for sketch to upload.
  11.       Audio output on digital pin 9 on a Uno or similar.
  12.  
  13.         Mozzi documentation/API
  14.         https://sensorium.github.io/Mozzi/doc/html/index.html
  15.  
  16.         Mozzi help/discussion/announcements:
  17.     https://groups.google.com/forum/#!forum/mozzi-users
  18.  
  19.     Tim Barrass 2013-14, CC by-nc-sa.
  20. */
  21.  
  22. #include <MIDIUSB.h>
  23. #include <MozziGuts.h>
  24. #include <Oscil.h> // oscillator template
  25. #include <tables/sin2048_int8.h> // sine table for oscillator
  26. #include <mozzi_midi.h>
  27. #include <ADSR.h>
  28. #include <Phasor.h>
  29.  
  30.  
  31.  
  32. // use #define for CONTROL_RATE, not a constant
  33. #define CONTROL_RATE 128 // Hz, powers of 2 are most reliable
  34.  
  35. // audio sinewave oscillator
  36. //Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);
  37. //Oscil <2048, CONTROL_RATE> kVib(SIN2048_DATA);
  38. Phasor <AUDIO_RATE> aPhasor1;
  39. Phasor <AUDIO_RATE> aPhasor2;
  40.  
  41. // envelope generator
  42. ADSR <CONTROL_RATE, AUDIO_RATE> envelope;
  43.  
  44. #define LED 13 // shows if MIDI is being recieved
  45.  
  46. void controlChange(byte channel, byte vv, byte velocity) {
  47.  
  48. }
  49.  
  50. void noteOn(byte channel, byte pitch, byte velocity) {
  51. //aSin.setFreq(mtof(float(pitch)));
  52. aPhasor1.setFreq(mtof(float(pitch)));
  53. //aPhasor2.setFreq(mtof(float(pitch+.2)));
  54.   envelope.noteOn();
  55.   digitalWrite(LED,HIGH);
  56. }
  57.  
  58. void noteOff(byte channel, byte pitch, byte velocity) {
  59.   envelope.noteOff();
  60.   digitalWrite(LED,LOW);
  61. }
  62.  
  63.  
  64. void setup() {
  65.   pinMode(10,OUTPUT);
  66.   digitalWrite(10,LOW);
  67.   pinMode(LED, OUTPUT);
  68.  
  69.   // Connect the HandleNoteOn function to the library, so it is called upon reception of a NoteOn.
  70.   //MIDI.setHandleNoteOn(HandleNoteOn);  // Put only the name of the function
  71.   //MIDI.setHandleNoteOff(HandleNoteOff);  // Put only the name of the function
  72.   // Initiate MIDI communications, listen to all channels (not needed with Teensy usbMIDI)
  73.   //MIDI.begin(MIDI_CHANNEL_OMNI);
  74.  
  75.   envelope.setADLevels(255,70);
  76.   envelope.setTimes(10,50,50,200); // 1000 is so the note will sustain 1 seconds unless a noteOff comes
  77.  
  78.   //aSin.setFreq(440); // default frequency
  79.   float freq = 55.f;
  80.   aPhasor1.setFreq(freq);
  81.   //aPhasor2.setFreq(freq+0.2f);
  82.   //kVib.setFreq(6.5f);
  83.   startMozzi(CONTROL_RATE);
  84. }
  85.  
  86.  
  87. void updateControl(){
  88.   midiEventPacket_t rx = MidiUSB.read();
  89.   switch (rx.header) {
  90.     case 0:
  91.       break; //No pending events
  92.      
  93.     case 0x9:
  94.       noteOn(rx.byte1 & 0xF,rx.byte2,rx.byte3);
  95.       break;
  96.      
  97.     case 0x8:
  98.       noteOff(rx.byte1 & 0xF,rx.byte2,rx.byte3);
  99.       break;
  100.      
  101.     case 0xB:
  102.       controlChange(rx.byte1 & 0xF,rx.byte2,rx.byte3);
  103.       break;
  104.     default:
  105.     break;
  106.   }
  107.  
  108.   envelope.update();
  109. }
  110.  
  111.  
  112. int updateAudio(){
  113.   char asig1 = (char)(aPhasor1.next()>>24);
  114.   //char asig2 = (char)(aPhasor2.next()>>24);
  115.   return ((int)(envelope.next() * asig1)/2);
  116.   //return (int) (envelope.next() * aSin.next())>>8;
  117. }
  118.  
  119.  
  120. void loop() {
  121.   audioHook(); // required here
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement