File size: 3,771 Bytes
9d0d223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

import pytest
from omegaconf import OmegaConf

from audiocraft.utils.audio_effects import AudioEffects, get_audio_effects, select_audio_effects

from ..common_utils import get_batch_white_noise


class TestAudioEffect:
    SR = 16_000

    @pytest.fixture(autouse=True)
    def audio_effects(self):
        cfg = {
            "audio_effects": {
                "speed": {
                    "sample_rate": self.SR,
                    "speed_range": [0.8, 1.2]
                },
                "updownresample": {
                    "sample_rate": self.SR,
                    "intermediate_freq": 32_000,
                },
                "echo": {
                    "sample_rate": self.SR,
                    "volume_range": [0.1, 0.5],
                },
                "random_noise": {
                    "noise_std": 0.001,
                },
                "pink_noise": {
                    "noise_std": 0.01,
                },
                "lowpass_filter": {
                    "sample_rate": self.SR,
                    "cutoff_freq": 5_000,
                },
                "highpass_filter": {
                    "sample_rate": self.SR,
                    "cutoff_freq": 500,
                },
                "bandpass_filter": {
                    "sample_rate": self.SR,
                    "cutoff_freq_low": 300,
                    "cutoff_freq_high": 8_000,
                },
                "smooth": {
                    "window_size_range": [2, 10],
                },
                "boost_audio": {
                    "amount": 20,
                },
                "duck_audio": {
                    "amount": 20,
                },
                "mp3_compression": {
                    "sample_rate": self.SR,
                    "bitrate": "128k",
                },
                "aac_compression": {
                    "sample_rate": self.SR,
                    "bitrate": "128k",
                    "lowpass_freq": None,
                }
            }
        }
        weights = {
            "speed": 2.0,
            "updownresample": 0.4,
            "echo": 1.0,
            "random_noise": 3.0,
            "pink_noise": 0.5,
            "lowpass_filter": 4.0,
            "highpass_filter": 5.0,
            "bandpass_filter": 6.0,
            "smooth": 1.0,
        }
        return get_audio_effects(OmegaConf.structured(cfg)), weights

    def test_select_empty_effects(self):
        effects = select_audio_effects({})
        assert "identity" in effects and effects["identity"] == AudioEffects.identity

    def test_select_wrong_strategy(self):
        with pytest.raises(ValueError):
            _ = select_audio_effects(
                audio_effects={},
                mode="some invalid mode"
            )

    def test_selection(self, audio_effects):
        effect_cfg, weights = audio_effects
        effects = select_audio_effects(
            audio_effects=effect_cfg,
            weights=weights,
            mode="weighted"
        )
        b, c, t = 2, 4, 32000
        audio = get_batch_white_noise(b, c, t)
        for effect_name, effect_func in effects.items():
            modified_audio = effect_func(audio)
            # It is quite hard to unit test the content of the modified_audio though
            if effect_name == "speed":  # Speeding up audio should return in more frames
                assert modified_audio.size()[-1] > audio.size()[-1]
            else:
                assert modified_audio.size() == audio.size(), f"Wrong dimension in {effect_name}"