def main(argv=None):
program_name = os.path.basename(sys.argv[<NUM_LIT:0>])<EOL>program_version = version<EOL>program_build_date = "<STR_LIT:%s>" % __updated__<EOL>program_version_string = '<STR_LIT>' % (program_version, program_build_date)<EOL>program_longdesc = '''<STR_LIT>''' <EOL>program_license = "<STR_LIT>"<EOL>if argv is None:<EOL><INDENT>argv = sys.argv[<NUM_LIT:1>:]<EOL><DEDENT>try:<EOL><INDENT>parser = OptionParser(version=program_version_string, epilog=program_longdesc, description=program_license)<EOL>group = OptionGroup(parser, "<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT:input>", help="<STR_LIT>", metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=str, default=None, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=float, default=None, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=str, default=None, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=str, default=None, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=str, default=None, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=str, default="<STR_LIT:1>", metavar="<STR_LIT>")<EOL>parser.add_option_group(group)<EOL>group = OptionGroup(parser, "<STR_LIT>", "<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=float, default=<NUM_LIT>, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=float, default=<NUM_LIT>, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=float, default=<NUM_LIT:5>, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=float, default=<NUM_LIT>, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>",  action="<STR_LIT:store_true>", default=False)<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=float, default=<NUM_LIT:50>, metavar="<STR_LIT>")<EOL>parser.add_option_group(group)<EOL>group = OptionGroup(parser, "<STR_LIT>", "<STR_LIT>")        <EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=int, default=<NUM_LIT>, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT:-c>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=int, default=<NUM_LIT:1>, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=int, default=<NUM_LIT:2>, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=int, default=None, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", type=int, default=<NUM_LIT>, metavar="<STR_LIT>")<EOL>parser.add_option_group(group)<EOL>group = OptionGroup(parser, "<STR_LIT>", "<STR_LIT>") <EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>", default=None, type=str, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>",  action="<STR_LIT:store_true>", default=False)<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>",  action="<STR_LIT:store_true>", default=False)<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>",  type=str, default=None, metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>",  type=str, default="<STR_LIT>", metavar="<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>",  type=str, default="<STR_LIT>", metavar="<STR_LIT>")<EOL>parser.add_option_group(group)<EOL>parser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>",  action="<STR_LIT:store_true>", default=False)<EOL>parser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>",  action="<STR_LIT:store_true>", default=False)<EOL>parser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>", help="<STR_LIT>",  type=str, default=None, metavar="<STR_LIT>")<EOL>(opts, args) = parser.parse_args(argv)<EOL>if opts.input == "<STR_LIT:->":<EOL><INDENT>asource = StdinAudioSource(sampling_rate = opts.sampling_rate,<EOL>sample_width = opts.sample_width,<EOL>channels = opts.channels)<EOL><DEDENT>elif opts.input is not None:<EOL><INDENT>asource = file_to_audio_source(filename=opts.input, filetype=opts.input_type, uc=opts.use_channel)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>asource = PyAudioSource(sampling_rate = opts.sampling_rate,<EOL>sample_width = opts.sample_width,<EOL>channels = opts.channels,<EOL>frames_per_buffer = opts.frame_per_buffer,<EOL>input_device_index = opts.input_device_index)<EOL><DEDENT>except Exception:<EOL><INDENT>sys.stderr.write("<STR_LIT>")<EOL>sys.stderr.write("<STR_LIT>")<EOL>sys.exit(<NUM_LIT:2>)<EOL><DEDENT><DEDENT>logger = logging.getLogger(LOGGER_NAME)<EOL>logger.setLevel(logging.DEBUG)<EOL>handler = logging.StreamHandler(sys.stdout)<EOL>if opts.quiet or not opts.debug:<EOL><INDENT>handler.setLevel(logging.CRITICAL)<EOL><DEDENT>else:<EOL><INDENT>handler.setLevel(logging.DEBUG)<EOL><DEDENT>logger.addHandler(handler)<EOL>if opts.debug_file is not None:<EOL><INDENT>logger.setLevel(logging.DEBUG)<EOL>opts.debug = True<EOL>handler = logging.FileHandler(opts.debug_file, "<STR_LIT:w>")<EOL>fmt = logging.Formatter('<STR_LIT>')<EOL>handler.setFormatter(fmt)<EOL>handler.setLevel(logging.DEBUG)<EOL>logger.addHandler(handler)<EOL><DEDENT>record = opts.output_main is not None or opts.plot or opts.save_image is not None<EOL>ads = ADSFactory.ads(audio_source = asource, block_dur = opts.analysis_window, max_time = opts.max_time, record = record)<EOL>validator = AudioEnergyValidator(sample_width=asource.get_sample_width(), energy_threshold=opts.energy_threshold)<EOL>if opts.drop_trailing_silence:<EOL><INDENT>mode = StreamTokenizer.DROP_TRAILING_SILENCE<EOL><DEDENT>else:<EOL><INDENT>mode = <NUM_LIT:0><EOL><DEDENT>analysis_window_per_second = <NUM_LIT:1.> / opts.analysis_window<EOL>tokenizer = StreamTokenizer(validator=validator, min_length=opts.min_duration * analysis_window_per_second,<EOL>max_length=int(opts.max_duration * analysis_window_per_second),<EOL>max_continuous_silence=opts.max_silence * analysis_window_per_second,<EOL>mode = mode)<EOL>observers = []<EOL>tokenizer_worker = None<EOL>if opts.output_tokens is not None:<EOL><INDENT>try:<EOL><INDENT>fname  = opts.output_tokens.format(N=<NUM_LIT:0>, start=<NUM_LIT:0>, end=<NUM_LIT:0>)<EOL>tok_type =  opts.output_type<EOL>if tok_type is None:<EOL><INDENT>tok_type = os.path.splitext(opts.output_tokens)[<NUM_LIT:1>][<NUM_LIT:1>:]<EOL><DEDENT>if tok_type == "<STR_LIT>": <EOL><INDENT>tok_type = "<STR_LIT>"<EOL><DEDENT>token_saver = TokenSaverWorker(name_format=opts.output_tokens, filetype=tok_type,<EOL>debug=opts.debug, logger=logger, sr=asource.get_sampling_rate(),<EOL>sw=asource.get_sample_width(),<EOL>ch=asource.get_channels())<EOL>observers.append(token_saver)<EOL><DEDENT>except Exception:<EOL><INDENT>sys.stderr.write("<STR_LIT>".format(opts.output_tokens))<EOL>sys.exit(<NUM_LIT:2>)<EOL><DEDENT><DEDENT>if opts.echo:<EOL><INDENT>try:<EOL><INDENT>player = player_for(asource)<EOL>player_worker = PlayerWorker(player=player, debug=opts.debug, logger=logger)<EOL>observers.append(player_worker)<EOL><DEDENT>except Exception:<EOL><INDENT>sys.stderr.write("<STR_LIT>")<EOL>sys.stderr.write("<STR_LIT>")<EOL>sys.exit(<NUM_LIT:2>)<EOL><DEDENT><DEDENT>if opts.command is not None and len(opts.command) > <NUM_LIT:0>:<EOL><INDENT>cmd_worker = CommandLineWorker(command=opts.command, debug=opts.debug, logger=logger)<EOL>observers.append(cmd_worker)<EOL><DEDENT>if not opts.quiet or opts.plot is not None or opts.save_image is not None:    <EOL><INDENT>oformat = opts.printf.replace("<STR_LIT>", "<STR_LIT:\n>").replace("<STR_LIT>", "<STR_LIT:\t>").replace("<STR_LIT>", "<STR_LIT:\r>")<EOL>converter = seconds_to_str_fromatter(opts.time_format)<EOL>log_worker = LogWorker(print_detections = not opts.quiet, output_format=oformat,<EOL>time_formatter=converter, logger=logger, debug=opts.debug)<EOL>observers.append(log_worker)<EOL><DEDENT>tokenizer_worker = TokenizerWorker(ads, tokenizer, opts.analysis_window, observers)<EOL>def _save_main_stream():<EOL><INDENT>main_type =  opts.output_type<EOL>if main_type is None:<EOL><INDENT>main_type = os.path.splitext(opts.output_main)[<NUM_LIT:1>][<NUM_LIT:1>:]<EOL><DEDENT>if main_type == "<STR_LIT>": <EOL><INDENT>main_type = "<STR_LIT>"<EOL><DEDENT>ads.close()<EOL>ads.rewind()<EOL>data = ads.get_audio_source().get_data_buffer()<EOL>if len(data) > <NUM_LIT:0>:<EOL><INDENT>save_audio_data(data=data, filename=opts.output_main, filetype=main_type, sr=asource.get_sampling_rate(),<EOL>sw = asource.get_sample_width(),<EOL>ch = asource.get_channels())<EOL><DEDENT><DEDENT>def _plot():<EOL><INDENT>import numpy as np<EOL>ads.close()<EOL>ads.rewind()<EOL>data = ads.get_audio_source().get_data_buffer()<EOL>signal = AudioEnergyValidator._convert(data, asource.get_sample_width())<EOL>detections = [(det[<NUM_LIT:3>] , det[<NUM_LIT:4>]) for det in log_worker.detections]<EOL>max_amplitude = <NUM_LIT:2>**(asource.get_sample_width() * <NUM_LIT:8> - <NUM_LIT:1>) - <NUM_LIT:1><EOL>energy_as_amp = np.sqrt(np.exp(opts.energy_threshold * np.log(<NUM_LIT:10>) / <NUM_LIT:10>)) / max_amplitude<EOL>plot_all(signal / max_amplitude, asource.get_sampling_rate(), energy_as_amp, detections, show = opts.plot, save_as = opts.save_image)<EOL><DEDENT>for obs in observers:<EOL><INDENT>obs.start()<EOL><DEDENT>tokenizer_worker.start()<EOL>while True:<EOL><INDENT>time.sleep(<NUM_LIT:1>)<EOL>if len(threading.enumerate()) == <NUM_LIT:1>:<EOL><INDENT>break<EOL><DEDENT><DEDENT>tokenizer_worker = None<EOL>if opts.output_main is not None:<EOL><INDENT>_save_main_stream()<EOL><DEDENT>if opts.plot or opts.save_image is not None:<EOL><INDENT>_plot()<EOL><DEDENT>return <NUM_LIT:0><EOL><DEDENT>except KeyboardInterrupt:<EOL><INDENT>if tokenizer_worker is not None:<EOL><INDENT>tokenizer_worker.stop()<EOL><DEDENT>for obs in observers:<EOL><INDENT>obs.stop()<EOL><DEDENT>if opts.output_main is not None:<EOL><INDENT>_save_main_stream()<EOL><DEDENT>if opts.plot or opts.save_image is not None:<EOL><INDENT>_plot()<EOL><DEDENT>return <NUM_LIT:0><EOL><DEDENT>except Exception as e:<EOL><INDENT>sys.stderr.write(program_name + "<STR_LIT>" + str(e) + "<STR_LIT:\n>")<EOL>sys.stderr.write("<STR_LIT>")<EOL>return <NUM_LIT:2><EOL><DEDENT>
Command line options.