/
comparison.py
executable file
·103 lines (73 loc) · 2.41 KB
/
comparison.py
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
import fingerprint
import sys
import model
import base_app
import cPickle
import os
import subprocess
import collections
def change_stereo_to_mono(filename):
'''
Change the submitted .wav file from stereo (default through getUserMedia()) to mono
for easier fingerprinting.
'''
if os.path.exists("new_user_input.wav"):
os.remove("new_user_input.wav")
ffmpeg_command = "ffmpeg -i user_input.wav -ac 1 new_user_input.wav"
f_ffmpeg=subprocess.Popen(ffmpeg_command, shell=True)
return
def align(audio1, audio2):
'''
Align fingerprint to fingerprint and determine time offset
ex. sig = [ (freq, time), (freq, time) ]
'''
aligned_frequencies = {}
histogram = {}
for peak in audio1:
freq, time = peak
if not aligned_frequencies.get(freq):
aligned_frequencies[freq] = ([], [])
aligned_frequencies[freq][0].append(time)
for peak in audio2:
freq, time = peak
if not aligned_frequencies.get(freq):
aligned_frequencies[freq] = ([], [])
aligned_frequencies[freq][1].append(time)
for freq, timings in aligned_frequencies.items():
audio1_timings = timings[0]
audio2_timings = timings[1]
for t1 in audio1_timings:
for t2 in audio2_timings:
delta = t2-t1
histogram[delta] = histogram.get(delta, 0) + 1
matches = histogram.items()
ranked_matches = sorted(matches, key=lambda x: x[1], reverse=True)
return ranked_matches
def compare_fingerprint_to_database(filename):
'''
Takes the mono .wav file and creates a fingerprint of it, which it then compares
to the existing database of fingerprints, returning information of most likely match.
'''
user_audio = fingerprint.location_fingerprint(filename)
fingerprints = model.Fingerprint.query.all()
database_iteration = []
max_offset = 0
for row in fingerprints:
db_audio = cPickle.loads(row.fingerprint)
ranked_matches = align(user_audio, db_audio)
current_song = {}
song_match = {}
current_song["title"] = row.title
current_song["artist"] = row.artist
current_song["offset"] = ranked_matches[0][1]
current_song["high_match"] = False
database_iteration.append(current_song)
for current_song in database_iteration:
if current_song["offset"] > max_offset:
max_offset = current_song["offset"]
for current_song in database_iteration:
if current_song["offset"] == max_offset:
current_song["high_match"] = True
return database_iteration
if __name__ == '__main__':
compare_fingerprint_to_database(filename)