<s> from __future__ import division <EOL> from pydub import AudioSegment <EOL> from dejavu . decoder import path_to_songname <EOL> from dejavu import Dejavu <EOL> from dejavu . fingerprint import * <EOL> import traceback <EOL> import fnmatch <EOL> import os , re , ast <EOL> import subprocess <EOL> import random <EOL> import logging <EOL> def set_seed ( seed = None ) : <EOL> """<STR_LIT>""" <EOL> if seed != None : <EOL> random . seed ( seed ) <EOL> def get_files_recursive ( src , fmt ) : <EOL> """<STR_LIT>""" <EOL> for root , dirnames , filenames in os . walk ( src ) : <EOL> for filename in fnmatch . filter ( filenames , '<STR_LIT:*>' + fmt ) : <EOL> yield os . path . join ( root , filename ) <EOL> def get_length_audio ( audiopath , extension ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> audio = AudioSegment . from_file ( audiopath , extension . replace ( "<STR_LIT:.>" , "<STR_LIT>" ) ) <EOL> except : <EOL> print "<STR_LIT>" % traceback . format_exc ( ) <EOL> return None <EOL> return int ( len ( audio ) / <NUM_LIT> ) <EOL> def get_starttime ( length , nseconds , padding ) : <EOL> """<STR_LIT>""" <EOL> maximum = length - padding - nseconds <EOL> if padding > maximum : <EOL> return <NUM_LIT:0> <EOL> return random . randint ( padding , maximum ) <EOL> def generate_test_files ( src , dest , nseconds , fmts = [ "<STR_LIT>" , "<STR_LIT>" ] , padding = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> for directory in [ src , dest ] : <EOL> try : <EOL> os . stat ( directory ) <EOL> except : <EOL> os . mkdir ( directory ) <EOL> for fmt in fmts : <EOL> testsources = get_files_recursive ( src , fmt ) <EOL> for audiosource in testsources : <EOL> print "<STR_LIT>" , audiosource <EOL> filename , extension = os . path . splitext ( os . path . basename ( audiosource ) ) <EOL> length = get_length_audio ( audiosource , extension ) <EOL> starttime = get_starttime ( length , nseconds , padding ) <EOL> test_file_name = "<STR_LIT>" % ( <EOL> os . path . join ( dest , filename ) , starttime , <EOL> nseconds , extension . replace ( "<STR_LIT:.>" , "<STR_LIT>" ) ) <EOL> subprocess . check_output ( [ <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" % starttime , <EOL> '<STR_LIT>' , "<STR_LIT>" % nseconds , <EOL> "<STR_LIT>" , audiosource , <EOL> test_file_name ] ) <EOL> def log_msg ( msg , log = True , silent = False ) : <EOL> if log : <EOL> logging . debug ( msg ) <EOL> if not silent : <EOL> print msg <EOL> def autolabel ( rects , ax ) : <EOL> for rect in rects : <EOL> height = rect . get_height ( ) <EOL> ax . text ( rect . get_x ( ) + rect . get_width ( ) / <NUM_LIT> , <NUM_LIT> * height , <EOL> '<STR_LIT>' % int ( height ) , ha = '<STR_LIT>' , va = '<STR_LIT>' ) <EOL> def autolabeldoubles ( rects , ax ) : <EOL> for rect in rects : <EOL> height = rect . get_height ( ) <EOL> ax . text ( rect . get_x ( ) + rect . get_width ( ) / <NUM_LIT> , <NUM_LIT> * height , <EOL> '<STR_LIT:%s>' % round ( float ( height ) , <NUM_LIT:3> ) , ha = '<STR_LIT>' , va = '<STR_LIT>' ) <EOL> class DejavuTest ( object ) : <EOL> def __init__ ( self , folder , seconds ) : <EOL> super ( DejavuTest , self ) . __init__ ( ) <EOL> self . test_folder = folder <EOL> self . test_seconds = seconds <EOL> self . test_songs = [ ] <EOL> print "<STR_LIT>" , self . test_seconds <EOL> self . test_files = [ <EOL> f for f in os . listdir ( self . test_folder ) <EOL> if os . path . isfile ( os . path . join ( self . test_folder , f ) ) <EOL> and re . findall ( "<STR_LIT>" , f ) [ <NUM_LIT:0> ] in self . test_seconds ] <EOL> print "<STR_LIT>" , self . test_files <EOL> self . n_columns = len ( self . test_seconds ) <EOL> self . n_lines = int ( len ( self . test_files ) / self . n_columns ) <EOL> print "<STR_LIT>" , self . n_columns <EOL> print "<STR_LIT>" , len ( self . test_files ) <EOL> print "<STR_LIT>" , self . n_lines <EOL> self . result_match = [ [ <NUM_LIT:0> for x in xrange ( self . n_columns ) ] for x in xrange ( self . n_lines ) ] <EOL> print "<STR_LIT>" , self . result_match <EOL> self . result_matching_times = [ [ <NUM_LIT:0> for x in xrange ( self . n_columns ) ] for x in xrange ( self . n_lines ) ] <EOL> self . result_query_duration = [ [ <NUM_LIT:0> for x in xrange ( self . n_columns ) ] for x in xrange ( self . n_lines ) ] <EOL> self . result_match_confidence = [ [ <NUM_LIT:0> for x in xrange ( self . n_columns ) ] for x in xrange ( self . n_lines ) ] <EOL> self . begin ( ) <EOL> def get_column_id ( self , secs ) : <EOL> for i , sec in enumerate ( self . test_seconds ) : <EOL> if secs == sec : <EOL> return i <EOL> def get_line_id ( self , song ) : <EOL> for i , s in enumerate ( self . test_songs ) : <EOL> if song == s : <EOL> return i <EOL> self . test_songs . append ( song ) <EOL> return len ( self . test_songs ) - <NUM_LIT:1> <EOL> def create_plots ( self , name , results , results_folder ) : <EOL> for sec in range ( <NUM_LIT:0> , len ( self . test_seconds ) ) : <EOL> ind = np . arange ( self . n_lines ) <EOL> width = <NUM_LIT> <EOL> fig = plt . figure ( ) <EOL> ax = fig . add_subplot ( <NUM_LIT> ) <EOL> ax . set_xlim ( [ - <NUM_LIT:1> * width , <NUM_LIT:2> * width ] ) <EOL> means_dvj = [ x [ <NUM_LIT:0> ] for x in results [ sec ] ] <EOL> rects1 = ax . bar ( ind , means_dvj , width , color = '<STR_LIT:r>' ) <EOL> ax . set_ylabel ( name ) <EOL> ax . set_title ( "<STR_LIT>" % ( self . test_seconds [ sec ] , name ) ) <EOL> ax . set_xticks ( ind + width ) <EOL> labels = [ <NUM_LIT:0> for x in range ( <NUM_LIT:0> , self . n_lines ) ] <EOL> for x in range ( <NUM_LIT:0> , self . n_lines ) : <EOL> labels [ x ] = "<STR_LIT>" % ( x + <NUM_LIT:1> ) <EOL> ax . set_xticklabels ( labels ) <EOL> box = ax . get_position ( ) <EOL> ax . set_position ( [ box . x0 , box . y0 , box . width * <NUM_LIT> , box . height ] ) <EOL> if name == '<STR_LIT>' : <EOL> autolabel ( rects1 , ax ) <EOL> else : <EOL> autolabeldoubles ( rects1 , ax ) <EOL> plt . grid ( ) <EOL> fig_name = os . path . join ( results_folder , "<STR_LIT>" % ( name , self . test_seconds [ sec ] ) ) <EOL> fig . savefig ( fig_name ) <EOL> def begin ( self ) : <EOL> for f in self . test_files : <EOL> log_msg ( '<STR_LIT>' ) <EOL> log_msg ( '<STR_LIT>' % f ) <EOL> col = self . get_column_id ( re . findall ( "<STR_LIT>" , f ) [ <NUM_LIT:0> ] ) <EOL> song = path_to_songname ( f ) . split ( "<STR_LIT:_>" ) [ <NUM_LIT:0> ] <EOL> line = self . get_line_id ( song ) <EOL> result = subprocess . check_output ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:file>' , <EOL> self . test_folder + "<STR_LIT:/>" + f ] ) <EOL> if result . strip ( ) == "<STR_LIT:None>" : <EOL> log_msg ( '<STR_LIT>' ) <EOL> self . result_match [ line ] [ col ] = '<STR_LIT>' <EOL> self . result_matching_times [ line ] [ col ] = <NUM_LIT:0> <EOL> self . result_query_duration [ line ] [ col ] = <NUM_LIT:0> <EOL> self . result_match_confidence [ line ] [ col ] = <NUM_LIT:0> <EOL> else : <EOL> result = result . strip ( ) <EOL> result = result . replace ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> result = result . replace ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> result = result . replace ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> result = result . replace ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> result = ast . literal_eval ( result ) <EOL> song_result = result [ "<STR_LIT>" ] <EOL> log_msg ( '<STR_LIT>' % song ) <EOL> log_msg ( '<STR_LIT>' % song_result ) <EOL> if song_result != song : <EOL> log_msg ( '<STR_LIT>' ) <EOL> self . result_match [ line ] [ col ] = '<STR_LIT>' <EOL> self . result_matching_times [ line ] [ col ] = <NUM_LIT:0> <EOL> self . result_query_duration [ line ] [ col ] = <NUM_LIT:0> <EOL> self . result_match_confidence [ line ] [ col ] = <NUM_LIT:0> <EOL> else : <EOL> log_msg ( '<STR_LIT>' ) <EOL> print self . result_match <EOL> self . result_match [ line ] [ col ] = '<STR_LIT:yes>' <EOL> self . result_query_duration [ line ] [ col ] = round ( result [ Dejavu . MATCH_TIME ] , <NUM_LIT:3> ) <EOL> self . result_match_confidence [ line ] [ col ] = result [ Dejavu . CONFIDENCE ] <EOL> song_start_time = re . findall ( "<STR_LIT>" , f ) <EOL> song_start_time = song_start_time [ <NUM_LIT:0> ] . lstrip ( "<STR_LIT>" ) <EOL> result_start_time = round ( ( result [ Dejavu . OFFSET ] * DEFAULT_WINDOW_SIZE * <EOL> DEFAULT_OVERLAP_RATIO ) / ( DEFAULT_FS ) , <NUM_LIT:0> ) <EOL> self . result_matching_times [ line ] [ col ] = int ( result_start_time ) - int ( song_start_time ) <EOL> if ( abs ( self . result_matching_times [ line ] [ col ] ) == <NUM_LIT:1> ) : <EOL> self . result_matching_times [ line ] [ col ] = <NUM_LIT:0> <EOL> log_msg ( '<STR_LIT>' % round ( result [ Dejavu . MATCH_TIME ] , <NUM_LIT:3> ) ) <EOL> log_msg ( '<STR_LIT>' % result [ Dejavu . CONFIDENCE ] ) <EOL> log_msg ( '<STR_LIT>' % song_start_time ) <EOL> log_msg ( '<STR_LIT>' % result_start_time ) <EOL> if ( self . result_matching_times [ line ] [ col ] == <NUM_LIT:0> ) : <EOL> log_msg ( '<STR_LIT>' ) <EOL> else : <EOL> log_msg ( '<STR_LIT>' ) <EOL> log_msg ( '<STR_LIT>' ) </s>
