summaryrefslogtreecommitdiff
path: root/src/audio_core/renderer/effect/compressor.cpp
blob: 3b32670f706ba889ce58cd0db90f26ddddb0101d (plain)
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
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-FileCopyrightText: Copyright 2025 citron Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <cmath>
#include "audio_core/renderer/effect/compressor.h"

namespace AudioCore::Renderer {

void CompressorInfo::Update(BehaviorInfo::ErrorInfo& error_info,
                            const InParameterVersion1& in_params, const PoolMapper& pool_mapper) {}

void CompressorInfo::Update(BehaviorInfo::ErrorInfo& error_info,
                            const InParameterVersion2& in_params, const PoolMapper& pool_mapper) {
    auto in_specific{reinterpret_cast<const ParameterVersion1*>(in_params.specific.data())};
    auto params{reinterpret_cast<ParameterVersion1*>(parameter.data())};

    std::memcpy(params, in_specific, sizeof(ParameterVersion1));
    mix_id = in_params.mix_id;
    process_order = in_params.process_order;
    enabled = in_params.enabled;

    error_info.error_code = ResultSuccess;
    error_info.address = CpuAddr(0);
}

void CompressorInfo::UpdateForCommandGeneration() {
    if (enabled) {
        usage_state = UsageState::Enabled;
    } else {
        usage_state = UsageState::Disabled;
    }

    auto params{reinterpret_cast<ParameterVersion1*>(parameter.data())};
    params->state = ParameterState::Updated;
}

CpuAddr CompressorInfo::GetWorkbuffer(s32 index) {
    return GetSingleBuffer(index);
}

CompressorEffect::CompressorEffect(std::size_t sample_count_) : sample_count{sample_count_} {}

void CompressorEffect::Process(std::span<f32> output_buffer, std::span<const f32> input_buffer) {
    if (!IsEnabled() || !parameter.IsChannelCountValid()) {
        std::copy(input_buffer.begin(), input_buffer.end(), output_buffer.begin());
        return;
    }

    if (!result_state.empty() && parameter.statistics_reset) {
        statistics.Reset(static_cast<u16>(parameter.channel_count));
    }

    // Process audio with compressor effect
    for (u32 sample_index = 0; sample_index < sample_count; sample_index++) {
        float mean = 0.0f;
        for (u32 channel = 0; channel < parameter.channel_count; channel++) {
            const auto sample = input_buffer[sample_index * parameter.channel_count + channel];
            mean += sample * sample;
        }
        mean /= static_cast<float>(parameter.channel_count);

        // Calculate compression gain
        float compression_gain = 1.0f;
        if (mean > parameter.threshold) {
            compression_gain = parameter.threshold / mean;
            compression_gain = std::pow(compression_gain, 1.0f - (1.0f / parameter.ratio));
        }

        // Apply compression
        for (u32 channel = 0; channel < parameter.channel_count; channel++) {
            const auto in_sample = input_buffer[sample_index * parameter.channel_count + channel];
            const auto out_sample = in_sample * compression_gain * parameter.input_gain;
            output_buffer[sample_index * parameter.channel_count + channel] = out_sample;

            // Update statistics if enabled
            if (parameter.statistics_enabled) {
                statistics.maximum_mean = std::max(statistics.maximum_mean, mean);
                statistics.minimum_gain = std::min(statistics.minimum_gain, compression_gain);
                statistics.last_samples[channel] = std::abs(in_sample) * (1.0f / 32768.0f);
            }
        }
    }
}

} // namespace AudioCore::Renderer