Mercurial > hg > auditok
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demos/audio_tokenize_demo.py Thu Sep 17 22:01:30 2015 +0200 @@ -0,0 +1,50 @@ +""" +@author: Amine SEHILI <amine.sehili@gmail.com> +September, 2015 +""" + +from auditok import ADSFactory, AudioEnergyValidator, StreamTokenizer, player_for, dataset + +# We set the `record` argument to True so that we can rewind the source +asource = ADSFactory.ads(filename=dataset.one_to_six_arabic_16000_mono_bc_noise, record=True) + +validator = AudioEnergyValidator(sample_width=asource.get_sample_width(), energy_threshold=65) + +# Defalut analysis window is 10 ms (float(asource.get_block_size()) / asource.get_sampling_rate()) +# min_length=20 : minimum length of a valid audio activity is 20 * 10 == 200 ms +# max_length=400 : maximum length of a valid audio activity is 400 * 10 == 4000 ms == 4 seconds +# max_continuous_silence=30 : maximum length of a tolerated silence within a valid audio activity is 30 * 30 == 300 ms +tokenizer = StreamTokenizer(validator=validator, min_length=20, max_length=400, max_continuous_silence=30) + +asource.open() +tokens = tokenizer.tokenize(asource) + +# Play detected regions back + +player = player_for(asource) + +# Rewind and read the whole signal +asource.rewind() +original_signal = [] + +while True: + w = asource.read() + if w is None: + break + original_signal.append(w) + +original_signal = ''.join(original_signal) + +print("\n ** Playing original file...") +player.play(original_signal) + +print("\n ** playing detected regions...\n") +for i,t in enumerate(tokens): + print("Token [{0}] starts at {1} and ends at {2}".format(i+1, t[1], t[2])) + data = ''.join(t[0]) + player.play(data) + +assert len(tokens) == 8 + +asource.close() +player.stop()