Mercurial > hg > auditok
comparison demos/echo.py @ 331:9741b52f194a
Reformat code and documentation
author | Amine Sehili <amine.sehili@gmail.com> |
---|---|
date | Thu, 24 Oct 2019 20:49:51 +0200 |
parents | 9be2d0ca4c00 |
children |
comparison
equal
deleted
inserted
replaced
330:9665dc53c394 | 331:9741b52f194a |
---|---|
1 | 1 from auditok import ( |
2 from auditok import ADSFactory, AudioEnergyValidator, StreamTokenizer, player_for | 2 ADSFactory, |
3 AudioEnergyValidator, | |
4 StreamTokenizer, | |
5 player_for, | |
6 ) | |
3 import pyaudio | 7 import pyaudio |
4 import sys | 8 import sys |
5 | 9 |
6 try: | 10 try: |
7 | 11 |
8 energy_threshold = 45 | 12 energy_threshold = 45 |
9 duration = 10 # seconds | 13 duration = 10 # seconds |
10 | 14 |
15 if len(sys.argv) > 1: | |
16 energy_threshold = float(sys.argv[1]) | |
11 | 17 |
12 if len(sys.argv) > 1: | 18 if len(sys.argv) > 2: |
13 energy_threshold = float(sys.argv[1]) | 19 duration = float(sys.argv[2]) |
14 | 20 |
15 if len(sys.argv) > 2: | 21 # record = True so that we'll be able to rewind the source. |
16 duration = float(sys.argv[2]) | 22 # max_time = 10: read 10 seconds from the microphone |
23 asource = ADSFactory.ads(record=True, max_time=duration) | |
17 | 24 |
18 # record = True so that we'll be able to rewind the source. | 25 validator = AudioEnergyValidator( |
19 # max_time = 10: read 10 seconds from the microphone | 26 sample_width=asource.get_sample_width(), |
20 asource = ADSFactory.ads(record=True, max_time = duration) | 27 energy_threshold=energy_threshold, |
28 ) | |
29 tokenizer = StreamTokenizer( | |
30 validator=validator, | |
31 min_length=20, | |
32 max_length=250, | |
33 max_continuous_silence=30, | |
34 ) | |
21 | 35 |
22 validator = AudioEnergyValidator(sample_width=asource.get_sample_width(), energy_threshold = energy_threshold) | 36 player = player_for(asource) |
23 tokenizer = StreamTokenizer(validator=validator, min_length=20, max_length=250, max_continuous_silence=30) | |
24 | 37 |
25 player = player_for(asource) | 38 def echo(data, start, end): |
39 print("Acoustic activity at: {0}--{1}".format(start, end)) | |
40 player.play(b"".join(data)) | |
26 | 41 |
27 def echo(data, start, end): | 42 asource.open() |
28 print("Acoustic activity at: {0}--{1}".format(start, end)) | |
29 player.play(b''.join(data)) | |
30 | 43 |
31 asource.open() | 44 print( |
45 "\n ** Make some noise (dur:{}, energy:{})...".format( | |
46 duration, energy_threshold | |
47 ) | |
48 ) | |
32 | 49 |
33 print("\n ** Make some noise (dur:{}, energy:{})...".format(duration, energy_threshold)) | 50 tokenizer.tokenize(asource, callback=echo) |
34 | 51 |
35 tokenizer.tokenize(asource, callback=echo) | 52 asource.close() |
36 | 53 player.stop() |
37 asource.close() | |
38 player.stop() | |
39 | 54 |
40 except KeyboardInterrupt: | 55 except KeyboardInterrupt: |
41 | 56 |
42 player.stop() | 57 player.stop() |
43 asource.close() | 58 asource.close() |
44 sys.exit(0) | 59 sys.exit(0) |
45 | 60 |
46 except Exception as e: | 61 except Exception as e: |
47 | 62 |
48 sys.stderr.write(str(e) + "\n") | 63 sys.stderr.write(str(e) + "\n") |
49 sys.exit(1) | 64 sys.exit(1) |