Skip to content

Midi Photos

2022-12-263 min read
Photography
Python
Midi Photos

GitHub

Midi Photos is a project I started at the end of December 2022 that makes photo editing more tactile.

I had seen other projects that interface MIDI controllers with Adobe Photoshop or Lightroom, but none with the default Photos app on mac.

Here's a video of it in action:

The MIDI Controller

The Samson Graphite MF8 is a portable MIDI controller with 8 sliders, a jog wheel, and various other buttons and knobs. In this project, I decided to use the sliders as coarse-grain control for 7 sliders in Photos (Brilliance, Exposure, Highlights, Shadows, Brightness, Contrast, and Black Point). I used the buttons below each slider as a 'track select' for each slider. I also used the jog wheel as a fine-grain adjustment for any currently-selected channel (denoted with a green LED).

Finding a MIDI library

With a Samson Graphite MF8 Midi Controller in hand, I began searching for Python libraries to use with MIDI inputs and outputs. Why Python? Ease of integration for iteration and quick hacking.

I settled on using mido (after finding that py-midi had install issues) for MIDI input/output.

To interface with the Samson MF8, I mapped the inputs by manually midi-learning all the parameters. In the future, it would be convenient to implement an automatic midi-learn feature that maps all the controller's faders/sliders to controls in the Photos app.

Closer to the end of the project, I wrote a script to brute-force scan for LEDs that could be controlled using MIDI 'note on' messages in the range [0-127]. This allows me to control the LED state per-channel as either off, red, or green.

Finding a way to control the Photos UI

First, I tested Mido with the applescript library to programmatically control (i.e. 'get' and 'set') slider and button UI elements in Photos. Unfortunately, the latency was too high.

Testing MidiPipe

To see if the Mido library was a bottleneck, I tested a tool called MidiPipe. It allows you to run AppleScripts on MIDI trigger events. Upon running the same AppleScript to control the Photos UI, the latency improved, but it was still too high. MidiPipe gave less programmatic control, and I preferred the extensibility of Python, so I binned this tool.

Limitations of Finding Element References with AppleScript

I realized that using my AppleScript to both 'get' and 'set' values from/to the Photos UI was too slow. To speed things up, I decided to only 'get' slider values at the start of the program, or when running the Photos 'Auto Enhance' feature (toggled with CMD + E). (Although not currently implemented, it would make sense to 'get' slider values when switching to the next/previous image.) After getting the slider values, the state of each slider is tracked using a 2-element buffer per slider in software. Any fine or coarse-grain adjustment is tracked in the buffers.

'getting' is still relatively slow in my script. To make this faster, it would be smarter to find a reference to the Photos UI element, and retain a persistent reference to it. Unfortunately, I found AppleScript references to be volatile.

Quantization and Sampling for Latency Speedup

In search of reducing the latency between AppleScript and the MIDI controller, I decided that I could adjust the sample rate of MIDI events with Mido, and quantize the sliders in hardware and software. Since the MIDI sliders are each 14 bits, each has a range of -2^14 to 2^14 - 1. In Photos, the 'Light' sliders range from -1.00 to 1.00, in increments of 0.01. As a result, there are only 200 steps in software compared to 16 384 in hardware. In my script, I divide the hardware slider into 8 steps, which maps to increments of 0.25 in software. Quantization optimization led to considerable latency improvement.

Setting slider values

Since AppleScript remained a slow alternative for getting and setting, I decided to avoid using it for setting slider values. Instead, I used pyautogui to control the UI. Pyautogui can be used to move the mouse and click at a specified (x, y) location on the screen. Each slider's top-left (x, y) coordinate is stored in a per-channel array called slider_coords. The corresponding hardware slider value is used with the slider_coords to compute a new mouse location to drag the slider to.

What's Next?

As stated earlier, it would be beneficial to speed up the scripts I had written in AppleScript, or better to move away from AppleScript altogether. I can see this tool being used in the future to control all sorts of other applications, maybe an LED light strip, an interactive art exhibit, maybe video editing software. MIDI is a great tool to have fun with, and I highly encourage others to give it a try with a cheap controller if you have one lying around, and a desire to learn and make stuff!