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