File size: 1,569 Bytes
9ae08c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import numpy as np
from typing import List, Dict, Any
from .flanger import apply_flanger
from .normalize import apply_normalize
from .pitch_shift import apply_pitch_shift
from .random_semitone_sawtooth_wave import apply_effect
from .speed_change import apply_speed_change


class EffectChainProcessor:
    def __init__(self):
        # Mapping from effect name to its corresponding module & function
        self.effect_map = {
            "flanger": apply_flanger,
            "normalize": apply_normalize,
            "pitch_shift": apply_pitch_shift,
            "random_semitone_sawtooth_wave": apply_effect,
            "speed_change": apply_speed_change,
        }

    def apply_chain(self, audio: np.ndarray, framerate: int, chain: List[Dict[str, Any]]) -> np.ndarray:
        """
        Applies a sequence of effects to audio.

        Args:
            audio (np.ndarray): Input audio signal.
            framerate (int): Sample rate in Hz.
            chain (List[Dict]): List of effect dicts with 'name' and 'params'.

        Returns:
            np.ndarray: Processed audio.
        """
        for effect_conf in chain:
            effect_name = effect_conf.get("name")
            params = effect_conf.get("params", {})

            if effect_name not in self.effect_map:
                raise ValueError(f"Unknown effect: {effect_name}")

            effect_func = self.effect_map[effect_name]

            print(f"Applying effect: {effect_name} with params {params}")
            audio = effect_func(audio, framerate, **params)

        return audio