play mp3 files from python

A

Adam Endicott

I got all excited when iTunes was released for Windows. Then I was
quickly disappointed when I found out it only works for XP or 2000 (I'm
out of luck with 98 or ME). So I figured, hey, I've been wanting to write
my own python player anyway, why not make a PyTunes?

I hit my first stumbling block almost immediately. I can't seem to find
an easy module that will let me play an mp3 file. I'm sure it must be out
there, but I can't figure it out (I'm a newbie to all but the most simple
Python scripts, and I know nothing about audio processing). About an hour
of searching kept turning up the same couple of things (PyGame and
mpg123).

PyGame - tried this, and got it working very easily. It played, but
sounded terrible. I'm using a sample script which I think came from
c.l.py:

mixer.init(44100)
mixer.music.load(filename)
mixer.music.play()
mixer.music.get_busy()

print "Press enter to quit."
raw_input()

Again, that works, but the sound is absolutely terrible. The file I'm
using to test gives me the following information in winamp (in case this
may be the reason).
209kbit (VBR), 7316 frames
44100Hz Joint Stereo
The PyGame documentation in this area seemed pretty sparse to me (as
someone who doesn't know anything about audio files). Is there a way to
tweak the sound?

mpg123 - I saw this referenced a number of times, but I don't know how to
use it. I've never used any C code from Python. Is there a Python wrapper
for this that I could use? I didn't see one, but I may have missed it.

Sorry for the long post, but my main question is, what's the easiest way
to play an mp3 file from Python on a windows system? If one of the above
is my best option, can someone help me with the next step to get it
working?

Thanks in advance for any help.
 
A

Adam Endicott

Adam,

I have just done the same thing. Here's a solution for you. Pymad org. code

#!/usr/bin/env python
import mad, ao, sys
mf = mad.MadFile(sys.argv[1])
dev = ao.AudioDevice('oss', rate=mf.samplerate())
while 1:
buf = mf.read()
if buf is None:
break
dev.play(buf, len(buf))

You need to get the pymad module, libmad mp3 decoder and the ao module. Do
a google and you will find them all.

Best of luck

Tim Ronning

Thank you for the quick reply. I found pymad, libmad, and ao, but I can't
get them installed. If I run 'python setup.py build' (or install) for
either pymad or ao, I just get "No 'Setup' file. Perhaps you need to run
the configure script.". So I run config_unix.py not expecting it to work,
and of course it doesn't. I'm on Windows ME. The website isn't much help
for clueless windows users like myself. I can't just put the .c and .h
files in site-packages right? I'm sure I must need *some* python code
somewhere.
 
T

Tim Ronning

I got all excited when iTunes was released for Windows. Then I was
quickly disappointed when I found out it only works for XP or 2000 (I'm
out of luck with 98 or ME). So I figured, hey, I've been wanting to write
my own python player anyway, why not make a PyTunes?

I hit my first stumbling block almost immediately. I can't seem to find
an easy module that will let me play an mp3 file. I'm sure it must be out
there, but I can't figure it out (I'm a newbie to all but the most simple

Adam,

I have just done the same thing. Here's a solution for you. Pymad org. code

#!/usr/bin/env python
import mad, ao, sys
mf = mad.MadFile(sys.argv[1])
dev = ao.AudioDevice('oss', rate=mf.samplerate())
while 1:
buf = mf.read()
if buf is None:
break
dev.play(buf, len(buf))

You need to get the pymad module, libmad mp3 decoder and the ao module. Do
a google and you will find them all.

Best of luck

Tim Ronning
 
J

JanC

Adam Endicott said:
I got all excited when iTunes was released for Windows. Then I was
quickly disappointed when I found out it only works for XP or 2000 (I'm
out of luck with 98 or ME). So I figured, hey, I've been wanting to write
my own python player anyway, why not make a PyTunes?

I hit my first stumbling block almost immediately. I can't seem to find
an easy module that will let me play an mp3 file. I'm sure it must be out
there, but I can't figure it out (I'm a newbie to all but the most simple
Python scripts, and I know nothing about audio processing).

Bookmarked but never tried this: <http://audiere.sourceforge.net/>
The "features" mention working on Windows and having Python bindings...
 
A

Anand Pillai

This question has been asked many times here before.

mpg123 is a pretty good player for linux and unix systems
.. Perhaps you can consider writing a python extension for it.
It would benefit the python community I think.

For information on how to write extensions consult the
python documentation, at http://www.python.org/doc/current

-Anand
 
P

Paul Foley

This question has been asked many times here before.
mpg123 is a pretty good player for linux and unix systems
. Perhaps you can consider writing a python extension for it.
It would benefit the python community I think.

Better yet, make something to play Ogg/Vorbis files from Python, if
that's what you want to do, and convert all your mp3s to Vorbis
(http://www.xiph.org)

Smaller files, higher quality, and no license fees.
 
C

Chris Liechti

(e-mail address removed) (Anand Pillai) wrote in

This question has been asked many times here before.

mpg123 is a pretty good player for linux and unix systems
. Perhaps you can consider writing a python extension for it.
It would benefit the python community I think.

ure use an existing one:
http://www.tf.hut.fi/~flu/cplay/

altough it uses curses and linux/posix features, probably no option for the
OP.

chris
 
C

Christos TZOTZIOY Georgiou

Better yet, make something to play Ogg/Vorbis files from Python, if
that's what you want to do, and convert all your mp3s to Vorbis
(http://www.xiph.org)

Smaller files, higher quality, and no license fees.

Let's start a fight over at hydrogenaudio. I'm using the following
attack: "Musepack beats Ogg Vorbis hands down."
Your move :)
 
J

JanC

Paul Foley said:
Better yet, make something to play Ogg/Vorbis files from Python, if
that's what you want to do, and convert all your mp3s to Vorbis

If you look at the link I gave, it says:

Audiere is a high-level audio API. It can play Ogg Vorbis, MP3, FLAC,
uncompressed WAV, AIFF, MOD, S3M, XM, and IT files. For audio output,
Audiere supports DirectSound or WinMM in Windows, OSS on Linux and Cygwin,
and SGI AL on IRIX.

and on the features page:

* Braindead easy API
* Supported file formats: Uncompressed WAV*, Uncompressed AIFF*, Ogg
Vorbis*, FLAC*, MP3, MOD, S3M, IT, XM (* supports seeking)
* Streaming and buffered audio
* Volume, pan, and pitch shift modification
* Flat tone, square wave, white noise, and pink noise generation
* Runtime enumeration of audio devices and supported file formats
* Custom file streams
* Python, Delphi, Java, XPCOM (JavaScript in Mozilla) bindings

If it works as advertised, this should do what you want... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top