/usr/share/pyshared/chromaprint.py is in python-acoustid 1.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | # Copyright (C) 2011 Lukas Lalinsky
# (Minor modifications by Adrian Sampson.)
# Distributed under the MIT license, see the LICENSE file for details.
"""Low-level ctypes wrapper from the chromaprint library."""
import sys
import ctypes
# Find the base library and declare prototypes.
def _guess_lib_name():
if sys.platform == 'darwin':
return ('libchromaprint.0.dylib',)
elif sys.platform == 'win32':
return ('chromaprint.dll', 'libchromaprint.dll')
elif sys.platform == 'cygwin':
return ('libchromaprint.dll.a', 'cygchromaprint-0.dll')
return ('libchromaprint.so.0',)
for name in _guess_lib_name():
try:
_libchromaprint = ctypes.cdll.LoadLibrary(name)
break
except OSError:
pass
else:
raise ImportError("couldn't find libchromaprint")
_libchromaprint.chromaprint_get_version.argtypes = ()
_libchromaprint.chromaprint_get_version.restype = ctypes.c_char_p
_libchromaprint.chromaprint_new.argtypes = (ctypes.c_int,)
_libchromaprint.chromaprint_new.restype = ctypes.c_void_p
_libchromaprint.chromaprint_free.argtypes = (ctypes.c_void_p,)
_libchromaprint.chromaprint_free.restype = None
_libchromaprint.chromaprint_start.argtypes = \
(ctypes.c_void_p, ctypes.c_int, ctypes.c_int)
_libchromaprint.chromaprint_start.restype = ctypes.c_int
_libchromaprint.chromaprint_feed.argtypes = \
(ctypes.c_void_p, ctypes.POINTER(ctypes.c_char), ctypes.c_int)
_libchromaprint.chromaprint_feed.restype = ctypes.c_int
_libchromaprint.chromaprint_finish.argtypes = (ctypes.c_void_p,)
_libchromaprint.chromaprint_finish.restype = ctypes.c_int
_libchromaprint.chromaprint_get_fingerprint.argtypes = \
(ctypes.c_void_p, ctypes.POINTER(ctypes.c_char_p))
_libchromaprint.chromaprint_get_fingerprint.restype = ctypes.c_int
_libchromaprint.chromaprint_decode_fingerprint.argtypes = \
(ctypes.POINTER(ctypes.c_char), ctypes.c_int,
ctypes.POINTER(ctypes.POINTER(ctypes.c_int32)),
ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int), ctypes.c_int)
_libchromaprint.chromaprint_decode_fingerprint.restype = ctypes.c_int
_libchromaprint.chromaprint_encode_fingerprint.argtypes = \
(ctypes.POINTER(ctypes.c_int32), ctypes.c_int, ctypes.c_int,
ctypes.POINTER(ctypes.POINTER(ctypes.c_char)),
ctypes.POINTER(ctypes.c_int), ctypes.c_int)
_libchromaprint.chromaprint_encode_fingerprint.restype = ctypes.c_int
_libchromaprint.chromaprint_dealloc.argtypes = (ctypes.c_void_p,)
_libchromaprint.chromaprint_dealloc.restype = None
# Main interface.
class FingerprintError(Exception):
"""Raised when a call to the underlying library fails."""
def _check(res):
"""Check the result of a library call, raising an error if the call
failed.
"""
if res != 1:
raise FingerprintError()
class Fingerprinter(object):
ALGORITHM_TEST1 = 0
ALGORITHM_TEST2 = 1
ALGORITHM_TEST3 = 2
ALGORITHM_DEFAULT = ALGORITHM_TEST2
def __init__(self, algorithm=ALGORITHM_DEFAULT):
self._ctx = _libchromaprint.chromaprint_new(algorithm)
def __del__(self):
_libchromaprint.chromaprint_free(self._ctx)
del self._ctx
def start(self, sample_rate, num_channels):
"""Initialize the fingerprinter with the given audio parameters.
"""
_check(_libchromaprint.chromaprint_start(
self._ctx, sample_rate, num_channels
))
def feed(self, data):
"""Send raw PCM audio data to the fingerprinter. Data may be
either a bytestring or a buffer object.
"""
if isinstance(data, buffer):
data = str(data)
elif not isinstance(data, str):
raise TypeError('data must be str or buffer')
_check(_libchromaprint.chromaprint_feed(
self._ctx, data, len(data) // 2
))
def finish(self):
"""Finish the fingerprint generation process and retrieve the
resulting fignerprint as a bytestring.
"""
_check(_libchromaprint.chromaprint_finish(self._ctx))
fingerprint_ptr = ctypes.c_char_p()
_check(_libchromaprint.chromaprint_get_fingerprint(
self._ctx, ctypes.byref(fingerprint_ptr)
))
fingerprint = fingerprint_ptr.value
_libchromaprint.chromaprint_dealloc(fingerprint_ptr)
return fingerprint
def decode_fingerprint(data, base64=True):
result_ptr = ctypes.POINTER(ctypes.c_int32)()
result_size = ctypes.c_int()
algorithm = ctypes.c_int()
_check(_libchromaprint.chromaprint_decode_fingerprint(
data, len(data), ctypes.byref(result_ptr), ctypes.byref(result_size),
ctypes.byref(algorithm), 1 if base64 else 0
))
result = result_ptr[:result_size.value]
_libchromaprint.chromaprint_dealloc(result_ptr)
return result, algorithm.value
def encode_fingerprint(fingerprint, algorithm, base64=True):
fp_array = (ctypes.c_int * len(fingerprint))()
for i in range(len(fingerprint)):
fp_array[i] = fingerprint[i]
result_ptr = ctypes.POINTER(ctypes.c_char)()
result_size = ctypes.c_int()
_check(_libchromaprint.chromaprint_encode_fingerprint(
fp_array, len(fingerprint), algorithm, ctypes.byref(result_ptr),
ctypes.byref(result_size), 1 if base64 else 0
))
result = result_ptr[:result_size.value]
_libchromaprint.chromaprint_dealloc(result_ptr)
return result
|