comparison demos/audio_trim_demo.py @ 3:364eeb8e8bd2

README.md, typos fixes
author Amine Sehili <amine.sehili@gmail.com>
date Tue, 22 Sep 2015 10:49:57 +0200
parents edee860b9f61
children 31c97510b16b
comparison
equal deleted inserted replaced
2:edee860b9f61 3:364eeb8e8bd2
1 """ 1 """
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 # Trim leading and trailing silence from a record 6 # Trim leading and tailing silence from a record
7 7
8 from auditok import ADSFactory, AudioEnergyValidator, StreamTokenizer, player_for, dataset 8 from auditok import ADSFactory, AudioEnergyValidator, StreamTokenizer, player_for, dataset
9 import pyaudio 9 import pyaudio
10 10
11 """ 11 """
12 The tokenizer in the following example is set up to remove the silence 12 The tokenizer in the following example is set up to remove the silence
13 that precedes the first acoustic activity or follows the last activity 13 that precedes the first acoustic activity or follows the last activity
14 in a record. It preserves whatever it founds between the two activities. 14 in a record. It preserves whatever it founds between the two activities.
15 In other words, it removes the leading and trailing silence. 15 In other words, it removes the leading and tailing silence.
16 16
17 Sampling rate is 44100 sample per second, we'll use an analysis window of 100 ms 17 Sampling rate is 44100 sample per second, we'll use an analysis window of 100 ms
18 (i.e. bloc_ksize == 4410) 18 (i.e. bloc_ksize == 4410)
19 19
20 Energy threshold is 50. 20 Energy threshold is 50.
21 21
22 The tokenizer will start accumulating windows up from the moment it encounters 22 The tokenizer will start accumulating windows up from the moment it encounters
23 the first analysis window of an energy >= 50. ALL the following windows will be 23 the first analysis window of an energy >= 50. ALL the following windows will be
24 kept regardless of their energy. At the end of the analysis, it will drop trailing 24 kept regardless of their energy. At the end of the analysis, it will drop tailing
25 windows with an energy below 50. 25 windows with an energy below 50.
26 26
27 This is an interesting example because the audio file we're analyzing contains a very 27 This is an interesting example because the audio file we're analyzing contains a very
28 brief noise that occurs within the leading silence. We certainly do want our tokenizer 28 brief noise that occurs within the leading silence. We certainly do want our tokenizer
29 to stop at this point and considers whatever it comes after as a useful signal. 29 to stop at this point and considers whatever it comes after as a useful signal.
43 43
44 """ 44 """
45 45
46 46
47 # record = True so that we'll be able to rewind the source. 47 # record = True so that we'll be able to rewind the source.
48 asource = ADSFactory.ads(filename=dataset.was_der_mensch_saet_mono_44100_lead_trail_silence, 48 asource = ADSFactory.ads(filename=dataset.was_der_mensch_saet_mono_44100_lead_tail_silence,
49 record=True, block_size=4410) 49 record=True, block_size=4410)
50 asource.open() 50 asource.open()
51 51
52 original_signal = [] 52 original_signal = []
53 # Read the whole signal 53 # Read the whole signal
65 65
66 # Create a validator with an energy threshold of 50 66 # Create a validator with an energy threshold of 50
67 validator = AudioEnergyValidator(sample_width=asource.get_sample_width(), energy_threshold=50) 67 validator = AudioEnergyValidator(sample_width=asource.get_sample_width(), energy_threshold=50)
68 68
69 # Create a tokenizer with an unlimited token length and continuous silence within a token 69 # Create a tokenizer with an unlimited token length and continuous silence within a token
70 # Note the DROP_TRAILING_SILENCE mode that will ensure removing trailing silence 70 # Note the DROP_TRAILING_SILENCE mode that will ensure removing tailing silence
71 trimmer = StreamTokenizer(validator, min_length = 20, max_length=99999999, 71 trimmer = StreamTokenizer(validator, min_length = 20, max_length=99999999,
72 max_continuous_silence=9999999, mode=StreamTokenizer.DROP_TRAILING_SILENCE, init_min=3, init_max_silence=1) 72 max_continuous_silence=9999999, mode=StreamTokenizer.DROP_TRAILING_SILENCE, init_min=3, init_max_silence=1)
73 73
74 74
75 tokens = trimmer.tokenize(asource) 75 tokens = trimmer.tokenize(asource)
79 79
80 trimmed_signal = ''.join(tokens[0][0]) 80 trimmed_signal = ''.join(tokens[0][0])
81 81
82 player = player_for(asource) 82 player = player_for(asource)
83 83
84 print("\n ** Playing original signal (with leading and trailing silence)...") 84 print("\n ** Playing original signal (with leading and tailing silence)...")
85 player.play(original_signal) 85 player.play(original_signal)
86 print("\n ** Playing trimmed signal...") 86 print("\n ** Playing trimmed signal...")
87 player.play(trimmed_signal) 87 player.play(trimmed_signal)
88 88
89 player.stop() 89 player.stop()