This Python module provides a high-level core Music player interface where you are supposed to provide all the remaining high-level logic like the user interface, the playlist logic and the audio data.

Example

A very simple player with gapless playback:

importmusicplayer,sys,os,fnmatch,random,pprint,TkinterclassSong:def__init__(self,fn):self.url=fnself.f=open(fn)# `__eq__` is used for the peek stream managementdef__eq__(self,other):returnself.url==other.url# this is used by the player as the data interfacedefreadPacket(self,bufSize):returnself.f.read(bufSize)defseekRaw(self,offset,whence):r=self.f.seek(offset,whence)returnself.f.tell()files=[]defgetFiles(path):forfinsorted(os.listdir(path),key=lambdak:random.random()):f=os.path.join(path,f)ifos.path.isdir(f):getFiles(f)# recurseiflen(files)>1000:break# break if we have enoughiffnmatch.fnmatch(f,'*.mp3'):files.append(f)getFiles(os.path.expanduser("~/Music"))random.shuffle(files)# shuffle some morei=0defsongs():globali,fileswhileTrue:yieldSong(files[i])i+=1ifi>=len(files):i=0defpeekSongs(n):nexti=i+1ifnexti>=len(files):nexti=0returnmap(Song,(files[nexti:]+files[:nexti])[:n])# Create our Music Player.player=musicplayer.createPlayer()player.outSamplerate=96000# support high quality :)player.queue=songs()player.peekQueue=peekSongs# Setup a simple GUI.window=Tkinter.Tk()window.title("Music Player")songLabel=Tkinter.StringVar()defonSongChange(**kwargs):songLabel.set(pprint.pformat(player.curSongMetadata))defcmdPlayPause(*args):player.playing=notplayer.playingdefcmdNext(*args):player.nextSong()Tkinter.Label(window,textvariable=songLabel).pack()Tkinter.Button(window,text="Play/Pause",command=cmdPlayPause).pack()Tkinter.Button(window,text="Next",command=cmdNext).pack()player.onSongChange=onSongChangeplayer.playing=True# start playingwindow.mainloop()

Description

It provides a player object which represents the player. It needs a generator player.queue which yields Song objects which provide a way to read file data and seek in the file. See the source code for further detailed reference.

MacOSX

Other notes

Chromaprint depends on FFmpeg, so if you have a custom FFmpeg install, you might also want to install that manually. ./configure && make && sudo make install should work for FFmpeg and PortAudio. You might also want to use --enable-shared for FFmpeg. cmake . && sudo make install for Chromaprint.)

Building

Then call python setup.py build or ./compile.py to build the Python modules (it will build the Python module musicplayer.so).

Similar projects

PyFFmpeg. LGPL. FFmpeg wrapper. Thus, prettylow-level and no sound output. You could probably glue PyFFmpeg and PyAudio together for something useful but I expect it to be quite unstable and too slow. Basically, tis glue is done in C++ in this module.

GStreamer Python Bindings. GStreamer is powerful but still too limited as a cross-platform music player backend solution. Quite heavy. That was my intuition. Maybe it’s wrong and it would have been a perfect solution. But I think, in contrast, this module does a lot of things in a more compact and automatic/simpler way and at the same time provides more music player centric features.

Beets. In its core, it is a music library manager and manages the metadata. It can calculate ReplayGain and AcoustID fingerprint. Via BPD plugin, it becomes a MPD compatible daemon player, based on GStreamer.

Probably dead projects:

PyMedia. LGPL, GPL. FFmpeg-based encoding/decoding of audio+video, sound input/output via OSS/Waveout/Wavein. Unfornutaley not well tuned for usage in a high-quality music player. Last update from 2006.