amine@2: amine@2: from auditok import ADSFactory, AudioEnergyValidator, StreamTokenizer, player_for amine@2: import pyaudio amine@2: import sys amine@2: amine@10: try: amine@2: amine@10: energy_threshold = 45 amine@10: duration = 10 # seconds amine@2: amine@2: amine@10: if len(sys.argv) > 1: amine@10: energy_threshold = float(sys.argv[1]) amine@2: amine@10: if len(sys.argv) > 2: amine@10: duration = float(sys.argv[2]) amine@2: amine@10: # record = True so that we'll be able to rewind the source. amine@10: # max_time = 10: read 10 seconds from the microphone amine@10: asource = ADSFactory.ads(record=True, max_time = duration) amine@2: amine@10: validator = AudioEnergyValidator(sample_width=asource.get_sample_width(), energy_threshold = energy_threshold) amine@10: tokenizer = StreamTokenizer(validator=validator, min_length=20, max_length=250, max_continuous_silence=30) amine@2: amine@10: player = player_for(asource) amine@2: amine@10: def echo(data, start, end): amine@10: print("Acoustic activity at: {0}--{1}".format(start, end)) amine@10: player.play(b''.join(data)) amine@2: amine@10: asource.open() amine@2: amine@10: print("\n ** Make some noise (dur:{}, energy:{})...".format(duration, energy_threshold)) amine@2: amine@10: tokenizer.tokenize(asource, callback=echo) amine@10: amine@10: asource.close() amine@10: player.stop() amine@10: amine@10: except KeyboardInterrupt: amine@10: amine@10: player.stop() amine@10: asource.close() amine@10: sys.exit(0) amine@10: amine@10: except Exception as e: amine@10: amine@10: sys.stderr.write(str(e) + "\n") amine@10: sys.exit(1)