Other things on this site...

MCLD
music
Evolutionary sound
Listen to Flat Four Internet Radio
Learn about
The Molecules of HIV
MCLD
software
Make Oddmusic!
Make oddmusic!
[Blog archives] [Categories]

Running Mopidy as a service with PulseAudio on Ubuntu - gotcha

I've been enjoying using Mopidy and ncmpcpp as my music player.

On Ubuntu though I encountered a persistent problem:

  1. I had set Mopidy up to run as a service, which seemed like a good idea - it's not just some application I want to start from time-to-time, it's the main music player for my flat, so I want it to be there on tap.
  2. I had set Mopidy up to connect to PulseAudio, which seemed like a good idea since then it should "play nicely" with other things on the computer, e.g. it should be able to play music even when there's a video playing from Firefox.

However, it kept failing to connect to PulseAudio. It seemed to be unable to make any sound play back, unless I manually killed pulse, after which it could start playing.

Yes I had followed all the instructions. I had changed Mopidy's config as well as PulseAudio's config. I tried everything, setting PulseAudio to be as permissive as possible (allowing remote connections etc).

The solution, as far as I can tell, for my standard Ubuntu 18.04 is: you should not do both of these things. This is because if you run as a service, you're running something that starts up as soon as the computer boots, and doesn't use your login userid. PulseAudio, however, does not run on startup, but is part of your login session. So it simply isn't there for Mopidy to connect to, until you log in. (I still don't know why that should mean it's unable to connect, even after logging in. I suspect it's because it's running under a different user id.)

So here's what I've got now: instead of running Mopidy as a service (using mopidyctl to control it), I'm running it as a "startup application", i.e. something that runs on my own user account as part of my graphical desktop login. On Ubuntu there's a program called "Startup Applications" that you use to add/remove things easily. The one small drawback with this approach is that the music player won't be running if the machine reboots and I haven't logged in yet. However, there's usually a logged-in session running.

You might think that "running as a service" and "outputting audio via PulseAudio" would be compatible, especially as they're both listed on the same piece of documentation on the Mopidy website. Perhaps there's an extra trick (e.g. some permissions) that my system needs. But this non-service setup works fine.

| linux |

How to use a different key for Return on Ubuntu

My Return key is getting a bit flaky, so I've just worked out how to reuse my "AltGr" key as a backup in case of emergency. Noting it down for my own reference.

In Linux you can configure anything, including custom keyboard layouts, but it can be tricky to find the advice online that actually matches your system. (Standard Ubuntu has some helper apps that a LXDE desktop doesn't, etc etc.) I think this method will work with any Debian-based system, and I took it from Debian's multimedia-keys guide.

Basically, follow the instructions in that wiki page:

  • I used "xev" to discover that my AltGr key emits keycode 108. I also noticed that when I pressed the Return key, it confirmed that the official name for the Return key event is "Return".
  • I made a file ".xmodmaprc" containing just the text inside these quotes: "keycode 108 = Return"
  • I ran "xmodmap .xmodmaprc"

That's enough to enable it for this session. Then you just have to add that last command as one of your autostart programs so it'll be available in future sessions. I did it by running "gnome-session-properties" tool, but also belt-and-braces in my .zshrc because of some other config I have in there.

| linux |

How to make a debian package of SuperCollider

I've been making debian packages of SuperCollider recently. Here's a quick note of how to do it - it should work on any debian-based system though my experience is on ubuntu and puredyne:

First make sure you have the standard packages needed for getting and building things:

    sudo apt-get install build-essential debuild subversion gnupg \
      libfftw3-dev libsndfile1-dev scons libjack-dev libreadline6-dev \
      libicu-dev libavahi-client-dev libasound2-dev libcwiid1-dev \
      libxt-dev python-support sharutils chrpath

Then we'll create a folder "scdeb" in which to do our work, and in it we'll get a fresh checkout of the latest SuperCollider source:

    mkdir ~/scdeb
    cd ~/scdeb
    svn co https://supercollider.svn.sourceforge.net/svnroot/supercollider/trunk scsvn

Now, inside the source tree is a script which will build a neat source package for us (it strips out the SVN folders as well as various non-linux stuff):

    cd scsvn/common/Packager
    ./package -s

When this is done there's a .tar.gz file called something like SuperCollider-2010-02-22-Source-linux.tar.gz (the date will be today's date).

