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