Mercurial > hg > auditok
comparison demos/audio_tokenize_demo.py @ 10:9be2d0ca4c00
Python 3.x support
author | Amine Sehili <amine.sehili@gmail.com> |
---|---|
date | Tue, 24 Nov 2015 01:57:53 +0100 |
parents | edee860b9f61 |
children | 421d2f534c14 |
comparison
equal
deleted
inserted
replaced
9:c2ddae4d2c36 | 10:9be2d0ca4c00 |
---|---|
2 @author: Amine SEHILI <amine.sehili@gmail.com> | 2 @author: Amine SEHILI <amine.sehili@gmail.com> |
3 September, 2015 | 3 September, 2015 |
4 """ | 4 """ |
5 | 5 |
6 from auditok import ADSFactory, AudioEnergyValidator, StreamTokenizer, player_for, dataset | 6 from auditok import ADSFactory, AudioEnergyValidator, StreamTokenizer, player_for, dataset |
7 import sys | |
7 | 8 |
8 # We set the `record` argument to True so that we can rewind the source | 9 try: |
9 asource = ADSFactory.ads(filename=dataset.one_to_six_arabic_16000_mono_bc_noise, record=True) | |
10 | 10 |
11 validator = AudioEnergyValidator(sample_width=asource.get_sample_width(), energy_threshold=65) | 11 # We set the `record` argument to True so that we can rewind the source |
12 asource = ADSFactory.ads(filename=dataset.one_to_six_arabic_16000_mono_bc_noise, record=True) | |
12 | 13 |
13 # Defalut analysis window is 10 ms (float(asource.get_block_size()) / asource.get_sampling_rate()) | 14 validator = AudioEnergyValidator(sample_width=asource.get_sample_width(), energy_threshold=65) |
14 # min_length=20 : minimum length of a valid audio activity is 20 * 10 == 200 ms | |
15 # max_length=400 : maximum length of a valid audio activity is 400 * 10 == 4000 ms == 4 seconds | |
16 # max_continuous_silence=30 : maximum length of a tolerated silence within a valid audio activity is 30 * 30 == 300 ms | |
17 tokenizer = StreamTokenizer(validator=validator, min_length=20, max_length=400, max_continuous_silence=30) | |
18 | 15 |
19 asource.open() | 16 # Defalut analysis window is 10 ms (float(asource.get_block_size()) / asource.get_sampling_rate()) |
20 tokens = tokenizer.tokenize(asource) | 17 # min_length=20 : minimum length of a valid audio activity is 20 * 10 == 200 ms |
18 # max_length=400 : maximum length of a valid audio activity is 400 * 10 == 4000 ms == 4 seconds | |
19 # max_continuous_silence=30 : maximum length of a tolerated silence within a valid audio activity is 30 * 30 == 300 ms | |
20 tokenizer = StreamTokenizer(validator=validator, min_length=20, max_length=400, max_continuous_silence=30) | |
21 | 21 |
22 # Play detected regions back | 22 asource.open() |
23 tokens = tokenizer.tokenize(asource) | |
23 | 24 |
24 player = player_for(asource) | 25 # Play detected regions back |
25 | 26 |
26 # Rewind and read the whole signal | 27 player = player_for(asource) |
27 asource.rewind() | |
28 original_signal = [] | |
29 | 28 |
30 while True: | 29 # Rewind and read the whole signal |
31 w = asource.read() | 30 asource.rewind() |
32 if w is None: | 31 original_signal = [] |
33 break | |
34 original_signal.append(w) | |
35 | 32 |
36 original_signal = ''.join(original_signal) | 33 while True: |
34 w = asource.read() | |
35 if w is None: | |
36 break | |
37 original_signal.append(w) | |
38 | |
37 | 39 |
38 print("\n ** Playing original file...") | 40 original_signal = b''.join(original_signal) |
39 player.play(original_signal) | 41 player.play(original_signal) |
40 | 42 |
41 print("\n ** playing detected regions...\n") | 43 print("\n ** playing detected regions...\n") |
42 for i,t in enumerate(tokens): | 44 for i,t in enumerate(tokens): |
43 print("Token [{0}] starts at {1} and ends at {2}".format(i+1, t[1], t[2])) | 45 print("Token [{0}] starts at {1} and ends at {2}".format(i+1, t[1], t[2])) |
44 data = ''.join(t[0]) | 46 data = b''.join(t[0]) |
45 player.play(data) | 47 player.play(data) |
46 | 48 |
47 assert len(tokens) == 8 | 49 assert len(tokens) == 8 |
48 | 50 |
49 asource.close() | 51 asource.close() |
50 player.stop() | 52 player.stop() |
53 | |
54 except KeyboardInterrupt: | |
55 | |
56 player.stop() | |
57 asource.close() | |
58 sys.exit(0) | |
59 | |
60 """ | |
61 except Exception as e: | |
62 | |
63 sys.stderr.write(str(e) + "\n") | |
64 sys.exit(1) | |
65 """ |