Advertisement
TolentinoCotesta

Untitled

Apr 1st, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.11 KB | None | 0 0
  1. // Include the Control Surface library
  2. #include <Control_Surface.h>        
  3. USBMIDI_Interface midi;
  4.  
  5. #define TOLERANCE 25
  6. typedef struct {
  7.   MIDIAddress note;
  8.   uint8_t analogIn;
  9.   uint8_t tolerance = TOLERANCE;
  10.   bool playing = false;
  11. } JoyNote;
  12.  
  13.  
  14. typedef struct {
  15.   MIDIAddress controller;
  16.   uint8_t analogIn;
  17.   uint8_t tolerance = TOLERANCE;
  18.   uint16_t lastSent;
  19. } JoyController;
  20.  
  21.  
  22. #define UP_BUTTON   2
  23. #define DOWN_BUTTON 3
  24.  
  25. #define NUM_NOTES 7
  26. #define NUM_CONTROLLERS 3
  27. JoyNote joyNotes[NUM_NOTES];
  28. JoyController joyControl[NUM_CONTROLLERS];
  29.  
  30. using namespace MIDI_Notes;
  31.  
  32. // The velocity of the note events
  33. const uint8_t velocity = 0x7F;
  34.  
  35. // Variabile per cambiare ottava.
  36. int actualOctave = 3;
  37. #define MIN_OCTAVE 2
  38. #define MAX_OCTAVE 6
  39.  
  40. void setup() {
  41.   //Serial.begin(115200);
  42.   //while (!Serial) ;
  43.   pinMode(UP_BUTTON, INPUT_PULLUP);
  44.   pinMode(DOWN_BUTTON, INPUT_PULLUP);
  45.   initNotesAndControllers();
  46.   midi.begin();
  47. }
  48.  
  49. // ---------------------------------- Loop ---------------------------------- //
  50. // ========================================================================== //
  51.  
  52. void loop() {
  53.   midi.update(); // read and handle or discard MIDI input
  54.  
  55.   // Inviamo il messaggio per le note, se necessario
  56.   // for(uint8_t i=0; i<NUM_NOTES; i++){
  57.  
  58.   for(uint8_t i=0; i<3; i++){  // SOLO PER TEST
  59.     uint16_t analogVal = abs(512 - analogRead(joyNotes[i].analogIn));
  60.     uint8_t tolValue = joyNotes[i].tolerance;
  61.  
  62.     // Joypad azionato e la nota deve essere ancora riprodotta
  63.     if(analogVal >= tolValue && !joyNotes[i].playing ){
  64.       midi.sendNoteOn(joyNotes[i].note, velocity);  
  65.       joyNotes[i].playing = true;
  66.     }
  67.    
  68.     // Joypad in posizione di riposo e la nota non deve essere più riprodotta
  69.     if(analogVal < tolValue && joyNotes[i].playing) {
  70.       midi.sendNoteOff(joyNotes[i].note, velocity);
  71.       joyNotes[i].playing = false;
  72.     }
  73.   }
  74.  
  75.  
  76.   // Inviamo il messaggio per i controller, se necessario
  77.   //for(uint8_t i=0; i<NUM_CONTROLLERS; i++){
  78.   for(uint8_t i=0; i<2; i++){   // SOLO PER TEST
  79.     uint16_t analogVal = abs(512 - analogRead(joyControl[i].analogIn));
  80.     uint8_t tolValue = joyControl[i].tolerance;
  81.     uint8_t midiValue = map(analogVal, 0, 512, 0 ,127);
  82.        
  83.     // Joypad azionato, inviamo il messaggio MIDI (una sola volta)
  84.     if(analogVal >= tolValue && joyControl[i].lastSent != midiValue ){  
  85.       midi.sendCC(joyControl[i].controller, midiValue );      
  86.       joyControl[i].lastSent = midiValue;
  87.     }
  88.   }
  89.  
  90.   // Incremento-decremento dell'ottava (per un'escursione totale di NUM_OCTAVES - 1 ottave)
  91.   static uint32_t upDebounce, downDebounce;
  92.   if(digitalRead(UP_BUTTON) == LOW && millis() - upDebounce > 250){
  93.     upDebounce = millis();
  94.     if(actualOctave < MAX_OCTAVE){
  95.       actualOctave++ ;
  96.       updateOctave(actualOctave);
  97.     }
  98.   }
  99.  
  100.   if(digitalRead(DOWN_BUTTON) == LOW && millis() - downDebounce > 250){
  101.     downDebounce = millis();
  102.     if(actualOctave > MIN_OCTAVE){
  103.       actualOctave-- ;
  104.       updateOctave(actualOctave);
  105.     }
  106.   }
  107.  
  108.  
  109. }
  110.  
  111.  
  112. /*
  113. static const uint8_t A6 = 24; // D4
  114. static const uint8_t A7 = 25; // D6
  115. static const uint8_t A8 = 26; // D8
  116. static const uint8_t A9 = 27; // D9
  117. static const uint8_t A10 = 28; // D10
  118. static const uint8_t A11 = 29; // D12
  119. */
  120.  
  121.  
  122. //   {0,   1,   2,    3,   4,   5,   6  }
  123. enum {Do3, Mi3, Sol3, Fa3, Si4, Re4, La4};  
  124.  
  125. void initNotesAndControllers(){
  126.   joyNotes[Do3].note = {note(C, octave), CHANNEL_1};
  127.   joyNotes[Do3].analogIn = A0;
  128.   // E' necessario assegnare solo se diversa da quella di default
  129.   // Es. per correggere singolo joypad "sfasato"
  130.   joyNotes[Do3].tolerance = TOLERANCE + 1; // +- single adjustment
  131.  
  132.   joyNotes[Mi3].note = {note(E, actualOctave), CHANNEL_1};
  133.   joyNotes[Mi3].analogIn = A1;  
  134.   joyNotes[Sol3].note = {note(G, actualOctave), CHANNEL_1};
  135.   joyNotes[Sol3].analogIn = A2;
  136.   joyNotes[Fa3].note = {note(F, actualOctave), CHANNEL_1};
  137.   joyNotes[Fa3].analogIn = A3;
  138.   joyNotes[Si4].note = {note(B, actualOctave +1), CHANNEL_1};
  139.   joyNotes[Si4].analogIn = A4;
  140.   joyNotes[Re4].note = {note(D, actualOctave +1), CHANNEL_1};
  141.   joyNotes[Re4].analogIn = A5;
  142.   joyNotes[La4].note = {note(A, actualOctave +1), CHANNEL_1};
  143.   joyNotes[La4].analogIn = A6;
  144.  
  145.   joyControl[0].controller = {MIDI_CC::General_Purpose_Controller_1, CHANNEL_1};
  146.   joyControl[0].analogIn = A7;  
  147.   joyControl[0].tolerance = 10;  
  148.  
  149.   joyControl[1].controller = {MIDI_CC::General_Purpose_Controller_2, CHANNEL_1};
  150.   joyControl[1].analogIn = A8;  
  151.   joyControl[1].tolerance = 10;
  152.  
  153.   joyControl[2].controller = {MIDI_CC::General_Purpose_Controller_3, CHANNEL_1};
  154.   joyControl[2].analogIn = A9;    
  155.   joyControl[2].tolerance = 10;
  156. }
  157.  
  158. void updateOctave( uint8_t octave){
  159.   joyNotes[Do3].note = {note(C, octave), CHANNEL_1};
  160.   joyNotes[Mi3].note = {note(E, octave), CHANNEL_1};
  161.   joyNotes[Sol3].note = {note(G, octave), CHANNEL_1};
  162.   joyNotes[Fa3].note = {note(F, octave), CHANNEL_1};
  163.   joyNotes[Si4].note = {note(B, octave+1), CHANNEL_1};
  164.   joyNotes[Re4].note = {note(D, octave+1), CHANNEL_1};
  165.   joyNotes[La4].note = {note(A, octave+1), CHANNEL_1};
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement