[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] music module bindings for python



hi,

pygame has mod support through sdl_mixer.


On 5/24/07, Andrew Pape <andrew_pape@xxxxxxxxxxxx> wrote:

Hi Greg and others,

For the last few weeks I have been trying to get MODule format music to work
with Python. I have found a fair bit of MODule playing code written in other
languages, mostly c, c++, and assembler, but no code or bindings for Python,
except PySonic. I have tried to get PySonic to work with my linux box, but I
can't get it to work. I've heard that people have got it to work for linux,
but I've had no luck. Greg wrote below:

----- Original Message ----
From: Greg Ewing <greg.ewing@xxxxxxxxxxxxxxxx>

It has a setup.py file, so you should be able to compile
it with distutils instead of invoking gcc yourself.

 From the setup.py, on Unix systems it expects the
fmod distribution to be in a subdirectory called "fmod"
under the directory where the .pyx files etc are. Then
you should just be able to say

    python setup.py install

At least that's the way it looks like it's mean to work.
I don't have a Linux system handy to try it on right now.

If it can't find Python.h, check whether you have a
directory called /usr/include/pythonX.Y (where X.Y
is whatever Python version you're using) containing
Python.h and the other .h files that it uses. If not,
you may have to tracl down and install the appropriate
"developer" package for Python (it will be called something
like "python-X.Y-devel"). You shouldn't have to install
the complete Python source.

--

I have followed the advice above, but still got hundreds of compilation or
linking errors. Sorry I'm not specific about the errors, but I don't have
the technical expertise to get the installation working. I'm almost totally
new to Python, and so I'm not up to extending and embedding with Pyrex,
SWIG, etc. Has anyone got PySonic (and hence Fmod) to work under linux?

Another MODule player I know of is called MikMod, which is Open Source,
unlike FMod. I've searched the web for Python bindings to MikMod, and almost
had success. Specifically, I searched for "mikmod binding for python". I got
a result titled "Lateral Opinion: 13.10.2004".  The page's links are all
dead, but the author had the following to say:



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.






As you can see, the author has been successful in getting mikmod to work by
writing his own bindings for Python with pyrex. The problem is he hasn't
shown how he did it, and I can't find out because the links on the page have
all been removed. It'd be great to have the mikmod functionality available
in Python, as shown in the sample code above. Does anyone know how to do it?

There is a lot of great music modules available and I'm sure the pygame
community would benefit by having mikmod or fmod functionality available to
them, so hopefully someone can help.

Thanks in advance.

Andrew.

 ________________________________
 How would you spend $50,000 to create a more sustainable environment in
Australia? Go to Yahoo!7 Answers and share your idea.