-
-
Save bazo/df0a7926816058801f538581d83bdc54 to your computer and use it in GitHub Desktop.
Example audio fingerprint server using Chromaprint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import chromaprint | |
from flask import Flask, request, jsonify | |
SQL_TABLES = """ | |
CREATE TABLE IF NOT EXISTS fingerprint | |
( | |
id INTEGER PRIMARY KEY, | |
fingerprint TEXT NOT NULL | |
); | |
CREATE TABLE IF NOT EXISTS fingerprint_meta | |
( | |
fingerprint_id NOT NULL REFERENCES fingerprint (id) ON DELETE CASCADE, | |
name TEXT NOT NULL, | |
value TEXT NOT NULL, | |
PRIMARY KEY (fingerprint_id, name) | |
); | |
CREATE TABLE IF NOT EXISTS fingerprint_index | |
( | |
fingerprint_id NOT NULL REFERENCES fingerprint (id) ON DELETE CASCADE, | |
hash INTEGER NOT NULL, | |
PRIMARY KEY (fingerprint_id, name) | |
); | |
CREATE INDEX IF NOT EXISTS fingerprint_index_idx_hash ON fingerprint_index (hash); | |
""" | |
class Server(object): | |
def __init__(self): | |
self.db = None | |
def prepare(self): | |
if self.db is not None: | |
return | |
self.db = sqlite3.connect('fingerprints.db') | |
self.db.executescript(SQL_TABLES) | |
def reset(self): | |
self.db.execute("DELETE FROM fingerprint_hash") | |
self.db.execute("DELETE FROM fingerprint_meta") | |
self.db.execute("DELETE FROM fingerprint") | |
def submit(self, fingerprint, meta): | |
fingerprint_version, raw_fingerprint = chromaprint.decode_fingerprint(fingerprint) | |
cursor = self.db.cursor() | |
cursor.execute("INSERT INTO fingerprint (fingerprint) VALUES (?)", (fingerprint,)) | |
fingerprint_id = cursor.lastrowid | |
cursor.executemany("INSERT INTO fingerprint_index (fingerprint_id, hash) VALUES (?, ?)", | |
((fingerprint_id, hash) for hash in raw_fingerprint)) | |
cursor.executemany("INSERT INTO fingerprint_meta (fingerprint_id, name, value) VALUES (?, ?, ?)", | |
((fingerprint_id, name, value) for name, value in meta.iteritems())) | |
def lookup(self): | |
fingerprint_version, raw_fingerprint = chromaprint.decode_fingerprint(fingerprint) | |
cursor = self.db.cursor() | |
cursor.execute(""" | |
SELECT fingerprint_id, count(*) FROM fingerprint_index | |
WHERE hash IN (""" + ",".join((str(hash) for hash in raw_fingerprint)) + """) | |
GROUP BY fingerprint_id | |
""") | |
candidates = dict((row[0], row[1]) for row in cursor) | |
return candidates | |
app = Flask(__name__) | |
server = Server() | |
server.prepare() | |
app.route('/reset') | |
def reset() | |
with server.db: | |
server.reset() | |
return jsonify({'status': 'ok'}) | |
app.route('/submit') | |
def submit(): | |
fingerprint = request.args['fingerprint'] | |
with server.db: | |
pass | |
return jsonify({'status': 'ok'}) | |
app.route('/lookup') | |
def lookup() | |
fingerprint = request.args['fingerprint'] | |
return jsonify(server.lookup(fingerprint)) | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment