.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/effects/plot_compressor_and_limiter.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_compressor_and_limiter.py: Compressor and limiter ====================== A compressor reduces the level above a threshold and a limiter holds the peak under a ceiling. Both are in-place methods, so the examples work on copies. .. 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 A quiet tone, then the same tone 14 dB louder. .. GENERATED FROM PYTHON SOURCE LINES 16-21 .. code-block:: Python sr = 22050 t = np.arange(2 * sr) / sr level = np.where(t < 1.0, 0.1, 0.5) audio = aus.AudioSamples.new_mono((level * np.sin(2 * np.pi * 330.0 * t)).astype(np.float32), sr) .. GENERATED FROM PYTHON SOURCE LINES 22-41 .. code-block:: Python compressed = audio.copy() config = aus.CompressorConfig.vocal print(f"threshold {config.threshold_db} dB, ratio {config.ratio}, makeup {config.makeup_gain_db} dB") compressed.apply_compressor(config) limited = audio.copy() ceiling = aus.LimiterConfig.mastering() print(f"ceiling {ceiling.ceiling_db} dB") limited.apply_limiter(ceiling) print(f"peak: original {audio.peak():.3f}, compressed {compressed.peak():.3f}, limited {limited.peak():.3f}") loud = compressed.to_numpy()[sr + sr // 2 :] quiet = compressed.to_numpy()[sr // 2 : sr] assert loud.max() / quiet.max() < 5.0 # the input steps up 5 times (14 dB) assert limited.peak() <= 10 ** (ceiling.ceiling_db / 20) * 1.01 fig, axes = mpl.plot_waveform_comparison( [audio, compressed, limited], ["Original", "Compressor", "Limiter"], title="Dynamics" ) .. image-sg:: /gallery/effects/images/sphx_glr_plot_compressor_and_limiter_001.png :alt: Dynamics :srcset: /gallery/effects/images/sphx_glr_plot_compressor_and_limiter_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none threshold -18.0 dB, ratio 3.0, makeup 3.0 dB ceiling -0.3 dB peak: original 0.500, compressed 0.706, limited 0.500 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.236 seconds) .. _sphx_glr_download_gallery_effects_plot_compressor_and_limiter.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_compressor_and_limiter.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_compressor_and_limiter.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_compressor_and_limiter.zip `