code: def trim_long_silences(
      wav,
      vad_window_length: int,
      vad_moving_average_width: int,
      vad_max_silence_length: int,
      sampling_rate: int,
  ):
      """
      Ensures that segments without voice in the waveform remain no longer than a
      threshold determined by the VAD parameters in params.py.
  
      :param wav: the raw waveform as a numpy array of floats
      :return: the same waveform with silences trimmed away (length <= original wav length)
      """
      # Compute the voice detection window size
      samples_per_window = (vad_window_length * sampling_rate) // 1000
  
      # Trim the end of the audio to have a multiple of the window size
      wav = wav[: len(wav) - (len(wav) % samples_per_window)]
  
      # Convert the float waveform to 16-bit mono PCM
      pcm_wave = struct.pack(
          "%dh" % len(wav),
          *(np.round(wav * INT16_MAX)).astype(np.int16),
      )
  
      # Perform voice activation detection
      voice_flags = []
      vad = webrtcvad.Vad(mode=3)
      for window_start in range(0, len(wav), samples_per_window):
          window_end = window_start + samples_per_window
          voice_flags.append(
              vad.is_speech(
                  pcm_wave[window_start * 2 : window_end * 2],
                  sample_rate=sampling_rate,
              )
          )
      voice_flags = np.array(voice_flags)
  
      # Smooth the voice detection with a moving average
      def moving_average(array, width):
          array_padded = np.concatenate(
              (np.zeros((width - 1) // 2), array, np.zeros(width // 2))
          )
          ret = np.cumsum(array_padded, dtype=float)
          ret[width:] = ret[width:] - ret[:-width]
          return ret[width - 1 :] / width
  
      audio_mask = moving_average(voice_flags, vad_moving_average_width)
      audio_mask = np.round(audio_mask).astype(np.bool)
  
      # Dilate the voiced regions
      audio_mask = binary_dilation(
          audio_mask, np.ones(vad_max_silence_length + 1)
      )
      audio_mask = np.repeat(audio_mask, samples_per_window)
  
      return wav[audio_mask]
  
file_path: PaddleHub/modules/audio/voice_cloning/lstm_tacotron2/audio_processor.py
imports: from pathlib import Path
  from warnings import warn
  import struct
  from scipy.ndimage.morphology import binary_dilation
  import numpy
  import librosa
label_desc: Use image node_id 2 for calling a global function with example usage: trim_long_silences(wav, vad_window_length, vad_moving_average_width, vad_max_silence_length, sampling_rate) and returns: wav, unknown