Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Include the Control Surface library
- #include <Control_Surface.h>
- USBMIDI_Interface midi;
- // logarithmic correction table
- unsigned int linearized[513] = {
- 0, 1, 1, 2, 3, 3, 4, 4, 5, 6,
- 6, 7, 8, 8, 9, 9, 10, 11, 11, 12,
- 12, 13, 14, 14, 15, 15, 16, 17, 17, 18,
- 18, 19, 19, 20, 21, 21, 22, 22, 23, 23,
- 24, 25, 25, 26, 26, 27, 27, 28, 28, 29,
- 29, 30, 31, 31, 32, 32, 33, 33, 34, 34,
- 35, 35, 36, 36, 37, 37, 38, 38, 39, 39,
- 40, 40, 41, 41, 42, 42, 43, 43, 44, 44,
- 45, 45, 46, 46, 47, 47, 48, 48, 49, 49,
- 49, 50, 50, 51, 51, 52, 52, 53, 53, 54,
- 54, 54, 55, 55, 56, 56, 57, 57, 57, 58,
- 58, 59, 59, 60, 60, 60, 61, 61, 62, 62,
- 62, 63, 63, 64, 64, 64, 65, 65, 66, 66,
- 66, 67, 67, 68, 68, 68, 69, 69, 70, 70,
- 70, 71, 71, 71, 72, 72, 73, 73, 73, 74,
- 74, 74, 75, 75, 75, 76, 76, 76, 77, 77,
- 77, 78, 78, 78, 79, 79, 79, 80, 80, 80,
- 81, 81, 81, 82, 82, 82, 83, 83, 83, 84,
- 84, 84, 85, 85, 85, 86, 86, 86, 86, 87,
- 87, 87, 88, 88, 88, 89, 89, 89, 89, 90,
- 90, 90, 91, 91, 91, 91, 92, 92, 92, 92,
- 93, 93, 93, 93, 94, 94, 94, 95, 95, 95,
- 95, 96, 96, 96, 96, 97, 97, 97, 97, 98,
- 98, 98, 98, 98, 99, 99, 99, 99, 100, 100,
- 100, 100, 101, 101, 101, 101, 101, 102, 102, 102,
- 102, 103, 103, 103, 103, 103, 104, 104, 104, 104,
- 104, 105, 105, 105, 105, 105, 106, 106, 106, 106,
- 106, 107, 107, 107, 107, 107, 108, 108, 108, 108,
- 108, 108, 109, 109, 109, 109, 109, 109, 110, 110,
- 110, 110, 110, 110, 111, 111, 111, 111, 111, 111,
- 112, 112, 112, 112, 112, 112, 113, 113, 113, 113,
- 113, 113, 113, 114, 114, 114, 114, 114, 114, 114,
- 115, 115, 115, 115, 115, 115, 115, 115, 116, 116,
- 116, 116, 116, 116, 116, 116, 117, 117, 117, 117,
- 117, 117, 117, 117, 118, 118, 118, 118, 118, 118,
- 118, 118, 118, 119, 119, 119, 119, 119, 119, 119,
- 119, 119, 119, 120, 120, 120, 120, 120, 120, 120,
- 120, 120, 120, 120, 121, 121, 121, 121, 121, 121,
- 121, 121, 121, 121, 121, 121, 122, 122, 122, 122,
- 122, 122, 122, 122, 122, 122, 122, 122, 122, 123,
- 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,
- 123, 123, 123, 123, 123, 124, 124, 124, 124, 124,
- 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
- 124, 124, 124, 124, 125, 125, 125, 125, 125, 125,
- 125, 125, 125, 125, 125, 125, 125, 125, 125, 125,
- 125, 125, 125, 125, 125, 125, 125, 125, 125, 126,
- 126, 126, 126, 126, 126, 126, 126, 126, 126, 126,
- 126, 126, 126, 126, 126, 126, 126, 126, 126, 126,
- 126, 126, 126, 126, 126, 126, 126, 126, 126, 126,
- 126, 126, 126, 126, 127, 127, 127, 127, 127, 127,
- 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
- 127, 127, 127
- };
- #define TOLERANCE 25
- typedef struct {
- MIDIAddress note;
- uint8_t analogIn;
- uint8_t tolerance = TOLERANCE;
- bool playing = false;
- } JoyNote;
- typedef struct {
- MIDIAddress controller;
- uint8_t analogIn;
- uint8_t tolerance = TOLERANCE;
- uint16_t lastSent;
- } JoyController;
- #define NUM_NOTES 7
- #define NUM_CONTROLLERS 3
- JoyNote joyNotes[NUM_NOTES];
- JoyController joyControl[NUM_CONTROLLERS];
- using namespace MIDI_Notes;
- // The velocity of the note events
- const uint8_t velocity = 0x7F;
- void setup() {
- /*
- Serial.begin(115200);
- while (!Serial) ;
- Serial.println();
- */
- initNotesAndControllers();
- }
- // ---------------------------------- Loop ---------------------------------- //
- // ========================================================================== //
- void loop() {
- midi.update(); // read and handle or discard MIDI input
- // Inviamo il messaggio per le note, se necessario
- // for(uint8_t i=0; i<NUM_NOTES; i++){
- for(uint8_t i=0; i<3; i++){ // SOLO PER TEST
- uint16_t analogVal = abs(512 - analogRead(joyNotes[i].analogIn));
- uint8_t tolValue = joyNotes[i].tolerance;
- // Joypad azionato e la nota deve essere ancora riprodotta
- if(analogVal >= tolValue && !joyNotes[i].playing ){
- midi.sendNoteOn(joyNotes[i].note, velocity);
- joyNotes[i].playing = true;
- }
- // Joypad in posizione di riposo e la nota non deve essere più riprodotta
- if(analogVal < tolValue && joyNotes[i].playing) {
- midi.sendNoteOff(joyNotes[i].note, velocity);
- joyNotes[i].playing = false;
- }
- }
- // Inviamo il messaggio per i controller, se necessario
- //for(uint8_t i=0; i<NUM_CONTROLLERS; i++){
- for(uint8_t i=0; i<1; i++){ // SOLO PER TEST
- uint16_t analogVal = abs(512 - analogRead(joyControl[i].analogIn));
- uint8_t tolValue = joyControl[i].tolerance;
- // Joypad azionato, inviamo il messaggio MIDI (una sola volta)
- if(analogVal >= tolValue && joyControl[i].lastSent != analogVal ){
- uint8_t midiValue = 127 - linearized[512 - analogVal];
- midi.sendCC(joyControl[i].controller, midiValue );
- joyControl[i].lastSent = analogVal;
- }
- }
- }
- /*
- static const uint8_t A6 = 24; // D4
- static const uint8_t A7 = 25; // D6
- static const uint8_t A8 = 26; // D8
- static const uint8_t A9 = 27; // D9
- static const uint8_t A10 = 28; // D10
- static const uint8_t A11 = 29; // D12
- */
- void initNotesAndControllers(){
- // {0, 1, 2, 3, 4, 5, 6 }
- enum {Do3, Mi3, Sol3, Fa3, Si4, Re4, La4};
- joyNotes[Do3].note = {note(C, 3), CHANNEL_1};
- joyNotes[Do3].analogIn = A0;
- // E' necessario assegnare solo se diversa da quella di default
- // Es. per correggere singolo joypad "sfasato"
- joyNotes[Do3].tolerance = TOLERANCE + 1; // +- single adjustment
- joyNotes[Mi3].note = {note(E, 3), CHANNEL_1};
- joyNotes[Mi3].analogIn = A1;
- joyNotes[Sol3].note = {note(G, 3), CHANNEL_1};
- joyNotes[Sol3].analogIn = A2;
- joyNotes[Fa3].note = {note(F, 3), CHANNEL_1};
- joyNotes[Fa3].analogIn = A3;
- joyNotes[Si4].note = {note(B, 4), CHANNEL_1};
- joyNotes[Si4].analogIn = A4;
- joyNotes[Re4].note = {note(D, 4), CHANNEL_1};
- joyNotes[Re4].analogIn = A5;
- joyNotes[La4].note = {note(A, 4), CHANNEL_1};
- joyNotes[La4].analogIn = A6;
- joyControl[0].controller = {MIDI_CC::Bank_Select, CHANNEL_1};
- joyControl[0].analogIn = A7;
- joyControl[1].controller = {MIDI_CC::General_Purpose_Controller_1, CHANNEL_1};
- joyControl[1].analogIn = A8;
- joyControl[2].controller = {MIDI_CC::General_Purpose_Controller_2, CHANNEL_1};
- joyControl[2].analogIn = A9;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement