From ece1c8b22e2500b0a6a206b3e4a3d02659ed548a Mon Sep 17 00:00:00 2001 From: Will Drevo Date: Mon, 15 Dec 2014 21:30:33 -0500 Subject: [PATCH] Fixes #65, fixes #60, merges #64 --- dejavu.py | 26 ++++++++++++++------------ dejavu/fingerprint.py | 10 ++-------- example.py | 13 ++++++++++--- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/dejavu.py b/dejavu.py index a50c9da..0bb4d0c 100755 --- a/dejavu.py +++ b/dejavu.py @@ -1,5 +1,6 @@ #!/usr/bin/python +import os import sys import json import warnings @@ -12,15 +13,15 @@ from argparse import RawTextHelpFormatter warnings.filterwarnings("ignore") -DEFAULT_CONFIG_FILE = "dejavu.cnf" +DEFAULT_CONFIG_FILE = "dejavu.cnf.SAMPLE" -def init(config_file): - """ Load config from a JSON file - or anything outputting a python dictionary +def init(configpath): + """ + Load config from a JSON file """ try: - with open(config_file) as f: + with open(configpath) as f: config = json.load(f) except IOError as err: print("Cannot open configuration: %s. Exiting" % (str(err))) @@ -31,15 +32,13 @@ def init(config_file): if __name__ == '__main__': - """ If running from terminal. - """ parser = argparse.ArgumentParser( - description="Audio Fingerprinting library", + description="Dejavu: Audio Fingerprinting library", formatter_class=RawTextHelpFormatter) parser.add_argument('-c', '--config', nargs='?', help='Path to configuration file\n' 'Usages: \n' - '--config /path/to/congfile\n') + '--config /path/to/config-file\n') parser.add_argument('-f', '--fingerprint', nargs='*', help='Fingerprint files in a directory\n' 'Usages: \n' @@ -54,7 +53,7 @@ if __name__ == '__main__': args = parser.parse_args() if not args.fingerprint and not args.recognize: - print("No arguments") + parser.print_help() sys.exit(0) config_file = args.config @@ -65,15 +64,18 @@ if __name__ == '__main__': djv = init(config_file) if args.fingerprint: # Fingerprint all files in a directory - if 2 == len(args.fingerprint): + if len(args.fingerprint) == 2: directory = args.fingerprint[0] extension = args.fingerprint[1] print("Fingerprinting all .%s files in the %s directory" % (extension, directory)) djv.fingerprint_directory(directory, ["." + extension], 4) - elif 1 == len(args.fingerprint): + elif len(args.fingerprint) == 1: filepath = args.fingerprint[0] + if os.path.isdir(filepath): + print("Please specify an extension if you'd like to fingerprint a directory!") + sys.exit(1) djv.fingerprint_file(filepath) elif args.recognize: diff --git a/dejavu/fingerprint.py b/dejavu/fingerprint.py index 95c0076..4db321b 100755 --- a/dejavu/fingerprint.py +++ b/dejavu/fingerprint.py @@ -136,26 +136,20 @@ def generate_hashes(peaks, fan_value=DEFAULT_FAN_VALUE): sha1_hash[0:20] time_offset [(e05b341a9b77a51fd26, 32), ... ] """ - fingerprinted = set() # to avoid rehashing same pairs - if PEAK_SORT: peaks.sort(key=itemgetter(1)) for i in range(len(peaks)): for j in range(1, fan_value): - if (i + j) < len(peaks) and not (i, i + j) in fingerprinted: + if (i + j) < len(peaks): + freq1 = peaks[i][IDX_FREQ_I] freq2 = peaks[i + j][IDX_FREQ_I] - t1 = peaks[i][IDX_TIME_J] t2 = peaks[i + j][IDX_TIME_J] - t_delta = t2 - t1 if t_delta >= MIN_HASH_TIME_DELTA and t_delta <= MAX_HASH_TIME_DELTA: h = hashlib.sha1( "%s|%s|%s" % (str(freq1), str(freq2), str(t_delta))) yield (h.hexdigest()[0:FINGERPRINT_REDUCTION], t1) - - # ensure we don't repeat hashing - fingerprinted.add((i, i + j)) diff --git a/example.py b/example.py index e2769cb..991c6a9 100755 --- a/example.py +++ b/example.py @@ -16,12 +16,19 @@ djv.fingerprint_directory("mp3", [".mp3"]) # Recognize audio from a file from dejavu.recognize import FileRecognizer song = djv.recognize(FileRecognizer, "mp3/Sean-Fournier--Falling-For-You.mp3") +print "From file we recognized: %s\n" % song -# Or recognize audio from your microphone for 10 seconds +# Or recognize audio from your microphone for `secs` seconds from dejavu.recognize import MicrophoneRecognizer -song = djv.recognize(MicrophoneRecognizer, seconds=2) +secs = 5 +song = djv.recognize(MicrophoneRecognizer, seconds=secs) +if song is None: + print "Nothing recognized -- did you play the song out loud so your mic could hear it? :)" +else: + print "From mic with %d seconds we recognized: %s\n" % (secs, song) # Or use a recognizer without the shortcut, in anyway you would like from dejavu.recognize import FileRecognizer recognizer = FileRecognizer(djv) -song = recognizer.recognize_file("mp3/Josh-Woodward--I-Want-To-Destroy-Something-Beautiful.mp3") \ No newline at end of file +song = recognizer.recognize_file("mp3/Josh-Woodward--I-Want-To-Destroy-Something-Beautiful.mp3") +print "No shortcut, we recognized: %s\n" % song \ No newline at end of file