M4L - Clip Detection

I built a Max4Live device that monitors a track for clipping and adjusts the track volume to keep from clipping the mixer. Here’s the presentation view of the device:

ifClippedAdjustVolume Device Overview

And here’s the complete logic:

Device Logic

The device works by identifying the track it’s placed on, by requesting the path this_device, and reading the current volume setting off of the track:

Track Identification Logic

The track volume is saved, so if the Reset button is pushed, the original track volume setting can be restored. The Store Current Volume button will update the cached volume setting.

Separately, the device uses the peakamp~ block to get an updated peak amplitude value of the audio every 100ms. It takes the maximum value of the two audio tracks. The value is retained until the Reset button is clicked:

Level Detect Logic

The peak amplitude is compared to a clipping threshold, set to -0.3 dBFS by default, and a new volume setting is calculated. Currently the volume setting is limited to +6 dB to -18dB. Over that range there’s a linear relationship between the volume control and the gain: 0.025 per 1 dB of adjustment, where 0.85 yields 0 dB:

Volume Update Logic

There are a couple things I’d still like to do with this device:

  • Modify it to work on the Master output. Currently, the device doesn’t properly detect the track when it’s on the Master, so it can’t adjust the volume.
  • Extend the range of control beyond -18 dB.

The device is available on Github. I’d be happy to review any pull requests. It’s also listed on maxforlive.com. I’m distributing it under the CC BY-SA 4.0 license.