This is the Day 1 post of the TouchDesigner Advent Calendar 2024!

This article is about lighting up the LEDs on a MIDI controller called the Akai Pro APC40 MK2 (just APC40 from here on) from TouchDesigner, and learning about LED control on MIDI controllers along the way.

The library and samples used here are available in this GitHub repository.

What this article covers

  • How to control a MIDI controller's LEDs from TouchDesigner, using the APC40 as an example
  • The basics of using your own Python modules in TouchDesigner

Controlling MIDI controller LEDs from TouchDesigner

Why control the LEDs

A MIDI controller sends the state of its buttons, pads, knobs, sliders and other controls as MIDI events.
On top of that, controllers with LEDs can often update the state of those LEDs when they receive MIDI events.

In that case, the controller can carry information from the user to the PC, and the PC can also control the LEDs to give information back to the user.

Some controllers come with presets for specific software such as Ableton Live, where the LEDs change in response to the software. You can build that kind of thing yourself, too.

If you are building a system in TouchDesigner that will be operated on site (where it's usually dark!), for video playout, lighting control, VJing and so on, setting up the LEDs properly makes it easier to operate and helps reduce mistakes.

I VJ with a system I built in TouchDesigner.
Here is how I use the LEDs on the APC40.

IMG_6374.JPG
APC40 with its LEDs controlled from TouchDesigner

At first glance it looks no different from the controller's layout at startup, but the control runs both ways: the LEDs reflect the tempo and the state of each channel in the VJ system, and the LEDs below the knobs that control the hue of the video show the resulting colors.

Hue control demo video – YouTube

The annoying part of controlling LEDs

On many controllers, each LED is assigned a MIDI channel and a note or CC index.
You control an LED by sending its designated note or CC to the controller as a MIDI event.
When the color can be set as well, it is often specified by the note's velocity.

For the APC40, this control is defined in the following protocol.
Akai APC40 Mk2 Communication Protocol – Akai Pro
"Protocol" here means the definition of the format of the MIDI events the APC40 sends, and of how it behaves for each value it receives.

For example, to light the top-left of the APC40's 40 rectangular pads (Clip Launch) in red, in schematic form you prepare information like

{
  "channel": 1,
  "note_number": "0x20",
  "velocity": 5
}

and send it to the APC40 as a MIDI note. (Example 1)

This is not intuitive.
It's hard to directly grasp which LED on the controller corresponds to which MIDI channel, note and CC, so you end up checking the protocol over and over.
The same goes for color: there is no clear rule relating velocity to color, so just finding the color you want in the protocol's list is a chore.

With pads laid out in a grid like the APC40's, it would be much easier to work with if you could say which row and column of Clip Launch to light, and in what color.

In other words, it would be intuitive if you could specify it, in schematic form, like this:

{
  "type": "Clip Launch",
  "row": 1,
  "column": 1,
  "color": "red"
}

(Example 2)

Controlling the MIDI Out CHOP from Python

With the MIDI Out CHOP and the midioutCHOP Class, you can send MIDI events from a Python script in a DAT, for example.

Take Example 1 above:

# Specify the operator that sends the MIDI events
midiout = op('midiout1')

# Specify the type of lighting: solid or blinking
channel = 1 
# Specify the position of the LED to control
note = 0x20 
# Specify the LED color
velocity = 5 

# Send the MIDI event (values are adjusted slightly because of how sending works)
midiout.sendNoteOn(channel, note + 1, velocity / 127) 

# The top-left of Clip Launch lights up red

These are the corresponding commands.

Making MIDI events easier to handle with a custom module

In addition to this built-in class, I'll prepare a custom module that converts the otherwise complicated MIDI event assignments into something you can handle intuitively.
With these, I'll try to control the LEDs with simple commands.

The set of classes I wrote, packaged as a module, is /src/apc40mk2.py in the sample repository.

With it, you can light the top-left of Clip Launch in red along the lines of Example 2 above:

# Specify the operator that sends the MIDI events and the device mode
import apc40mk2
apc = apc40mk2.APC40MK2(op('midiout1'))
apc.mode.set_device_mode(mode=2)
# Nothing up to here directly concerns the color of individual LEDs, so don't worry about it too much

# Specify the row of the LED to control
row = 1 
# Specify the column of the LED to control
column = 1
# Specify the LED color. Values like `#ff0000` also work
color = "red" 
# Specify the type of lighting: solid or blinking
led_type = 0 

# Send the MIDI event
apc.led.set_clip_launch(row, column, color, led_type) 

# The top-left of Clip Launch lights up red

This is how it can be written.

The way you specify things is now close to the physical layout of the MIDI controller, and I think it's clearer than before.

Importing a custom module

In TouchDesigner, you can import a DAT located at the same level as the DAT running the Python, as a module, like this:

import datName

Reference: MOD Class – Derivative

Here, dragging and dropping apc40mk2.py into the Network Editor creates a Text DAT called apc40mk2, and a DAT at the same level loads it as a module.

Once it's loaded, you can use even classes you wrote yourself.

In this case, first do

apc = apc40mk2.APC40MK2(op('midiout1'))

passing the MIDI Out CHOP used to send MIDI events as an argument to the parent class, and the various commands become available.

An example of a convenience: specifying LED colors

For the velocity that sets the color, you can of course use an index such as 5 as defined in the protocol, but also a color code like '#FF0000' or a common color name like "red".

As for color codes, the APC40's LEDs can actually produce at most 128 colors, so the module looks for the index closest to the color code you gave.

You don't have to face off with the color code table in the protocol; you can just specify roughly the color you're after.

The actual implementation is a simple algorithm: of the color codes listed in the protocol as actually available, it just picks the one with the smallest Euclidean distance (the sum of squared differences of the RGB values) from the given color code.

As for common color names, you can specify these 13:

self.color_map = {
    "black": 0, "dark gray": 1, "gray": 2, "white": 3, "red": 5, 
    "orange": 9, "yellow": 13, "green": 21, "cyan": 37, "blue": 45, 
    "purple": 49, "magenta": 53, "pink": 57
}

For simpler use, I think these are enough.

Being able to add touches like this is what makes writing your own module fun and useful.

Notes on apc40mk2.py

apc40mk2.py is divided into a parent class, APC40MK2, and three child classes: LEDController, which controls the LEDs of pads and buttons; KnobController, which controls the knob values and the ring LEDs around them; and DeviceModeController, which sets the device's operating mode.

For the detailed usage of each function, please see the docstrings in the module.

Comparing them with the APC40's protocol as you read may make it easier to see what each one is doing.

The led.set_clip_launch(row, column, color, led_type) from earlier specifies the MIDI note index as follows. Note that it's partly simplified here.

def set_clip_launch(self, row, column, color, led_type):
    self.midiout.sendNoteOn(led_type + 1, (5 - row) * 8 + column, color / 127)

Going further

The approach above can be applied to MIDI controllers other than the APC40.
The flow is the same: look at the MIDI protocol of the controller you want to use, prepare a module that can send MIDI events in a form that's easy for you, and make the actual calls through that module.

This one would get long on its own, so I'll save it for another time, but I've also tried preparing a module for Novation's Launchpad series, pad controllers with an 8x8 grid of RGB LEDs, and lighting the pads in the same colors as an 8x8 TOP image in TouchDesigner.

Try it with the MIDI controller you have, too!

In closing

If anything in this article is unclear, or if you spot something that needs correcting, I'd be glad to hear from you in whatever form.
Comments, replies, DMs, anything is welcome!
I'm looking forward to reading the rest of the articles!