albertz / music-player-core

Music player core Python module
BSD 2-Clause "Simplified" License
76 stars 22 forks source link

========================== Music player Python module

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:

.. code-block:: python

import musicplayer, sys, os, fnmatch, random, pprint, Tkinter

class Song:
    def __init__(self, fn):
        self.url = fn
        self.f = open(fn)
    # `__eq__` is used for the peek stream management
    def __eq__(self, other):
        return self.url == other.url
    # this is used by the player as the data interface
    def readPacket(self, bufSize):
        return self.f.read(bufSize)
    def seekRaw(self, offset, whence):
        r = self.f.seek(offset, whence)
        return self.f.tell()

files = []
def getFiles(path):
    for f in sorted(os.listdir(path), key=lambda k: random.random()):
        f = os.path.join(path, f)
        if os.path.isdir(f): getFiles(f) # recurse
        if len(files) > 1000: break # break if we have enough
        if fnmatch.fnmatch(f, '*.mp3'): files.append(f)
getFiles(os.path.expanduser("~/Music"))
random.shuffle(files) # shuffle some more

i = 0

def songs():
    global i, files
    while True:
        yield Song(files[i])
        i += 1
        if i >= len(files): i = 0

def peekSongs(n):
    nexti = i + 1
    if nexti >= len(files): nexti = 0
    return map(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()

def onSongChange(**kwargs): songLabel.set(pprint.pformat(player.curSongMetadata))
def cmdPlayPause(*args): player.playing = not player.playing
def cmdNext(*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 = onSongChange
player.playing = True # start playing
window.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.

It has the following functionality:

Usages

The main usage is probably in the MusicPlayer project <http://albertz.github.io/music-player/>_ - a full featured high-quality music player.

Installation

To get the source working, you need these requirements:

Debian/Ubuntu +++++++++++++

::

apt-get install python-dev libsnappy-dev libtool yasm libchromaprint-dev portaudio19-dev libboost-dev

FFmpeg:

apt-get install libavformat-dev libavresample-dev

If your FFmpeg in Debian/Ubuntu is too old (lacks libswresample), do::

add-apt-repository ppa:jon-severinsson/ffmpeg
apt-get update
apt-get install libavformat-dev libswresample-dev

or install it from source.

MacOSX ++++++

::

brew install boost
brew install portaudio
brew install ffmpeg
brew install chromaprint

Other notes +++++++++++

Chromaprint <http://acoustid.org/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).

The module is also registered on PyPI <https://pypi.python.org/pypi/musicplayer>_, so you can also install via::

pip install musicplayer

.. image:: https://travis-ci.org/albertz/music-player-core.png :target: https://travis-ci.org/albertz/music-player-core

Similar projects

Probably dead projects: