She's a lot like you, but she don't look like you... Okay, she's not you, but she'll do fine. -- Barenaked Ladies, Intermittently

MythTV: Volume Leveling

Back at the other end of this year, I decided to retire my aging TiVo, and become a full-time user of the magic that is MythTV. For those of you who don’t know, MythTV is a software package you can run on a computer (Linux based. I won’t swear that there isn’t a Windows port, but of this, I know little) which does more or less what a Digital Video Recorder does — it can record TV to its hard drive so you can watch it whenever you like. But because you’ve got a whole computer which is under your control, it can also do, well, anything else you want. For my purposes, the most useful thing that it does is to act as a sort of video jukebox: I can back up all my DVDs to a network hard drive, and thereby avoid all the hassle of (a) having to keep piles of DVDs in the living room, (b) risking scratches, and (c) dealing with temperamental DVD players. Another of its nice features is that, with an add-on called MythNetTV, you can subscribe to video podcasts via MythTV, and it will deliver new episodes to you just as if they’d been broadcast over the air, allowing you to watch grainy, low-resolution YouTube quality video of cats doing amusing things on your 40 inch HDTV. I’ve long found it ironic that as TVs get bigger and resolutions increase, we’re increasingly willing to huddle around a laptop monitor to watch a 320×200 viral video. Well, suck it, losers, because I’m watching The Spoony Experiment and The Nostalgia Critic on the big screen.
Now, like most Linux projects, it’s not all sunshine. I’ve got two cheap TV tuner dongles, which don’t work with it (There’s several very nice tuners which work with it, but I really wanted just a cheap one to use as a secondary tuner). And the usability is not nearly as polished as, say, TiVo (That said, it’s miles beyond most cable box DVRs in the UI department). There’s a few annoyances that I have yet to be able to overcome (The size at which subtitles render is hard-coded, which means that it displays at a size which was plainly selected for a Standard Definition screen, making it slightly microsocopic at 1080p), but, like I said, it’s a whole computer, and you can bring to bear all that implies.
I’d been meaning for some time to write a series of articles about the cool things I’ve written to bend the Mythtv to my will, but actually banging any of my hacks into a presentable state has required a bit more time than I’ve been willing to invest. But this week, I found something so handy and so elegant that I thought it was time to share it.
So, MythTV trick Number One:
One problem with playing back video from various disparate sources is the volume level. You know how when you’re watching regular old-fashioned TV, more often than not, the commercials will be about a million decibels louder than the show? The volume will be different from one channel to the next. When you’re also downloading New Media from The Intertubes, those too will be at radically different levels from TV, and from each other. DVDs are usually at a much lower level than TV (I think this may be caused by the downmix from 5.1 to stereo). And if, say, you’re watching a third generation rip from a grainy VHS of a film so rare that no one involved in it will even admit to having heard of it, you’re talking borderline inaudible.
With months of training, I’ve got Leah to the point where she’ll actually give me a fair chance to reach the remote control and turn the volume down before she yells at me to turn it down the instant the sound starts, but it’s still not really an optimal solution for me to keep having to adjust the volume from one video to the next.
If you are a modern person who keeps all your music in digital format, you may be familiar with the concept of volume normalizing, which analyses a whole song and works out how to adjust the overall volume to the song so that you don’t blow out your ear drums if Shuffle Play puts a John Tesh song right after one by Alice in Chains (Which is not to say that you don’t deserve deafness for your taste in music).
But the tools for doing this to video are less mature, and besides, you might be willing to spend 2 minutes preprocessing a 4 minute song you’re going to keep for the rest of your life, but I’m not willing to spend 30 minutes processing an episode of Stargate Universe which I’m going to delete as soon as I’ve finished watching it.
As it turns out, though, since you’re running a whole computer, and it’s Linux, the Magical World Where You Can Basically Do Anything You Want So Long As You’re Willing To Carve It From the Solid Granite of the OS With Your Bare Hands, it’s possible to just order your sound card to do that normalization for you as it plays — in this case, it’s called Compression and Limiting.
I could just about muddle through the science of how it works, but probably not well enough to explain it to anyone in detail. The general gist of it is that a “compressor” squishes audio such that it reduces the difference between the loudest sounds and the softest. When a sound is louder than some threshhold, it reduces the volume, but it does it in a very smooth way that sounds good. This is something radio stations do so that you can turn the volume up loud enough to hear the soft bits without blowing out your speakers for the loud bits. A “limiter” is the same basic process, but it’s much more powerful and lacks the subtlety of a lower-rate compressor. Basically, the purpose of the compressor is to make the audio all “fit” within a certain range of loud-to-soft, and then the limiter boosts the gain (ie. “Turns the volume up”) while keeping it from exceeding a certain threshold.
In Linux’s ALSA sound system, you can create plugins which (long story short) basically act like virtual sound devices. You tell an application to use that sound device, and any audio the application tries to put out will be sent through the plugin before it’s turned into sweet delicious audio. Here’s an audio compressor that I threw together based on some stuff I found on the ALSA wiki:

pcm.ladcomp {
type plug
slave.pcm "ladcomp_compressor";
}
pcm.ladcomp_compressor {
type ladspa
slave.pcm "ladcomp_limiter";
path "/usr/lib/ladspa";
plugins [
{
label dysonCompress
input {
controls [0 1 0.5 0.99]
}
}
]
}
pcm.ladcomp_limiter {
type ladspa
slave.pcm "default";
path "/usr/lib/ladspa";
plugins [
{
label fastLookaheadLimiter
input {
controls [ 15 0 0.8  ]
}
}
]
}

This code can be put in your /etc/asound.conf, then just tell MythTV to use the sound device ALSA:ladcomp (It’s under Utilities / Setup -> Setup -> General if you’re using the default menus. I’ve hacked mine up a bit, so it took me longer to find it). It should have defaulted to something like ALSA:default. Restart MythFrontend, and voila: all your audio should play at around and about the same level. To use this, you’ll need the ladspa plugins. If your MythTV is running on Ubuntu Linux (I use Mythbuntu, a version of Ubuntu oriented toward MythTV (For the non-Linux experienced, you can basically run any Linux software on any Linux box. The major difference between various Linux distributions is basically which software it installs by default, as opposed to which ones you have to download and install on your own. Ubuntu is a distribution which takes the radical step of assuming that its users may include actual human beings and might want to spend more time actually using their computer than assembling it.)), you can get them by running “sudo apt-get install ladspa-sdk swh-plugins”.
If you want this trick to apply to other applications, you can tell them to use ladcomp as their audio device too. For instance, with mplayer, try mplayer -ao alsa:device=ladcomp.
If you want to do some fine tuning, you can try changing that 15 to other numbers to change the range for the final audio (You have to restart MythFrontend before the changes will be honored). I haven’t found quite the right setting for me personally yet — 15 is a bit higher than I want, I think, since it makes the “comfortable” position on my stereo’s volume dial around 6 out of 30 — I think somewhere in the 10-15 range would be better. But, at least for me, it does put MythTV in about the same volume zone as the Nintendo Wii, so I’m not racing to turn the volume down when we turn the game consoles on.
So, with any luck, and a little bit of work, you too can bend the sound system to your will, and watch whatever you like without fear of getting yelled at by your fiancee for having the TV turned up too high.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.