def scan_dir_list( subdirlist , options , out_dir ): a = time.time() success = False for cur_dir in subdirlist : dr = DynamicRangeMeter() print_msg ( "\n------------------------------------------------------------ " ) print_msg ( "> Scan Dir: %s \n" % cur_dir ) cpu = multiprocessing.cpu_count() if ( options.disable_multithread == True ) : r = dr.scan_dir( cur_dir ) else: cpu = cpu / 2 if cpu <= 2: cpu = 2 else: cpu = int( round( cpu ) ) r = dr.scan_mp( cur_dir , cpu ) if r == 0: continue else: success = True write_results( dr , options , out_dir , cur_dir ) clock = time.time() - a return (success,clock,r)
def scan_dir_list(subdirlist, options, out_dir): a = time.time() success = False for cur_dir in subdirlist: dr = DynamicRangeMeter() dr.write_to_local_db(db_is_enabled()) print_msg( "\n------------------------------------------------------------ ") print_msg("> Scan Dir: %s \n" % cur_dir) cpu = multiprocessing.cpu_count() if (options.disable_multithread == True): r = dr.scan_dir(cur_dir) else: cpu = get_thread_cnt() r = dr.scan_mp(cur_dir, cpu) if r == 0: continue else: success = True write_results(dr, options, out_dir, cur_dir) clock = time.time() - a return (success, clock, r)
def read_audio_file_new( self , file_name , target ): time_a = time.time() full_command = self.__cmd (head, file) = os.path.split( file_name ) tmp_dir = tempfile.gettempdir() tmp_file = os.path.join( tmp_dir , file ) + ".wav" file_name = re.sub( "(\"|`|\$)" , r"\\\1" , file_name ) tmp_file = re.sub( "(\"|`|\$)" , r"_xyz_" , tmp_file ) full_command = full_command + " " + self.get_cmd_options( file_name , tmp_file ) #print_msg( full_command ) r = subprocess.call( full_command , shell=True , stderr=subprocess.PIPE , stdout=subprocess.PIPE ) #read_wav.read_wav( tmp_file ) ret_f = self.read_wav( tmp_file , target ) if os.path.exists( tmp_file ) : os.remove( tmp_file ) else: print_msg( file_name + ": unsupported encoder" ) time_b = time.time() dr14_log_info( "AudioFileReader.read_audio_file_new: Clock: %2.8f" % (time_b - time_a ) ) return ret_f
def scan_dir_list(subdirlist, options, out_dir): a = time.time() success = False for cur_dir in subdirlist: dr = DynamicRangeMeter() dr.write_to_local_db(db_is_enabled()) print_msg("\n------------------------------------------------------------ ") print_msg("> Scan Dir: %s \n" % cur_dir) cpu = multiprocessing.cpu_count() if options.disable_multithread == True: r = dr.scan_dir(cur_dir) else: cpu = get_thread_cnt() r = dr.scan_mp(cur_dir, cpu) if r == 0: continue else: success = True write_results(dr, options, out_dir, cur_dir) clock = time.time() - a return (success, clock, r)
def fwrite_dr(self, file_name, tm, ext_table=False, std_out=False, append=False, dr_database=True): if ext_table: wr = WriteDrExtended() else: wr = WriteDr() wr.set_loudness_war_db_compatible(dr_database) self.table_txt = wr.write_dr(self, tm) if std_out: print_out(self.table_txt) return if append: file_mode = "a" else: file_mode = "w" try: out_file = codecs.open(file_name, file_mode, "utf-8-sig") except: print_msg("File opening error [%s] :" % file_name, sys.exc_info()[0]) return False out_file.write(self.table_txt) out_file.close() return True
def fwrite_dr( self , file_name , tm , ext_table=False , std_out=False , append=False , dr_database=True ): if ext_table : wr = WriteDrExtended() else : wr = WriteDr() wr.set_dr_database( dr_database ) self.table_txt = wr.write_dr( self , tm ) if std_out: print_out( self.table_txt ) return if append : file_mode = "a" else : file_mode = "w" try: out_file = codecs.open( file_name , file_mode , "utf-8-sig" ) except: print_msg ( "File opening error [%s] :" % file_name , sys.exc_info()[0] ) return False out_file.write( self.table_txt ) out_file.close() return True
def test_compress_modules(): try: import scipy except: print_msg( "The compression function require the installation of SciPy: http://www.scipy.org/" ) return False return True
def virt_compute(self): (foo,fn) = os.path.split( self.getFileName() ) title = self.getMetaData().get_value( fn , "title" ) print_msg( "Track Title: %s " % title ) at = self.getAudioTrack() plot_track( at.Y , at.Fs )
def test_hist_modules(): try: import matplotlib except ImportError: print_msg( "The histogram function require the installation of MatPlotLib: http://matplotlib.sourceforge.net" ) return False return True
def test_matplotlib_modules( fun_name ): try: import matplotlib except ImportError: print_msg( "The %s function require the installation of MatPlotLib: http://matplotlib.sourceforge.net" % fun_name ) return False return True
def virt_compute(self): (foo, fn) = os.path.split(self.getFileName()) title = self.getMetaData().get_value(fn, "title") print_msg("Track Title: %s " % title) at = self.getAudioTrack() plot_track(at.Y, at.Fs)
def test_compress_modules(): try: import scipy except: print_msg( "The compression function require the installation of SciPy: http://www.scipy.org/") return False return True
def test_hist_modules(): try: import matplotlib except ImportError: print_msg( "The histogram function require the installation of MatPlotLib: http://matplotlib.sourceforge.net") return False return True
def test_matplotlib_modules(fun_name): try: import matplotlib except ImportError: print_msg( "The %s function require the installation of MatPlotLib: http://matplotlib.sourceforge.net" % fun_name) return False return True
def virt_compute(self): (foo, fn) = os.path.split(self.getFileName()) title = self.getMetaData().get_value(fn, "title") print_msg("Track Title: %s " % title) at = self.getAudioTrack() compute_lev_hist(at.Y, at.Fs, self.getDuration(), title=title)
def virt_compute(self): (foo,fn) = os.path.split( self.getFileName() ) title = self.getMetaData().get_value( fn , "title" ) print_msg( "Track Title: %s " % title ) at = self.getAudioTrack() compute_hist( at.Y , at.Fs , self.getDuration() , title=title )
def __compute_and_append( self , at , file_name ): duration = StructDuration() #( dr14, dB_peak, dB_rms ) = self.compute_dr.compute( at.Y , at.Fs ) ( dr14, dB_peak, dB_rms ) = compute_dr14( at.Y , at.Fs , duration ) self.dr14 = self.dr14 + dr14 res = { 'file_name': file_name , 'dr14': dr14 , 'dB_peak': dB_peak , 'dB_rms': dB_rms , 'duration':duration.to_str() } self.res_list.append(res) print_msg( file_name + ": \t DR " + str( int(dr14) ) )
def virt_compute(self): (head, file_n) = os.path.split( self.getFileName() ) comp = DynCompressor() comp.set_compression_modality( self.compression_modality ) full_file = os.path.join( tempfile.gettempdir() , "%s%s.wav" % ( file_n , "-compressed-" ) ) at = self.getAudioTrack() cY = comp.dyn_compressor( at.Y , at.Fs ) wav_write( full_file , at.Fs , cY ) print_msg( "The resulting compressed audiotrack has been written in: %s " % full_file )
def virt_compute(self): (head, file_n) = os.path.split(self.getFileName()) comp = DynCompressor() comp.set_compression_modality(self.compression_modality) full_file = os.path.join(tempfile.gettempdir(), "%s%s.wav" % (file_n, "-compressed-")) at = self.getAudioTrack() cY = comp.dyn_compressor(at.Y, at.Fs) wav_write(full_file, at.Fs, cY) print_msg( "The resulting compressed audiotrack has been written in: %s " % full_file)
def read_wav(self, file_name, target): time_a = time.time() convert_8_bit = numpy.float32(2**8 + 1.0) convert_16_bit = numpy.float32(2**15 + 1.0) convert_32_bit = numpy.float32(2**31 + 1.0) try: wave_read = wave.open(file_name, 'r') target.channels = wave_read.getnchannels() target.Fs = wave_read.getframerate() target.sample_width = wave_read.getsampwidth() nframes = wave_read.getnframes() #print_msg( file_name + "!!!!!!!!!!!!: " + str(target.channels) + " " + str(target.sample_width ) + " " + str( target.Fs ) + " " + str( nframes ) ) X = wave_read.readframes(wave_read.getnframes()) sample_type = "int%d" % (target.sample_width * 8) target.Y = numpy.fromstring(X, dtype=sample_type).reshape( nframes, target.channels) wave_read.close() if sample_type == 'int16': target.Y = target.Y / (convert_16_bit) elif sample_type == 'int32': target.Y = target.Y / (convert_32_bit) else: target.Y = target.Y / (convert_8_bit) #print_msg( "target.Y: " + str(target.Y.dtype) ) except: self.__init__() print_msg("Unexpected error: %s" % str(sys.exc_info())) print_msg("\n - ERROR ! ") return False time_b = time.time() dr14_log_info("AudioFileReader.read_wav: Clock: %2.8f" % (time_b - time_a)) return True
def read_wav( self , file_name , target ): time_a = time.time() convert_8_bit = numpy.float32( 2**8 + 1.0 ) convert_16_bit = numpy.float32( 2**15 + 1.0 ) convert_32_bit = numpy.float32( 2**31 + 1.0 ) try: wave_read = wave.open( file_name , 'r' ) target.channels = wave_read.getnchannels() target.Fs = wave_read.getframerate() target.sample_width = wave_read.getsampwidth() nframes = wave_read.getnframes() #print_msg( file_name + "!!!!!!!!!!!!: " + str(target.channels) + " " + str(target.sample_width ) + " " + str( target.Fs ) + " " + str( nframes ) ) X = wave_read.readframes( wave_read.getnframes() ) sample_type = "int%d" % ( target.sample_width * 8 ) target.Y = numpy.fromstring(X, dtype=sample_type ).reshape( nframes , target.channels ) wave_read.close() if sample_type == 'int16': target.Y = target.Y / ( convert_16_bit ) elif sample_type == 'int32': target.Y = target.Y / ( convert_32_bit ) else : target.Y = target.Y / ( convert_8_bit ) #print_msg( "target.Y: " + str(target.Y.dtype) ) except: self.__init__() print_msg ( "Unexpected error: %s" % str( sys.exc_info() ) ) print_msg ( "\n - ERROR ! " ) return False time_b = time.time() dr14_log_info( "AudioFileReader.read_wav: Clock: %2.8f" % (time_b - time_a ) ) return True
def __compute_and_append(self, at, file_name): duration = StructDuration() #( dr14, dB_peak, dB_rms ) = self.compute_dr.compute( at.Y , at.Fs ) (dr14, dB_peak, dB_rms) = compute_dr14(at.Y, at.Fs, duration) sha1 = sha1_track_v1(at.Y, at.get_file_ext_code()) self.dr14 = self.dr14 + dr14 res = {'file_name': file_name, 'dr14': dr14, 'dB_peak': dB_peak, 'dB_rms': dB_rms, 'duration': duration.to_str(), 'sha1': sha1} self.res_list.append(res) print_msg(file_name + ": \t DR " + str(int(dr14)))
def run_mp( self , dir_name , lock_j , lock_res_list , job_free , res_array_sh ): at = AudioTrack() duration = StructDuration() #print_msg("start .... ") while True: #Aquire the next free job lock_j.acquire() if job_free.value >= len(res_array_sh): lock_j.release() return curr_job = job_free.value file_name = res_array_sh[curr_job].file_name job_free.value = job_free.value + 1 lock_j.release() full_file = os.path.join( dir_name , file_name ) #print ( full_file ) if at.open( full_file ): ( dr14, dB_peak, dB_rms ) = compute_dr14( at.Y , at.Fs , duration ) lock_res_list.acquire() print_msg( file_name + ": \t DR " + str( int(dr14) ) ) #res_list[curr_job] = { 'file_name': file_name , 'dr14': dr14 , 'dB_peak': dB_peak , 'dB_rms': dB_rms , 'duration':duration.to_str() } res_array_sh[curr_job].dr14 = dr14 res_array_sh[curr_job].dB_peak = dB_peak res_array_sh[curr_job].dB_rms = dB_rms res_array_sh[curr_job].duration = duration.to_float() lock_res_list.release() else: print_msg( "- fail - " + full_file )
def read_audio_file_new(self, file_name, target): time_a = time.time() full_command = self.__cmd (head, file) = os.path.split(file_name) tmp_dir = tempfile.gettempdir() tmp_file = os.path.join(tmp_dir, file) + ".wav" file_name = re.sub("(\"|`|\$)", r"\\\1", file_name) tmp_file = re.sub("(\"|`|\$)", r"_xyz_", tmp_file) full_command = full_command + " " + \ self.get_cmd_options(file_name, tmp_file) #print_msg( full_command ) r = subprocess.Popen(full_command, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) stdout_data, stderr_data = r.communicate() #read_wav.read_wav( tmp_file ) ret_f = self.read_wav(tmp_file, target) if os.path.exists(tmp_file): os.remove(tmp_file) else: print_msg(file_name + ": unsupported encoder") time_b = time.time() dr14_log_info("AudioFileReader.read_audio_file_new: Clock: %2.8f" % (time_b - time_a)) return ret_f
def run_mp(self, job_queue_sh, res_queue_sh): at = AudioTrack() duration = StructDuration() #print_msg("start .... ") while True: if job_queue_sh.empty(): return job = job_queue_sh.get() full_file = os.path.join(job.dir_name, job.file_name) #print ( full_file ) if at.open(full_file): (dr14, dB_peak, dB_rms) = compute_dr14(at.Y, at.Fs, duration) sha1 = sha1_track_v1(at.Y, at.get_file_ext_code()) print_msg(job.file_name + ": \t DR " + str(int(dr14))) flush_msg() job.dr14 = dr14 job.dB_peak = dB_peak job.dB_rms = dB_rms job.duration = duration.to_float() job.sha1 = sha1 else: job.fail = True print_msg("- fail - " + full_file) res_queue_sh.put(job) job_queue_sh.task_done()
def run_mp( self , job_queue_sh , res_queue_sh ): at = AudioTrack() duration = StructDuration() #print_msg("start .... ") while True: if job_queue_sh.empty() : return job = job_queue_sh.get() full_file = os.path.join( job.dir_name , job.file_name ) #print ( full_file ) if at.open( full_file ): ( dr14, dB_peak, dB_rms ) = compute_dr14( at.Y , at.Fs , duration ) sha1 = sha1_track_v1( at.Y , at.get_file_ext_code() ) print_msg( job.file_name + ": \t DR " + str( int(dr14) ) ) flush_msg() job.dr14 = dr14 job.dB_peak = dB_peak job.dB_rms = dB_rms job.duration = duration.to_float() job.sha1 = sha1 else: job.fail = True print_msg( "- fail - " + full_file ) res_queue_sh.put( job ) job_queue_sh.task_done()
def write_results(dr, options, out_dir, cur_dir): out_list = "" table_format = not (options.basic_table) if out_dir == None: full_out_dir = os.path.join(cur_dir) else: full_out_dir = out_dir print_msg("DR = " + str(dr.dr14)) if not (os.access(full_out_dir, os.W_OK)): full_out_dir = tempfile.gettempdir() print_msg("--------------------------------------------------------------- ") print_msg("- ATTENTION !") print_msg("- You do not have the write permission for the directory: %s " % full_out_dir) print_msg("- The result files will be written in the tmp dir: %s " % full_out_dir) print_msg("--------------------------------------------------------------- ") if options.print_std_out: dr.fwrite_dr("", TextTable(), table_format, True) if options.turn_off_out: return all_tables = False if "a" in options.out_tables: all_tables = True tables_list = { "b": ["dr14_bbcode.txt", BBcodeTable()], "t": ["dr14.txt", TextTable()], "h": ["dr14.html", HtmlTable()], "w": ["dr14_mediawiki.txt", MediaWikiTable()], } out_list = "" dr.write_to_local_database() for code in tables_list.keys(): if code in options.out_tables or all_tables: dr.fwrite_dr( os.path.join(full_out_dir, tables_list[code][0]), tables_list[code][1], table_format, append=options.append, dr_database=options.dr_database, ) out_list = out_list + " %s " % tables_list[code][0] print_msg("") print_msg("- The full result has been written in the files: %s" % out_list) print_msg("- located in the directory: ") print_msg(full_out_dir) print_msg("")
def run_analysis_opt(options, path_name): flag = False if options.compress: if test_compress_modules() == False: sys.exit(1) print_msg("Start compressor:") comp = AudioCompressor() comp.setCompressionModality(options.compress) comp.compute_track(path_name) flag = True if options.spectrogram: if test_matplotlib_modules("Spectrogram") == False: sys.exit(1) print_msg("Start spectrogram:") spectr = AudioSpectrogram() spectr.compute_track(path_name) flag = True if options.plot_track: if test_matplotlib_modules("Plot track") == False: sys.exit(1) print_msg("Start Plot Track:") spectr = AudioPlotTrack() spectr.compute_track(path_name) flag = True if options.plot_track_dst: if test_matplotlib_modules("Plot track dst") == False: sys.exit(1) print_msg("Start Plot Track:") spectr = AudioPlotTrackDistribution() spectr.compute_track(path_name) flag = True if options.histogram: if test_hist_modules() == False: sys.exit(1) print_msg("Start histogram:") hist = AudioDrHistogram() hist.compute_track(path_name) flag = True if options.lev_histogram: if test_hist_modules() == False: sys.exit(1) print_msg("Start level histogram:") hist = AudioLevelHistogram() hist.compute_track(path_name) flag = True if options.dynamic_vivacity: if test_hist_modules() == False: sys.exit(1) print_msg("Start Dynamic vivacity:") viva = AudioDynVivacity() viva.compute_track(path_name) flag = True return flag
def write_results(dr, options, out_dir, cur_dir): out_list = "" table_format = not (options.basic_table) if out_dir == None: full_out_dir = os.path.join(cur_dir) else: full_out_dir = out_dir print_msg("DR = " + str(dr.dr14)) if not (os.access(full_out_dir, os.W_OK)): full_out_dir = tempfile.gettempdir() print_msg( "--------------------------------------------------------------- ") print_msg("- ATTENTION !") print_msg( "- You do not have the write permission for the directory: %s " % full_out_dir) print_msg("- The result files will be written in the tmp dir: %s " % full_out_dir) print_msg( "--------------------------------------------------------------- ") if options.print_std_out: dr.fwrite_dr("", TextTable(), table_format, True) if options.turn_off_out: return all_tables = False if 'a' in options.out_tables: all_tables = True tables_list = { 'b': ["dr14_bbcode.txt", BBcodeTable()], 't': ["dr14.txt", TextTable()], 'h': ["dr14.html", HtmlTable()], 'w': ["dr14_mediawiki.txt", MediaWikiTable()] } out_list = "" dr.write_to_local_database() for code in tables_list.keys(): if code in options.out_tables or all_tables: dr.fwrite_dr(os.path.join(full_out_dir, tables_list[code][0]), tables_list[code][1], table_format, append=options.append, dr_database=options.dr_database) out_list = out_list + " %s " % tables_list[code][0] print_msg("") print_msg("- The full result has been written in the files: %s" % out_list) print_msg("- located in the directory: ") print_msg(full_out_dir) print_msg("")