comparison demos/audio_tokenize_demo.py @ 2:edee860b9f61

First release on Github
author Amine Sehili <amine.sehili@gmail.com>
date Thu, 17 Sep 2015 22:01:30 +0200
parents
children 9be2d0ca4c00
comparison
equal deleted inserted replaced
1:78ba0ead5f9f 2:edee860b9f61
1 """
2 @author: Amine SEHILI <amine.sehili@gmail.com>
3 September, 2015
4 """
5
6 from auditok import ADSFactory, AudioEnergyValidator, StreamTokenizer, player_for, dataset
7
8 # We set the `record` argument to True so that we can rewind the source
9 asource = ADSFactory.ads(filename=dataset.one_to_six_arabic_16000_mono_bc_noise, record=True)
10
11 validator = AudioEnergyValidator(sample_width=asource.get_sample_width(), energy_threshold=65)
12
13 # Defalut analysis window is 10 ms (float(asource.get_block_size()) / asource.get_sampling_rate())
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
19 asource.open()
20 tokens = tokenizer.tokenize(asource)
21
22 # Play detected regions back
23
24 player = player_for(asource)
25
26 # Rewind and read the whole signal
27 asource.rewind()
28 original_signal = []
29
30 while True:
31 w = asource.read()
32 if w is None:
33 break
34 original_signal.append(w)
35
36 original_signal = ''.join(original_signal)
37
38 print("\n ** Playing original file...")
39 player.play(original_signal)
40
41 print("\n ** playing detected regions...\n")
42 for i,t in enumerate(tokens):
43 print("Token [{0}] starts at {1} and ends at {2}".format(i+1, t[1], t[2]))
44 data = ''.join(t[0])
45 player.play(data)
46
47 assert len(tokens) == 8
48
49 asource.close()
50 player.stop()