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")
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: c.fatal("File \'{}\' could not be matched".format(signal.filename))
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: c.fatal( "File \'{}\' could not be matched".format(signal.filename) )