A few days ago someone said something that reminded me about my audio player, which I had abandoned for more than a year already. The reason was mostly that the two Phonon backends, VLC and gstreamer, for some reason or other couldn't play the files I had without any gaps between songs.
To be honest, the first bug end up being me not properly encoding the filenames.
If you first URL-encoded the filename and then built a Q/KURL with that, then
it's all fine. It took me more than 12 months and a few rereads of the thread
to realize it. Fixes apart, it seems that the bug still exists for other
instances of gstreamer errors, so we're not out of the woods. In any case, I
switched to the VLC backend and it seems that now is able to fire the
aboutToFinnish() signal properly, so for the moment I'm using that.
All that is fine, but that's not what I wanted to talk about in this post. Given
that this project largely precedes my interest on testing, it has no testing at
all. Most of the project is straightforward enough to almost no need any, but
there's a critic part that would not suffer at all if it had any, namely the
Collections handling, including passing files from one to another and
automatically updating new/removed
So after fixing the bug mentioned above I tried to figure out the current state
of affaires regarding
Collections, and boy, they're in bad shape. The code was
locally modified, never commited, deactivating any notifications of filesystem
changes (new or removed files), and other code I can't really understand the
Because if this last detail is that I decided to start testing three classes:
Collection, which handles a set a
Songs with a common root directory;
CollectionAggregator, which handles a set of collections and should coordinate
Song from one
Collection to another; and
CollectionIndexer, which scans
Collection's root dir to find
All went fine while I tested the first class, Collection. There was a tricky
part where I had to setup a
QApplication in order to make signals work. The
problems began when I started testing
CollectionIndexer. Tests started blocking
endlessly, signals stopped being either emited or firing the connected slots,
life was bad.
I tried to search the available documentation and mailing lists for a hint about the problem, but besides a quite complex example that didn't seem to properly converge to anything useful, I was mostly on my own.
This morning I got my eureka moment: I noticed that if I executed each test class
by itself, it worked, but both at the same time blocked and never finished. Then
I remembered something said in
For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time.
That was it: I was creating the application, first in the
setUp() method, then
as a class attribute, but I had one test class per class to test, each in its
own file. Somehow this last fact lead me to think that somehow they were executed
in separate processes, which is not true. Luckily, even with this limitation, there's
none on the amount of times you can
quit() the same instance, so
that's what I did: I created only one instance and reused it everywhere. I was
already doing that for each test method, but again, somehow having several files
mislead me to think they were isolated from each other.
So now all my unit tests work without mysteriously blocking forever. Now I just
hope I can keep riding the success wave and bring
satyr into good shape. A new
release wouldn't hurt.
 No matter how much I try, I can't get any vaguer.
 Ok, maybe the
