<s> """<STR_LIT>""" <EOL> import optparse , math , sys <EOL> import scikits . audiolab as audiolab <EOL> import ImageFilter , ImageChops , Image , ImageDraw , ImageColor <EOL> import numpy <EOL> class TestAudioFile ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , num_frames , has_broken_header = False ) : <EOL> self . seekpoint = <NUM_LIT:0> <EOL> self . num_frames = num_frames <EOL> self . has_broken_header = has_broken_header <EOL> def seek ( self , seekpoint ) : <EOL> self . seekpoint = seekpoint <EOL> def get_nframes ( self ) : <EOL> return self . num_frames <EOL> def get_samplerate ( self ) : <EOL> return <NUM_LIT> <EOL> def get_channels ( self ) : <EOL> return <NUM_LIT:1> <EOL> def read_frames ( self , frames_to_read ) : <EOL> if self . has_broken_header and self . seekpoint + frames_to_read > self . num_frames / <NUM_LIT:2> : <EOL> raise IOError ( ) <EOL> num_frames_left = self . num_frames - self . seekpoint <EOL> will_read = num_frames_left if num_frames_left < frames_to_read else frames_to_read <EOL> self . seekpoint += will_read <EOL> return numpy . random . random ( will_read ) * <NUM_LIT:2> - <NUM_LIT:1> <EOL> class AudioProcessor ( object ) : <EOL> def __init__ ( self , audio_file , fft_size , channel , window_function = numpy . ones ) : <EOL> self . fft_size = fft_size <EOL> self . window = window_function ( self . fft_size ) <EOL> self . audio_file = audio_file <EOL> self . frames = audio_file . get_nframes ( ) <EOL> self . samplerate = audio_file . get_samplerate ( ) <EOL> self . channels = audio_file . get_channels ( ) <EOL> self . spectrum_range = None <EOL> self . lower = <NUM_LIT:100> <EOL> self . higher = <NUM_LIT> <EOL> self . lower_log = math . log10 ( self . lower ) <EOL> self . higher_log = math . log10 ( self . higher ) <EOL> self . clip = lambda val , low , high : min ( high , max ( low , val ) ) <EOL> self . channel = channel <EOL> def read ( self , start , size , resize_if_less = False ) : <EOL> """<STR_LIT>""" <EOL> add_to_start = <NUM_LIT:0> <EOL> add_to_end = <NUM_LIT:0> <EOL> if start < <NUM_LIT:0> : <EOL> if size + start <= <NUM_LIT:0> : <EOL> return numpy . zeros ( size ) if resize_if_less else numpy . array ( [ ] ) <EOL> else : <EOL> self . audio_file . seek ( <NUM_LIT:0> ) <EOL> add_to_start = - start <EOL> to_read = size + start <EOL> if to_read > self . frames : <EOL> add_to_end = to_read - self . frames <EOL> to_read = self . frames <EOL> else : <EOL> self . audio_file . seek ( start ) <EOL> to_read = size <EOL> if start + to_read >= self . frames : <EOL> to_read = self . frames - start <EOL> add_to_end = size - to_read <EOL> try : <EOL> samples = self . audio_file . read_frames ( to_read ) <EOL> except IOError : <EOL> return numpy . zeros ( size ) if resize_if_less else numpy . zeros ( <NUM_LIT:2> ) <EOL> if self . channels > <NUM_LIT:1> : <EOL> if self . channel == <NUM_LIT:1> : <EOL> samples = samples [ : , <NUM_LIT:0> ] <EOL> if self . channel == <NUM_LIT:2> : <EOL> samples = samples [ : , <NUM_LIT:1> ] <EOL> if resize_if_less and ( add_to_start > <NUM_LIT:0> or add_to_end > <NUM_LIT:0> ) : <EOL> if add_to_start > <NUM_LIT:0> : <EOL> samples = numpy . concatenate ( ( numpy . zeros ( add_to_start ) , samples ) , axis = <NUM_LIT:1> ) <EOL> if add_to_end > <NUM_LIT:0> : <EOL> samples = numpy . resize ( samples , size ) <EOL> samples [ size - add_to_end : ] = <NUM_LIT:0> <EOL> return samples <EOL> """<STR_LIT>""" <EOL> def spectral_centroid ( self , seek_point , spec_range = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> samples = self . read ( seek_point - self . fft_size / <NUM_LIT:2> , self . fft_size , True ) <EOL> samples *= self . window <EOL> fft = numpy . fft . fft ( samples ) <EOL> spectrum = numpy . abs ( fft [ : fft . shape [ <NUM_LIT:0> ] / <NUM_LIT:2> + <NUM_LIT:1> ] ) / float ( self . fft_size ) <EOL> length = numpy . float64 ( spectrum . shape [ <NUM_LIT:0> ] ) <EOL> db_spectrum = ( ( <NUM_LIT:20> * ( numpy . log10 ( spectrum + <NUM_LIT> ) ) ) . clip ( - spec_range , <NUM_LIT:0.0> ) + spec_range ) / spec_range <EOL> energy = spectrum . sum ( ) <EOL> spectral_centroid = <NUM_LIT:0> <EOL> if energy > <NUM_LIT> : <EOL> if self . spectrum_range == None : <EOL> self . spectrum_range = numpy . arange ( length ) <EOL> spectral_centroid = ( spectrum * self . spectrum_range ) . sum ( ) / ( energy * ( length - <NUM_LIT:1> ) ) * self . samplerate * <NUM_LIT:0.5> <EOL> spectral_centroid = ( math . log10 ( self . clip ( spectral_centroid , self . lower , self . higher ) ) - self . lower_log ) / ( self . higher_log - self . lower_log ) <EOL> return ( spectral_centroid , db_spectrum ) <EOL> def peaks ( self , start_seek , end_seek ) : <EOL> """<STR_LIT>""" <EOL> block_size = <NUM_LIT> <EOL> max_index = - <NUM_LIT:1> <EOL> max_value = - <NUM_LIT:1> <EOL> min_index = - <NUM_LIT:1> <EOL> min_value = <NUM_LIT:1> <EOL> if end_seek > self . frames : <EOL> end_seek = self . frames <EOL> if block_size > end_seek - start_seek : <EOL> block_size = end_seek - start_seek <EOL> if block_size <= <NUM_LIT:1> : <EOL> samples = self . read ( start_seek , <NUM_LIT:1> ) <EOL> return samples [ <NUM_LIT:0> ] , samples [ <NUM_LIT:0> ] <EOL> elif block_size == <NUM_LIT:2> : <EOL> samples = self . read ( start_seek , True ) <EOL> return samples [ <NUM_LIT:0> ] , samples [ <NUM_LIT:1> ] <EOL> for i in range ( start_seek , end_seek , block_size ) : <EOL> samples = self . read ( i , block_size ) <EOL> local_max_index = numpy . argmax ( samples ) <EOL> local_max_value = samples [ local_max_index ] <EOL> if local_max_value > max_value : <EOL> max_value = local_max_value <EOL> max_index = local_max_index <EOL> local_min_index = numpy . argmin ( samples ) <EOL> local_min_value = samples [ local_min_index ] <EOL> if local_min_value < min_value : <EOL> min_value = local_min_value <EOL> min_index = local_min_index <EOL> return ( min_value , max_value ) if min_index < max_index else ( max_value , min_value ) <EOL> def interpolate_colors ( colors , flat = False , num_colors = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> palette = [ ] <EOL> for i in range ( num_colors ) : <EOL> index = ( i * ( len ( colors ) - <NUM_LIT:1> ) ) / ( num_colors - <NUM_LIT:1.0> ) <EOL> index_int = int ( index ) <EOL> alpha = index - float ( index_int ) <EOL> if alpha > <NUM_LIT:0> : <EOL> r = ( <NUM_LIT:1.0> - alpha ) * colors [ index_int ] [ <NUM_LIT:0> ] + alpha * colors [ index_int + <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> g = ( <NUM_LIT:1.0> - alpha ) * colors [ index_int ] [ <NUM_LIT:1> ] + alpha * colors [ index_int + <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> b = ( <NUM_LIT:1.0> - alpha ) * colors [ index_int ] [ <NUM_LIT:2> ] + alpha * colors [ index_int + <NUM_LIT:1> ] [ <NUM_LIT:2> ] <EOL> else : <EOL> r = ( <NUM_LIT:1.0> - alpha ) * colors [ index_int ] [ <NUM_LIT:0> ] <EOL> g = ( <NUM_LIT:1.0> - alpha ) * colors [ index_int ] [ <NUM_LIT:1> ] <EOL> b = ( <NUM_LIT:1.0> - alpha ) * colors [ index_int ] [ <NUM_LIT:2> ] <EOL> if flat : <EOL> palette . extend ( ( int ( r ) , int ( g ) , int ( b ) ) ) <EOL> else : <EOL> palette . append ( ( int ( r ) , int ( g ) , int ( b ) ) ) <EOL> return palette <EOL> class WaveformImage ( object ) : <EOL> def __init__ ( self , image_width , image_height , palette ) : <EOL> self . image = Image . new ( "<STR_LIT>" , ( image_width , image_height ) ) <EOL> self . image_width = image_width <EOL> self . image_height = image_height <EOL> self . draw = ImageDraw . Draw ( self . image ) <EOL> self . previous_x , self . previous_y = None , None <EOL> if palette == <NUM_LIT:2> : <EOL> colors = [ <EOL> ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT:64> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT:30> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:8> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:20> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT:8> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT:0> ) <EOL> ] <EOL> elif palette == <NUM_LIT:1> : <EOL> colors = [ <EOL> ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> / <NUM_LIT:4> , <NUM_LIT> / <NUM_LIT:4> , <NUM_LIT> / <NUM_LIT:4> ) , <EOL> ( <NUM_LIT> / <NUM_LIT:2> , <NUM_LIT:100> / <NUM_LIT:2> , <NUM_LIT> / <NUM_LIT:2> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:100> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT:30> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) <EOL> ] <EOL> self . color_lookup = interpolate_colors ( colors ) <EOL> self . pix = self . image . load ( ) <EOL> def color_from_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return ImageColor . getrgb ( "<STR_LIT>" % ( int ( ( <NUM_LIT:1.0> - value ) * <NUM_LIT> ) , <NUM_LIT> , <NUM_LIT:50> ) ) <EOL> def draw_peaks ( self , x , peaks , spectral_centroid ) : <EOL> """<STR_LIT>""" <EOL> y1 = self . image_height * <NUM_LIT:0.5> - peaks [ <NUM_LIT:0> ] * ( self . image_height - <NUM_LIT:4> ) * <NUM_LIT:0.5> <EOL> y2 = self . image_height * <NUM_LIT:0.5> - peaks [ <NUM_LIT:1> ] * ( self . image_height - <NUM_LIT:4> ) * <NUM_LIT:0.5> <EOL> line_color = self . color_lookup [ int ( spectral_centroid * <NUM_LIT> ) ] <EOL> if self . previous_y != None : <EOL> self . draw . line ( [ self . previous_x , self . previous_y , x , y1 , x , y2 ] , line_color ) <EOL> else : <EOL> self . draw . line ( [ x , y1 , x , y2 ] , line_color ) <EOL> self . previous_x , self . previous_y = x , y2 <EOL> self . draw_anti_aliased_pixels ( x , y1 , y2 , line_color ) <EOL> def draw_anti_aliased_pixels ( self , x , y1 , y2 , color ) : <EOL> """<STR_LIT>""" <EOL> y_max = max ( y1 , y2 ) <EOL> y_max_int = int ( y_max ) <EOL> alpha = y_max - y_max_int <EOL> if alpha > <NUM_LIT:0.0> and alpha < <NUM_LIT:1.0> and y_max_int + <NUM_LIT:1> < self . image_height : <EOL> current_pix = self . pix [ x , y_max_int + <NUM_LIT:1> ] <EOL> r = int ( ( <NUM_LIT:1> - alpha ) * current_pix [ <NUM_LIT:0> ] + alpha * color [ <NUM_LIT:0> ] ) <EOL> g = int ( ( <NUM_LIT:1> - alpha ) * current_pix [ <NUM_LIT:1> ] + alpha * color [ <NUM_LIT:1> ] ) <EOL> b = int ( ( <NUM_LIT:1> - alpha ) * current_pix [ <NUM_LIT:2> ] + alpha * color [ <NUM_LIT:2> ] ) <EOL> self . pix [ x , y_max_int + <NUM_LIT:1> ] = ( r , g , b ) <EOL> y_min = min ( y1 , y2 ) <EOL> y_min_int = int ( y_min ) <EOL> alpha = <NUM_LIT:1.0> - ( y_min - y_min_int ) <EOL> if alpha > <NUM_LIT:0.0> and alpha < <NUM_LIT:1.0> and y_min_int - <NUM_LIT:1> >= <NUM_LIT:0> : <EOL> current_pix = self . pix [ x , y_min_int - <NUM_LIT:1> ] <EOL> r = int ( ( <NUM_LIT:1> - alpha ) * current_pix [ <NUM_LIT:0> ] + alpha * color [ <NUM_LIT:0> ] ) <EOL> g = int ( ( <NUM_LIT:1> - alpha ) * current_pix [ <NUM_LIT:1> ] + alpha * color [ <NUM_LIT:1> ] ) <EOL> b = int ( ( <NUM_LIT:1> - alpha ) * current_pix [ <NUM_LIT:2> ] + alpha * color [ <NUM_LIT:2> ] ) <EOL> self . pix [ x , y_min_int - <NUM_LIT:1> ] = ( r , g , b ) <EOL> def save ( self , filename ) : <EOL> a = <NUM_LIT> <EOL> for x in range ( self . image_width ) : <EOL> self . pix [ x , self . image_height / <NUM_LIT:2> ] = tuple ( map ( lambda p : p + a , self . pix [ x , self . image_height / <NUM_LIT:2> ] ) ) <EOL> self . image . save ( filename ) <EOL> class SpectrogramImage ( object ) : <EOL> def __init__ ( self , image_width , image_height , fft_size , f_max , f_min , nyquist_freq , palette ) : <EOL> self . image = Image . new ( "<STR_LIT:P>" , ( image_height , image_width ) ) <EOL> self . image_width = image_width <EOL> self . image_height = image_height <EOL> self . fft_size = fft_size <EOL> self . f_max = f_max <EOL> self . f_min = f_min <EOL> self . nyquist_freq = nyquist_freq <EOL> self . palette = palette <EOL> if nyquist_freq < f_max : <EOL> print "<STR_LIT>" % ( f_max , nyquist_freq ) <EOL> if palette == <NUM_LIT:2> : <EOL> colors = [ <EOL> ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT:64> , <NUM_LIT> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT:30> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:255> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:8> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:20> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT:8> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT:0> ) <EOL> ] <EOL> elif palette == <NUM_LIT:1> : <EOL> colors = [ <EOL> ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> / <NUM_LIT:4> , <NUM_LIT> / <NUM_LIT:4> , <NUM_LIT> / <NUM_LIT:4> ) , <EOL> ( <NUM_LIT> / <NUM_LIT:2> , <NUM_LIT:100> / <NUM_LIT:2> , <NUM_LIT> / <NUM_LIT:2> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:100> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT:30> ) , <EOL> ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) <EOL> ] <EOL> self . image . putpalette ( interpolate_colors ( colors , True ) ) <EOL> self . y_to_bin = [ ] <EOL> y_min = math . log10 ( f_min ) <EOL> y_max = math . log10 ( f_max ) <EOL> for y in range ( self . image_height ) : <EOL> freq = f_min + y / ( image_height - <NUM_LIT:1.0> ) * ( f_max - f_min ) <EOL> bin = freq / nyquist_freq * ( self . fft_size / <NUM_LIT:2> + <NUM_LIT:1> ) <EOL> if bin < self . fft_size / <NUM_LIT:2> : <EOL> alpha = bin - int ( bin ) <EOL> self . y_to_bin . append ( ( int ( bin ) , alpha * <NUM_LIT:255> ) ) <EOL> self . pixels = [ ] <EOL> def draw_spectrum ( self , x , spectrum ) : <EOL> for ( index , alpha ) in self . y_to_bin : <EOL> self . pixels . append ( int ( ( ( <NUM_LIT> - alpha ) * spectrum [ index ] + alpha * spectrum [ index + <NUM_LIT:1> ] ) ) ) <EOL> for y in range ( len ( self . y_to_bin ) , self . image_height ) : <EOL> self . pixels . append ( <NUM_LIT:0> ) <EOL> def save ( self , filename ) : <EOL> self . image . putdata ( self . pixels ) <EOL> self . image . transpose ( Image . ROTATE_90 ) . save ( filename ) <EOL> def create_png ( input_filename , output_filename_w , output_filename_s , image_width , image_height , fft_size , f_max , f_min , wavefile , palette , channel ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" % input_file , <EOL> audio_file = audiolab . sndfile ( input_filename , '<STR_LIT>' ) <EOL> samples_per_pixel = audio_file . get_nframes ( ) / float ( image_width ) <EOL> nyquist_freq = ( audio_file . get_samplerate ( ) / <NUM_LIT:2> ) + <NUM_LIT:0.0> <EOL> """<STR_LIT>""" <EOL> processor = AudioProcessor ( audio_file , fft_size , channel , numpy . hanning ) <EOL> if wavefile == <NUM_LIT:1> : <EOL> waveform = WaveformImage ( image_width , image_height , palette ) <EOL> spectrogram = SpectrogramImage ( image_width , image_height , fft_size , f_max , f_min , nyquist_freq , palette ) <EOL> for x in range ( image_width ) : <EOL> if x % ( image_width / <NUM_LIT:10> ) == <NUM_LIT:0> : <EOL> sys . stdout . write ( '<STR_LIT:.>' ) <EOL> sys . stdout . flush ( ) <EOL> seek_point = int ( x * samples_per_pixel ) <EOL> next_seek_point = int ( ( x + <NUM_LIT:1> ) * samples_per_pixel ) <EOL> ( spectral_centroid , db_spectrum ) = processor . spectral_centroid ( seek_point ) <EOL> if wavefile == <NUM_LIT:1> : <EOL> peaks = processor . peaks ( seek_point , next_seek_point ) <EOL> waveform . draw_peaks ( x , peaks , spectral_centroid ) <EOL> spectrogram . draw_spectrum ( x , db_spectrum ) <EOL> if wavefile == <NUM_LIT:1> : <EOL> waveform . save ( output_filename_w ) <EOL> spectrogram . save ( output_filename_s ) <EOL> print "<STR_LIT>" <EOL> def processWav ( filename , channel ) : <EOL> """<STR_LIT>""" <EOL> audio_file = audiolab . sndfile ( filename , '<STR_LIT>' ) <EOL> import contextlib <EOL> import wave <EOL> with contextlib . closing ( wave . open ( filename , '<STR_LIT:r>' ) ) as f : <EOL> frames = f . getnframes ( ) <EOL> rate = f . getframerate ( ) <EOL> duration = frames / float ( rate ) <EOL> duration *= <NUM_LIT:30> <EOL> duration = int ( duration ) <EOL> samples_per_pixel = audio_file . get_nframes ( ) / float ( duration ) <EOL> nyquist_freq = ( audio_file . get_samplerate ( ) / <NUM_LIT:2> ) + <NUM_LIT:0.0> <EOL> processor = AudioProcessor ( audio_file , <NUM_LIT> , channel , numpy . hanning ) <EOL> centroids = [ ] <EOL> frequencies = [ ] <EOL> volumes = [ ] <EOL> for x in range ( duration ) : <EOL> seek_point = int ( x * samples_per_pixel ) <EOL> next_seek_point = int ( ( x + <NUM_LIT:1> ) * samples_per_pixel ) <EOL> ( spectral_centroid , db_spectrum ) = processor . spectral_centroid ( seek_point ) <EOL> peaks = processor . peaks ( seek_point , next_seek_point ) <EOL> centroids . append ( spectral_centroid ) <EOL> frequencies . append ( db_spectrum ) <EOL> volumes . append ( peaks ) <EOL> for i in range ( len ( volumes ) ) : <EOL> volumes [ i ] = abs ( volumes [ i ] [ <NUM_LIT:0> ] ) + abs ( volumes [ i ] [ <NUM_LIT:1> ] ) <EOL> for i in range ( len ( frequencies ) ) : <EOL> for j in range ( len ( frequencies [ i ] ) ) : <EOL> frequencies [ i ] [ j ] = round ( frequencies [ i ] [ j ] , <NUM_LIT:4> ) <EOL> return centroids , frequencies , volumes </s>
