def populate(self, directory): ''' Analyzes all audio files in a directory and add their fingerprint to the database. Writes result to disk when done. Args: directory: directory containing audio files. ''' audio_files = sorted(soundfiles.find_files(directory + '/*')) c.notice("Reading {} files...".format(len(audio_files))) i = 0 for filename in audio_files: i += 1 c.write('\r\t-file ' + str(i) + ' of ' + str(len(audio_files))) signal = soundfiles.load_signal(filename) self.add(fingerprint.get_tokens(signal)) c.write("\n") c.notice("Writing database to disk...") self.save() c.succes("Database written")
exit() if args.train: database.populate(args.train) if args.f: if not database.read: c.notice('Loading database from disk...') database.load() c.succes('Loaded {} entries.'.format(database.get_size())) c.notice('Converting database to classifier...') classifier = database.as_classifier() c.notice('Loading \'{}\' from disk...'.format(args.f)) signal = load_signal(args.f) if signal is None: c.fatal('This file could not be loaded') parser.print_help() exit() c.notice('Analyzing \'{}\'...'.format(signal.get_filename())) tokens = get_tokens(signal) c.notice("Classifying...") match = classifier.classify(tokens) if match: c.notice("File \'{}\' matches with database entry {}".format( signal.filename, match)) else:
exit() if args.train: database.populate(args.train) if args.f: if not database.read: c.notice('Loading database from disk...') database.load() c.succes('Loaded {} entries.'.format(database.get_size())) c.notice('Converting database to classifier...') classifier = database.as_classifier() c.notice('Loading \'{}\' from disk...'.format(args.f)) signal = load_signal(args.f) if signal is None: c.fatal('This file could not be loaded') parser.print_help() exit() c.notice('Analyzing \'{}\'...'.format(signal.get_filename())) tokens = get_tokens(signal) c.notice("Classifying...") match = classifier.classify(tokens) if match: c.notice("File \'{}\' matches with database entry {}".format( signal.filename, match ))
import soundfiles import random import matplotlib.pyplot as plt import wave import numpy as np import os if not os.path.exists("audio/pokemon"): os.makedirs("audio/pokemon") directory = "training/pokemon/*.wav" files = soundfiles.find_files(directory) for f in files: signal = soundfiles.load_signal(f) ''' initialize the suffix of the new file and the new samples ''' suffix = "" new_samples = signal.samples if random.random() < 0.5: ''' add noise ''' new_samples += 0.05 - 0.1 * np.random.rand(len(new_samples)) suffix += "-noise" if random.random() < 0.5:
import soundfiles import random import matplotlib.pyplot as plt import wave import numpy as np import os if not os.path.exists("audio/pokemon"): os.makedirs("audio/pokemon") directory = "training/pokemon/*.wav" files = soundfiles.find_files(directory) for f in files: signal = soundfiles.load_signal(f) ''' initialize the suffix of the new file and the new samples ''' suffix = "" new_samples = signal.samples if random.random() < 0.5: ''' add noise ''' new_samples += 0.05 - 0.1 * np.random.rand(len(new_samples)) suffix += "-noise" if random.random() < 0.5: '''
pokemon_nr = "103" # "003" filenames = [ "training/pokemon/{}.wav".format(pokemon_nr), "audio/presentatie/{}-divided.wav".format(pokemon_nr), "audio/presentatie/{}-noise.wav".format(pokemon_nr), "audio/presentatie/{}-translated.wav".format(pokemon_nr), ] n = len(filenames) fig, ax = plt.subplots(n) for i in xrange(n): filename = filenames[i] signal = soundfiles.load_signal(filename) fingerprints = fingerprint.get_fingerprints(signal, 1024, 2) x = [time for (time, peaks) in fingerprints for freq in peaks] y = [freq for (time, peaks) in fingerprints for freq in peaks] if "translated" in filename: shift = 16384 / 1024 x = [e - shift for e in x] for h in xrange(128, 256, 32): ax[i].axhline(y=h, color="grey", alpha=0.25) plot = ax[i].scatter(x, y, color="red") ax[i].set_xlim((0, 23)) ax[i].set_ylim((128, 256))
print "Start testing classifier with {} files\n".format(len(filenames)) shuffle(filenames) correct_match = 0 wrong_match = 0 no_match = 0 wrong_matches = "" correct_matches = "" no_matches = "" for filename in filenames: print "Classifying {}".format(filename) signal = load_signal(filename) tokens = get_tokens(signal) match = classifier.classify(tokens) if match is None: print "-> No match found" no_matches += filename.split("/")[2][:-4] no_match += 1 elif match.split("/")[2][:3] == filename.split("/")[2][:3]: print "-> Correct match found" correct_match += 1 correct_matches += filename.split("/")[2][:-4] else: print "-> Wrong match found" wrong_matches += filename.split("/")[2][:-4] wrong_match += 1
pokemon_nr = "103" # "003" filenames = [ "training/pokemon/{}.wav".format(pokemon_nr), "audio/presentatie/{}-divided.wav".format(pokemon_nr), "audio/presentatie/{}-noise.wav".format(pokemon_nr), "audio/presentatie/{}-translated.wav".format(pokemon_nr) ] n = len(filenames) fig, ax = plt.subplots(n) for i in xrange(n): filename = filenames[i] signal = soundfiles.load_signal(filename) fingerprints = fingerprint.get_fingerprints(signal, 1024, 2) x = [time for (time, peaks) in fingerprints for freq in peaks] y = [freq for (time, peaks) in fingerprints for freq in peaks] if "translated" in filename: shift = 16384 / 1024 x = [e - shift for e in x] for h in xrange(128, 256, 32): ax[i].axhline(y=h, color="grey", alpha=0.25) plot = ax[i].scatter(x, y, color="red") ax[i].set_xlim((0, 23)) ax[i].set_ylim((128, 256))