Playlist combo wouldn't hurt to have UTs either.
After several months thinking about it, and just two requests, I finally decided
satyr's code. I decided to use github because I already switched to
git, mainly for testing and understanding it. I think I
can live with
hg, althought branch management in
git seems to be given more
thought and a good implementation.
So, without further ado: satyr in github
Remember, it's still a test software, by no means polished or ready for human
consumption, and with very low development force. Still, I think it has some
nice features, like interchangeable skins and a well defined backend,
support, quick tag edition, reasonable collection managment, and, thanks to
Phonon, almost-gapless playback and things like «stop after playing the
current this file» (but not «after any given file» yet).
In Debian Sid it mostly works only with the
GStreamer backend; I haven't tried
xine one and I know
VLC does not emit a signal needed for queueing the
next song, so you have to press «next» after each song. AFAIK this is fixed
satyr-0.3.2 "I should install my own food" is out. The Changelog is not very impressive:
- We can save the list of queued Songs on exit and load it at startup.
- The queue position is not presented when editing the title of a Song that is queued.
- Setting the track number works now (was a bug).
- Fixed a bug in setup.py.
but the last one is somewhat important (Thanks Chipaca). Also, 2 months ago, I made the satyr-0.3.1 "freudian slip" release, when user 'dglent' from http://kde-apps.org/ found a packaging bug. It was not only a bugfixing revision, it also included new features:
- nowPlaying(), exported via dbus.
- 'now playing' plugin for irssi.
- Changes in tags is copied to selected cells (in the same column). This allows 'Massive tag writing'.
- Fixed "NoneType has no attribute 'row'" bug, but this is just a workaround.
- Forgot to install complex.ui.
Now go get it!
Since a long ago I'm looking for a media player for listening my music. In that aspect I'm really exigent. Is not that I need lots of plugins and eye candy, no, I just need a media player that fits my way to listen music.
How do I listen to music? All day long, for starters. I have a collection of
.ogg files, which I normally listen in random mode. From time to time I
want to listen certain song, so I either queue it or simply stop the current
one and start the one I want. Sometimes I enqueue several songs, which might
not be related between them (maybe only in my head they are).
I've been using
Amarok, I really like its random albums feature; that is,
I listen to a whole album, and when it finishes, another album is picked at
random and I listen to all its songs. The last feature, a really important
one: My collection is my playlist and viceversa. I don't build playlists; if
I want to listen to certain songs I just queue them. One feature I like also
is a tag editor and the posibility to rearrange the songs based on its tags
(with support for albums with songs from various authors, like OST's). Last
but no least, reacting to new files in the collection is also well regarded.
I used to use
xmms. I still think it's a very good player for me, but it
lacks utf support and doesn't react when I add songs to the collection. Then
Audacious (I was using it up to
today) and probably a couple more. None of them support all the features, so
today, completely tired of this situation, I started writing my own. I
Satyr. Another reason to do it is to play a little more with
PyKDE. Talking about
KDE, I know the existence of
minirok, but it uses
GStreamer, and I wanted to play with
So, what's different in this media player? If you think about it, if you have a CD (vinyl, cassettes maybe?) collection in your home, what you have is exactly that: a collection of Albums. Most media players manage Songs, grouping them in Albums and by Author too. Notably Amarok used to manage Albums with several artists (what's called a 'Various artists' Album), but since Amarok 2 it doesn't do it anymore, nor the queuing works. So the basic idea is exactly that: you have a Collection of Albums, most of them with songs from the same Author (and sometimes Featuring some other Authors), but sometimes with Songs from different Authors. Of course I will try to implement all the features I mentioned above.
Ok, enough introduction. This post was aimed to show some code, and that's what I'm going to do now. This afternoon I was testing the Phonon Python bindings, trying to make a script to play a simple file. This snippet works:
# qt/kde related from <span class="createlink">PyKDE4</span>.kdecore import KCmdLineArgs, KAboutData, i18n, ki18n from <span class="createlink">PyKDE4</span>.kdeui import KApplication # from <span class="createlink">PyKDE4</span>.phonon import Phonon from <span class="createlink">PyQt4</span>.phonon import Phonon from <span class="createlink">PyQt4</span>.QtCore import SIGNAL media= Phonon.MediaObject () ao= Phonon.AudioOutput (Phonon.MusicCategory, app) print ao.outputDevice ().name () Phonon.createPath (media, ao) media.setCurrentSource (Phonon.MediaSource ("/home/mdione/test.ogg") media.play () app.connect (media, SIGNAL("finished ()"), app.quit) app.exec_ ()
Of course, this must be preceded by the bureaucratic creation of a
KApplication, but it basically plays an ogg file and quits. You just have
to define a
MediaObject as the source, an
AudioOutput as the sink, and
createPath between them. As you can see, with Phonon you don't
even have to worry about where the output will be going: that is defined by
the system/user configuration. You only have to declare that your
AudioOutput is going to play Music (the second actual line of code).
There are a couple of peculiarities with the
Python bindings. First of
Phonon comes both with Qt and separately. The separate one has a
binding in the
PyKDE4 package, but it seems that it doesn't work very
well, so I used the
PyQt binding. For that, I had to install the
python-pyqt4-phonon package. Second, the bindings don't support to call
setCurrentSource() with a string; you have to wrap it in a
The original API supports it. Third, it seems that
is not supported by the bindings either, so I had to build the
by hand. I don't care, it's just a couple lines more.
This code also shows the name of the selected
OutputDevice. I my machine
HDA Intel (STAC92xx Analog).
In the following days I'll be posting more info about what comes out of this project. I will only reveal that right now the code has classes called Player,and Collection. It can scan a Collection from a path given in the command line and play all the files found there. Soon more features will come.
 I'm not planing to do anything about it... yet.