We'll need to move that around as well as unpack it into the special positions that debian's bundling expects (it's quite strict about the filenaming conventions):

    cd ~/scdeb
    cp scsvn/common/Packager/SuperCollider-2010-02-22-Source-linux.tar.gz supercollider_3.3.1~svn9872.orig.tar.gz
    tar zxvf supercollider_3.3.1~svn9872.orig.tar.gz
    mv SuperCollider-Source supercollider-3.3.1~svn9872

A couple of things to note about the filenames:

  • The .tar.gz also has that orig to indicate it's the "upstream" source without any debian patches or additions.
  • The .tar.gz name must always be the packagename, followed by underscore, followed by the version string. In my example I've used 3.3.1~svn9872 as the version string but you'll need to adapt that to whatever version you're actually packaging.
  • The extracted folder name is similar to the .tar.gz name EXCEPT that there's a dash (a minus sign) rather than an underscore - that's important!

OK, next we go into the nice clean source folder we've made, add the debian stuff, and edit the changelog to say what's new in this release.

     cd supercollider-3.3.1~svn9872
     svn export https://supercollider.svn.sourceforge.net/svnroot/supercollider/packages/ubuntu debian
     vim debian/changelog

The format of the changelog is again quite strict - I won't describe it here but search the web for debian changelog format to learn more. Don't include all the changes that have happened, only the ones relevant to this debian packaging (e.g. "fixed patch for debian paths", or also "new upstream release").

Now you're ready to do the build.

      debuild

When that process finishes it'll ask you to sign the package using a GPG signature associated with your email address. (You can deactivate this signing using a flag to debuild, but if you don't sign it then no-one can trust where it came from so it won't be allowed into package repositories.)

Alternatively for a debian source package you can run

      debuild -S

Whichever of those two you run, you'll end up with packages built in the parent folder (~/scdeb) which you can install locally using dkpg -i or upload to ubuntu/debian using dput.

| linux |

MythTV: adding iPlayer downloads to the menu

MythTV does good stuff but it doesn't (yet) do BBC iPlayer integration. I found this great blog post about adding BBC live streaming to MythTV and it's almost what I wanted, but I don't want the live streams cos I already get them through the receiver!

What I do is download automatically using get_iplayer so that BBC programmes become a lot like podcasts/vodcasts. So what I want is a MythTV menu showing the media files I've downloaded that way. Here's how I did it, based heavily on that blog post I linked above:

(Note: MythTV v0.22 only since 0.22 introduces a new GUI system and 0.23 is going to introduce a nice iplayer-compatible thing I think...)

First of all, I already have a bash script called automatically to do the downloading. Whenever the downloading is done, I want to regenerate an XML menu of available files, for Myth to show me. Here's the code I added to my bash script:

# Now write the XML menu. this folder I created myself, choose yr own path:
datadir=/var/lib/iplayer
cd $datadir
menupath=$datadir/watch_iplayer.xml

xmltop="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<mythmenu name=\"WATCH_IPLAYER\">
"
xmlbot="
</mythmenu>
"

echo $xmltop > $menupath
# NOTE: this ls command only catches files with extension .m__
for f in `ls -1t *.m?? | head -n 15`; do
        trimname=`echo "$f" | sed -e s/_default//g -e s/_//g -e s/\.m..$//g`
        echo "
    <button>
        <type>TV_WATCH_IPLAYERTHING</type>
        <text>$trimname</text>
        <description></description>
        <action>EXEC xterm -e mplayer -fs /var/lib/iplayer/$f</action>
    </button>" >> $menupath
done
echo $xmlbot >> $menupath

So just to unpack that script a little bit: we're building up some XML code bit-by-bit. First I specify the folder, then define some strings which will be the top and bottom of the XML menu. Then the choice of which files to list comes from this line:

ls -1t *.m?? | head -n 15

That asks the ls command to list files in reverse order of their date, and only to include files whose file extension starts with "m" (.mp3, .mov, .mp4 - it's just luck they all have the "m" in common). Then the head command trims that down to the most recent 15 - you might not want that limit but the interface becomes a bit unwieldy otherwise.

For each file we create a menu entry which executes:

xterm -e mplayer -fs /var/lib/iplayer/$f

This is just invoking mplayer, but wrapping it in an xterm call because otherwise I don't seem to have any option to exit the programme!

So when this script runs, it iterates those 15 most recent files and outputs an entry in the XML file for each of them. It generates this watch_iplayer.xml file.

The final thing that needs doing is telling MythTV to include this menu in its interface. I did this in almost exactly the same way as described near the end of the blog I linked above: copy one of myth's menu layouts to a special folder in your home:

cp /usr/share/mythtv/themes/defaultmenu/library.xml ~/.mythtv/library.xml

and then edit it to add a link to your new menu:

<button>
    <type>MENU_WATCH_IPLAYER</type>
    <text>Watch iPlayer downloads</text>
    <description>watch live TV streamed from BBC</description>
    <action>MENU watch_iplayer.xml</action>
</button>

(I used a softlink to jump out of this folder to where my watch_iplayer.xml file really is.) Hey presto, we can watch timeshifted iPlayer just like we can watch other timeshifted TV.

| linux |

Radar chart in Gnuplot

Gnuplot's documentation is downright baffling so it took me ages to work out if it was even possible to do a radar chart (aka spider chart, star plot, polar chart). Here's how I managed it in the end.

  set angles degrees
  set polar
  set grid polar 120.
  unset border
  unset param

  set style data filledcurves 
  set style fill solid 0.5

  set datafile separator ","

  set xtics axis nomirror
  set ytics axis nomirror
  set yrange [-45:45]
  set xrange [-45:45]
  set size square
  set title "Radar chart"

  plot '~/docs/mydata.csv' using 1:2 notitle, '' using 1:3 notitle, '' using 1:14 notitle, '' using 1:25 notitle

My datafile was a CSV file where the first column gave the angle in degrees (as you can see in the plot, I've only used three angles: 0, 120, 240). You can see in the last line of that code that I've separately chosen to plot polygons for columns 2, 3, 14, 25 from my data.

| linux |

Applying ReplayGain to all yr MP3s

I've just discovered the really handy mpd (music player daemon) which is pretty much the perfect command-line geek's music player service. No graphical interface, no nothing.

Anyway

It's currently playing my MP3s on shuffle and it's great, but the MP3s are a bit too varied in their volume: some are too loud, some are too quiet. And this is where the magic of ReplayGain comes in. ReplayGain is a hint that can get embedded in an MP3 about how loud it should be, and there's a linux command called "mp3gain" that can analyse your files for you and make sure they're all equalised volumewise. If you have a folder containing ten MP3s from an album, you can run a command like

   mp3gain ~/Music/theAforementionedAlbum/*.mp3

and it'll do the business. The extra-clever bit is that it also applies album-wise ReplayGain - assuming that all the files you've given it are off the same album, which has been carefully mixed to have loud bits and quiet bits, it'll specify an albumwise level as well as an individual trackwise level.

So far so good, but what if you have a million albums of a billion MP3s? You'd need to run that command once for each album.

Or you could do some command-line kung-fu. This command is what's running on my linux box right now (should work on mac too):

   find -L ~/Music/ -name "*.mp3" -exec dirname "{}" \; | uniq | while read line; do mp3gain -k -o -r "$line"/*.mp3; done

It uses find to find all my MP3s, dirname to find which folder they're in, uniq to make that a unique list of folders, then while read line to feed that info one line at a time to the mp3gain command. Therefore it's feeding it with one folderfull at a time, rather than just one MP3-file at a time, which should allow it to do the ReplayGain thing best.

| linux |

puredyne: new linux for real-time audio+video

Puredyne is a linux distro specialised for real-time audio+video art. It's also a live distro, meaning it boots straight off a USB stick or CD - so you can use it without trashing your current mac/windows/linux install, or plug it in when you turn up at some random …

| linux |

Simple GNU Readline callback-style example

GNU Readline is a linuxy tool for user-friendly command-line handling. The Readline documentation is good, but it doesn't give an example of using it in the alternative "callback" style, so here's a simple example:

#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <stdlib.h>  /* for free …
| linux |

MythBuntu/MythTV: A week of tweaks

Getting to grips with MythBuntu: an edition of Ubuntu tailored specifically for providing MythTV, the so-called "mythical convergence system" - a combination of TV, video recorder, media player, and computer.

MythTV has loads of good features. For example, it provides a webpage so you can remotely browse the TV schedules, then …

| linux |

social