summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibDSP/ProcessorParameter.h
blob: aa545397d42f92a6b183dfd77b8081954e5e560c (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
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
/*
 * Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include "AK/Forward.h"
#include "LibGUI/Label.h"
#include "Music.h"
#include <AK/FixedPoint.h>
#include <AK/Format.h>
#include <AK/Types.h>
#include <LibCore/Object.h>

namespace LibDSP {

using ParameterFixedPoint = FixedPoint<8, i64>;

// Processors have modifiable parameters that should be presented to the UI in a uniform way without requiring the processor itself to implement custom interfaces.
class ProcessorParameter {
public:
    ProcessorParameter(String name)
        : m_name(move(name))
    {
    }

    String const& name() const { return m_name; }

private:
    String const m_name;
};

namespace Detail {

struct ProcessorParameterSetValueTag {
    explicit ProcessorParameterSetValueTag() = default;
};

template<typename ParameterT>
class ProcessorParameterSingleValue : public ProcessorParameter {

public:
    ProcessorParameterSingleValue(String name, ParameterT initial_value)
        : ProcessorParameter(move(name))
        , m_value(move(initial_value))
    {
    }

    operator ParameterT() const
    {
        return value();
    }

    operator double() const requires(IsSame<ParameterT, ParameterFixedPoint>)
    {
        return static_cast<double>(value());
    }

    ParameterT value() const { return m_value; };
    void set_value(ParameterT value)
    {
        set_value_sneaky(value, LibDSP::Detail::ProcessorParameterSetValueTag {});
        if (did_change_value)
            did_change_value(value);
    }

    // Use of this function is discouraged. It doesn't notify the value listener.
    void set_value_sneaky(ParameterT value, [[maybe_unused]] Detail::ProcessorParameterSetValueTag)
    {
        if (value != m_value)
            m_value = value;
    }

    Function<void(ParameterT const&)> did_change_value;

protected:
    ParameterT m_value;
};
}

class ProcessorBooleanParameter final : public Detail::ProcessorParameterSingleValue<bool> {
public:
    ProcessorBooleanParameter(String name, bool initial_value)
        : Detail::ProcessorParameterSingleValue<bool>(move(name), move(initial_value))
    {
    }
};

class ProcessorRangeParameter final : public Detail::ProcessorParameterSingleValue<ParameterFixedPoint> {
public:
    ProcessorRangeParameter(String name, ParameterFixedPoint min_value, ParameterFixedPoint max_value, ParameterFixedPoint initial_value)
        : Detail::ProcessorParameterSingleValue<ParameterFixedPoint>(move(name), move(initial_value))
        , m_min_value(move(min_value))
        , m_max_value(move(max_value))
        , m_default_value(move(initial_value))
    {
        VERIFY(initial_value <= max_value && initial_value >= min_value);
    }

    ProcessorRangeParameter(ProcessorRangeParameter const& to_copy)
        : ProcessorRangeParameter(to_copy.name(), to_copy.min_value(), to_copy.max_value(), to_copy.value())
    {
    }

    ParameterFixedPoint min_value() const { return m_min_value; }
    ParameterFixedPoint max_value() const { return m_max_value; }
    ParameterFixedPoint default_value() const { return m_default_value; }
    void set_value(ParameterFixedPoint value)
    {
        VERIFY(value <= m_max_value && value >= m_min_value);
        Detail::ProcessorParameterSingleValue<ParameterFixedPoint>::set_value(value);
    }

private:
    double const m_min_value;
    double const m_max_value;
    double const m_default_value;
};

}
template<>
struct AK::Formatter<LibDSP::ProcessorRangeParameter> : AK::StandardFormatter {

    Formatter() = default;
    explicit Formatter(StandardFormatter formatter)
        : StandardFormatter(formatter)
    {
    }
    void format(FormatBuilder& builder, LibDSP::ProcessorRangeParameter value)
    {
        if (m_mode == Mode::Pointer) {
            Formatter<FlatPtr> formatter { *this };
            formatter.format(builder, reinterpret_cast<FlatPtr>(&value));
            return;
        }

        if (m_sign_mode != FormatBuilder::SignMode::Default)
            VERIFY_NOT_REACHED();
        if (m_alternative_form)
            VERIFY_NOT_REACHED();
        if (m_zero_pad)
            VERIFY_NOT_REACHED();
        if (m_mode != Mode::Default)
            VERIFY_NOT_REACHED();
        if (m_width.has_value() && m_precision.has_value())
            VERIFY_NOT_REACHED();

        m_width = m_width.value_or(0);
        m_precision = m_precision.value_or(NumericLimits<size_t>::max());

        builder.put_literal(String::formatted("[{} - {}]: {}", value.min_value(), value.max_value(), value.value()));
    }
};