Saturday, April 9, 2011

Make your music pump like Parisian house

The Prydzifier* (or Daft Punkifier or Parisian House-ifier)

First of all, welcome to my blog, to whoever may be reading it. This is my first post, as opposed to an introductory post, however in order to properly introduce my blog, I will say a few words about how it came to be. I am Samuel, a computer scientist who majored at NYU and this blog is designed to share my small projects and code that I am working on. It is pretty much a run of the mill software developer blog in which I ramble about things I have worked on and maybe someone will find it interesting or helpful.

That being out of the way, I recently have been working in ActionScript and Flash. I know that the initial reaction of many developers is one of distaste when hearing about Flash development, and I was one of those people fairly recently, myself. However after years and years of putting it off, I took a Flash Programming class my senior year because I knew that I should at least know how it works for my resumé, even if I didn't think I would ever use it.

As it turned out, I really enjoyed it and found it more powerful than I had expected. It still has many of the shortcomings you probably could rattle off to me in a heartbeat but it also is much cleaner these days than what I always imagined it to be like. Plus, the work I do in it is easily accessible inside the browser so that I can show off things I create somewhat easier. You also you have to have good development practices and knowing your application is going to be running inside Flash inside a browser means you really have to make sure that everything is coded tightly.

This post is specifically about a spin off from a larger project (a game actually) that I am working on. My game has an entire audio engine that is being worked on as necessary for the game, and one of the things I needed was the ability to plug in various effects, especially a dynamic range compressor. I hope to talk about the game more in future posts. After creating the compressor for the game, I ported it over to a standalone Flash application so that it could be played with on its own.

Dynamic Range Compression

For those of you who don't know, a dynamic range compressor is a plugin or hardware device used in audio production to reduce the dynamic range of audio. It serves many purposes in the world of audio engineering and music production and is infamous for its use in the "loudness war". However, a compressor has many other uses as well. The dynamic range of a track is, simply put, the difference between the loudest and softest points. However, my compressor doesn't focus on the finer points of dynamic range compression for audio production. Instead, it focuses on a popular somewhat recent usage of the compressor in pop/dance music.

Compressor from Logic Pro

Sidechain compression is the cause of the pumping sound heard in many dance tracks, especially in the French/Paris house scene. Of course, it has made it's way to a lot of popular music these days, but some classic examples you may or may not recognize include Daft Punk's One More Time and the slightly not safe for work Eric Prydz' Call On Me. If you listen closely, that in and out "sucking" sound corresponds to the kick drum. This is because the producers "sidechain" the kick drum track to the tracks they want to compress. In essence, the kick drum triggers the compressor and causes the tracks with the compressor hooked up to them, to duck.

To understand what exactly sidechaining is, you have to understand the basics of how a compressor works. A compressor that isn't sidechained to anything is essentially sidechained to it's own input. Ignoring sidechaining completely, a compressor is fed an input stream of audio and outputs processed audio. The two main parameters of the compressor are threshold and ratio. The threshold parameter defines at what point of loudness (in decibels) should the audio input stream have to hit before compression is triggered. The ratio parameter defines by how much audio breaking the threshold should be compressed.

Here is an example borrowed from Wikipedia. Let's say you have a simple compressor and you set the threshold to -10dB. You then set the ratio to 4:1. Any input below -10dB is going to come out unchanged. However, if you have input that hits -6dB (4dB over the threshold), you will reduce the amount over threshold to 1dB (4dB -> 1dB, due to the 4:1 ratio of compression). Ultimately, the output will only be -9dB (-10dB + 1dB) as opposed to -6dB. Should the input hit -2dB (8dB over), the 4:1 ratio will compress the overage to 2dB and your output will only be -8dB instead of -2dB.

The above examples assume the compressor's sidechain is the same input that the compressor is compressing. The sidechain is what the threshold is measured against and what triggers the actual compression on the input. The input acting as its own sidechain is the normal means of using a compressor. (It gets a bit more complicated than this but for the purposes of this explanation that is all that you need to understand.)

However, one can feed a different signal to the sidechain as desired. In the instances of the aforementioned house music, the sidechain is generally the kick drum track. In these instances, the kick drum has to break the threshold in order to trigger compression on the input (based on how much the kick drum went over and the ratio settings). This leads to the other tracks which are being compressed pumping in and out with the kick drum as they lower and raise in volume (although by compression and not direct gain change).

The compressor also has some other parameters depending on how complicated, including attack and release times which specify in milliseconds how long it should take for the compressor to kick in fully or how long it should take to completely stop. Super-short release times can sound ugly but they shouldn't be too long in general and attack times should be also somewhat low (sometimes even 0) for the purposes of this effect. It is the fast attack time that actually contributes to the sound (side note: the Autotune effect also relies on (mis-)using Autotune with a very low, or 0 attack time). The otherworldy speed of 0ms digital adjustment is part of the popular sound these days.

My Compressor

Getting away from this long aside, my compressor is special in that what it does is allow you to simulate the effect of pumping House songs with any MP3 you want. Well, there are limitations, but I will get to that later. My compressor is fairly standard, but in order to feed the sidechain I had to figure out a way to simulate the kick drum track from the song. It is pretty common knowledge that taking tracks out of a mixed song is like taking eggs out of a baked cake, so I had to wing it and figure out the best way to simulate sidechaining to the track's actual kick.

I decided that the sidechain initially needed to be a low-passed version of the input. If I could have a low passed version of the input, I would cut out the noise from above a certain frequency keep the kick drum which sits low on the track. Afterwords, I could run an envelope detector on this low-passed audio. The envelope detector would have to be good for detecting beats, or spikes, in the audio. After playing around with simple envelope detection algorithms, I came across a beat detection algorithm on Music-DSP and ported/retooled it for my needs. It has a envelope detector designed for peak detection and a 2nd order low-pass filter for sifting out the noise. It gave me the best results out of my other algorithms. I ignored the Schmitt Trigger and Rising Edge Detector since I am not actually trying to detect beats specifically, but build an envelope that detects peaks in the lower frequency range (thus responding strongly to beats by its nature).

One thing I changed was that I implemented the ability for attack time in my code. While the beat detector on Music-DSP had a hardcoded attack time of 0ms, I wanted the attack to be controllable. The attack uses the same algorithm as the release. It should be noted that the attack and release help to shape the envelope; they aren't a hard delay time before the compressor kicks in (or out). Their goal is to smooth, not delay, the compressor.

The final result works pretty well. It works best on tracks that have a four to the floor kick drum beat such as pop and dance music. Rock and rap don't work as well given that they don't have as pronounced of a kick and a lot of confounding noise near the lower end of the spectrum.

I have samples which include presets for the compressor that work well and these should serve as a good jumping off point to play around.

One last thing, the gain is used to add volume to the compressed output and the beat frequency is used to help figure out where the low pass filter should should be (in order to better isolate the kick drum). Have fun!

Also I would like to thank Flexible Factory, the makers of MP3FileReferenceLoaderLib for AS3 which made the seemingly trivial task of loading MP3 locally into Flash actually trivial because its not actually as built in as you would expect.

You can download the source code under the MIT License, but I warn you it isn't as clean as it could be! However it still is somewhat clean and readable.

Sample Tracks Credit

*Not endorsed by Eric Prydz or Daft Punk

No comments:

Post a Comment