speechbrain.inference.VAD module

Specifies the inference interfaces for Voice Activity Detection (VAD) modules.

Authors:
  • Aku Rouhe 2021

  • Peter Plantinga 2021

  • Loren Lugosch 2020

  • Mirco Ravanelli 2020

  • Titouan Parcollet 2021

  • Abdel Heba 2021

  • Andreas Nautsch 2022, 2023

  • Pooneh Mousavi 2023

  • Sylvain de Langen 2023

  • Adel Moumen 2023

  • Pradnya Kandarkar 2023

Summary

Classes:

VAD

A ready-to-use class for Voice Activity Detection (VAD) using a pre-trained model.

Reference

class speechbrain.inference.VAD.VAD(*args, **kwargs)[source]

Bases: Pretrained

A ready-to-use class for Voice Activity Detection (VAD) using a pre-trained model.

Example

>>> import torchaudio
>>> from speechbrain.inference.VAD import VAD
>>> # Model is downloaded from the speechbrain HuggingFace repo
>>> tmpdir = getfixture("tmpdir")
>>> VAD = VAD.from_hparams(
...     source="speechbrain/vad-crdnn-libriparty",
...     savedir=tmpdir,
... )
>>> # Perform VAD
>>> boundaries = VAD.get_speech_segments("tests/samples/single-mic/example1.wav")
HPARAMS_NEEDED = ['sample_rate', 'time_resolution', 'device']
MODULES_NEEDED = ['compute_features', 'mean_var_norm', 'model']
get_speech_prob_file(audio_file, large_chunk_size=30, small_chunk_size=10, overlap_small_chunk=False)[source]

Outputs the frame-level speech probability of the input audio file using the neural model specified in the hparam file. To make this code both parallelizable and scalable to long sequences, it uses a double-windowing approach. First, we sequentially read non-overlapping large chunks of the input signal. We then split the large chunks into smaller chunks and we process them in parallel.

Parameters:
  • audio_file (path) – Path of the audio file containing the recording. The file is read with torchaudio.

  • large_chunk_size (float) – Size (in seconds) of the large chunks that are read sequentially from the input audio file.

  • small_chunk_size – Size (in seconds) of the small chunks extracted from the large ones. The audio signal is processed in parallel within the small chunks. Note that large_chunk_size/small_chunk_size must be an integer.

  • overlap_small_chunk (bool) – True, creates overlapped small chunks. The probabilities of the overlapped chunks are combined using hamming windows.

Returns:

prob_vad – Tensor containing the frame-level speech probabilities for the input audio file.

Return type:

torch.Tensor

get_speech_prob_chunk(wavs, wav_lens=None)[source]

Outputs the frame-level posterior probability for the input audio chunks Outputs close to zero refers to time steps with a low probability of speech activity, while outputs closer to one likely contain speech.

Parameters:
  • wavs (torch.Tensor) – Batch of waveforms [batch, time, channels] or [batch, time] depending on the model. Make sure the sample rate is fs=16000 Hz.

  • wav_lens (torch.Tensor) – Lengths of the waveforms relative to the longest one in the batch, tensor of shape [batch]. The longest one should have relative length 1.0 and others len(waveform) / max_length. Used for ignoring padding.

Returns:

The encoded batch

Return type:

torch.Tensor

apply_threshold(vad_prob, activation_th=0.5, deactivation_th=0.25)[source]

Scans the frame-level speech probabilities and applies a threshold on them. Speech starts when a value larger than activation_th is detected, while it ends when observing a value lower than the deactivation_th.

Parameters:
  • vad_prob (torch.Tensor) – Frame-level speech probabilities.

  • activation_th (float) – Threshold for starting a speech segment.

  • deactivation_th (float) – Threshold for ending a speech segment.

Returns:

vad_th – Tensor containing 1 for speech regions and 0 for non-speech regions.

Return type:

torch.Tensor

get_boundaries(prob_th, output_value='seconds')[source]

Computes the time boundaries where speech activity is detected. It takes in input frame-level binary decisions (1 for speech, 0 for non-speech) and outputs the begin/end second (or sample) of each detected speech region.

Parameters:
  • prob_th (torch.Tensor) – Frame-level binary decisions (1 for speech frame, 0 for a non-speech one). The tensor can be obtained from apply_threshold.

  • output_value ('seconds' or 'samples') – When the option ‘seconds’ is set, the returned boundaries are in seconds, otherwise, it reports them in samples.

