This is

as days pass by, by Stuart Langridge

. Here I write about many things. In the past I wrote about other things but the past is past. I write code for people to play with, I write about my life on Twitter, and I write here.

On I wrote Playing system sounds from Python programs on Ubuntu, on the subject of Howtos and Sundry Hacks.

Jono asked me yesterday how you play a system beep from a Python program on Ubuntu. Now, the way you're meant to play system sounds is by using libcanberra; it's the standard platform library for playing a sound. Sounds come from your sound theme.

A sidebar: themes in Ubuntu

The icons used by your desktop are part of an icon theme. This means that there are a set of names defined for particular things -- there's an icon name for "sound at maximum volume", another name for "sound at minimum volume", another for "wireless network connected", and so on. Then the applications on the desktop use those icons by name, so the Sound indicator displays the "sound at maximum volume" icon in the panel at the top of the screen when your sound's at maximum volume. This means that you can change the look of your desktop by providing a different icon theme (a whole set of icons, named after the specified names), and all your applications use your new icons so they all look consistent. A sound theme works on the same principle; there are a defined set of sounds (there's one for "your battery is low", one for "a window has been maximised", one for "you have logged in", and so on), and your sound theme defines what those sounds are. So you can switch in a different sound theme, and now all your applications will change to use the sounds from your sound theme. End of sidebar.

What all this boils down to is that you ought to be able to play the standard warning sound by telling libcanberra to "play a sound named dialog-warning", and it'll play the correct sound.

However, there isn't a Python version of libcanberra, sad to say. I complained about this on twitter, and psykoyiko brilliantly responded with a pointer to a Python libcanberra library he wrote: pycanberra.py. So, you can now play system sounds from your Python program, because this is basically a set of Python bindings for libcanberra.

To play a sound:

import pycanberra
canberra = pycanberra.Canberra()
canberra.easy_play_sync("dialog-warning")
canberra.destroy()

Hooray! Good job psykoyiko! Someone should get this into Ubuntu.

Roger

Debian import freeze is in 9 days - it should be possible!

Jon Nordby

No, someone should get this into GNOME.

sil

Jon: I would anticipate that Gnome would have a proper version: perhaps libcanberra should do GObject introspection (if it's gobject-based), or proper Python bindings rather than ctypes. The Gnome project is averse, I believe, to including hacky solutions rather than doing it properly, as befits a careful and conservative upstream. In the absence of those, though, this slightly-hacky-but-still-works library would be useful to get out to people in some way, hence suggesting that it goes into Ubuntu.

Jon

Yeah, you're probably right that Gnome would probably not like this solution much. libcanberra is not gobject based, but the proper solution is likely to write a wrapper and then use g-o-i.

sil

Agreed. In the interim, though, having this wrapper library available to people would be handy.

alex

i wonder why do you want to play sound in sync? U want to freeze application? :) Better look for an async way.

sil

alex: then do canberra.play(1, canberra.CA_PROP_EVENT_ID, "dialog-warning" ,None).

This website belongs to Stuart Langridge. Contact details are available. Don't eat yellow snow. Valid HTML5, at least in theory, except for the bits that aren't because I'm that futuristic that I'm ahead of the spec, oh yes. HTML5 help from Bruce Lawson, among others. Fonts from the superb FontSquirrel. End.