annotate demos/echo.py @ 130:7c5f325dae7a
Add tests for _load_wave with channel mixing
author |
Amine Sehili <amine.sehili@gmail.com> |
date |
Fri, 08 Feb 2019 20:51:36 +0100 |
parents |
9be2d0ca4c00 |
children |
9741b52f194a |
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@10
|
6 try:
|
amine@2
|
7
|
amine@10
|
8 energy_threshold = 45
|
amine@10
|
9 duration = 10 # seconds
|
amine@2
|
10
|
amine@2
|
11
|
amine@10
|
12 if len(sys.argv) > 1:
|
amine@10
|
13 energy_threshold = float(sys.argv[1])
|
amine@2
|
14
|
amine@10
|
15 if len(sys.argv) > 2:
|
amine@10
|
16 duration = float(sys.argv[2])
|
amine@2
|
17
|
amine@10
|
18 # record = True so that we'll be able to rewind the source.
|
amine@10
|
19 # max_time = 10: read 10 seconds from the microphone
|
amine@10
|
20 asource = ADSFactory.ads(record=True, max_time = duration)
|
amine@2
|
21
|
amine@10
|
22 validator = AudioEnergyValidator(sample_width=asource.get_sample_width(), energy_threshold = energy_threshold)
|
amine@10
|
23 tokenizer = StreamTokenizer(validator=validator, min_length=20, max_length=250, max_continuous_silence=30)
|
amine@2
|
24
|
amine@10
|
25 player = player_for(asource)
|
amine@2
|
26
|
amine@10
|
27 def echo(data, start, end):
|
amine@10
|
28 print("Acoustic activity at: {0}--{1}".format(start, end))
|
amine@10
|
29 player.play(b''.join(data))
|
amine@2
|
30
|
amine@10
|
31 asource.open()
|
amine@2
|
32
|
amine@10
|
33 print("\n ** Make some noise (dur:{}, energy:{})...".format(duration, energy_threshold))
|
amine@2
|
34
|
amine@10
|
35 tokenizer.tokenize(asource, callback=echo)
|
amine@10
|
36
|
amine@10
|
37 asource.close()
|
amine@10
|
38 player.stop()
|
amine@10
|
39
|
amine@10
|
40 except KeyboardInterrupt:
|
amine@10
|
41
|
amine@10
|
42 player.stop()
|
amine@10
|
43 asource.close()
|
amine@10
|
44 sys.exit(0)
|
amine@10
|
45
|
amine@10
|
46 except Exception as e:
|
amine@10
|
47
|
amine@10
|
48 sys.stderr.write(str(e) + "\n")
|
amine@10
|
49 sys.exit(1)
|