def match_pitch(filename, dbasename, density=18, radius=1, step=1 / 20): matcher = audfprint_match.Matcher() hash_tab = hash_table.HashTable(dbasename) analyzer = audfprint_analyze.Analyzer() analyzer.density = density bestScore = 0 bestMatch = "NOMATCH" song = AudioSegment.from_mp3(filename) aa = AudioAugmentation() for i in range(-radius, radius + 1): # Speed up/slow down the sample if i != 0: octave = i * step shifted, _ = aa.pitch_shift(song, octave) shifted.export("tmp.mp3", format="mp3") matches, _, _ = matcher.match_file( analyzer, hash_tab, "tmp.mp3" if not i == 0 else filename) if len(matches) == 0: continue songid, score = matches[0][:2] songname = hash_tab.names[songid] if score > bestScore: bestScore = score bestMatch = songname return bestMatch
def match(self): matcher = audfprint_match.Matcher() matcher.find_time_range = True matcher.verbose = 1 matcher.max_returns = 100 analyzer = audfprint_analyze.Analyzer() analyzer.n_fft = 512 analyzer.n_hop = analyzer.n_fft / 2 analyzer.shifts = 1 # analyzer.exact_count = True analyzer.density = 20.0 analyzer.target_sr = 11025 hash_tab = hash_table.HashTable("./samples.pklz") hash_tab.params['samplerate'] = analyzer.target_sr qry = "./Samples/viral.afpt" rslts, dur, nhash = matcher.match_file(analyzer, hash_tab, "./Samples/viral.afpt", 0) t_hop = analyzer.n_hop / float(analyzer.target_sr) qrymsg = qry + (' %.1f ' % dur) + "sec " + str(nhash) + " raw hashes" msgrslt = [] if len(rslts) == 0: nhashaligned = 0 msgrslt.append("NOMATCH " + qrymsg) else: for (tophitid, nhashaligned, aligntime, nhashraw, rank, min_time, max_time) in rslts: # msg = ("Matched {:6.1f} s starting at {:6.1f} s in {:s}" # " to time {:6.1f} s in {:s}").format( # (max_time - min_time) * t_hop, min_time * t_hop, qry, # (min_time + aligntime) * t_hop, hash_tab.names[tophitid]) msg = ( "Matched {:6.1f} s starting at {:6.1f} s in {:s}" " to time {:6.1f} s in {:n}; max {:6.1f} min {:6.1f} align {:6.1f} hop {:6.1f}" ).format( (max_time - min_time) * t_hop, min_time * t_hop, qry, (min_time + aligntime) * t_hop, tophitid, #), max_time * t_hop, min_time * t_hop, aligntime * t_hop, t_hop) msgrslt.append(msg) dumper.dump(msgrslt)
def match(event, context): s3.Bucket(BUCKET_NAME).download_file('_test-fprint.afpt', '/tmp/_test-fprint.afpt') s3.Bucket(BUCKET_NAME).download_file('_test-db.pklz', '/tmp/_test-db.pklz') qry = '/tmp/_test-fprint.afpt' hashFile = '/tmp/_test-db.pklz' matcher = audfprint_match.Matcher() matcher.find_time_range = True matcher.verbose = False matcher.max_returns = 100 matcher.exact_count = True matcher.max_alignments_per_id = 20 analyzer = audfprint_analyze.Analyzer() analyzer.n_fft = 512 analyzer.n_hop = analyzer.n_fft / 2 analyzer.shifts = 1 # analyzer.exact_count = True analyzer.density = 20.0 analyzer.target_sr = 11025 analyzer.verbose = False hash_tab = hash_table.HashTable(hashFile) hash_tab.params['samplerate'] = analyzer.target_sr rslts, dur, nhash = matcher.match_file(analyzer, hash_tab, qry, 0) t_hop = analyzer.n_hop / float(analyzer.target_sr) qrymsg = qry + (' %.1f ' % dur) + "sec " + str(nhash) + " raw hashes" # print "duration,start,from,time,source,sourceId,nhashaligned,aligntime,nhashraw,rank,min_time,max_time, t_hop" matches = [] if len(rslts) == 0: nhashaligned = 0 else: for (tophitid, nhashaligned, aligntime, nhashraw, rank, min_time, max_time) in rslts: msg = ( "{:f},{:f},{:s},{:f},{:s},{:n},{:n},{:n},{:n},{:n},{:n},{:n},{:f}" ).format((max_time - min_time) * t_hop, min_time * t_hop, qry, (min_time + aligntime) * t_hop, hash_tab.names[tophitid], tophitid, nhashaligned, aligntime, nhashraw, rank, min_time, max_time, t_hop) matches.append(msg) response = {"statusCode": 200, "body": json.dumps(matches)} return response
def setup_matcher(args): """Create a new matcher objects, set parameters from docopt structure""" matcher = audfprint_match.Matcher() matcher.window = int(args['--match-win']) matcher.threshcount = int(args['--min-count']) matcher.max_returns = int(args['--max-matches']) matcher.search_depth = int(args['--search-depth']) matcher.sort_by_time = args['--sortbytime'] matcher.exact_count = args['--exact-count'] | args['--illustrate'] | args['--illustrate-hpf'] matcher.illustrate = args['--illustrate'] | args['--illustrate-hpf'] matcher.illustrate_hpf = args['--illustrate-hpf'] matcher.verbose = args['--verbose'] matcher.find_time_range = args['--find-time-range'] matcher.time_quantile = float(args['--time-quantile']) return matcher
def regular_matching(): #get find_peaks from analyze analyzer = audfprint_analyze.Analyzer() hash_tab = hash_table.HashTable('fpdbase.pklz') matcher = audfprint_match.Matcher() sampling_seconds = 10 sampling_interval = 15 prev_resultID = [] count = 0 while True: start = time.time() twoSecondArray = different_record(sampling_seconds) peakLists = analyzer.find_peaks(twoSecondArray, 11025) landmarkLists = analyzer.peaks2landmarks(peakLists) hashesLists = audfprint_analyze.landmarks2hashes(landmarkLists) print(hashesLists) hashes_hashes = (((hashesLists[:, 0].astype(np.uint64)) << 32) + hashesLists[:, 1].astype(np.uint64)) unique_hash_hash = np.sort(np.unique(hashes_hashes)) unique_hashes = np.hstack([ (unique_hash_hash >> 32)[:, np.newaxis], (unique_hash_hash & ((1 << 32) - 1))[:, np.newaxis] ]).astype(np.int32) hashes = unique_hashes #now the matching # for num, filename in enumerate(filename_iter): # # count += 1 # msgs = matcher.file_match_to_msgs(analyzer, hash_tab, filename, num) # report(msgs) # file_match_to_msgs(self, analyzer, ht, qry, number=None) # print(matcher.file_match_to_msgs(analyzer, hash_tab, "Some qry name")) # rslts, dur, nhash = match_file(matcher, analyzer, hash_tab, "some query", hashesLists) message, results = file_match_to_msgs(matcher, analyzer, hash_tab, "FROM MICROPHONE", hashes) print(sampling_seconds, sampling_interval) count += 1 end = time.time() - start print(end) time.sleep(sampling_interval - (end - sampling_seconds)) print(count)
def fingerprint_filename(filename, dbasename): matcher = audfprint_match.Matcher() matcher.window = 2 matcher.threshcount = 5 matcher.max_returns = 1 matcher.search_depth = 100 matcher.sort_by_time = False matcher.exact_count = False matcher.illustrate = False matcher.illustrate_hpf = False matcher.verbose = 1 matcher.find_time_range = False matcher.time_quantile = 0.05 analyzer = audfprint_analyze.Analyzer() # Read parameters from command line/docopts analyzer.density = 20 analyzer.maxpksperframe = 5 analyzer.maxpairsperpeak = 3 analyzer.f_sd = 30.0 analyzer.shifts = 0 # fixed - 512 pt FFT with 256 pt hop at 11025 Hz analyzer.target_sr = 11025 analyzer.n_fft = 512 analyzer.n_hop = analyzer.n_fft // 2 # set default value for shifts depending on mode if analyzer.shifts == 0: # Default shift is 4 for match, otherwise 1 analyzer.shifts = 4 analyzer.fail_on_error = not False hashtable = hash_table.HashTable(dbasename) results, dur, nhash = matcher.match_file(analyzer, hashtable, filename, 0) try: (first_hit, nhashaligned, aligntime, nhashraw, rank, min_time, max_time) = results[0] filename = hashtable.names[first_hit] return nhashraw, filename except: return None, None
def kalman_matching(): #get find_peaks from analyze analyzer = audfprint_analyze.Analyzer() hash_tab = hash_table.HashTable('fpdbase.pklz') matcher = audfprint_match.Matcher() sampling_seconds = 10 sampling_interval = 15 prev_resultID = [] len_prev_results = 0 count = 0 true_start = time.time() print("click now!") time.sleep(1) while True: start = time.time() twoSecondArray = different_record(sampling_seconds) peakLists = analyzer.find_peaks(twoSecondArray, 11025) landmarkLists = analyzer.peaks2landmarks(peakLists) hashesLists = audfprint_analyze.landmarks2hashes(landmarkLists) hashes_hashes = (((hashesLists[:, 0].astype(np.uint64)) << 32) + hashesLists[:, 1].astype(np.uint64)) unique_hash_hash = np.sort(np.unique(hashes_hashes)) unique_hashes = np.hstack([ (unique_hash_hash >> 32)[:, np.newaxis], (unique_hash_hash & ((1 << 32) - 1))[:, np.newaxis] ]).astype(np.int32) hashes = unique_hashes #now the matching # for num, filename in enumerate(filename_iter): # # count += 1 # msgs = matcher.file_match_to_msgs(analyzer, hash_tab, filename, num) # report(msgs) # file_match_to_msgs(self, analyzer, ht, qry, number=None) # print(matcher.file_match_to_msgs(analyzer, hash_tab, "Some qry name")) # rslts, dur, nhash = match_file(matcher, analyzer, hash_tab, "some query", hashesLists) message, results = file_match_to_msgs(matcher, analyzer, hash_tab, "FROM MICROPHONE", hashes) print(message) if len(results) == 0: sampling_interval -= 5 if sampling_interval <= 10: sampling_interval = 10 sampling_seconds += 5 elif len(results) > len_prev_results * 2 and len_prev_results > 0: for result in results: if prev_resultID != result[0]: prev_resultID = result[0] len_prev_results = len(results) break elif results[0][0] == prev_resultID: #increase sampling_interval sampling_interval +=5 sampling_seconds -= 5 if sampling_seconds <= 10: sampling_seconds = 7 prev_resultID = results[0][0] else: sampling_interval -= 5 if sampling_interval <= 10: sampling_interval = 10 sampling_seconds += 5 prev_resultID = results[0][0] # (tophitid, nhashaligned, aligntime, nhashraw, rank, # min_time, max_time) print(sampling_seconds, sampling_interval) count += 1 end = time.time() - start print("how much this iteration took", end) print("how many seconds so far", time.time() - true_start) time.sleep(sampling_interval - (end - sampling_seconds)) print(count)
def match(event, context): hash = event['Records'][0]['body'] id = event['Records'][0]['messageAttributes']['Id']['stringValue'] date = event['Records'][0]['messageAttributes']['Date']['stringValue'] # print(id) s3.Bucket(BUCKET_NAME).download_file( 'wave/{}/{}/audio.afpt'.format(date, id), '/tmp/{}.afpt'.format(id)) s3.Bucket(BUCKET_NAME).download_file( hash, '/tmp/{}'.format(hash.split('/').pop())) qry = '/tmp/{}.afpt'.format(id) hashFile = '/tmp/{}'.format(hash.split('/').pop()) # print(qry) matcher = audfprint_match.Matcher() matcher.find_time_range = True matcher.verbose = False matcher.max_returns = 100 # matcher.threshcount = 20 matcher.exact_count = True matcher.max_alignments_per_id = 20 analyzer = audfprint_analyze.Analyzer() analyzer.n_fft = 512 analyzer.n_hop = analyzer.n_fft // 2 analyzer.shifts = 1 # analyzer.exact_count = True analyzer.density = 20.0 analyzer.target_sr = 11025 analyzer.verbose = False hash_tab = hash_table.HashTable(hashFile) os.remove(hashFile) hash_tab.params['samplerate'] = analyzer.target_sr rslts, dur, nhash = matcher.match_file(analyzer, hash_tab, qry, 0) t_hop = analyzer.n_hop / float(analyzer.target_sr) qrymsg = qry + (' %.1f ' % dur) + "sec " + str(nhash) + " raw hashes" # print "duration,start,from,time,source,sourceId,nhashaligned,aligntime,nhashraw,rank,min_time,max_time, t_hop" matches = [] if len(rslts) == 0: nhashaligned = 0 else: for (tophitid, nhashaligned, aligntime, nhashraw, rank, min_time, max_time) in rslts: msg = ( "{:f},{:f},{:s},{:f},{:s},{:n},{:n},{:n},{:n},{:n},{:n},{:n},{:f}" ).format((max_time - min_time) * t_hop, min_time * t_hop, qry, (min_time + aligntime) * t_hop, hash_tab.names[tophitid], tophitid, nhashaligned, aligntime, nhashraw, rank, min_time, max_time, t_hop) print(msg) duration = (max_time - min_time) * t_hop start = min_time * t_hop time = (min_time + aligntime) * t_hop matches.append({ 'duration': np.asscalar(duration) if isinstance(duration, np.generic) else duration, 'start': np.asscalar(start) if isinstance(start, np.generic) else start, 'time': np.asscalar(time) if isinstance(time, np.generic) else time, 'match': hash_tab.names[tophitid].split('/').pop().replace('.afpt', ''), 'alignedHashes': np.asscalar(nhashaligned) if isinstance( nhashaligned, np.generic) else nhashaligned, 'totalHashes': np.asscalar(nhashraw) if isinstance(nhashraw, np.generic) else nhashraw, 'rank': np.asscalar(rank) if isinstance(rank, np.generic) else rank }) data = {'id': id, 'matches': matches, 'hash': hash} pubnub.publish().channel(id).message(data).pn_async(publish_callback) if len(matches) > 0: s3.Object( BUCKET_NAME, 'query/{}/{}.json'.format( id, hash.split('/').pop().replace('.pklz', ''))).put( Body=(bytes(json.dumps(data, indent=2).encode('UTF-8')))) response = { "statusCode": 200, # "body": json.dumps(matches), "event": event } return response