RPG Music’N’SFX – How to filter using tags

I’ve been pondering over the same question for the best part of a day today. It is a very simple question, but difficult to answer, because it depends on habits.

My filtering system relies on tags for each file, i.e. every file has a potentially unlimited amount of tags you can add to it. But the big question I have is: how should I filter it?

There are two possibilities that I can see; Say you are searching for the tags “safe” and… Dunno “arabic”. Does that now mean that only the files that have “both” those tags should show up or should all files that have at least one of them show up…

Talking python code, for the former option, I would imagine that I would have a dictionary of the tags as keys and lists of filepaths. These would feed into a set as a tracklist pool a bit like the following:

tag_dict = {"safe" : ["an/example/file.mp3", "another/file.wav"],
           "travel" : ["an/example/file.mp3",
                       "unrelated/other/file.ogg"]}

# this would lead to playlist with both tags present looking
# as follows:

playlist = set(["an/example/file.mp3",
                "another/file.wav",
               "unrelated/other/file.ogg"])

In the latter case, I would probably just have a dictionary of the filepath as the key and its tags as a list in the values. Then whenever another tag is added or removed, I would iterate over the dictionary and check if all tags are present in the list using set.intersection (That is something I learnt from here). If that is the case, it will be added to the playlist. A bit like so:

track_dict = {"an/example/file.mp3" : ["safe", "travel"],
              "another/file.wav" : ["safe"],
              "unrelated/other/file.ogg" : ["travel"]}
 
playlist = []

choices = set(["safe", "travel"])
# i.e. only the first should be in the playlist

for key in track_dict:
    tags = set(track_dict[key])
    intersect = list(tags.intersection(choices))
    if intersect == list(choices):
        playlist.append(key)

print playlist

My tendency, user-wise, goes towards the latter as it seems more logical to me, but I have noticed on several separate occasions that this means, in fact, nothing. My brain tends not realise the most “common” modus operandi.

I am tempted to try and implement both and give the user the choice…

Advertisements

Using Gstreamer with Python2.6 in Windows XP – Pt. 4 Incorporating it all into wxPython

(Original post from 01. Nov. 2011)

Hi folks, back again for the next round. This time all I will do, is just putting Gstreamer and wxPython together. Nothing fancy at all. This post is based on the GStreamer Tutorial with the difference that I will only use the playbin instead of the pipeline, simply because I could not find a suitable mp3 decoder available for Windows at the time of writing this. I am sure there are suitable ones out there, but my measly skills couldn’t make them work. I could have tried the python ctypes library to get the winLame DLLs to work, but it just didn’t seem worth the hassle with mp3play available…

Anyhow, so essentially I just inserted the code from the previous post into a simply GUI with wxPython. Anyone who knows wxPython and read the previous post, will not see anything new. So here is the code and we’ll talk about it afterwards.

NOTE: I used an XRC file to represent the GUI. This is personal preference and the files can be downloaded here.

#!usr/bin/python

import os
import urllib

import pygst
pygst.require("0.10")
import gst

import wx
from wx import xrc

class SimpleAudio(wx.App):
    """
    Class definition of the GUI front end and its functionality
    """

    def OnInit(self):
       
        # Load GUI info frm xrc file
        self.res = xrc.XmlResource('simpleAudio.xrc')
        self.init_frame()
        return True

    def init_frame(self):
       
        self.frame = self.res.LoadFrame(None, 'simple_audio')
        self.panel = xrc.XRCCTRL(self.frame, "main_panel")
       
        self.init_panel()
       
        self.frame.Show()
       
    def init_panel(self):
       
        # Gstreamer player
        self.player = gst.element_factory_make("playbin2", "player")
        fakesink = gst.element_factory_make("fakesink", "fakesink")
        self.player.set_property("video-sink", fakesink)
       
        self.path_txt = xrc.XRCCTRL(self.panel, "path_txt")
        self.play_btn = xrc.XRCCTRL(self.panel, "play_btn")
        self.open_btn = xrc.XRCCTRL(self.panel, "open_btn")
       
        self.panel.Bind(wx.EVT_BUTTON, self.on_play, id=xrc.XRCID('play_btn'))
        self.panel.Bind(wx.EVT_BUTTON, self.on_open, id=xrc.XRCID('open_btn'))
   
    def on_play(self, event):
        if self.play_btn.GetLabel() == "Play":
            filepath = self.path_txt.GetValue()
            path = urllib.pathname2url(filepath)
           
            if os.path.isfile(filepath):
                self.play_btn.SetLabel("Stop")
                self.player.set_property("uri", "file:" + path)
                self.player.set_state(gst.STATE_PLAYING)
        else:
            self.player.set_state(gst.STATE_NULL)
            self.play_btn.SetLabel("Play")
           
    def on_open(self, event):
       
        dlg = wx.FileDialog(self.frame, message="Open an ogg or wav file",
                    wildcard = "OGG, WAV Files (.ogg;.wav;)|*.ogg;*.wav;",
                    defaultDir=os.getcwd(),
                    defaultFile="", style=wx.OPEN)
       
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            self.path_txt.SetValue(path)
                         
        dlg.Destroy()

