.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/effects/plot_silence_and_speech.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_effects_plot_silence_and_speech.py: Trim silence and find active regions ==================================== ``trim_db`` removes leading and trailing quiet, ``split_db`` lists the sounding intervals, and ``speech_regions`` runs voice activity detection. .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: Python import numpy as np import audio_samples as aus from audio_samples import mpl .. GENERATED FROM PYTHON SOURCE LINES 15-16 Two bursts of a tone with silence around and between them. .. GENERATED FROM PYTHON SOURCE LINES 16-21 .. code-block:: Python sr = 16000 tone = aus.generation.sine_wave(300.0, 0.5, sr, amplitude=0.5).as_f32() gap = aus.generation.silence(0.5, sr).as_f32() audio = aus.AudioSamples.concatenate([gap, tone, gap, tone, gap]) .. GENERATED FROM PYTHON SOURCE LINES 22-30 .. code-block:: Python trimmed, interval = audio.trim_db(top_db=40.0) print(f"kept samples {interval[0]} to {interval[1]} of {audio.samples_per_channel()}") assert trimmed.samples_per_channel() < audio.samples_per_channel() intervals = audio.split_db(top_db=40.0) print(intervals / sr) assert len(intervals) == 2 .. rst-class:: sphx-glr-script-out .. code-block:: none kept samples 7168 to 33280 of 40000 [[0.448 1.088] [1.44 2.08 ]] .. GENERATED FROM PYTHON SOURCE LINES 31-36 .. code-block:: Python config = aus.VadConfig.default() regions = audio.speech_regions(config) print(regions) fig, ax = mpl.plot_speech_regions(audio, config=config) .. image-sg:: /gallery/effects/images/sphx_glr_plot_silence_and_speech_001.png :alt: Speech Regions :srcset: /gallery/effects/images/sphx_glr_plot_silence_and_speech_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [(8192, 19456), (24064, 35328)] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.074 seconds) .. _sphx_glr_download_gallery_effects_plot_silence_and_speech.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_silence_and_speech.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_silence_and_speech.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_silence_and_speech.zip `