Zrythm v2.0.0-alpha.1
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/qt.h"
12#include "utils/types.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 (zrythm::dsp::Port * 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 MeterProcessor (QObject * parent = nullptr);
63
64 // ================================================================
65 // QML Interface
66 // ================================================================
67
68 dsp::Port * port () const { return port_; }
69 void setPort (dsp::Port * port);
70
71 int channel () const { return channel_; }
72 void setChannel (int channel);
73
74 dsp::AudioEngine * audioEngine () const { return audio_engine_; }
75 void setAudioEngine (dsp::AudioEngine * engine) { audio_engine_ = engine; }
76
77 float currentAmplitude () const
78 {
79 return current_amp_.load (std::memory_order_relaxed);
80 }
81 Q_SIGNAL void currentAmplitudeChanged (float value);
82
83 float peakAmplitude () const
84 {
85 return peak_amp_.load (std::memory_order_relaxed);
86 }
87 Q_SIGNAL void peakAmplitudeChanged (float value);
88
89 Q_INVOKABLE float toDBFS (float amp) const;
90 Q_INVOKABLE float toFader (float amp) const;
91
92 // ================================================================
93
94private:
100 void get_value (AudioValueFormat format, float * val, float * max);
101
102public:
104 QPointer<dsp::Port> port_;
105
106 // RAII request for port ring buffers to be filled
107 std::optional<dsp::RingBufferOwningPortMixin::RingBufferReader>
108 ring_buffer_reader_;
111 std::unique_ptr<zrythm::dsp::TruePeakDsp> true_peak_processor_;
112 std::unique_ptr<zrythm::dsp::TruePeakDsp> true_peak_max_processor_;
115 float true_peak_ = 0.f;
116 float true_peak_max_ = 0.f;
119 std::unique_ptr<zrythm::dsp::KMeterDsp> kmeter_processor_;
120
121 std::unique_ptr<zrythm::dsp::PeakDsp> peak_processor_;
122
131 float prev_max_ = 0.f;
132
135 float last_amp_ = 0.f;
138 SteadyTimePoint last_draw_time_;
139
140 qint64 last_midi_trigger_time_ = 0;
141
142private:
143 std::vector<float> tmp_buf_;
144
145 std::atomic<float> current_amp_ = 0.f;
146 std::atomic<float> peak_amp_ = 0.f;
147
148 // Audio port channel, if audio meter.
149 int channel_{};
150
151 QPointer<dsp::AudioEngine> audio_engine_;
152
154
155 boost::container::static_vector<dsp::MidiEvent, 256> tmp_events_;
156};
157}
The audio engine.
Definition engine.h:28
A base class for ports used for connecting processors in the DSP graph.
Definition port.h:34
A meter processor for a single GUI element.
@ 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.
QPointer< dsp::Port > port_
Port associated with this meter.
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.
A unique pointer for QObject objects that also works with QObject-based ownership.
Definition qt.h:36
AudioValueFormat
Definition types.h:85