if __name__ == '__main__':
    app = SimpleAudio(False)
    app.MainLoop()

So, as you can see, we’ve essentially transferred parts of the GStreamer syntax into the frame initialisation, so that it is ready.

        ...
        # Gstreamer player
        self.player = gst.element_factory_make("playbin2", "player")
        fakesink = gst.element_factory_make("fakesink", "fakesink")
        self.player.set_property("video-sink", fakesink)
        ...

This is a direct copy and paste job. The only thing that is a little step further is in the function “on_play”:

    ...
    def on_play(self, event):
        if self.play_btn.GetLabel() == "Play":
            filepath = self.path_txt.GetValue()
            path = urllib.pathname2url(filepath)
           
            if os.path.isfile(filepath):
                self.play_btn.SetLabel("Stop")
                self.player.set_property("uri", "file:" + path)
                self.player.set_state(gst.STATE_PLAYING)
        else:
            self.player.set_state(gst.STATE_NULL)
            self.play_btn.SetLabel("Play")
    ... 

here you will find the second part of the code from the last post with the difference that we now also give the option of “stopping” the song. Yes it may seem outlandish to want to stop a song, but I incorporated it anyway for all those non-conventional types 😉
Summing the code up: We get the path from the text control, check if the path is valid, convert to a url format and play it. That’s it really. No big deal.
Well, if I feel like it, I will try and expand on this player and include a song progress bar and a playlist, just to show you more of it, but we’ll see. Not sure yet, to be honest.

Using Gstreamer with Python2.6 in Windows XP – Pt. 3 Playing Sound Files – Which Formats Work?

(Original post from 30. Oct. 2011)

