diff demos/audio_trim_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 31c97510b16b
children 9741b52f194a
line wrap: on
line diff
--- a/demos/audio_trim_demo.py	Tue Nov 24 01:41:19 2015 +0100
+++ b/demos/audio_trim_demo.py	Tue Nov 24 01:57:53 2015 +0100
@@ -3,16 +3,17 @@
 September, 2015
 """
 
-# Trim leading and tailing silence from a record
+# Trim leading and trailing silence from a record
 
 from auditok import ADSFactory, AudioEnergyValidator, StreamTokenizer, player_for, dataset
 import pyaudio
+import sys
 
 """
 The  tokenizer in the following example is set up to remove the silence
 that precedes the first acoustic activity or follows the last activity 
 in a record. It preserves whatever it founds between the two activities.
-In other words, it removes the leading and tailing silence.
+In other words, it removes the leading and trailing silence.
 
 Sampling rate is 44100 sample per second, we'll use an analysis window of 100 ms
 (i.e. bloc_ksize == 4410)
@@ -21,7 +22,7 @@
 
 The tokenizer will start accumulating windows up from the moment it encounters
 the first analysis window of an energy >= 50. ALL the following windows will be 
-kept regardless of their energy. At the end of the analysis, it will drop tailing
+kept regardless of their energy. At the end of the analysis, it will drop trailing
  windows with an energy below 50.
 
 This is an interesting example because the audio file we're analyzing contains a very
@@ -43,48 +44,59 @@
  
 """
 
+try:
+   # record = True so that we'll be able to rewind the source.
+   asource = ADSFactory.ads(filename=dataset.was_der_mensch_saet_mono_44100_lead_tail_silence,
+             record=True, block_size=4410)
+   asource.open()
 
-# record = True so that we'll be able to rewind the source.
-asource = ADSFactory.ads(filename=dataset.was_der_mensch_saet_mono_44100_lead_tail_silence,
-          record=True, block_size=4410)
-asource.open()
+   original_signal = []
+   # Read the whole signal
+   while True:
+      w = asource.read()
+      if w is None:
+         break
+      original_signal.append(w)
 
-original_signal = []
-# Read the whole signal
-while True:
-   w = asource.read()
-   if w is None:
-      break
-   original_signal.append(w)
+   original_signal = b''.join(original_signal)
 
-original_signal = ''.join(original_signal)
 
+   # rewind source
+   asource.rewind()
 
-# rewind source
-asource.rewind()
+   # Create a validator with an energy threshold of 50
+   validator = AudioEnergyValidator(sample_width=asource.get_sample_width(), energy_threshold=50)
 
-# Create a validator with an energy threshold of 50
-validator = AudioEnergyValidator(sample_width=asource.get_sample_width(), energy_threshold=50)
+   # Create a tokenizer with an unlimited token length and continuous silence within a token
+   # Note the DROP_TRAILING_SILENCE mode that will ensure removing trailing silence
+   trimmer = StreamTokenizer(validator, min_length = 20, max_length=99999999,
+                             max_continuous_silence=9999999, mode=StreamTokenizer.DROP_TRAILING_SILENCE, init_min=3, init_max_silence=1)
 
-# Create a tokenizer with an unlimited token length and continuous silence within a token
-# Note the DROP_TAILING_SILENCE mode that will ensure removing tailing silence
-trimmer = StreamTokenizer(validator, min_length = 20, max_length=99999999,
-                          max_continuous_silence=9999999, mode=StreamTokenizer.DROP_TAILING_SILENCE, init_min=3, init_max_silence=1)
 
+   tokens = trimmer.tokenize(asource)
 
-tokens = trimmer.tokenize(asource)
+   # Make sure we only have one token
+   assert len(tokens) == 1, "Should have detected one single token"
 
-# Make sure we only have one token
-assert len(tokens) == 1, "Should have detected one single token"
+   trimmed_signal = b''.join(tokens[0][0])
 
-trimmed_signal = ''.join(tokens[0][0])
+   player = player_for(asource)
 
-player = player_for(asource)
+   print("\n ** Playing original signal (with leading and trailing silence)...")
+   player.play(original_signal)
+   print("\n ** Playing trimmed signal...")
+   player.play(trimmed_signal)
 
-print("\n ** Playing original signal (with leading and tailing silence)...")
-player.play(original_signal)
-print("\n ** Playing trimmed signal...")
-player.play(trimmed_signal)
+   player.stop()
+   asource.close()
 
-player.stop()
-asource.close()
+except KeyboardInterrupt:
+
+   player.stop()
+   asource.close()
+   sys.exit(0)
+
+except Exception as e:
+   
+   sys.stderr.write(str(e) + "\n")
+   sys.exit(1)