Posts about python (old posts, page 4)

2004-10-13 16:03

Adventures in Hi-Fi

As I blogged earlier I am writing a game (and yes, it's pretty much playable already).

One thing I didn't mention is that I never wrote a game before. Yeah, I know everyone does it as one of his first projects, but I never did.

So, there are some things I really have no clue about [1], like sound and moving graphics around.

For the graphics stuff, QCanvas is just fine and dandy, but to make things bloop and warble and squeak when the time is right, I found Qt's sound support somewhat depressing.

Come on, NAS? Who uses that? And what about music? I had no idea.

So, I started trying to follow one of my leading principles of development: find a way to make it Someone Else's Problem (TM).

The usual way to do that is finding a library that handles the problem, write minimal glue, stick it to the side of the program, tell the program that's his new arm, and forget about it quickly.

Here's what I found.

Mi Dios!

I thought I should start by adding one of those annoying little tunes every game has. It's just a game tune, I don't want to have to include a 3MB OGG file for it, so I wanted an instrument-based format.

I remembered MIDI tunes. You may know them as ringtones nowadays, but they used to be just cheesy tunes generated by your SBPro's FM generator, not your phone.

In fact, I remember having a little proggie called playmidi, that would do that in Linux.

Well, it seems that in the past few years, either sound cards have forgotten how to play them, they fell out of fashion, or something, because the only things I found that could play MIDI are monstrosities that require a 9MB digital instrument set. And how was I to include that along with my 25KB game???

So, what's next? I had a C64, so...

MOD me up!

MOD files are like MIDI files, only the MOD includes it's own instrument set, called samples, and instructions on how to repeat and alter those samples to make a tune.

Good news: there are nice-sounding, funny MOD files that are about 30KB in size.

Better news: There is a popular library to play them! It's called Mikmod, and your distro has it (and it's a dependency for KDE's multimedia packages too).

Even better news: It has support for playing simple sounds (samples in mod lingo) by calling a couple of functions.

Awesome news: It includes a software mixer so you can just tell it to play this, then play that, then that, and a tune in the background, and everything sounds at the same time.

So, we have a winner. This baby can handle everything I need for the game!

But... is that a snake in your pocket?

I can't find a Python binding for it. I am sure as soon as I post this article someone is going to come up and tell me, here they are, moron! But I just can't find any.

So, I decided to do something I wanted to do already and learn to use Pyrex. Pyrex is a tool to write python extensions, with almost-free access to C libraries, in an almost-python language (only minor syntax differences).

That way, I could write a Python module to use Mikmod.

You know what? It was almost scarily simple [2]. I didn't wrap all of Mikmod [3] because I don't need it, but now I can do stuff for games and apps almost trivially.

Even more: Pyrex has awesome distutils support, so building the extensions, usually a pain in the rear, is trivial (mostly you just copy and delete stuff, with some search and replace).

One thing I found I did nicely is this: Mikmod requires you to call Mikmod_Update every once in a while so it fills the soundcard's buffer with stuff to play. If you don't, it skips.

So, I just started a thread that loops and takes care of it. You don't even have to know about it to use the extension. Oh, sure, if your Mikmod is not threadsafe, it breaks. Well, get a decent Mikmod package, then.

How does it look?

Here's a whole noisy proggie

#Load the modules
import mikmod, time
#Init the library
mikmod.init()
#40 voices, 20 for music, 20 for random sounds (overkill)
mikmod.setNumVoices(20,20)
#Enable sound, starts the thread that pushes sound, too
mikmod.enableOutput()

#Create a module, that is, a music track
module=mikmod.Module("BasicInstinct.mod")

#Load two samples, just a couple of noises
s1=mikmod.Sample("lost.wav")
s2=mikmod.Sample("swap.wav")

#Start playing the song
module.play()


#For the duration of the song, each second, make some noise


while module.active():
        s1.play()
        time.sleep(0.5)
        s2.play()
        time.sleep(0.5)

#Close the mikmod library, stop the thread, etc.

mikmod.exit()
[1] As if that would surprise anyone!
[2] On the other hand... waiting for stuff to compile... how quaint.
[3] Actually, I am wrapping almost all of Mikmod, I am just not exposing it to Python because I don't need it.

2004-09-24 12:10

A good web-based password changer?

Does such a thing exist? There are dozens, but none seems very good.

By good I mean:

  • Has been maintained more recently than 4 years ago.
  • Works via PAM (and just plain works)
  • Is not awful to install
  • Doesn't make you do weird stuff like running a SUID httpd (yes, I actually saw that once)
  • secure (audited?)
  • readable sources
  • runs as a non-privileged user.

Usually this would be a SUID root cgi-bin, which is somewhat scary, and it would seem to me unnecessary.

Since the user will provide the current password, it should be possible for a non-privileged process to first switch to the desired user and then change the password, right?

Maybe someone can tell me. Or do I have to write it? I mean, it's going to be a python CGI if I do, and noone's gonna like it ;-)

