RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
rapp_detect_silence.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- encode: utf-8 -*-
3 
4 #Copyright 2015 RAPP
5 
6 #Licensed under the Apache License, Version 2.0 (the "License");
7 #you may not use this file except in compliance with the License.
8 #You may obtain a copy of the License at
9 
10  #http://www.apache.org/licenses/LICENSE-2.0
11 
12 #Unless required by applicable law or agreed to in writing, software
13 #distributed under the License is distributed on an "AS IS" BASIS,
14 #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #See the License for the specific language governing permissions and
16 #limitations under the License.
17 
18 # Authors: Manos Tsardoulias
19 # contact: etsardou@iti.gr
20 
21 import sys
22 import time
23 import os
24 from pylab import *
25 from scipy.io import wavfile
26 
27 ## @class DetectSilence
28 # Performs silence detection on an audio file
30 
31  ## Detects silence
32  #
33  # Handles service callback
34  # rapp_audio_processing.AudioProcessing#detectSilenceCallback
35  #
36  # @param audio_file [string] Audio file path
37  # @param threshold [float] Silence threshold
38  #
39  # @return rsd_sq [float] Noise relative standard deviation
40  # @return has_silence [bool] Indicates the existence of silence
41  def detectSilence(self, audio_file, threshold):
42  if not os.path.isfile(audio_file):
43  return [-1, False]
44  samp_freq, signal = wavfile.read(audio_file)
45  sq_signal = signal * 1.0
46  for i in range(0, len(sq_signal)):
47  sq_signal[i] *= sq_signal[i]
48  mean_sq = mean(sq_signal)
49  std_sq = std(sq_signal)
50  rsd_sq = std_sq / mean_sq
51  has_silence = False
52  if rsd_sq > threshold:
53  has_silence = False
54  else:
55  has_silence = True
56  return [rsd_sq, has_silence]
57 
Performs silence detection on an audio file.