Ok, so, the last time, I managed to write a very minimalistic script that gave off a static noise and nothing more. This time, I want to use the same core script, but manage to play a music file and while doing that test the waters for format compatibility. With open source libraries, there is always an issue with mp3, so I was not too hopeful.
So, here goes. This time, I will try to use the current GStreamer documentation (http://pygstdocs.berlios.de/pygst-tutorial/playbin.html ) and extract the basic functionality from the gtk clutter. What I came up with was the following:

import os
import urllib

import pygst
pygst.require("0.10")
import gst
import gobject
gobject.threads_init()

p = os.path.join("My", "path", "to", "my", "file")
path = urllib.pathname2url(p)

player = gst.element_factory_make("playbin2", "player")
fakesink = gst.element_factory_make("fakesink", "fakesink")
player.set_property("video-sink", fakesink)

player.set_property("uri", "file:" + path)
player.set_state(gst.STATE_PLAYING)

gobject.MainLoop().run()

Two things that have to be said about the above code.

[h3]1. Why urllib?[/h3]
I had a problem, feeding gstreamer a proper uri and I always had a horrible error

0:00:00.109375000 1472 00A55488 ERROR baesrc gstbasesrc.c:2974:gst_base_src_activate_pull:(source) Failed to start in pull mode

Converting the path to a url seemed to do the trick. I am assuming this has something to do with the way Windows marks paths as opposed to Linux.

[h3]2. Playbin2 – What’s that?[/h3]
Yea, that is from the tutorial. Apparently, just throw anything in and it tries to play it. If you want to only play Audio, you need to blank out the videosink with an in-build fakesink (as above).

Now when testing it, I checked the formats .ogg, .wav, and .mp3. Unfortunately, as I suspected, while ogg and wav work very well, mp3s are not played by gstreamer. There is Fluendo (http://www.fluendo.com/shop/product/fluendo-mp3-decoder/) but that does not supply for any Windows Versions… unfortunately. If anyone, finds a way to use gstreamer for mp3s, please let me know.

As an alternative, I found mp3play ( http://code.google.com/p/mp3play/ ) , which looks very good and seems to be able to do all the things I want it to do.

The last thing I wanted to test was, whether gstreamer can play more than one file at the same time. This is essential to what I am trying to achieve in the long run. Building on the above code, I just repeated what I did and created two instances. This worked as wanted. If I have any problems later on, i am sure, I will find out in the process.

import os
import urllib

import pygst
pygst.require("0.10")
import gst
import gobject
gobject.threads_init()

# First audio
p1 = os.path.join("My", "path", "to", "my", "file")
path1 = urllib.pathname2url(p1)

player1 = gst.element_factory_make("playbin2", "player1")
fakesink1 = gst.element_factory_make("fakesink", "fakesink")
player1.set_property("video-sink", fakesink1)

player1.set_property("uri", "file:" + path1)
player1.set_state(gst.STATE_PLAYING)

# Second audio
p2 = os.path.join("My", "path", "to", "my", "other", "file")
path2 = urllib.pathname2url(p2)

player2 = gst.element_factory_make("playbin2", "player2")
fakesink2 = gst.element_factory_make("fakesink", "fakesink")
player2.set_property("video-sink", fakesink2)

player2.set_property("uri", "file:" + path2)
player2.set_state(gst.STATE_PLAYING)

gobject.MainLoop().run()

Using Gstreamer with Python2.6 in Windows XP – Pt. 2 A Minimal Example

(Original post from 30. Oct. 2011)

Right folks, so first things first. Let’s try and get something easy going. You know, nothing fancy. Just a simple output. No gui nothing. For that, as a starting point, I chose the tutorial from the following website:

http://www.jonobacon.org/2006/08/28/getting-started-with-gstreamer-with-python/

In this tutorial (feel free to browse, it’s good), the author writes a very good introduction to what GStreamer is all about and explains the mechanics (Sinks, Pipelines etc.)… Unfortunately for me, for Linux. He gives a script that plays an audible tone, just to test if everything is working fine. Of course, taking it over to Windows is an issue.
Here is the code as quoted on JonoBacon’s website (many thanks for it):

#!/usr/bin/python

import pygst
pygst.require("0.10")
import gst
import pygtk
import gtk

class Main:
    def __init__(self):
        self.pipeline = gst.Pipeline("mypipeline")

        self.audiotestsrc = gst.element_factory_make("audiotestsrc", "audio")
        self.pipeline.add(self.audiotestsrc)

        self.sink = gst.element_factory_make("alsasink", "sink")
        self.pipeline.add(self.sink)

        self.audiotestsrc.link(self.sink)

        self.pipeline.set_state(gst.STATE_PLAYING)

start=Main()
gtk.main()

If you try the same code as stated on the website, you will receive a

gst.ElementNotFoundError: alsasink

in windows. After some browsing the web and learning about the combination of cygwin and the gst-inspect and gst-launch commands (google it, if you want to know more), I finally found the answer. First though, here are the website, I had that were useful to me in this instance:

http://www.jonobacon.org/2006/08/28/getting-started-with-gstreamer-with-python/

http://gstreamer.freedesktop.org/data/doc/gstreamer/head/faq/html/chapter-using.html#using-sound

The problem lies with using “alsasink”. ALSA is Linux’ way of playing sound. The equivalent of DirectSound for Windows if you want. Therefore, you need to find the right sink, to play audio on Windows. Alsasink won’t cut it, as it quite clearly is made for Linux. After trying several sinks via trial and error (osssink, esdsink, artsdsink, etc.) and no joy, I tried cygwin and gst-inspect to figure out which one I had, but this was also not successful. Finally, after a while, I came across a totally unrelated site, that mentions “autoaudiosink” as the auto-detect for your sound. Replacing “alsasink” with “autoaudiosink” and voilá, the sound was there when the script was run. Just careful and don’t turn the volume up too high. I did after a million and two failed tries and it almost blew me off my chair :D.

One last thing that I wanted to address was the pseudo-dependency on gtk. I say, pseudo, because the grand majority of examples were given with pygtk and thus most people have used gtk with it. Again a little bit of searching around, I found that the need for a gtk.main() call, was simply because GStreamer uses the Gobject module and its threads (see among others, this forum post: http://ubuntuforums.org/showthread.php?t=1168455 ). Once I found that, I was able to remove the gtk and pygtk import and replace it with the gobject import and the code still ran.
Thus, to some things up, the minimal example that works for Windows is as follows:

#!/usr/bin/python

import pygst
pygst.require("0.10")
import gst

# replaces the call to gtk and pygtk
import gobject
gobject.threads_init()

class Main:
def __init__(self):
self.pipeline = gst.Pipeline("mypipeline")

self.audiotestsrc = gst.element_factory_make("audiotestsrc", "audio")
self.pipeline.add(self.audiotestsrc)

self.sink = gst.element_factory_make("autoaudiosink", "sink")
self.pipeline.add(self.sink)

self.audiotestsrc.link(self.sink)

self.pipeline.set_state(gst.STATE_PLAYING)

start=Main()
gobject.MainLoop().run() # instead of gtk.main()

Having this in hand, I will next look at playing a simple audio file and testing the scope, i.e. which common formats work and if I can play several at the same time. Since I was able to remove gtk and replace it with gobject, I can now use wxpython to serve as my GUI of choice.

RPG Music’N’SFX – The “Previous Song” Function

(Original post from 25.11.2011)

Another little bloglet about my soon-to-be oh so awesome RPG Audioplayer. This time, I have been wondering how to implement a useable previous function.

I decided to create a list of integers, where each integer represents an index of the playlist. Initially, this list will collect the previous indices of the list, e.g. a list of 40 songs. It starts at 0 and goes regularly and playing the first 5 songs, i.e. the list will be [0,1,2,3,4]. Then it is switched to random and songs number 34 and 27 are played, leaving the list at [0,1,2,3,4,33,26]. This will go on until a certain threshold length (say 20 items). Once it reaches length 20, the first item will be removed and the latest one will be appended to the end. That way, this list will not reach ridiculous lengths when used for a long time.

When someone moves backwards, the new index will be the last item in the list, which will then be removed from the list. If someone then truly presses 20 times previous, then the code should just check for an empty list and stay at its current index.

How the audiere code is introduced with first resetting and only if x milliseconds were run before allowing a previous to be called, remains to be seen, but we’ll cross that bridge when we come to it.

So talking in code all the above operations should be standard python fare. Let’s try it:

l = range(0,5)
for i in range(15):
    l.append(random.choice(xrange(41)))

# example l is:
# [0, 1, 2, 3, 4, 14, 17, 33, 12, 20, 16, 7, 28, 
# 38, 24, 18, 29, 5, 36]

# Another index is added
l.pop(0)
l.append(nex_index)

# New list is:
# [1, 2, 3, 4, 14, 17, 33, 12, 20, 16, 7, 28, 
# 38, 24, 18, 29, 5, 36, new_index]

# Someone presses "Previous"
if l = []: # when someone presses previous 20 times
    new_index = index
else:
    new_index = l.pop() # removes last list item

I’m a little surprised myself now, that I only had to use the list.pop() method, but indeed using it, should solve all the problems outlined here.

I shall now implement them and see if it works 😀

RPG Music’N’SFX – Handling AudioFile in AudioPlayer

(Original post from 25. Dec. 2011)

I’ve had a fundamental, little problem on my route to a successful audioplayer. I wasn’t sure how to let my AudioFile class interact with my AudioPlayer class.

I use the pyaudiere library and as a small example this is how you would play an audiofile with pyaudiere:

# Start a device with which you can play more than one file
device = audiere.open_device()

# Load a sound file into memory, then play it looped:
l = d.open_file('/path/to/your/audiofile', True) # True for streaming
l.play()

# Note, I always use True for streaming because when using
# an ogg file without streaming, a blue screen occurs on my PC.

The main problem was that, if I create an instance of AudioFile on initialisation of my AudioPlayer class, then I would have to provide a valid path to an audiofile which would not allow for having an empty track folder (No files in the folder => no valid paths => Error on initialisation).

However, if I choose to initiate an AudioFile instance outside AudioPlayer, I had no control over the instance from the AudioPlayer class which is rubbish, too.

As a consequence, I decided to create an attribute called

When pressing any of the controls, I would call the following method:

# Avoids NoneTypeError when calling NoneType.stop()
if self.track.__class__.__name__ == "AudioFile":

That way, I managed to avoid the invalid path problem at the beginning and also sidestep the control problem. I know, it isn’t the most elegant way of dealing with this, but it is good enough for now…