Package: pylastfp
This is a Python interface to Last.fm’s acoustic fingerprinting library (calledfplib) and its related API services. It performs fingerprint extraction,
fingerprint ID lookup, and track metadata lookup.InstallationTo install, you will need a compiler and the dependencies required by fplib
itself:fftw(compiled for single-precision floats) andlibsamplerate.
(On Debian/Ubuntu, the packages arelibfftw3-devandlibsamplerate0-dev.)Once you have these, you can easily install from PyPI usingpip:$ pip install pylastfpOr, if you don’t have pip (or easy_install), head to thedownload page.
The normal install command should work:$ python setup.py installTo build from the version control source (i.e., not from a release
tarball), you will also needCython. (The source distributions include
the generated C++ file, avoiding the need for Cython. This package’ssetup.pyplays tricks to detect whether you have Cython installed.)This library also depends onaudioreadto decode audio, although this
dependency is technically optional. If you already have a mechanism for decoding
audio files, there is no need to install audioread.RunningYou can run the included fingerprinter/lookup script,lastmatch.py,
to test your installation:$ lastmatch.py mysterious_music.mp3This will show metadata matches from Last.fm’s database. The script usesaudioreadto decode music, so it should transparently use a media library
available on your system (GStreamer, FFmpeg, MAD, or Core Audio on Mac OS X).Using in Your CodeThe script exhibits the usual way to use pylastfp, which is this:>>> import lastfp
>>> xml = lastfp.match_file(apikey, path)
>>> matches = lastfp.parse_metadata(xml)
>>> print matches[0]['artist'], '-', matches[0]['title']
The National - Fake EmprireThis example uses thematch_fileconvenience function, which usesaudioreadto decode audio data. The function imports theaudioreadmodule
when called, so if you don’t want to depend on that, just don’t call this
function.If you have your own way of decoding audio, you can use the lower-level
interface:>>> xml = lastfp.match(apikey, pcmdata, samplerate, time_in_secs)Of course, you’ll need a PCM stream for the audio you want to
fingerprint. The pcmdata parameter must be an iterable of Pythonstrorbufferobjects containing PCM data as arrays of Cshort(16-bit integer) values.Both functions (matchandmatch_file) accept an additional optional
parameter calledmetadata. It should be a dict containing your current guess
at the file’s metadata. Last.fm might use this information to improve their
database. The dict should use these keys (all of which are optional):"artist","album", and"track".The module internally performs thread-safe API limiting to 5 queries per
second, in accordance withLast.fm’s API TOS.To-DoThe fingerprinting library allows for an optimization that skips decoding
a few milliseconds at the beginning of every file. (SeeFingerprintExtractor::getToSkipMs(), as demonstrated by theexample client.) Taking advantage of this will complicate the module’s
interface a bit because the decoding source will need to know the amount of
time to skip.Version History0.6Useaudioreadinstead of the includedpygstandpymaddecoders.0.5Handle empty responses from the API.setup.pynow searches theHomebrewuser-local prefix.0.4Fix cleanup bug in gstdec that was causing files to remain open.0.3Fix typo in handling of HTTP errors.
Handle cases when HTTP status line is malformed.0.2Fix a horrible memory leak.
Fail safely when file is too short.
Safely handle malformed XML returned from the API.
Handle and expose HTTP failures.0.1Initial release.CreditsThis library is byAdrian Sampson. It includes the fplib source code, which
is byLast.fm. fplib is licensed under the LGPLv3, so pylastfp uses the same
license. pylastfp was written to be used withbeets, which you should
probably check out.