Satyr handles paths. There are some problems with paths and (sigh)
encondings. Of those, here are two: there's no way to know in which encoding
the filenames in a filesystem are enconded (f.i., there's no way to ask the
filesystem), and even if that were possible, the filenames might not even be
enconded in that enconding. In these (still!) transitioning times, lots and
lots and shitloads of filesystems are used in UTF-8 environments, but some
filenames are still in old ISO-8859-1 or whatever the system was using
QString. I'm taking a path from the command line; this path is
the location of the (right now only)
Collection for the player. I'm
handling the command line using
KCmdLineOptions, which returns
As we all know,
QString, just like the
unicode type in
all the data internally as Unicode, which is The Right Thing™. If you
really need the internal data, say as bytes, you can always call the
constData() method and be happy with it. This would be the case for paths;
you need the bytes.
PyQt4. For some reason, which maybe I will ask in the pyqt
constData() is not available. What to do? Well, that's what this post is
about. What you're about to read is hacky as it can be, but then it works. I
might feel dirty, but I can live with it. As long as I mark it as a
utter/über hack and promise to revert it once that's possible...
# path is a QString qba= QByteArray () qba.append (path) path= str (qba) # now path is a list of bytes/string.
Even if this part of the bug is fixed, then
Phonon.MediaObject.play() fails when feeded that same path with this
ERROR: backend <span class="createlink">MediaObject</span> reached <span class="createlink">ErrorState</span> after 1 . It seems a <span class="createlink">KioMediaStream</span> will not help here, trying anyway.
or simply refusing to continue. Sure, in my case I should simply ignore the filename and inform the user what's going on, but sometimes you can't be so gentle.
 but then it doesn't make much sense now since Phil wants to get rid of QString (for several reasons, which might most possibly include this one).
satyr got the possibility to queue songs for playing very early. At that
moment there wasn't any GUI, so the only way to (de)queue songs was via
dbus. Once the GUI was there, we had to provide a way to queue songs. As
satyr aims to be fully usable only using the keyboard, the obvious way was to
setup some shortcut for the action.
Qt and then
KDE have a very nice API for defining 'actions' that can be
fired by the user. The ways to fire them include a shortcut, a menu entry or a
button in a toolbar. I decided to go with the
According to [the
classKAction.html#_details), to create an action is just a matter of creating
the action and to add it to an
actionCollection(). The problem is that nowhere
it says where this collection comes from. There's the
but creating one and adding the actions to it seems to be not enough.
If you instead follow the
see that it refers to a
KXmlGuiWindow, which I revised when I was desperately
looking for the misterious
actionCollection(). I don't know why, but the
documentation generated by
PyKDE does not include that method. All in all, the
tutorial code works, so I just ported my
class MainWindow (KXmlGuiWindow): def __init__ (self, parent=None): KXmlGuiWindow.__init__ (self, parent) uipath= __file__[:__file__.rfind ('.')]+'.ui' UIMainWindow, _= uic.loadUiType (uipath) self.ui= UIMainWindow () self.ui.setupUi (self) [...] self.ac= KActionCollection (self) actions.create (self, self.actionCollection ())
def create (parent, ac): '''here be common actions for satyr skins''' queueAction= KAction (parent) queueAction.setShortcut (Qt.CTRL + Qt.Key_Q) ac.addAction ("queue", queueAction) # FIXME? if we don't do this then the skin will be able to choose whether it # wants the action or not. with this it will still be able to do it, but via # implementing empty slots queueAction.triggered.connect (parent.queue)
Very simple, really. But then the action doesn't work! I tried different approaches, but none worked.
The tutorial I mentioned talks about some capabilities of the
one of them, the ability to have the inclusion of actions in menues and
toolbars dumped and loaded from XML file (hence, the XML part of the class
name), and that is handled by the
setupGUI() method. From its documentation:
«Configures the current windows and its actions in the typical
[...] Typically this function replaces
createGUI()». In my case the GUI is
already built by that
self.ui.setupUi() that you see up there, so I ignored
this method. But the thing is that if you don't call this method, the actions
will not be properly hooked, so they don't work; hence, my problem. But just
calling it make the actions work! I'll check later what's the magic behind this
method. For now just adding
self.setupGUI() at the end of the
So, that's it with actions. As a result I also get several things: a populated menu bar with Settings and Help options (but no File; that'll come later with standard actions, of which I'll talk about later, I think), with free report bug, about satyr and configure shortcuts options, among others. The later does work but its state is not saved. That also will come in the same post that standard actions.
PD: First post from my new internet connection. Will
satyr suffer from this?
Only time will tell...
 then you had to pick the index of the songs either by guessing or looking at the dump of the playlist.
More than two months ago I globed about QStrings and
The problem was this: my app accepts paths via command line, which are
KCmdLineOptions; which in turn converts everything to
QStrings. What I wanted were paths, which are more like
QStrings (because the latter have internally an unicode representation;
more on that later). Including
PyQt4 in the equation forced me to resort
QByteArray to get the path as a
str instead of using
PyQt4 doesn't export that function). But that's
only the beginning of the problem.
Take for instance this situation. I have a music collection that I've been
building for years now (more that 10, I think). In the old times of this
collection the filenames were encoded in
iso-8859-1. Then the future came
and converted all my machines to
utf-8. But only the software; the
filesystems were in one way or another inherited from system to system, from
machine to machine. So I ended with a mixture of utf and iso filenames, to
the point where I have a file whose filename is in iso, but the directory
where it is is in utf. Yes, I know, it is a mess. But if I take any decent
media player, I can play the file allright. That's because the filesystem
knows nothing of encodings (otherwise it would reject badly encoded
I just spent last saturday making sure that
satyr only stored filepaths in
QStrings. It took concentration, but having just
a bunch of classes and only 3 or 4 points where the filepaths are managed it
wasn't that difficult. Still, it took a day. But then, as I mentioned in
Phonon the is not able to play such files... or so I thought.
If you run
satyr after executing
export PHONON_XINE_DEBUG=1 you'll see a
Phonon debug info in the console (not that there is another way to
satyr right now anyways). Among all that info you'll see lines such as
void Phonon::Xine::XineStream::setMrl(const QByteArray&, Phonon::Xine::XineStream::StateForNewMrl) ... bool Phonon::Xine::XineStream::xineOpen(Phonon::State) xine_open succeeded for m_mrl = ...
If you're sharp enough (I'm not; sandsmark from
#phonon had to tell me)
you'll note the mention of MRL's. MRL's are
xine's URL for media. As any
URL, they can (and most of the time must) encode 'strange' characters with
the so-called "percent encoding". This means that no matter what encodings
the different parts of a filepath is in, I just add
file:// at the
beginning and then I can safely encode it scaping non-ascii characters to
%xx representations... or that's what the theory says. One thing to note is
file:// part must not be scaped;
xine complains that the file
does not exist in that case.
Looking for help in
Qt's classes one can find
QUrl and the already known
QByteArray. I can call
QByteArray.toPercentEnconding() from my
feed that to
QUrl.fromPercentEncoding() (which strangely returns a
QString, which is exactly what we're avoiding) or
But then the first function encodes too much, replacing
%3A%2F%2F. No fun.
Ok, let's try creating a
QByteArray with only the
file:// and then
toPercentEncoding() of the path only. It works:
But then calling
<span class="createlink">PyQt4</span>.QtCore.QUrl("file://xn--/home/mdione.../ltimo bondi a finisterre/07- la pequea novia del carioca-wkmz60758d.wav")
The URL got somehow puny-encoded,
which of course
xine doesn't recognize for local files.
Another option is to create an empty
QUrl.StrictMode so we avoid 50 lines of code that start
that try to escape everything all over again (and I already had some
double-or-even-triple-enconding nightmares parsing RSS/Atom feeds last year,
thank you), but we get puny-encoded again (maybe it is 'pwny-encoded'?).
Last resort: backtrack to the point were we created only one
with the path and call
toPercentEncoding(); feed that to the method
setEncodedPath() of an empty
QUrl. Then we add the last piece calling
setScheme('file') and we're ready! Of course we're not:
Notice the lack of the two
xine doesn't like it;
hence, I don't either.
Ok, this post got too long. I hope I can resolve this soon, I already spent too much time on it. At least a good part of it was expaining it, so others don't have to suffer the same as I did.
satyr will shortly be released, whether I fix this bug or not.
 Look at the size of that file! 6k lines to handle URL's! Who would say it was so difficult... Once more I'm remembered of how lucky I am to have this libraries at the tips of my fingers, yay!
I certainly hope this is the last post in the
Phonon-and-badly-encoded/mixed-encodings-filenames saga, but I know is just
wishful thinking: as all encoding-related problems they never really dissapear,
it's just that you hadn't hit the right wrong stone yet. In any case, I fixed
all my later problems wherever they where, and now I can answer this question:
how to play files whose filenames are badly encoded and/or have mixed
encodings, all this in
Right now the answer is: you have to provide a properly encoded
QUrl. How, you
might ask, can I get one of those? Are they selled in the same odly-looking
places where you can buy cigarretes, or even marihuana? The answer, luckly,
is way more simple.
Putting together all the code I've been showing about
Phonon recently, it comes down to this:
# path is a str() qba= QByteArray (path) # the exceptions are not needed, # but is cleaner if you print the outcome of this qu= QUrl.fromEncoded (qba.toPercentEncoding ("/ ")) # this is needed by the gstreamer backend, # and the xine backend doesn't complain qu.setScheme ('file')
... and that's it. You can now create a
MediaSource with this
There are a couple of ideas that I want to express as conclusion to all this:
- In an ideal world these things should not happen. But this is one of the lesser problems with this non-ideal world, so bear with it.
- Paths should not be stored in
QStrings, even if they can (and they do) store this kind of pathnames, because if you try to 'encode' its contents (in the Unicode sense; that is, convert it to an encoding like UTF-8) you get farts or barks at best. Yes, you always have
QString's class reference there is no warranty that this will keep being the case.
- In fact,
QString's class reference says at some point: «[one case] where
QByteArrayis appropriate are when you need to store raw binary data...», and [as I already wrote](http://grulicueva.homelinux.net/~mdione/glob/posts/from-qstring-to-bytes- in-pyqt/), «[t]his would be the case for paths; you need the bytes».
QDircan only be created from
QStrings. I'm not sure if, given all I wrote, that's right.
The good news is that
satyr now can play any file that the backends can
whatever their filename-as-string-of-bytes is, I'm a little bit happier about
I got another contribution to
KDE and might even have to close a lot of bugs!
 That question is only legal in Nederlands and very few others cities in the planet.
 Actually is not legal. See [this wikipedia article](http://en.wikipedia.org/wiki/Drug_policy_of_the_Netherlands#Non- enforcement).
 I might pull up my sleeves again and fix that.
 You might have already know this, but if you not: you cannot print Unicode,
because Unicode is not and encoding. You have to encode it first. Hence, the
toUtf8() and similar
QString methods, and also the inverse
 Of course the equivalent C++ code also works, with
path being a
 And in the case of
PyQt4, that method is not even available. But [I
Continuing with the development of
Satyr, which doesn't have any GUI. I
thought it would be faster to make a
DBus interface that a good GUI, not
to mention more interesting.
The code snippet of today is this:
class DBusServer (dbus.service.Object): def __init__ (self): bus= dbus.SessionBus () bus_name= dbus.service.BusName ('org.kde.satyr.dbus.test', bus=bus) dbus.service.Object.__init__ (self, bus_name, "/server") @dbus.service.method('org.kde.satyr.dbus.test', in_signature='', out_signature='') def bing (self): print "bing!" @dbus.service.method('org.kde.satyr.dbus.test', in_signature='', out_signature='') def quit (self): app.quit () dbus.mainloop.qt.DBusQtMainLoop (set_as_default=True) dbs= DBusServer () sys.exit (app.exec_ ())
This simply defines a class which registers itself with the session bus
under the name
org.kde.satyr.dbus.test, exporting itself under the path
/server and then defining a method that goes bing! :) and another one
that quits the app. Note the decorator for the methods.
You might notice the
call. This is needed because both Qt and DBus in asyncronous mode (which is
the one we're using and the only one that works under Qt or Gtk, AFAIK) both
have their own event loops, and that makes some kind of magic that let both
loops coexist without blocking the other. This must be called before
connecting to the bus; otherwise, you get this error:
<span class="createlink">RuntimeError</span>: To make asynchronous calls, receive signals or export objects, D-Bus connections must be attached to a main loop by passing mainloop=... to the constructor or calling dbus.set_default_main_loop(...)
So, let's test the beast. We run the script in one terminal and in the other:
mdione@mustang:~/src/projects/satyr/live$ qdbus | grep satyr org.kde.satyr.py-10154 org.kde.satyr.dbus.test mdione@mustang:~/src/projects/satyr/live$ qdbus org.kde.satyr.dbus.test / /server mdione@mustang:~/src/projects/satyr/live$ qdbus org.kde.satyr.dbus.test /server method void org.kde.satyr.dbus.test.bing() method QString org.freedesktop.DBus.Introspectable.Introspect() mdione@mustang:~/src/projects/satyr/live$ qdbus org.kde.satyr.dbus.test /server org.kde.satyr.dbus.test.bing mdione@mustang:~/src/projects/satyr/live$ qdbus org.kde.satyr.dbus.test /server org.kde.satyr.dbus.test.quit
And in the other console:
mdione@mustang:~/src/projects/satyr/live$ python dbus_test.py bing!
It goes bing!... and then finishes. The next step is to make my
class to export its methods via
DBus and that's it!. More info in the
There's a problem with
Phonon. If you try to play a file in a format that
it doesn't support, instead of failing or simply sending the
signal it just does nothing. I'll have to look more deeply into this and
figure out if it's a bug or feature or how to work-around it.
For now I just decided to use
python-magic to decide if the file is
playable or not. This module uses
libmagic, the same library that the
file utility uses. But I hit a problem with it. One of its ussages is to
magic.file() with the filename, but if the filename contains a
non-ascii character you get something like this:
Traceback (most recent call last): File "satyr.py", line 173, in next self.play () File "satyr.py", line 128, in play mimetype_enc= self.magic.file (self.filename) <span class="createlink">UnicodeEncodeError</span>: 'ascii' codec can't encode character u'\xed' in position 37: ordinal not in range(128)
I found a workaround. There's another usage, which is to call
magic.buffer() with a piece of data to recognize. So I just simply
open() the file,
read() 4KiB and pass the data to that function.
f= file (self.filename) data= f.read (4096) mimetype_enc= self.magic.buffer (data)
Now it works properly. I will file a bug report to the
file package once I
resort out my mail setup (I cannot send bugs with