PyConDE & PyData Berlin 2024

Replacing Callbacks with Generators: A Case Study in Computer-Assisted Live Music
2024-04-24 , B09

Callbacks have become an ubiquitous programming technique that we use every day without even thinking about it. They are definitely handy in many situations, but sometimes they feel more like a burden than a help. In developing an interactive realtime audio processing system for use on stage in live music, we encountered such a situation. This talk will present how a few dozen lines adding a thin abstraction layer allowed us to replace a complex callback mess with tremendously more readable generators (yes, you know, those functions which yield results instead of returning them...).


At Les Chemins de Traverse we explore ways of "augmenting" acoustical musical instruments with new sonic possibilities offered by computers (think "augmented reality" for live music). For doing so, we are using Olivier Bélanger's great pyo module for realtime audio processing. To make the system interactive, this module allows to register callbacks on some events. While this works great in many situation, it can get very cumbersome when we design a stateful system, where the same event must trigger different callbacks depending on the system's inner state.

This talk will present how we developed a thin abstraction layer that allows us to replace many callback functions together with many registering/unregistering of these functions by a nice, streamlined generator definition that's incomparably more readable than the many-callbacks version. This allows us to keep our mind focused on what's important, namely supporting the music we want to play, instead of tedious boilerplate code.

While our use case is admittedly very specific, we believe that the ideas we present could be adapted in many other situations where callbacks are used for technical reasons, but lead to bulky and contrived code.


Expected audience expertise: Domain:

None

Expected audience expertise: Python:

Intermediate

Abstract as a tweet (X) or toot (Mastodon):

How we made our code more readable by replacing intricated callback-based code with much more readable generators. Also a great example of using python in an unexpected domain: realtime audio processing for live music!

Public link to supporting material, e.g. videos, Github, etc.:

https://www.matthieuamiguet.ch/blog/PyConDE24

Trained both as a musician and a mathematician, Matthieu Amiguet took up programming as a hobby and somehow ended up making a PhD in computer science. He now works freelance - both as a musician and a developer. He is Artistic Director at Les Chemins de Traverse, jointly with Barbara Minder.

Les Chemins de Traverse is a collective of musicians, artists and researchers from a variety of backgrounds with a focus on sonic exploration and live performance. They cover a large musical territory from renaissance and baroque music to jazz, rock music and contemporary experimental noise. More often than not, they mix different styles and techniques - like in a weird chemical experiment that would produce nice colored fluids but might as well explode at any time.