File: limiter.cpp

package info (click to toggle)
pulseeffects 4.8.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,912 kB
  • sloc: cpp: 18,500; xml: 3,825; sh: 228; python: 56; makefile: 4
file content (160 lines) | stat: -rw-r--r-- 5,835 bytes parent folder | download
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 *  Copyright © 2017-2020 Wellington Wallace
 *
 *  This file is part of PulseEffects.
 *
 *  PulseEffects is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  PulseEffects is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with PulseEffects.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "limiter.hpp"
#include <glibmm/main.h>
#include <array>
#include "util.hpp"

namespace {

void on_post_messages_changed(GSettings* settings, gchar* key, Limiter* l) {
  const auto post = g_settings_get_boolean(settings, key);

  if (post) {
    if (!l->input_level_connection.connected()) {
      l->input_level_connection = Glib::signal_timeout().connect(
          [l]() {
            float inL = 0.0F;
            float inR = 0.0F;

            g_object_get(l->limiter, "meter-inL", &inL, nullptr);
            g_object_get(l->limiter, "meter-inR", &inR, nullptr);

            std::array<double, 2> in_peak = {inL, inR};

            l->input_level.emit(in_peak);

            return true;
          },
          100);
    }

    if (!l->output_level_connection.connected()) {
      l->output_level_connection = Glib::signal_timeout().connect(
          [l]() {
            float outL = 0.0F;
            float outR = 0.0F;

            g_object_get(l->limiter, "meter-outL", &outL, nullptr);
            g_object_get(l->limiter, "meter-outR", &outR, nullptr);

            std::array<double, 2> out_peak = {outL, outR};

            l->output_level.emit(out_peak);

            return true;
          },
          100);
    }

    if (!l->attenuation_connection.connected()) {
      l->attenuation_connection = Glib::signal_timeout().connect(
          [l]() {
            float att = 0.0F;

            g_object_get(l->limiter, "att", &att, nullptr);

            l->attenuation.emit(att);

            return true;
          },
          100);
    }

  } else {
    l->input_level_connection.disconnect();
    l->output_level_connection.disconnect();
    l->attenuation_connection.disconnect();
  }
}

}  // namespace

Limiter::Limiter(const std::string& tag, const std::string& schema, const std::string& schema_path)
    : PluginBase(tag, "limiter", schema, schema_path) {
  limiter = gst_element_factory_make("calf-sourceforge-net-plugins-Limiter", nullptr);

  if (is_installed(limiter)) {
    auto* audioconvert_in = gst_element_factory_make("audioconvert", "limiter_audioconvert_in");
    auto* audioconvert_out = gst_element_factory_make("audioconvert", "limiter_audioconvert_out");

    gst_bin_add_many(GST_BIN(bin), audioconvert_in, limiter, audioconvert_out, nullptr);

    gst_element_link_many(audioconvert_in, limiter, audioconvert_out, nullptr);

    auto* pad_sink = gst_element_get_static_pad(audioconvert_in, "sink");
    auto* pad_src = gst_element_get_static_pad(audioconvert_out, "src");

    gst_element_add_pad(bin, gst_ghost_pad_new("sink", pad_sink));
    gst_element_add_pad(bin, gst_ghost_pad_new("src", pad_src));

    gst_object_unref(GST_OBJECT(pad_sink));
    gst_object_unref(GST_OBJECT(pad_src));

    g_object_set(limiter, "bypass", 0, nullptr);

    bind_to_gsettings();

    g_signal_connect(settings, "changed::post-messages", G_CALLBACK(on_post_messages_changed), this);

    // useless write just to force callback call

    auto enable = g_settings_get_boolean(settings, "state");

    g_settings_set_boolean(settings, "state", enable);
  }
}

Limiter::~Limiter() {
  util::debug(log_tag + name + " destroyed");
}

void Limiter::bind_to_gsettings() {
  g_settings_bind_with_mapping(settings, "input-gain", limiter, "level-in", G_SETTINGS_BIND_DEFAULT,
                               util::db20_gain_to_linear, util::linear_gain_to_db20, nullptr, nullptr);

  g_settings_bind_with_mapping(settings, "limit", limiter, "limit", G_SETTINGS_BIND_GET, util::db20_gain_to_linear,
                               nullptr, nullptr, nullptr);

  // Calf limiter did automatic makeup gain by the same amount given as limit.
  // See https://github.com/calf-studio-gear/calf/issues/162
  // That is why we reduced the output level accordingly binding both limit and output-gain to the same,
  // gsettings key, but from 0.90.2 version the "auto-level" toggle was introduced, so we expose the
  // output gain as a separate parameter along with the automatic level button.
  // See changelog at https://freshcode.club/projects/calf

  g_settings_bind_with_mapping(settings, "output-gain", limiter, "level-out", G_SETTINGS_BIND_GET,
                               util::db20_gain_to_linear, util::linear_gain_to_db20, nullptr, nullptr);

  g_settings_bind_with_mapping(settings, "lookahead", limiter, "attack", G_SETTINGS_BIND_GET, util::double_to_float,
                               nullptr, nullptr, nullptr);

  g_settings_bind_with_mapping(settings, "release", limiter, "release", G_SETTINGS_BIND_GET, util::double_to_float,
                               nullptr, nullptr, nullptr);

  g_settings_bind(settings, "auto-level", limiter, "auto-level", G_SETTINGS_BIND_DEFAULT);

  g_settings_bind(settings, "asc", limiter, "asc", G_SETTINGS_BIND_DEFAULT);

  g_settings_bind_with_mapping(settings, "asc-level", limiter, "asc-coeff", G_SETTINGS_BIND_GET, util::double_to_float,
                               nullptr, nullptr, nullptr);

  g_settings_bind(settings, "oversampling", limiter, "oversampling", G_SETTINGS_BIND_DEFAULT);
}