Audacity 3.2.0
DynamicRangeProcessorHistoryPanel.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 DynamicRangeProcessorHistoryPanel.cpp
7
8 Matthieu Hodgkinson
9
10**********************************************************************/
12#include "AColor.h"
13#include "AllThemeResources.h"
14#include "AudioIO.h"
15#include "CompressorInstance.h"
18#include "Theme.h"
21#include "widgets/Ruler.h"
22#include <cassert>
23#include <cmath>
24#include <numeric>
25#include <wx/dcclient.h>
26#include <wx/graphics.h>
27#include <wx/platinfo.h>
28
29namespace
30{
31constexpr auto timerId = 7000;
32// Of course we aren't really targetting 200fps, but when specifying 50fps, we
33// rather get 30fps, with outliers at 20. Measurements (Windows) showed that,
34// when specifying 200, we get around 60fps on average, with outlier around 40.
35constexpr auto timerPeriodMs = 1000 / 200;
36
38{
39 // MacOS doesn't cope well with pen gradients. (Freezes in debug and is
40 // transperent in release.)
41 return wxPlatformInfo::Get().GetOperatingSystemId() & wxOS_WINDOWS;
42}
43
45{
46 using namespace DynamicRangeProcessorPanel;
48}
49} // namespace
50
56
58 wxWindow* parent, wxWindowID winid, CompressorInstance& instance,
59 std::function<void(float)> onDbRangeChanged)
60 : wxPanelWrapper { parent, winid }
61 , mCompressorInstance { instance }
62 , mOnDbRangeChanged { std::move(onDbRangeChanged) }
63 , mInitializeProcessingSettingsSubscription { static_cast<
65 instance)
66 .Subscribe(
67 [&](const std::optional<
69 evt) {
70 if (evt)
71 InitializeForPlayback(
72 instance,
73 evt->sampleRate);
74 else
75 // Stop the
76 // timer-based
77 // update but keep
78 // the history
79 // until playback
80 // is resumed.
81 mTimer.Stop();
82 }) }
83 , mRealtimeResumeSubscription { static_cast<RealtimeResumePublisher&>(
84 instance)
85 .Subscribe([this](auto) {
86 if (mHistory)
87 mHistory->BeginNewSegment();
88 }) }
89 , mPlaybackEventSubscription { AudioIO::Get()->Subscribe(
90 [this](const AudioIOEvent& evt) {
91 if (evt.type != AudioIOEvent::PAUSE)
92 return;
93 if (evt.on)
94 {
95 mTimer.Stop();
96 mClock.Pause();
97 }
98 else
99 {
100 mClock.Resume();
101 mTimer.Start(timerPeriodMs);
102 }
103 }) }
104{
105 if (const auto& sampleRate = instance.GetSampleRate();
106 sampleRate.has_value())
107 // Playback is ongoing, and so the `InitializeProcessingSettings` event
108 // was already fired.
109 InitializeForPlayback(instance, *sampleRate);
110
111 SetDoubleBuffered(true);
112 mTimer.SetOwner(this, timerId);
113 SetSize({ minWidth, DynamicRangeProcessorPanel::graphMinHeight });
114}
115
116namespace
117{
118double GetDisplayPixel(float elapsedSincePacket, int panelWidth)
119{
120 const auto secondsPerPixel =
122 // A display delay to avoid the display to tremble near time zero because the
123 // data hasn't arrived yet.
124 // This is a trade-off between visual comfort and timely update. It was set
125 // empirically, but with a relatively large audio playback delay. Maybe it
126 // will be found to lag on lower-latency playbacks. Best would probably be to
127 // make it playback-delay dependent.
128 constexpr auto displayDelay = 0.2f;
129 return panelWidth - 1 -
130 (elapsedSincePacket - displayDelay) / secondsPerPixel;
131}
132
140 std::vector<wxPoint2DDouble>& A, std::vector<wxPoint2DDouble>& B)
141{
142 assert(A.size() == B.size());
143 if (A.size() != B.size())
144 return;
145 std::optional<bool> aWasBelow;
146 auto x0 = 0.;
147 auto y0_a = 0.;
148 auto y0_b = 0.;
149 auto it = A.begin();
150 auto jt = B.begin();
151 while (it != A.end())
152 {
153 const auto x2 = it->m_x;
154 const auto y2_a = it->m_y;
155 const auto y2_b = jt->m_y;
156 const auto aIsBelow = y2_a < y2_b;
157 if (aWasBelow.has_value() && *aWasBelow != aIsBelow)
158 {
159 // clang-format off
160 // We have a crossing of y2_a and y2_b between x0 and x2.
161 // y_a(x) = y0_a + (x - x0) / (x2 - x0) * (y2_a - y0_a)
162 // and likewise for y_b.
163 // Let y_a(x1) = y_b(x1) and solve for x1:
164 // x1 = x0 + (x2 - x0) * (y0_b - y0_a) / ((a_n - y0_a) - (b_n - y0_b))
165 // clang-format on
166 const auto x1 =
167 x0 + (x2 - x0) * (y0_a - y0_b) / (y2_b - y2_a + y0_a - y0_b);
168 const auto y = y0_a + (x1 - x0) / (x2 - x0) * (y2_a - y0_a);
169 if (std::isfinite(x1) && std::isfinite(y))
170 {
171 it = A.emplace(it, x1, y)++;
172 jt = B.emplace(jt, x1, y)++;
173 };
174 }
175 x0 = x2;
176 y0_a = y2_a;
177 y0_b = y2_b;
178 aWasBelow = aIsBelow;
179 ++it;
180 ++jt;
181 }
182}
183
189 std::vector<wxPoint2DDouble> lines, const wxColor& color,
190 wxGraphicsContext& gc, const wxRect& rect)
191{
192 const auto height = rect.GetHeight();
193 const auto left = std::max<double>(rect.GetX(), lines.front().m_x);
194 const auto right = std::min<double>(rect.GetWidth(), lines.back().m_x);
195 auto area = gc.CreatePath();
196 area.MoveToPoint(right, height);
197 area.AddLineToPoint(left, height);
198 std::for_each(lines.begin(), lines.end(), [&area](const auto& p) {
199 area.AddLineToPoint(p);
200 });
201 area.CloseSubpath();
202 gc.SetBrush(color);
203 gc.FillPath(area);
204}
205
206void DrawLegend(size_t height, wxPaintDC& dc, wxGraphicsContext& gc)
207{
208 using namespace DynamicRangeProcessorPanel;
209
210 constexpr auto legendWidth = 16;
211 constexpr auto legendHeight = 16;
212 constexpr auto legendSpacing = 8;
213 constexpr auto legendX = 5;
214 const auto legendY = height - 5 - legendHeight;
215 const auto legendTextX = legendX + legendWidth + 5;
216 const auto legendTextHeight = dc.GetTextExtent("X").GetHeight();
217 const auto legendTextYOffset = (legendHeight - legendTextHeight) / 2;
218 const auto legendTextY = legendY + legendTextYOffset;
219
220 struct LegendInfo
221 {
222 const wxColor color;
223 const TranslatableString text;
224 };
225
226 std::vector<LegendInfo> legends = {
227 { inputColor, XO("Input") },
228 { outputColor, XO("Output") },
229 };
230
231 int legendTextXOffset = 0;
232 dc.SetTextForeground(*wxBLACK);
233 dc.SetFont(
234 { 10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL });
235 for (const auto& legend : legends)
236 {
237 // First fill with background color so that transparent foreground colors
238 // yield the same result as on the graph.
239 gc.SetPen(*wxTRANSPARENT_PEN);
240 gc.SetBrush(backgroundColor);
241 gc.DrawRectangle(
242 legendX + legendTextXOffset, legendY, legendWidth, legendHeight);
243
244 gc.SetBrush(wxColor { legend.color.GetRGB() });
245 gc.DrawRectangle(
246 legendX + legendTextXOffset, legendY, legendWidth, legendHeight);
247
248 gc.SetPen(lineColor);
249 gc.SetBrush(*wxTRANSPARENT_BRUSH);
250 gc.DrawRectangle(
251 legendX + legendTextXOffset, legendY, legendWidth, legendHeight);
252
253 dc.DrawText(
254 legend.text.Translation(), legendTextX + legendTextXOffset,
255 legendTextY);
256 const auto legendTextWidth =
257 dc.GetTextExtent(legend.text.Translation()).GetWidth();
258 legendTextXOffset += legendWidth + 5 + legendTextWidth + legendSpacing;
259 }
260
261 const auto lineY = legendY + legendHeight / 2.;
262 constexpr auto lineWidth = 24;
263
264 // Actual compression
265 const auto actualX = legendX + legendTextXOffset + legendSpacing;
266 const std::array<wxPoint2DDouble, 2> actualLine {
267 wxPoint2DDouble(actualX, lineY),
268 wxPoint2DDouble(actualX + lineWidth, lineY)
269 };
270
272 gc.DrawLines(2, actualLine.data());
273
274 if (MayUsePenGradients())
275 {
276 wxGraphicsPenInfo penInfo;
277 wxGraphicsGradientStops stops { actualCompressionColor,
279 stops.Add(attackColor.GetRGB(), 1 / 4.);
280 stops.Add(actualCompressionColor, 2 / 4.);
281 stops.Add(releaseColor.GetRGB(), 3 / 4.);
282 penInfo.LinearGradient(actualX, 0, actualX + lineWidth, 0, stops)
284 gc.SetPen(gc.CreatePen(penInfo));
285 }
286 else
287 gc.SetPen(actualCompressionColor);
288
289 gc.DrawLines(2, actualLine.data());
290 const auto actualText = XO("Actual Compression");
291 const auto actualTextX = actualX + lineWidth + legendSpacing;
292 dc.DrawText(actualText.Translation(), actualTextX, legendTextY);
293
294 // Target compression
296 const auto targetX =
297 actualTextX + dc.GetTextExtent(actualText.Translation()).GetWidth() + 10;
298 gc.StrokeLine(targetX, lineY, targetX + lineWidth, lineY);
299 const auto compressionText = XO("Target Compression");
300 dc.DrawText(
301 compressionText.Translation(), targetX + lineWidth + 5, legendTextY);
302}
303} // namespace
304
306{
307 mShowInput = show;
308 Refresh(false);
309}
310
312{
313 mShowOutput = show;
314 Refresh(false);
315}
316
318{
319 mShowActual = show;
320 Refresh(false);
321}
322
324{
325 mShowTarget = show;
326 Refresh(false);
327}
328
330{
331 wxPaintDC dc(this);
332
333 using namespace DynamicRangeProcessorPanel;
334
335 const auto gc = MakeGraphicsContext(dc);
336 const auto rect = DynamicRangeProcessorPanel::GetPanelRect(*this);
337 const auto x = rect.GetX();
338 const auto y = rect.GetY();
339 const auto width = rect.GetWidth();
340 const auto height = rect.GetHeight();
341
342 gc->SetBrush(GetGraphBackgroundBrush(*gc, height));
343 gc->SetPen(wxTransparentColor);
344 gc->DrawRectangle(x, y, width, height);
345
346 Finally Do { [&] {
347 // The legend is causing problems color-wise, and in the end it may not be
348 // so useful since the different elements of the graph can be toggled.
349 // Keep it up our sleeve for now, though. (Anyone still sees this in the
350 // not-so-near future, feel free to clean up.)
351 constexpr auto drawLegend = false;
352 if (drawLegend)
353 DrawLegend(height, dc, *gc);
354 gc->SetBrush(*wxTRANSPARENT_BRUSH);
355 gc->SetPen(lineColor);
356 gc->DrawRectangle(x, y, width, height);
357 } };
358
359 if (!mHistory || !mSync)
360 {
362 {
363 const auto text = XO("awaiting playback");
364 const wxDCFontChanger changer { dc,
365 { 16, wxFONTFAMILY_DEFAULT,
366 wxFONTSTYLE_NORMAL,
367 wxFONTWEIGHT_NORMAL } };
368 const auto textWidth = dc.GetTextExtent(text.Translation()).GetWidth();
369 const auto textHeight =
370 dc.GetTextExtent(text.Translation()).GetHeight();
371 dc.SetTextForeground(wxColor { 128, 128, 128 });
372 dc.DrawText(
373 text.Translation(), (width - textWidth) / 2,
374 (height - textHeight) / 2);
375 }
376 return;
377 }
378
379 const auto& segments = mHistory->GetSegments();
380 const auto elapsedTimeSinceFirstPacket =
381 std::chrono::duration<float>(mSync->now - mSync->start).count();
382 const auto rangeDb = DynamicRangeProcessorPanel::GetGraphDbRange(height);
383 const auto dbPerPixel = rangeDb / height;
384
385 for (const auto& segment : segments)
386 {
387 mX.clear();
388 mTarget.clear();
389 mActual.clear();
390 mInput.clear();
391 mOutput.clear();
392
393 mX.resize(segment.size());
394 auto lastInvisibleLeft = 0;
395 auto firstInvisibleRight = 0;
396 std::transform(
397 segment.begin(), segment.end(), mX.begin(), [&](const auto& packet) {
398 const auto x = GetDisplayPixel(
399 elapsedTimeSinceFirstPacket -
400 (packet.time - mSync->firstPacketTime),
401 width);
402 if (x < 0)
403 ++lastInvisibleLeft;
404 if (x < width)
405 ++firstInvisibleRight;
406 return x;
407 });
408 lastInvisibleLeft = std::max<int>(--lastInvisibleLeft, 0);
409 firstInvisibleRight = std::min<int>(++firstInvisibleRight, mX.size());
410
411 mX.erase(mX.begin() + firstInvisibleRight, mX.end());
412 mX.erase(mX.begin(), mX.begin() + lastInvisibleLeft);
413
414 if (mX.size() < 2)
415 continue;
416
417 auto segmentIndex = lastInvisibleLeft;
418 mTarget.reserve(mX.size());
419 mActual.reserve(mX.size());
420 mInput.reserve(mX.size());
421 mOutput.reserve(mX.size());
422 std::for_each(mX.begin(), mX.end(), [&](auto x) {
423 const auto& packet = segment[segmentIndex++];
424 const auto elapsedSincePacket = elapsedTimeSinceFirstPacket -
425 (packet.time - mSync->firstPacketTime);
426 mTarget.emplace_back(x, -packet.target / dbPerPixel);
427 mActual.emplace_back(x, -packet.follower / dbPerPixel);
428 mInput.emplace_back(x, -packet.input / dbPerPixel);
429 mOutput.emplace_back(x, -packet.output / dbPerPixel);
430 });
431
432 if (mShowInput)
433 FillUpTo(mInput, inputColor, *gc, rect);
434
435 if (mShowOutput)
436 {
437 FillUpTo(mOutput, outputColor, *gc, rect);
438 const auto outputGc = MakeGraphicsContext(dc);
439 outputGc->SetPen({ wxColor { outputColor.GetRGB() }, 2 });
440 outputGc->DrawLines(mOutput.size(), mOutput.data());
441 }
442
443 if (mShowActual)
444 {
445 // First draw a thick line, and then the thinner line in its center
446 // with colors indicating overshoot and undershoot.
447 const auto actualGc = MakeGraphicsContext(dc);
448
449 actualGc->SetPen(
451 actualGc->DrawLines(mActual.size(), mActual.data());
452 if (MayUsePenGradients())
453 {
454 // So that we can converge to `actualCompressionColor` at the
455 // crossings of actual and target compression.
457
458 wxGraphicsGradientStops stops;
459 const auto xLeft = mActual.front().m_x;
460 const auto xRight = mActual.back().m_x;
461 const auto span = xRight - xLeft;
462 for (auto i = 0; i < mActual.size(); ++i)
463 {
464 const auto diff = mTarget[i].m_y - mActual[i].m_y;
465 const auto actualIsBelow = diff < 0;
466 const auto w = std::min(1.0, std::abs(diff) * dbPerPixel / 6);
467 const auto color = GetColorMix(
468 actualIsBelow ? releaseColor : attackColor,
470 .GetRGB();
471 stops.Add(color, (mActual[i].m_x - xLeft) / span);
472 }
473 wxGraphicsPenInfo penInfo;
474 penInfo
475 .LinearGradient(
476 mActual.front().m_x, 0, mActual.back().m_x, 0, stops)
478
479 actualGc->SetPen(actualGc->CreatePen(penInfo));
480 actualGc->DrawLines(mActual.size(), mActual.data());
481 }
482 }
483
484 if (mShowTarget)
485 {
486 const auto targetGc = MakeGraphicsContext(dc);
487 targetGc->SetPen(
489 targetGc->DrawLines(mTarget.size(), mTarget.data());
490 }
491 }
492}
493
495{
496 Refresh(false);
498 DynamicRangeProcessorPanel::GetGraphDbRange(GetSize().GetHeight()));
499}
500
502{
503 mPacketBuffer.clear();
505 while (mOutputQueue->Get(packet))
506 mPacketBuffer.push_back(packet);
507 mHistory->Push(mPacketBuffer);
508
509 if (mHistory->IsEmpty())
510 return;
511
512 // Do now get `std::chrono::steady_clock::now()` in the `OnPaint` event,
513 // because this can be triggered even when playback is paused.
514 const auto now = mClock.GetNow();
515 if (!mSync)
516 // At the time of writing, the realtime playback doesn't account for
517 // varying latencies. When it does, the synchronization will have to be
518 // updated on latency change. See
519 // https://github.com/audacity/audacity/issues/3223#issuecomment-2137025150.
520 mSync.emplace(
521 ClockSynchronization { mHistory->GetSegments().front().front().time +
523 now });
524 mPlaybackAboutToStart = false;
525
526 mSync->now = now;
527
528 Refresh(false);
529 wxPanelWrapper::Update();
530}
531
533 CompressorInstance& instance, double sampleRate)
534{
535 mSync.reset();
536 mHistory.emplace(sampleRate);
537 // We don't know for sure the least packet size (which is variable). 100
538 // samples per packet at a rate of 8kHz is 12.5ms, which is quite low
539 // latency. For higher sample rates that will be less.
540 constexpr auto leastPacketSize = 100;
541 const size_t maxQueueSize = DynamicRangeProcessorHistory::maxTimeSeconds *
542 sampleRate / leastPacketSize;
543 mPacketBuffer.reserve(maxQueueSize);
544 // Although `mOutputQueue` is a shared_ptr, we construct a unique_ptr and
545 // invoke the shared_ptr ctor overload that takes a unique_ptr.
546 // This way, we avoid the `error: aligned deallocation function of type
547 // 'void (void *, std::align_val_t) noexcept' is only available on
548 // macOS 10.13 or newer` compilation error.
550 std::make_unique<DynamicRangeProcessorOutputPacketQueue>(maxQueueSize);
552 mTimer.Start(timerPeriodMs);
554 Refresh(false);
555 wxPanelWrapper::Update();
556}
557
559{
560 return false;
561}
562
564{
565 return false;
566}
END_EVENT_TABLE()
int min(int a, int b)
XO("Cut/Copy/Paste")
#define A(N)
Definition: ToChars.cpp:62
static AudioIO * Get()
Definition: AudioIO.cpp:126
float GetLatencyMs() const
void SetOutputQueue(std::weak_ptr< DynamicRangeProcessorOutputPacketQueue >)
std::chrono::steady_clock::time_point GetNow() const
std::optional< DynamicRangeProcessorHistory > mHistory
std::optional< ClockSynchronization > mSync
void InitializeForPlayback(CompressorInstance &instance, double sampleRate)
std::shared_ptr< DynamicRangeProcessorOutputPacketQueue > mOutputQueue
std::vector< DynamicRangeProcessorOutputPacket > mPacketBuffer
const std::function< void(float)> mOnDbRangeChanged
An object that sends messages to an open-ended list of subscribed callbacks.
Definition: Observer.h:108
Subscription Subscribe(Callback callback)
Connect a callback to the Publisher; later-connected are called earlier.
Definition: Observer.h:199
Holds a msgid for the translation catalog; may also bind format arguments.
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:202
wxColor GetColorMix(const wxColor &a, const wxColor &b, double aWeight)
std::unique_ptr< wxGraphicsContext > MakeGraphicsContext(const wxPaintDC &dc)
wxRect GetPanelRect(const wxPanelWrapper &panel)
wxGraphicsBrush GetGraphBackgroundBrush(wxGraphicsContext &gc, int height)
void FillUpTo(std::vector< wxPoint2DDouble > lines, const wxColor &color, wxGraphicsContext &gc, const wxRect &rect)
void DrawLegend(size_t height, wxPaintDC &dc, wxGraphicsContext &gc)
void InsertCrossings(std::vector< wxPoint2DDouble > &A, std::vector< wxPoint2DDouble > &B)
STL namespace.
bool on
Definition: AudioIO.h:68
enum AudioIOEvent::Type type
"finally" as in The C++ Programming Language, 4th ed., p. 358 Useful for defining ad-hoc RAII actions...
Definition: MemoryX.h:175