Mercurial > hg > auditok
annotate demos/echo.py @ 9:c2ddae4d2c36
add .travis.yml
author | Amine Sehili <amine.sehili@gmail.com> |
---|---|
date | Tue, 24 Nov 2015 01:41:19 +0100 |
parents | edee860b9f61 |
children | 9be2d0ca4c00 |
rev | line source |
---|---|
amine@2 | 1 |
amine@2 | 2 from auditok import ADSFactory, AudioEnergyValidator, StreamTokenizer, player_for |
amine@2 | 3 import pyaudio |
amine@2 | 4 import sys |
amine@2 | 5 |
amine@2 | 6 energy_threshold = 45 |
amine@2 | 7 duration = 10 # seconds |
amine@2 | 8 |
amine@2 | 9 |
amine@2 | 10 if len(sys.argv) > 1: |
amine@2 | 11 energy_threshold = float(sys.argv[1]) |
amine@2 | 12 |
amine@2 | 13 if len(sys.argv) > 2: |
amine@2 | 14 duration = float(sys.argv[2]) |
amine@2 | 15 |
amine@2 | 16 # record = True so that we'll be able to rewind the source. |
amine@2 | 17 # max_time = 10: read 10 seconds from the microphone |
amine@2 | 18 asource = ADSFactory.ads(record=True, max_time = duration) |
amine@2 | 19 |
amine@2 | 20 validator = AudioEnergyValidator(sample_width=asource.get_sample_width(), energy_threshold = energy_threshold) |
amine@2 | 21 tokenizer = StreamTokenizer(validator=validator, min_length=20, max_length=250, max_continuous_silence=30) |
amine@2 | 22 |
amine@2 | 23 player = player_for(asource) |
amine@2 | 24 |
amine@2 | 25 def echo(data, start, end): |
amine@2 | 26 print("Acoustic activity at: {0}--{1}".format(start, end)) |
amine@2 | 27 player.play(''.join(data)) |
amine@2 | 28 |
amine@2 | 29 asource.open() |
amine@2 | 30 |
amine@2 | 31 print("\n ** Make some noise (dur:{}, energy:{})...".format(duration, energy_threshold)) |
amine@2 | 32 |
amine@2 | 33 tokenizer.tokenize(asource, callback=echo) |
amine@2 | 34 |
amine@2 | 35 asource.close() |
amine@2 | 36 player.stop() |