Returns:

boundaries – Tensor containing the start second (or sample) of speech segments in even positions and their corresponding end in odd positions (e.g, [1.0, 1.5, 5,.0 6.0] means that we have two speech segment;

one from 1.0 to 1.5 seconds and another from 5.0 to 6.0 seconds).

Return type:

torch.Tensor

merge_close_segments(boundaries, close_th=0.25)[source]

Merges segments that are shorter than the given threshold.

Parameters:
  • boundaries (str) – Tensor containing the speech boundaries. It can be derived using the get_boundaries method.

  • close_th (float) – If the distance between boundaries is smaller than close_th, the segments will be merged.

Returns:

The new boundaries with the merged segments.

Return type:

new_boundaries

remove_short_segments(boundaries, len_th=0.25)[source]

Removes segments that are too short.

Parameters:
  • boundaries (torch.Tensor) – Tensor containing the speech boundaries. It can be derived using the get_boundaries method.

  • len_th (float) – If the length of the segment is smaller than close_th, the segments will be merged.

Returns:

The new boundaries without the short segments.

Return type:

new_boundaries

save_boundaries(boundaries, save_path=None, print_boundaries=True, audio_file=None)[source]

Saves the boundaries on a file (and/or prints them) in a readable format.

Parameters:
  • boundaries (torch.Tensor) – Tensor containing the speech boundaries. It can be derived using the get_boundaries method.

  • save_path (path) – When to store the text file containing the speech/non-speech intervals.

  • print_boundaries (Bool) – Prints the speech/non-speech intervals in the standard outputs.

  • audio_file (path) – Path of the audio file containing the recording. The file is read with torchaudio. It is used here to detect the length of the signal.

energy_VAD(audio_file, boundaries, activation_th=0.5, deactivation_th=0.0, eps=1e-06)[source]

Applies energy-based VAD within the detected speech segments.The neural network VAD often creates longer segments and tends to merge segments that are close with each other.

The energy VAD post-processes can be useful for having a fine-grained voice activity detection.

The energy VAD computes the energy within the small chunks. The energy is normalized within the segment to have mean 0.5 and +-0.5 of std. This helps to set the energy threshold.

Parameters:
  • audio_file (path) – Path of the audio file containing the recording. The file is read with torchaudio.

  • boundaries (torch.Tensor) – Tensor containing the speech boundaries. It can be derived using the get_boundaries method.

  • activation_th (float) – A new speech segment is started it the energy is above activation_th.

  • deactivation_th (float) – The segment is considered ended when the energy is <= deactivation_th.

  • eps (float) – Small constant for numerical stability.

Returns:

The new boundaries that are post-processed by the energy VAD.

Return type:

new_boundaries

create_chunks(x, chunk_size=16384, chunk_stride=16384)[source]

Splits the input into smaller chunks of size chunk_size with an overlap chunk_stride. The chunks are concatenated over the batch axis.

Parameters:
  • x (torch.Tensor) – Signal to split into chunks.

  • chunk_size (str) – The size of each chunk.

  • chunk_stride – The stride (hop) of each chunk.

Returns:

x – A new tensors with the chunks derived from the input signal.

Return type:

torch.Tensor

upsample_VAD(vad_out, audio_file, time_resolution=0.01)[source]

Upsamples the output of the vad to help visualization. It creates a signal that is 1 when there is speech and 0 when there is no speech. The vad signal has the same resolution as the input one and can be opened with it (e.g, using audacity) to visually figure out VAD regions.

Parameters:
  • vad_out (torch.Tensor) – Tensor containing 1 for each frame of speech and 0 for each non-speech frame.

  • audio_file (path) – The original audio file used to compute vad_out

  • time_resolution (float) – Time resolution of the vad_out signal.

Returns:

The upsampled version of the vad_out tensor.

Return type:

vad_signal

upsample_boundaries(boundaries, audio_file)[source]

Based on the input boundaries, this method creates a signal that is 1 when there is speech and 0 when there is no speech. The vad signal has the same resolution as the input one and can be opened with it (e.g, using audacity) to visually figure out VAD regions.

Parameters:
  • boundaries (torch.Tensor) – Tensor containing the boundaries of the speech segments.

  • audio_file (path) – The original audio file used to compute vad_out

Returns:

The output vad signal with the same resolution of the input one.

