Mercurial > hg > audiodb
comparison bindings/python/examples/adbserver.py @ 733:4c89dccdd4a9
Initial commit of adbserver: combines metadata, adb search, playback
author | mas01mj |
---|---|
date | Wed, 15 Sep 2010 11:05:12 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
732:3a0b9700b3d2 | 733:4c89dccdd4a9 |
---|---|
1 #!/usr/bin/python | |
2 | |
3 import sqlite3 | |
4 import csv | |
5 import sys | |
6 import urllib | |
7 import web | |
8 from pyadb import * | |
9 import simplejson as json | |
10 import tempfile | |
11 import subprocess | |
12 | |
13 # For now | |
14 | |
15 featureFile = "AWAL2010.adb" | |
16 trackFile = "awal_id32.csv" | |
17 audioDir = "/home/mjewell/Music/mp3s/" | |
18 | |
19 urls = ( | |
20 '/track/(.*)', 'trackSearch', | |
21 '/audio/(.*)', 'audioPlay', | |
22 '/search/(.*)', 'segmentSearch', | |
23 '/crossdomain.xml', 'crossDomain' | |
24 ) | |
25 | |
26 app = web.application(urls, globals()) | |
27 db = Pyadb(path = featureFile, mode = "r") | |
28 dbfile = tempfile.NamedTemporaryFile(suffix = ".db") | |
29 mdb = web.database(dbn='sqlite', db="metadata.db") | |
30 | |
31 | |
32 | |
33 def buildDatabase(csvfile): | |
34 print "Build DB" | |
35 mdb._db_cursor().connection.text_factory=str | |
36 mdb._db_cursor().connection.execute('DROP TABLE IF EXISTS media') | |
37 mdb._db_cursor().connection.execute('CREATE TABLE media (uid TEXT NOT NULL PRIMARY KEY, artist TEXT, album TEXT, track TEXT, tracknum INTEGER, filename TEXT, seconds INTEGER)') | |
38 | |
39 trackReader = csv.reader(open(csvfile)) | |
40 head = True | |
41 for row in trackReader: | |
42 if head: | |
43 head = False | |
44 continue | |
45 mdb.insert('media', uid=row[0], filename=row[1], artist=row[2], track=row[3], album=row[4], tracknum=row[5], seconds=row[6]) | |
46 | |
47 | |
48 def retrieveTrackData(trackID): | |
49 results = mdb.select('media', dict(uid=trackID), where='uid = $uid') | |
50 res = dict() | |
51 try: | |
52 result = results[0] | |
53 except IndexError: | |
54 return False | |
55 for key in result.keys(): | |
56 res[key] = result[key] | |
57 | |
58 return res | |
59 | |
60 class trackSearch: | |
61 def GET(self, trackID): | |
62 data = retrieveTrackData(trackID) | |
63 if data: | |
64 return json.dumps(dict(status = "ok", data = data)) | |
65 else: | |
66 return json.dumps(dict(status = "error", message=str("Invalid key"))) | |
67 | |
68 class crossDomain: | |
69 def GET(self): | |
70 return """<?xml version="1.0"?> | |
71 <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> | |
72 <cross-domain-policy> | |
73 <allow-access-from domain="*" /> | |
74 </cross-domain-policy> """ | |
75 | |
76 class audioPlay: | |
77 def GET(self, trackID): | |
78 web.header("Content-Type", "audio/mpeg") | |
79 user_data = web.input() | |
80 track_data = retrieveTrackData(trackID) | |
81 tempMp3 = tempfile.NamedTemporaryFile(suffix = ".mp3") | |
82 tempWav = tempfile.NamedTemporaryFile(suffix = ".wav") | |
83 subprocess.call(["sox", audioDir+track_data['filename'], tempWav.name, "trim", user_data['start'], user_data['length'], "fade", "0.3", user_data['length']]) | |
84 subprocess.call(["lame", "--quiet", tempWav.name, tempMp3.name]) | |
85 | |
86 return tempMp3.read() | |
87 | |
88 class segmentSearch: | |
89 def GET(self, trackID): | |
90 params = web.input(db="AWAL",key="", ntracks=20, seqStart=0, seqLength=16, npoints=1, radius=1.0, hopSize=1, exhaustive=False, falsePositives=False, accumulation="track", distance="eucNorm", absThres=-6, relThres=10, durRatio=0, includeKeys=[], excludeKeys=[], resFmt="dict") | |
91 | |
92 db.configQuery["ntracks"] = int(params.ntracks) | |
93 db.configQuery["npoints"] = int(params.npoints) | |
94 db.configQuery["seqStart"] = int(params.seqStart) | |
95 db.configQuery["seqLength"] = int(params.seqLength) | |
96 db.configQuery["hopSize"] = int(params.hopSize) | |
97 db.configQuery["radius"] = float(params.radius) | |
98 db.configQuery["absThres"] = float(params.absThres) | |
99 db.configQuery["relThres"] = float(params.relThres) | |
100 db.configQuery["durRatio"] = float(params.durRatio) | |
101 db.configQuery["resFmt"] = "list"; | |
102 #db.configQuery["includeKeys"] = ["AWAL2000", "AWAL500", "AWAL1000"] | |
103 | |
104 results = dict() | |
105 try: | |
106 results = db.query(key = trackID) | |
107 return json.dumps(dict(status = "ok", data = results.rawData)) | |
108 except Exception: | |
109 return json.dumps(dict(status = "error", message=str("Fix inst"))) | |
110 | |
111 if __name__ == "__main__": | |
112 # Uncomment to rebuild the db at start | |
113 #buildDatabase(trackFile) | |
114 app.run() |