Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
meter_processor.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/engine.h"
7#include "dsp/kmeter_dsp.h"
8#include "dsp/peak_dsp.h"
9#include "dsp/port.h"
10#include "dsp/true_peak_dsp.h"
11#include "utils/types.h"
12#include "utils/variant_helpers.h"
13
14#include <QtQmlIntegration/qqmlintegration.h>
15
16#include <boost/container/static_vector.hpp>
17
18namespace zrythm::gui::qquick
19{
20
35class MeterProcessor : public QObject
36{
37 Q_OBJECT
38 QML_ELEMENT
39 Q_PROPERTY (QVariant port READ port WRITE setPort REQUIRED)
40 Q_PROPERTY (int channel READ channel WRITE setChannel)
41 Q_PROPERTY (
42 zrythm::dsp::AudioEngine * audioEngine READ audioEngine WRITE setAudioEngine
43 REQUIRED)
44 Q_PROPERTY (
45 float currentAmplitude READ currentAmplitude NOTIFY currentAmplitudeChanged)
46 Q_PROPERTY (float peakAmplitude READ peakAmplitude NOTIFY peakAmplitudeChanged)
48public:
49 enum class MeterAlgorithm : std::uint8_t
50 {
53
54 METER_ALGORITHM_DIGITAL_PEAK,
58 METER_ALGORITHM_RMS,
59 METER_ALGORITHM_K,
60 };
61
62 using MeterPortVariant = std::variant<dsp::MidiPort, dsp::AudioPort>;
63 using MeterPortPtrVariant = to_pointer_variant<MeterPortVariant>;
64
65 MeterProcessor (QObject * parent = nullptr);
66
67 // ================================================================
68 // QML Interface
69 // ================================================================
70
71 QVariant port () const { return QVariant::fromValue (port_obj_); }
72 void setPort (QVariant port_var);
73
74 int channel () const { return channel_; }
75 void setChannel (int channel);
76
77 dsp::AudioEngine * audioEngine () const { return audio_engine_; }
78 void setAudioEngine (dsp::AudioEngine * engine) { audio_engine_ = engine; }
79
80 float currentAmplitude () const
81 {
82 return current_amp_.load (std::memory_order_relaxed);
83 }
84 Q_SIGNAL void currentAmplitudeChanged (float value);
85
86 float peakAmplitude () const
87 {
88 return peak_amp_.load (std::memory_order_relaxed);
89 }
90 Q_SIGNAL void peakAmplitudeChanged (float value);
91
92 Q_INVOKABLE float toDBFS (float amp) const;
93 Q_INVOKABLE float toFader (float amp) const;
94
95 // ================================================================
96
97private:
103 void get_value (AudioValueFormat format, float * val, float * max);
104
105public:
107 QPointer<QObject> port_obj_;
108
109 // RAII request for port ring buffers to be filled
110 std::optional<dsp::RingBufferOwningPortMixin::RingBufferReader>
111 ring_buffer_reader_;
114 std::unique_ptr<zrythm::dsp::TruePeakDsp> true_peak_processor_;
115 std::unique_ptr<zrythm::dsp::TruePeakDsp> true_peak_max_processor_;
118 float true_peak_ = 0.f;
119 float true_peak_max_ = 0.f;
122 std::unique_ptr<zrythm::dsp::KMeterDsp> kmeter_processor_;
123
124 std::unique_ptr<zrythm::dsp::PeakDsp> peak_processor_;
125
134 float prev_max_ = 0.f;
135
138 float last_amp_ = 0.f;
141 SteadyTimePoint last_draw_time_;
142
143 qint64 last_midi_trigger_time_ = 0;
144
145private:
146 std::vector<float> tmp_buf_;
147
148 std::atomic<float> current_amp_ = 0.f;
149 std::atomic<float> peak_amp_ = 0.f;
150
151 // Audio port channel, if audio meter.
152 int channel_{};
153
154 QPointer<dsp::AudioEngine> audio_engine_;
155
156 boost::container::static_vector<dsp::MidiEvent, 256> tmp_events_;
157};
158}
The audio engine.
Definition engine.h:22
A meter processor for a single GUI element.
QPointer< QObject > port_obj_
Port associated with this meter.
@ METER_ALGORITHM_AUTO
Use default algorithm for the port.
SteadyTimePoint last_draw_time_
Time the last val was taken at (last draw time).
float prev_max_
Previous max, used when holding the max value.
float last_amp_
Last meter value (in amplitude), used to show a falloff and avoid sudden dips.
MeterAlgorithm algorithm_
Algorithm to use.
std::unique_ptr< zrythm::dsp::TruePeakDsp > true_peak_processor_
True peak processor.
std::unique_ptr< zrythm::dsp::KMeterDsp > kmeter_processor_
K RMS processor, if K meter.
AudioValueFormat
Definition types.h:117