-
Notifications
You must be signed in to change notification settings - Fork 36
/
listen.py
154 lines (114 loc) · 3.72 KB
/
listen.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
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
154
import os
import sys
import src
import src.analyzer as analyzer
import argparse
from argparse import RawTextHelpFormatter
from itertools import zip_longest
from termcolor import colored
from src.listener import Listener
from src.db import SQLiteDatabase
if __name__ == '__main__':
db = SQLiteDatabase()
parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter)
parser.add_argument('-s', '--seconds', nargs='?')
args = parser.parse_args()
if not args.seconds:
print (colored("Warning: You don't set any second. It's 10 by default", "yellow"))
args.seconds = "10"
seconds = int(args.seconds)
chunksize = 2**12
channels = 1
record_forever = False
listener = Listener()
listener.start_recording(seconds=seconds,
chunksize=chunksize,
channels=channels)
while True:
bufferSize = int(listener.rate / listener.chunksize * seconds)
print (colored("Listening....","green"))
for i in range(0, bufferSize):
nums = listener.process_recording()
if not record_forever: break
listener.stop_recording()
print (colored('Okey, enough', attrs=['dark']))
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return (filter(None, values) for values
in zip_longest(fillvalue=fillvalue, *args))
data = listener.get_recorded_data()
msg = 'Took %d samples'
print(colored(msg, attrs=['dark']) % len(data[0]))
Fs = analyzer.DEFAULT_FS
channel_amount = len(data)
result = set()
matches = []
def find_matches(samples, Fs=analyzer.DEFAULT_FS):
hashes = analyzer.fingerprint(samples, Fs=Fs)
return return_matches(hashes)
def return_matches(hashes):
mapper = {}
for hash, offset in hashes:
mapper[hash.upper()] = offset
values = mapper.keys()
for split_values in grouper(values, 1000):
query = """
SELECT upper(hash), song_fk, offset
FROM fingerprints
WHERE upper(hash) IN (%s)
"""
vals = list(split_values).copy()
length = len(vals)
query = query % ', '.join('?' * length)
x = db.executeAll(query, values=vals)
matches_found = len(x)
if matches_found > 0:
msg = 'I found %d hash in db'
print (colored(msg, 'green') % (
matches_found
))
for hash, sid, offset in x:
yield (sid, mapper[hash])
for channeln, channel in enumerate(data):
matches.extend(find_matches(channel))
def align_matches(matches):
diff_counter = {}
largest = 0
largest_count = 0
song_id = -1
for tup in matches:
sid, diff = tup
if diff not in diff_counter:
diff_counter[diff] = {}
if sid not in diff_counter[diff]:
diff_counter[diff][sid] = 0
diff_counter[diff][sid] += 1
if diff_counter[diff][sid] > largest_count:
largest = diff
largest_count = diff_counter[diff][sid]
song_id = sid
songM = db.get_song_by_id(song_id)
nseconds = round(float(largest) / analyzer.DEFAULT_FS *
analyzer.DEFAULT_WINDOW_SIZE *
analyzer.DEFAULT_OVERLAP_RATIO, 5)
return {
"SONG_ID" : song_id,
"SONG_NAME" : songM[1],
"CONFIDENCE" : largest_count,
"OFFSET" : int(largest),
"OFFSET_SECS" : nseconds
}
total_matches_found = len(matches)
if total_matches_found > 0:
msg = 'Totally found %d hash'
print (colored(msg, 'green') % total_matches_found)
song = align_matches(matches)
msg = ' => song: %s (id=%d)\n'
msg += ' offset: %d (%d secs)\n'
print (colored(msg, 'green') % (
song['SONG_NAME'], song['SONG_ID'],
song['OFFSET'], song['OFFSET_SECS']
))
else:
msg = 'Not anything matching'
print (colored(msg, 'red'))