Skip to main content

Dynamic range compression in Python based on Audacity's and Daniel Rudrich's algorithm

Project description

Dynamic Range Compression in Python

Dynamic Range Compression in Python is a high-performance, multi-channel compressor that implements a translation of Audacity's dynamic range compressor (inspired by Daniel Rudrich’s algorithm). This package leverages Numba for JIT compilation to accelerate processing and supports any audio format that FFmpeg can handle (e.g., WAV, MP3, M4A, etc.).

Features

  • Audacity-Inspired Compressor:
    Implements a dynamic range compression algorithm based on Audacity’s compressor (originally from Daniel Rudrich’s work).

  • Multi-Channel Support:
    Works with both mono and multi-channel audio.

  • Wide Audio Format Compatibility:
    Supports all FFmpeg-supported formats (WAV, MP3, M4A, etc.).

  • High Performance:
    Uses Numba to JIT compile critical processing routines for faster execution.

  • Flexible API:
    Two public API functions allow you to either process entire audio files or work directly with in-memory audio data.

Public API

compress_audio(...)

Loads an audio file from a specified path, converts it to an unnormalized 2D NumPy array of 16-bit samples, validates and initializes compressor settings, compresses the audio, and writes the compressed audio to a destination file.

Function Signature:

def compress_audio(
    filePath,
    destPath,
    threshold,
    makeupGain,
    kneeWidth,
    compressionRatio,
    lookAhead,
    attack,
    release,
):
    """
    Loads an audio file from filePath, converts it to an unnormalized 2D NumPy array of 16-bit samples,
    validates and initializes compressor settings, compresses the audio, and writes the compressed audio to destPath.

    Parameters:
        filePath (str): Path to the input audio file.
        destPath (str): Path where the compressed audio file will be saved.
        threshold (float): Compression threshold in dB.
        makeupGain (float): Makeup gain in dB.
        kneeWidth (float): Knee width in dB.
        compressionRatio (float): Compression ratio.
        lookAhead (float): Lookahead time in milliseconds.
        attack (float): Attack time in milliseconds.
        release (float): Release time in milliseconds.

    Returns:
        None
    """

compress(audio, settings)

This function applies dynamic range compression to an unnormalized 2D NumPy array of 16-bit audio samples. It first normalizes the audio to the range [-1, 1), then processes the audio in blocks by updating the compression envelope and the delayed input. Finally, it converts the processed audio back to 16-bit samples. This lower-level API provides granular control by allowing you to pass in audio data already loaded in memory along with a settings object.

def compress(audio, settings):
    """
    Applies dynamic range compression to an unnormalized 2D NumPy array of 16-bit audio samples.

    The function first normalizes the input audio, then processes it in blocks by updating the 
    compression envelope, updating the delayed input, and finally applying the envelope.
    After processing all blocks, the audio is converted back to 16-bit samples.

    Parameters:
        audio (np.ndarray): A 2D NumPy array of shape (channels, samples) with dtype np.int16
                            containing the input audio samples.
        settings (CompressorSettings): An instance of a compressor settings class that contains the following attributes:
            - thresholdDb (float): Compression threshold in dB.
            - makeupGainDb (float): Makeup gain in dB.
            - kneeWidthDb (float): Knee width in dB.
            - compressionRatio (float): Compression ratio.
            - lookAheadMs (float): Lookahead time in milliseconds.
            - attackMs (float): Attack time in milliseconds.
            - releaseMs (float): Release time in milliseconds.
            - sampleRate (int): The sample rate of the audio.

            To use this function, create your own class (or object) with these attributes, instantiate it with
            your desired settings, and then pass that instance to this function.

    Returns:
        np.ndarray: A 2D NumPy array of shape (channels, samples) with dtype np.int16 containing the
                    compressed audio.
    """

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dynamic_range_compression-1.0.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dynamic_range_compression-1.0.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file dynamic_range_compression-1.0.0.tar.gz.

File metadata

File hashes

Hashes for dynamic_range_compression-1.0.0.tar.gz
Algorithm Hash digest
SHA256 056236b6a00648a97da2ca200c0ac08233866c7aa85ced4f8006add060421358
MD5 258ff3fa71d9dbfe697a94c7a4acfa25
BLAKE2b-256 4f2a544b745a7b396b1a2dcba09f79731ab4b1248b7267c733dcd69af6b8a84d

See more details on using hashes here.

File details

Details for the file dynamic_range_compression-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dynamic_range_compression-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1f68b05f9a29d64b355707370532ab4fe2a45469b6423fd72c8adaafa19c6306
MD5 cd77b952976e0df79861c1798ea9f0bd
BLAKE2b-256 4fb91d72c81e900e5775dd4ba09540e634b7887318e1237e9d4b9080399fa973

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page