Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
meter.h
1// SPDX-FileCopyrightText: © 2020, 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "dsp/kmeter_dsp.h"
7#include "dsp/peak_dsp.h"
8#include "dsp/port.h"
9#include "dsp/true_peak_dsp.h"
10#include "utils/types.h"
11#include "utils/variant_helpers.h"
12
13#include <QtQmlIntegration>
14
15#include <boost/container/static_vector.hpp>
16
22
24{
27
28 METER_ALGORITHM_DIGITAL_PEAK,
29
32 METER_ALGORITHM_RMS,
33 METER_ALGORITHM_K,
34};
35
50class MeterProcessor : public QObject
51{
52 Q_OBJECT
53 QML_ELEMENT
54 Q_PROPERTY (QVariant port READ port WRITE setPort REQUIRED)
55 Q_PROPERTY (int channel READ channel WRITE setChannel)
56 Q_PROPERTY (
57 float currentAmplitude READ currentAmplitude NOTIFY currentAmplitudeChanged)
58 Q_PROPERTY (float peakAmplitude READ peakAmplitude NOTIFY peakAmplitudeChanged)
59
60public:
61 using MeterPortVariant = std::variant<dsp::MidiPort, dsp::AudioPort>;
62 using MeterPortPtrVariant = to_pointer_variant<MeterPortVariant>;
63
64 MeterProcessor (QObject * parent = nullptr);
65
66 // ================================================================
67 // QML Interface
68 // ================================================================
69
70 QVariant port () const { return QVariant::fromValue (port_obj_); }
71 void setPort (QVariant port_var);
72
73 int channel () const { return channel_; }
74 void setChannel (int channel);
75
76 float currentAmplitude () const
77 {
78 return current_amp_.load (std::memory_order_relaxed);
79 }
80 Q_SIGNAL void currentAmplitudeChanged (float value);
81
82 float peakAmplitude () const
83 {
84 return peak_amp_.load (std::memory_order_relaxed);
85 }
86 Q_SIGNAL void peakAmplitudeChanged (float value);
87
88 Q_INVOKABLE float toDBFS (float amp) const;
89 Q_INVOKABLE float toFader (float amp) const;
90
91 // ================================================================
92
93private:
99 void get_value (AudioValueFormat format, float * val, float * max);
100
101public:
103 QPointer<QObject> port_obj_;
104
105 // RAII request for port ring buffers to be filled
106 std::optional<dsp::RingBufferOwningPortMixin::RingBufferReader>
107 ring_buffer_reader_;
108
110 std::unique_ptr<zrythm::dsp::TruePeakDsp> true_peak_processor_;
111 std::unique_ptr<zrythm::dsp::TruePeakDsp> true_peak_max_processor_;
112
114 float true_peak_ = 0.f;
115 float true_peak_max_ = 0.f;
116
118 std::unique_ptr<zrythm::dsp::KMeterDsp> kmeter_processor_;
119
120 std::unique_ptr<zrythm::dsp::PeakDsp> peak_processor_;
121
128
130 float prev_max_ = 0.f;
131
134 float last_amp_ = 0.f;
135
137 SteadyTimePoint last_draw_time_;
138
139 qint64 last_midi_trigger_time_ = 0;
140
141private:
142 std::vector<float> tmp_buf_;
143
144 std::atomic<float> current_amp_ = 0.f;
145 std::atomic<float> peak_amp_ = 0.f;
146
147 // Audio port channel, if audio meter.
148 int channel_{};
149
150 boost::container::static_vector<dsp::MidiEvent, 256> tmp_events_;
151};
std::unique_ptr< zrythm::dsp::KMeterDsp > kmeter_processor_
K RMS processor, if K meter.
Definition meter.h:117
float true_peak_
Current true peak.
Definition meter.h:113
MeterAlgorithm
Definition meter.h:24
float prev_max_
Previous max, used when holding the max value.
Definition meter.h:129
std::unique_ptr< zrythm::dsp::TruePeakDsp > true_peak_processor_
True peak processor.
Definition meter.h:109
QPointer< QObject > port_obj_
Port associated with this meter.
Definition meter.h:102
float last_amp_
Last meter value (in amplitude), used to show a falloff and avoid sudden dips.
Definition meter.h:133
MeterAlgorithm algorithm_
Algorithm to use.
Definition meter.h:126
SteadyTimePoint last_draw_time_
Time the last val was taken at (last draw time).
Definition meter.h:136
@ METER_ALGORITHM_AUTO
Use default algorithm for the port.
Definition meter.h:26
@ METER_ALGORITHM_TRUE_PEAK
Definition meter.h:31
AudioValueFormat
Definition types.h:120