Skip to content

audio_enhancer.enhancer

audio_enhancer.enhancer

audio_enhancer.enhancer.AudioEnhancer

A facade and Singleton for the audio enhancement system.

Provides a simplified, high-level interface to the underlying audio loading, exporting, and pipeline building components.

Example

enhancer = AudioEnhancer.get_instance() audio = enhancer.load_audio("input.wav") builder = enhancer.get_builder() pipeline = builder.add_step(SpectralGatingNoiseReducer()).build() enhanced = pipeline.process(audio) enhancer.export_audio(enhanced, "output.wav", "wav")

Source code in audio_enhancer/enhancer.py
class AudioEnhancer:
    """A facade and Singleton for the audio enhancement system.

    Provides a simplified, high-level interface to the underlying audio loading,
    exporting, and pipeline building components.

    Example:
        >>> enhancer = AudioEnhancer.get_instance()
        >>> audio = enhancer.load_audio("input.wav")
        >>> builder = enhancer.get_builder()
        >>> pipeline = builder.add_step(SpectralGatingNoiseReducer()).build()
        >>> enhanced = pipeline.process(audio)
        >>> enhancer.export_audio(enhanced, "output.wav", "wav")
    """
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(AudioEnhancer, cls).__new__(cls)
        return cls._instance

    @classmethod
    def get_instance(cls) -> 'AudioEnhancer':
        """Provides a global access point to the Singleton instance.

        Returns:
            AudioEnhancer: The active singleton instance.
        """
        return cls()

    def load_audio(self, file_path: str) -> AudioSegment:
        """Loads an audio file from disk into an AudioSegment.

        This hides the detailed loader implementations and formats support.

        Args:
            file_path (str): Path to the input audio file.

        Returns:
            AudioSegment: The loaded pydub AudioSegment.

        Raises:
            FileNotFoundError: If the file does not exist.
            Exception: If pydub or ffmpeg fails to decode the file.
        """
        return load_audio(file_path)

    def export_audio(self, audio: AudioSegment, file_path: str, format: str):
        """Exports an AudioSegment to a file with the specified format.

        This hides the details of the exporter module and formats support.

        Args:
            audio (AudioSegment): The audio segment to export.
            file_path (str): The destination file path.
            format (str): The desired output format (e.g., 'mp3', 'wav', 'flac').

        Raises:
            Exception: If the audio export fails.
        """
        export_audio(audio, file_path, format)

    def get_builder(self) -> PipelineBuilder:
        """Returns a new PipelineBuilder instance.

        Integrates the Builder pattern into the facade to allow chaining steps.

        Returns:
            PipelineBuilder: A new pipeline builder instance.
        """
        return PipelineBuilder()

    def generate_report(self, original_path: str, processed_path: str, 
                        output_dir: str = ".reports") -> list:
        """Generate visual comparison reports for processed audio.

        Args:
            original_path (str): Path to the original audio file.
            processed_path (str): Path to the processed audio file.
            output_dir (str): Directory where comparison reports will be stored.

        Returns:
            list[Path]: List of paths to the generated report files.
        """
        from pathlib import Path
        from audio_enhancer.reports.generator import ReportGenerator
        generator = ReportGenerator.create_default(output_dir)
        return generator.generate_all(original_path, processed_path)

audio_enhancer.enhancer.AudioEnhancer.export_audio(audio, file_path, format)

Exports an AudioSegment to a file with the specified format.

This hides the details of the exporter module and formats support.

Parameters:

Name Type Description Default
audio AudioSegment

The audio segment to export.

required
file_path str

The destination file path.

required
format str

The desired output format (e.g., 'mp3', 'wav', 'flac').

required

Raises:

Type Description
Exception

If the audio export fails.

Source code in audio_enhancer/enhancer.py
def export_audio(self, audio: AudioSegment, file_path: str, format: str):
    """Exports an AudioSegment to a file with the specified format.

    This hides the details of the exporter module and formats support.

    Args:
        audio (AudioSegment): The audio segment to export.
        file_path (str): The destination file path.
        format (str): The desired output format (e.g., 'mp3', 'wav', 'flac').

    Raises:
        Exception: If the audio export fails.
    """
    export_audio(audio, file_path, format)

audio_enhancer.enhancer.AudioEnhancer.generate_report(original_path, processed_path, output_dir='.reports')

Generate visual comparison reports for processed audio.

Parameters:

Name Type Description Default
original_path str

Path to the original audio file.

required
processed_path str

Path to the processed audio file.

required
output_dir str

Directory where comparison reports will be stored.

'.reports'

Returns:

Type Description
list

list[Path]: List of paths to the generated report files.

Source code in audio_enhancer/enhancer.py
def generate_report(self, original_path: str, processed_path: str, 
                    output_dir: str = ".reports") -> list:
    """Generate visual comparison reports for processed audio.

    Args:
        original_path (str): Path to the original audio file.
        processed_path (str): Path to the processed audio file.
        output_dir (str): Directory where comparison reports will be stored.

    Returns:
        list[Path]: List of paths to the generated report files.
    """
    from pathlib import Path
    from audio_enhancer.reports.generator import ReportGenerator
    generator = ReportGenerator.create_default(output_dir)
    return generator.generate_all(original_path, processed_path)

audio_enhancer.enhancer.AudioEnhancer.get_builder()

Returns a new PipelineBuilder instance.

Integrates the Builder pattern into the facade to allow chaining steps.

Returns:

Name Type Description
PipelineBuilder PipelineBuilder

A new pipeline builder instance.

Source code in audio_enhancer/enhancer.py
def get_builder(self) -> PipelineBuilder:
    """Returns a new PipelineBuilder instance.

    Integrates the Builder pattern into the facade to allow chaining steps.

    Returns:
        PipelineBuilder: A new pipeline builder instance.
    """
    return PipelineBuilder()

audio_enhancer.enhancer.AudioEnhancer.get_instance() classmethod

Provides a global access point to the Singleton instance.

Returns:

Name Type Description
AudioEnhancer AudioEnhancer

The active singleton instance.

Source code in audio_enhancer/enhancer.py
@classmethod
def get_instance(cls) -> 'AudioEnhancer':
    """Provides a global access point to the Singleton instance.

    Returns:
        AudioEnhancer: The active singleton instance.
    """
    return cls()

audio_enhancer.enhancer.AudioEnhancer.load_audio(file_path)

Loads an audio file from disk into an AudioSegment.

This hides the detailed loader implementations and formats support.

Parameters:

Name Type Description Default
file_path str

Path to the input audio file.

required

Returns:

Name Type Description
AudioSegment AudioSegment

The loaded pydub AudioSegment.

Raises:

Type Description
FileNotFoundError

If the file does not exist.

Exception

If pydub or ffmpeg fails to decode the file.

Source code in audio_enhancer/enhancer.py
def load_audio(self, file_path: str) -> AudioSegment:
    """Loads an audio file from disk into an AudioSegment.

    This hides the detailed loader implementations and formats support.

    Args:
        file_path (str): Path to the input audio file.

    Returns:
        AudioSegment: The loaded pydub AudioSegment.

    Raises:
        FileNotFoundError: If the file does not exist.
        Exception: If pydub or ffmpeg fails to decode the file.
    """
    return load_audio(file_path)