MPlayer Gapless Audio

Share your wisdom. Not for support questions!

Moderator: How-to Curator

MPlayer Gapless Audio

Postby julian67 » September 27th, 2011, 2:35 am

This short script will enable true gapless playback of any gapless audio that's in a format supported by mplayer, which is everything without DRM as far as I can tell. It works equally well with .m3u playlist as with audio files. If you use some other playlist format you can see how easy it is to add or substitute that.

Requirements: mplayer, alsa, some (gapless) music. I use it on Debian Squeeze with mplayer-nogui from the debian-multimedia repo. I use pulseaudio with sample rate 48000. I can't think of any reason why it wouldn't work perfectly well on older or newer releases. You just need to put it in your path and make it executable and name it as you prefer. On my systems it is mpgapless. I don't have GUI mplayer so didn't try it but I assume using gmplayer in place of mplayer will work the same way.

Code: Select all
#!/bin/bash
# mpgapless

case "$@" in
   *.m3u) PL="-playlist"
   ;;
esac

rm /tmp/audiofifo
mkfifo /tmp/audiofifo
aplay -t raw -c 2 -f S16_LE -r 48000 /tmp/audiofifo &> \
/tmp/aplayfifo.log & mplayer -noconfig all -nolirc -nojoystick \
-novideo -af resample=48000 -nocache \
-ao pcm:nowaveheader:file=/tmp/audiofifo $PL "$@"


So
Code: Select all
mpgapless <your_playlist>
or
Code: Select all
mpgapless <audiofile> <audiofile1> <audiofile2>......
or
Code: Select all
mpgapless *.ogg
and enjoy the music the way it was meant to be heard.

How it works:

Named pipes (FIFO) naturally act as a buffer. Passing, from the gapless source, decoded raw audio without headers to a named pipe eliminates the artificial gap introduced by players as they finish one track and start to read the next one, and produces a gapless stream. If you have an audio player which can play headerless, raw audio then you now have a gapless audio player. I would never have thought of this so I feel really pleased to have come across it. I added a couple of things to make it a bit more sane and useful.

I would have preferred to use mplayer in place of aplay but when you have two instances of mplayer running you quickly find that issuing a "next track in the playlist" or "pause" probably won't be producing the behaviour you want....so aplay it is, which means this is Linux only (aplay is part of alsa, alsa is Linux only).

The resampling and -nocache are important.

Sample rate:

If you google "mplayer gapless" you'll see this script is expanded a little from the usual solutions found on a search (for example http://snipplr.com/view/18353/gapless-p ... yer-linux/), because the usual solutions only work well with 44100 audio. You get pitch changes (which many people won't notice) if you play back 48000 at 44100 without using a resample filter (with raw audio it's not automatic, it has to be specified) and if you play back 96000 at 44100 it gets comical - everyone sounds sloooooow. So it's best to use mplayer's -af resample and if your card supports 48000 then that rate is fine, because upsampling 44100 to 48000 is not going to produce any audible difference, but downsampling 4800 to 44100 might. The best rate does depend somewhat on your soundcard; if it only supports 44100 then use that value instead and remember to change the aplay value and the mplayer value. The other advantage of using mplayer's audio filter is that it enables this method to work equally well with DTS and other audio which has 96000 or 88200 Hz sample rate (or stuff with low rates like 22050, mono tracks, audiobooks and so on).

-nocache:

If you don't disable the cache when using mplayer to decode to a named pipe you'll run into buffer underruns. In practice this will mean that you can't resume playback if you pause for more than a few seconds.

I have the named pipe and log output created in /tmp. You may prefer a different location such as somewhere in your home directory.

Discussion is here
User avatar
julian67
 
Posts: 249
Joined: February 9th, 2011, 12:59 pm

Return to HowTo

Who is online

Users browsing this forum: No registered users and 1 guest

x