Return type:

vad_signal

double_check_speech_segments(boundaries, audio_file, speech_th=0.5)[source]

Takes in input the boundaries of the detected speech segments and double checks (using the neural VAD) that they actually contain speech.

Parameters:
  • boundaries (torch.Tensor) – Tensor containing the boundaries of the speech segments.

  • audio_file (path) – The original audio file used to compute vad_out.

  • speech_th (float) – Threshold on the mean posterior probability over which speech is confirmed. Below that threshold, the segment is re-assigned to a non-speech region.

Returns:

The boundaries of the segments where speech activity is confirmed.

Return type:

new_boundaries

get_segments(boundaries, audio_file, before_margin=0.1, after_margin=0.1)[source]

Returns a list containing all the detected speech segments.

Parameters:
  • boundaries (torch.Tensor) – Tensor containing the boundaries of the speech segments.

  • audio_file (path) – The original audio file used to compute vad_out.

  • before_margin (float) – Used to cut the segments samples a bit before the detected margin.

  • after_margin (float) – Use to cut the segments samples a bit after the detected margin.

Returns:

segments – List containing the detected speech segments

Return type:

list

get_speech_segments(audio_file, large_chunk_size=30, small_chunk_size=10, overlap_small_chunk=False, apply_energy_VAD=False, double_check=True, close_th=0.25, len_th=0.25, activation_th=0.5, deactivation_th=0.25, en_activation_th=0.5, en_deactivation_th=0.0, speech_th=0.5)[source]

Detects speech segments within the input file. The input signal can be both a short or a long recording. The function computes the posterior probabilities on large chunks (e.g, 30 sec), that are read sequentially (to avoid storing big signals in memory). Each large chunk is, in turn, split into smaller chunks (e.g, 10 seconds) that are processed in parallel. The pipeline for detecting the speech segments is the following:

1- Compute posteriors probabilities at the frame level. 2- Apply a threshold on the posterior probability. 3- Derive candidate speech segments on top of that. 4- Apply energy VAD within each candidate segment (optional). 5- Merge segments that are too close. 6- Remove segments that are too short. 7- Double check speech segments (optional).

Parameters:
  • audio_file (str) – Path to audio file.

  • large_chunk_size (float) – Size (in seconds) of the large chunks that are read sequentially from the input audio file.

  • small_chunk_size (float) – Size (in seconds) of the small chunks extracted from the large ones. The audio signal is processed in parallel within the small chunks. Note that large_chunk_size/small_chunk_size must be an integer.

  • overlap_small_chunk (bool) – If True, it creates overlapped small chunks (with 50% overlap). The probabilities of the overlapped chunks are combined using hamming windows.

  • apply_energy_VAD (bool) – If True, a energy-based VAD is used on the detected speech segments. The neural network VAD often creates longer segments and tends to merge close segments together. The energy VAD post-processes can be useful for having a fine-grained voice activity detection. The energy thresholds is managed by activation_th and deactivation_th (see below).

  • double_check (bool) – If True, double checks (using the neural VAD) that the candidate speech segments actually contain speech. A threshold on the mean posterior probabilities provided by the neural network is applied based on the speech_th parameter (see below).

  • activation_th (float) – Threshold of the neural posteriors above which starting a speech segment.

  • deactivation_th (float) – Threshold of the neural posteriors below which ending a speech segment.

  • en_activation_th (float) – A new speech segment is started it the energy is above activation_th. This is active only if apply_energy_VAD is True.

  • en_deactivation_th (float) – The segment is considered ended when the energy is <= deactivation_th. This is active only if apply_energy_VAD is True.

  • speech_th (float) – Threshold on the mean posterior probability within the candidate speech segment. Below that threshold, the segment is re-assigned to a non-speech region. This is active only if double_check is True.

  • close_th (float) – If the distance between boundaries is smaller than close_th, the segments will be merged.

  • len_th (float) – If the length of the segment is smaller than close_th, the segments will be merged.

Returns:

boundaries – Tensor containing the start second of speech segments in even positions and their corresponding end in odd positions (e.g, [1.0, 1.5, 5,.0 6.0] means that we have two speech segment;

one from 1.0 to 1.5 seconds and another from 5.0 to 6.0 seconds).

Return type:

torch.Tensor

forward(wavs, wav_lens=None)[source]

Gets frame-level speech-activity predictions

training: bool