2004-06-07 17:13

Good news, Bad news

Good news: It seems I have been accepted to teach a PyQt tutorial at akademy.

Bad news: a kitten I had adopted last monday died on saturday night. She was a sick kitten, and had a respiratory infection. Antibiotics helped for a few days, she looked very happy on friday, but on saturday she woke up weaker, and nothing I or the veterinarian did helped.

She died on my lap that night.

I am a grown up, and I had only had her for 5 days, so this shouldn't be important at all, but it is :-(

2004-06-03 10:22

There goes Captain Beto, through space!

Here is a new realtime PyQt tutorial. For those who have not seen the first one, here's the main idea:

  • I decide I want to write something
  • I write it (somewhat) quickly.
  • I write a description of what I am doing, as I am doing it, and slap some timestamps.

In this particular instance, on a discussion at the dot I had to open my mouth about how writing a spatial file manager is easy.

Well, here's a piece of one. In particular, it's a sort of simple spatial file browser since it doesn't manage files at all, but it's a start ;-)

2004-05-21 20:21

Qmail Address Lister

It has been way too long without posting a longer item, so... I recicled a script I wrote for a customer, and here it is:

A python script that lists (almost) all email addresses in a qmail system.

Also, a slightly tweaked CSS, thanks to Georg!

2004-05-09 15:42

Hard Python question

I am trying to do something which is, I think, pretty cool, in python.

However, I like showing things working, and I am having troubles with the last final step on what I am trying to achieve.

Since I know a few better python programmers read this...

Suppose I have this:

def fun(self,x):
        pass

class C:
        pass

C.a=fun
C.b=fun

What code should be in fun() so that it figures out if it has been called as C.a or as C.b?

I am thinking something like reading the higher step in a backtrace, but I don't know enough python to figure out how to do that.

2004-03-23 16:48

Dcoppython

In a whim, I checked out kdebindings/dcoppython from KDE's CVS.

I see the README: dcopython is broken

Then I said to myself: maybe I can fix it. And you know what? It seems to be not broken! :-)

At least for simple data types, that is.

dcoppython lets your python program become a DCOP server or client.

A DCOP server is capable of being controlled by KDE's kdcop, and is a very simple way to make your application externally scriptable.

A DCOP client is something that contacts a DCOP server, so that means you can control and script KDE applications (or other DCOP servers) from python scripts.

The neatest thing here is that this stuff doesn't require Qt!

I intend to use it to make some of my apps externally scriptable without PyKDE.

2004-03-08 11:51

Goats and cars

There's a problem often used to show the unintuitive nature of probability, which has become very well known.

In that problem a contestant in a gameshow has to choose between three doors (A,B,C), on one there is a car, on the other two are goats.

After the contestant chooses, the host opens another door and shows a goat.

Then, the host offers the contestant the chance to switch his closed door for the other closed door.

Should he switch?

The intuitive answer is "it doesn't matter", because there's two doors and one car, so it's a 50-50 chance.

But the real answer is that it does matter, because it's a 33-67 chance!

While it's simple to show this to be the case to a statistically-educated dude, it's somewhat harder for a layman.

In fact, I think most explanations suck.

Here's my shot at it:

If you were offered the chance to switch between your closed door and the other two closed doors, would you take it?

The intuitive answer to that is of course, yes, because it's 67-33 for the car to be on the other two doors.

Now, regardless of where the car is, can the host open one of those two doors and show a goat? Of course, yes.

So, would you feel your odds went down because the host showed one of your two closed doors had a goat behind it? No, because he could always do that, and you know there was (at least) one goat there!

So, what difference does it make if one door is open or not?

I don't expect this to convince anyone, really, but just in case, I have a python implementation of this problem (goatcar.py :-) if anyone wants it, if empiricism can convince you ;-)

Contents © 2000-2019 Roberto Alsina