Tuesday, May 16, 2017

Teensy 3.6 Basics - Receiving MIDI Notes

Overview
The Teensyduino add-on for Arduino allows the Teensy 3.6 to easily read MIDI messages such as notes and control changes over USB. A computer or other USB-enabled device sends a MIDI message, and then the Teensy received the message, and can perform a given set of actions based on the contents of that message. 

For example, Ableton Live sends a MIDI note on message for middle C, velocity 127 and channel 1 to the Teensy. 

The code on the Teensy can then read the USB MIDI bus, recognise that a note on message has been received, filter just the channel 1 messages, filter just the notes that have a velocity of 127 and so on, and then set the frequency of an oscillator based on the incoming pitch. 

The examples presented here provide a very basic structure that can be added to, modified and extended. Nonetheless, this is illustrative of receiving MIDI note messages and then responding in some way. 

Consider how note on / note off messages can be mapped to different hardware and software components, as there are many possibilities. To read more information about this, please see the Teensyduino documentation here: https://www.pjrc.com/teensy/td_midi.html




Hardware Setup


There is no hardware setup, as the onboard LED is used as a response to incoming MIDI data. 



Software Setup
A DAW must be configured so that the Teensy is an output device for note and control data. 

With Ableton Live, go to the Link MIDI preferences panel, and set Track, Sync and Remote MIDI In for the Teensy to OFF, Sync MIDI Out for the Teensy to OFF and Track and Remote MIDI Out for the Teensy to ON, as shown below. 



Using a MIDI track (without a virtual instrument), set the output to the Teensy, as shown below. 




Any note or control data sent from this track will now be received by the Teensy. To read and then respond to the data, the Teensy code must contain three components. 

The first is that the main loop() function should contain usbMIDI.read() in order to read any incoming data from the USB port. 

The second is that the setup() function should contain a usbMIDI.setHandleNoteOn(OnNoteOn) function and usbMIDI.setHandleNoteOff(OnNoteOff) function. 

The two arguments used for usbMIDI.setHandleNoteOn() and usbMIDI.setHandleNoteOff() point to two user-defined functions, and could have any name at all. usbMIDI.setHandleNoteOn() and usbMIDI.setHandleNoteOff() connect the receiving of MIDI note messages with a particular function that is executed whenever a note message is received. 

The third component is that for every argument used in a setHandle function, there must exist a corresponding user-defined function that takes a particular form depending on the type of MIDI data received. 

To read more information about this, please see the Teensyduino documentation here: https://www.pjrc.com/teensy/td_midi.html

Every time a new MIDI note on message is received by usbMIDI.read(), the function that is defined as the argument set by usbMIDI.setHandleNoteOn() then processes the note on message. This is illustrated in the following simple example, which lights up the LED if a note on message is received, and turns the LED off if a note off message is received. 

Note that the user-defined function named OnNoteOn must have three bytes as arguments, and the byte order must be channel, pitch, velocity. 



int LED = 13; 
void setup() {
  usbMIDI.setHandleNoteOn(OnNoteOn); 
  usbMIDI.setHandleNoteOff(OnNoteOff); 
  pinMode(LED, OUTPUT); 

}

void loop() {
  usbMIDI.read();

}

void OnNoteOn(byte channel, byte pitch, byte velocity) { 
  digitalWrite(LED, HIGH); 
}

void OnNoteOff(byte channel, byte pitch, byte velocity) { 
  digitalWrite(LED, LOW); 

}



Of course, OnNoteOn can be much more complex - for example, filtering based on pitch, velocity or channel variables. Nonetheless, it is the OnNoteOn and OnNoteOff functions that process the incoming MIDI notes. 





Example 1 - Turn LED On and Off from Note On and Note Off Messages
For every Note On message, the LED is turned on. For every Note On message, the LED is turned off. 


Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Receiving_MIDI_Notes_Example_1/



Example 2 - Turn LED On and Off from Note On and Note Off Messages - But Only on Channel 1
For every Note On message, the LED is turned on. For every Note On message, the LED is turned off - however, the Teensy will only respond on channel 1. 


Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Receiving_MIDI_Notes_Example_2/



Example 3 - Turn LED On and Off from Note On and Note Off Messages - But Only on Channel 1 and Pitch 60
For every Note On message, the LED is turned on. For every Note On message, the LED is turned off - however, the Teensy will only respond on channel 1 and for pitches of 60. 


Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Receiving_MIDI_Notes_Example_3/


Example 4 - Turn LED On and Off from Note On and Note Off Messages - But Only on Channel 1, Pitch 60 and Velocities Above 80
For every Note On message, the LED is turned on. For every Note On message, the LED is turned off - however, the Teensy will only respond on channel 1, for pitches of 60 and for velocities that are above 80. 


Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Receiving_MIDI_Notes_Example_4/





Summary
Responding to notes are an integral part of dealing with MIDI data when interfacing with external circuits, motors, lights, LEDs etc. 

0 comments: