<s> """<STR_LIT>""" <EOL> import sys <EOL> from subprocess import check_output <EOL> try : <EOL> import libxmp <EOL> LIBXMP = True <EOL> except ImportError : <EOL> LIBXMP = False <EOL> import numpy as N <EOL> def log_magnitude_spectrum ( frames ) : <EOL> """<STR_LIT>""" <EOL> return N . log ( N . abs ( N . fft . rfft ( frames ) ) . clip ( <NUM_LIT> , N . inf ) ) <EOL> def magnitude_spectrum ( frames ) : <EOL> """<STR_LIT>""" <EOL> return N . abs ( N . fft . rfft ( frames ) ) <EOL> def RMS_energy ( frames ) : <EOL> """<STR_LIT>""" <EOL> f = frames . flatten ( ) <EOL> return N . sqrt ( N . mean ( f * f ) ) <EOL> def normalize_features ( features ) : <EOL> """<STR_LIT>""" <EOL> return ( features - N . min ( features ) ) / ( N . max ( features ) - N . min ( features ) ) <EOL> def zero_crossing_last ( frames ) : <EOL> """<STR_LIT>""" <EOL> frames = N . array ( frames ) <EOL> crossings = N . where ( N . diff ( N . sign ( frames ) ) ) <EOL> if len ( crossings [ <NUM_LIT:0> ] ) == <NUM_LIT:0> : <EOL> print "<STR_LIT>" <EOL> return len ( frames ) - <NUM_LIT:1> <EOL> return crossings [ <NUM_LIT:0> ] [ - <NUM_LIT:1> ] <EOL> def zero_crossing_first ( frames ) : <EOL> """<STR_LIT>""" <EOL> frames = N . array ( frames ) <EOL> crossings = N . where ( N . diff ( N . sign ( frames ) ) ) <EOL> if len ( crossings [ <NUM_LIT:0> ] ) == <NUM_LIT:0> : <EOL> print "<STR_LIT>" <EOL> return <NUM_LIT:0> <EOL> return crossings [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> def log_factor ( arr ) : <EOL> return N . power ( arr , <NUM_LIT> ) <EOL> def limiter ( arr ) : <EOL> """<STR_LIT>""" <EOL> dyn_range = <NUM_LIT> / <NUM_LIT> <EOL> lim_thresh = <NUM_LIT> / <NUM_LIT> <EOL> lim_range = dyn_range - lim_thresh <EOL> new_arr = arr . copy ( ) <EOL> inds = N . where ( arr > lim_thresh ) [ <NUM_LIT:0> ] <EOL> new_arr [ inds ] = ( new_arr [ inds ] - lim_thresh ) / lim_range <EOL> new_arr [ inds ] = ( N . arctan ( new_arr [ inds ] ) * <NUM_LIT> / N . pi ) * lim_range + lim_thresh <EOL> inds = N . where ( arr < - lim_thresh ) [ <NUM_LIT:0> ] <EOL> new_arr [ inds ] = - ( new_arr [ inds ] + lim_thresh ) / lim_range <EOL> new_arr [ inds ] = - ( <EOL> N . arctan ( new_arr [ inds ] ) * <NUM_LIT> / N . pi * lim_range + lim_thresh ) <EOL> return new_arr <EOL> def linear ( arr1 , arr2 ) : <EOL> """<STR_LIT>""" <EOL> n = N . shape ( arr1 ) [ <NUM_LIT:0> ] <EOL> try : <EOL> channels = N . shape ( arr1 ) [ <NUM_LIT:1> ] <EOL> except : <EOL> channels = <NUM_LIT:1> <EOL> f_in = N . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , num = n ) <EOL> f_out = N . linspace ( <NUM_LIT:1> , <NUM_LIT:0> , num = n ) <EOL> if channels > <NUM_LIT:1> : <EOL> f_in = N . tile ( f_in , ( channels , <NUM_LIT:1> ) ) . T <EOL> f_out = N . tile ( f_out , ( channels , <NUM_LIT:1> ) ) . T <EOL> vals = f_out * arr1 + f_in * arr2 <EOL> return vals <EOL> def equal_power ( arr1 , arr2 ) : <EOL> """<STR_LIT>""" <EOL> n = N . shape ( arr1 ) [ <NUM_LIT:0> ] <EOL> try : <EOL> channels = N . shape ( arr1 ) [ <NUM_LIT:1> ] <EOL> except : <EOL> channels = <NUM_LIT:1> <EOL> f_in = N . arange ( n ) / float ( n - <NUM_LIT:1> ) <EOL> f_out = N . arange ( n - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) / float ( n ) <EOL> if channels > <NUM_LIT:1> : <EOL> f_in = N . tile ( f_in , ( channels , <NUM_LIT:1> ) ) . T <EOL> f_out = N . tile ( f_out , ( channels , <NUM_LIT:1> ) ) . T <EOL> vals = log_factor ( f_out ) * arr1 + log_factor ( f_in ) * arr2 <EOL> return limiter ( vals ) <EOL> def segment_array ( arr , length , overlap = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> arr = N . array ( arr ) <EOL> offset = float ( overlap ) * length <EOL> total_segments = int ( ( N . shape ( arr ) [ <NUM_LIT:0> ] - length ) / offset ) + <NUM_LIT:1> <EOL> other_shape = N . shape ( arr ) [ <NUM_LIT:1> : ] <EOL> out_shape = [ total_segments , length ] <EOL> out_shape . extend ( other_shape ) <EOL> out = N . empty ( out_shape ) <EOL> for i in xrange ( total_segments ) : <EOL> out [ i ] [ : ] = arr [ i * offset : i * offset + length ] <EOL> return out <EOL> def wav_to_mp3 ( wavfn , delete_wav = False , lame_quality = "<STR_LIT>" ) : <EOL> mp3fn = "<STR_LIT:.>" . join ( wavfn . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] ) + '<STR_LIT>' <EOL> check_output ( '<STR_LIT>' . format ( lame_quality , wavfn ) , shell = True ) <EOL> if LIBXMP : <EOL> xmpfile = libxmp . XMPFiles ( file_path = wavfn ) <EOL> xmpfile2 = libxmp . XMPFiles ( file_path = mp3fn , open_forupdate = True ) <EOL> xmp = xmpfile . get_xmp ( ) <EOL> try : <EOL> if xmpfile2 . can_put_xmp ( xmp ) : <EOL> xmpfile2 . put_xmp ( xmp ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> except : <EOL> print "<STR_LIT>" <EOL> xmpfile . close_file ( ) <EOL> xmpfile2 . close_file ( ) <EOL> if delete_wav : <EOL> check_output ( '<STR_LIT>' . format ( wavfn ) , shell = True ) </s>
