Repository navigation
Expand file tree
/
Copy pathui_sid.cpp
More file actions
1154 lines (987 loc) · 42.1 KB
/
Copy pathui_sid.cpp
File metadata and controls
1154 lines (987 loc) · 42.1 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of the Diffractor photo and video organizer
// Copyright 2026 Zac Walker
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Purpose: A flex-based SID monitor in Diffractor's colours, drawn as panels of the media stack: the file's other
// tunes, each voice's waveform, note, envelope and ADSR, the chip's output, its filter and volume, and the
// registers the tune writes - with the tune selector and per-voice mute and solo as controls, which one drawn for a
// movie leaves out. What moves with the music is drawn through a vertex buffer the display updates for every
// present, as the audio visualizer is.
#include "pch.h"
#include "app_text.h"
#include "model.h"
#include "ui_sid.h"
#include "ui_music.h"
#include "ui_view.h"
namespace
{
using monitor_palette::shade;
namespace palette = monitor_palette;
// A silenced voice is drawn faded, as the tune goes on playing it.
constexpr float silenced_alpha = 0.45f;
// How often the values, notes and register contents beside the moving parts are redrawn while they change.
// They are text, which only rebuilding the scene redraws, and ten times a second is as fast as they can be
// read.
constexpr double text_refresh_seconds = 0.1;
// A monitor not drawn for this long is not on screen. The view is rebuilt at least once a second while a
// tune plays, as the elapsed time it shows changes.
constexpr double hidden_after_seconds = 2.0;
// As the data sheet names the registers $D400-$D418.
constexpr std::string_view register_names[sid_register_count] = {
// clang-format off
"FQ1L", "FQ1H", "PW1L", "PW1H", "CTL1", "AD1", "SR1",
"FQ2L", "FQ2H", "PW2L", "PW2H", "CTL2", "AD2", "SR2",
"FQ3L", "FQ3H", "PW3L", "PW3H", "CTL3", "AD3", "SR3",
"FCLO", "FCHI", "RESF", "MVOL",
// clang-format on
};
constexpr int register_columns = 7;
constexpr int register_rows = (sid_register_count + register_columns - 1) / register_columns;
// Frames the register grid's highlight takes to fade: about a tenth of a second, as on turbo8bit.
constexpr size_t activity_frames = 6;
// Samples the scope reads behind the playback position, and the most it shows of them after the
// trigger.
constexpr size_t scope_window = 2048;
constexpr size_t scope_visible = 1024;
// A press that runs one action on release, so a press the user drags away from is abandoned. A release with no
// press of its own - the one after a press that closed a menu - does nothing.
class sid_click_controller final : public view_controller
{
public:
sid_click_controller(view_host_ptr host, const recti bounds, std::function<void()> invoke) :
view_controller(std::move(host), bounds), _invoke(std::move(invoke))
{
}
platform::cursor cursor() const override { return platform::cursor::link; }
void on_mouse_left_button_down(const pointi /*loc*/, const platform::key_state /*keys*/) override
{
_pressed = true;
}
void on_mouse_left_button_up(const pointi loc, const platform::key_state /*keys*/) override
{
if (std::exchange(_pressed, false) && _bounds.contains(loc)) _invoke();
}
private:
std::function<void()> _invoke;
bool _pressed = false;
};
// Text the monitor draws after its moving parts, so a label reads over the lamp behind it.
struct queued_text
{
std::string text;
recti bounds;
core::color colour;
render::text_style style = render::text_style::single_line;
};
// What the monitor draws with: the context, the alpha everything fades by, the display scale, and the
// text it holds back until the moving parts are drawn.
struct painter
{
render::draw_context& dc;
float alpha;
double scale;
std::vector<queued_text>& texts;
int px(const double v) const { return std::max(1, core::round(v * scale)); }
core::color c(const core::color32 rgb, const float a = 1.0f) const { return {rgb, alpha * a}; }
core::color c(const shade s, const float a = 1.0f) const { return {s.rgb, alpha * a * s.alpha}; }
void fill(const recti r, const core::color colour, const int radius) const
{
dc.draw_rounded_rect(r, colour, radius);
}
void text(const std::string_view s, const recti r, const core::color32 colour, const float a = 1.0f,
const render::text_style style = render::text_style::single_line) const
{
texts.push_back({std::string(s), r, c(colour, a), style});
}
int text_width(const std::string_view s) const
{
return dc.measure_text(s, render::font_face::code, render::text_style::single_line, 10000).cx;
}
void badge(const std::string_view s, const recti r, const core::color fill_colour, const core::color32 colour,
const float a = 1.0f) const
{
fill(r, fill_colour, px(3));
text(s, r, colour, a, render::text_style::single_line_center);
}
};
void add_rect(std::vector<render::vertex_rect>& out, const recti r, const core::color colour)
{
if (!r.is_empty() && colour.a > 0.0f) out.push_back({r, colour});
}
// The vertex buffer draws rectangles, not lines, so a trace is narrow columns, each joined to the one
// before it. ys holds one height per column.
void add_trace(std::vector<render::vertex_rect>& out, const recti r, const std::vector<int>& ys,
const int thickness, const core::color colour)
{
if (ys.empty()) return;
const auto column = static_cast<double>(r.width()) / static_cast<double>(ys.size());
for (auto i = 0_z; i < ys.size(); ++i)
{
const auto y = ys[i];
const auto previous = i > 0 ? ys[i - 1] : y;
const auto left = r.left + core::round(i * column);
const auto right = std::max(left + 1, r.left + core::round((i + 1) * column));
const auto top = std::min(y, previous) - thickness / 2;
const auto bottom = std::max(y, previous) + (thickness + 1) / 2;
add_rect(out, recti(left, top, right, bottom), colour);
}
}
// A meter's fill, inside its track.
void add_meter(std::vector<render::vertex_rect>& out, const recti track, const double fraction, const int inset,
const core::color colour)
{
if (track.is_empty()) return;
const auto inner = track.inflate(-inset);
const auto width = core::round(inner.width() * std::clamp(fraction, 0.0, 1.0));
if (width > 0) add_rect(out, recti(inner.left, inner.top, inner.left + width, inner.bottom), colour);
}
// One voice's registers, decoded.
struct voice_view
{
uint16_t frequency = 0;
uint16_t pulse_width = 0;
uint8_t control = 0;
int attack = 0, decay = 0, sustain = 0, release = 0;
uint8_t envelope = 0;
voice_view(const sid_frame_state& f, const int v)
{
const auto* const r = f.regs.data() + v * 7;
frequency = static_cast<uint16_t>(r[0] | (r[1] << 8));
pulse_width = static_cast<uint16_t>((r[2] | (r[3] << 8)) & 0x0FFF);
control = r[4];
attack = r[5] >> 4;
decay = r[5] & 0x0F;
sustain = r[6] >> 4;
release = r[6] & 0x0F;
envelope = f.envelope[v];
}
bool gate() const { return (control & 0x01) != 0; }
};
// Two cycles of the voice's waveform, as tall as its envelope, so pulse width and the ADSR read as
// shapes rather than numbers - turbo8bit's sketch, noise included.
std::vector<int> voice_shape(const voice_view& v, const recti r, const int columns)
{
std::vector<int> ys(static_cast<size_t>(std::max(columns, 1)));
const auto mid = (r.top + r.bottom) / 2;
const auto amplitude = (v.envelope / 255.0) * (r.height() / 2.0 - 3.0);
const auto duty = v.pulse_width / 4096.0;
for (auto x = 0_z; x < ys.size(); ++x)
{
const auto phase = std::fmod(static_cast<double>(x) / static_cast<double>(ys.size()) * 2.0, 1.0);
double level = 0.0;
if (v.control & 0x80)
{
// Any fixed scatter will do: it is the texture of noise, not a sample of it.
const auto h = (static_cast<uint32_t>(x) + 1u) * 2654435761u;
level = static_cast<double>((h >> 8) & 0xFFFF) / 32767.5 - 1.0;
}
else if (v.control & 0x40) level = phase < duty ? 1.0 : -1.0;
else if (v.control & 0x20) level = phase * 2.0 - 1.0;
else if (v.control & 0x10) level = phase < 0.5 ? phase * 4.0 - 1.0 : 3.0 - phase * 4.0;
ys[x] = mid - core::round(level * amplitude);
}
return ys;
}
// The output scope, triggered on a rising zero crossing so a steady note stands still.
std::vector<int> scope_trace(const int16_t* samples, const size_t count, const recti r, const int columns)
{
std::vector<int> ys(static_cast<size_t>(std::max(columns, 1)));
const auto mid = (r.top + r.bottom) / 2;
size_t start = 0;
for (auto i = 1_z; i < count / 2; ++i)
{
if (samples[i - 1] < 0 && samples[i] >= 0)
{
start = i;
break;
}
}
const auto visible = std::min(scope_visible, count - start);
const auto half = r.height() / 2.0 - 4.0;
for (auto x = 0_z; x < ys.size(); ++x)
{
double level = 0.0;
if (visible > 0)
{
const auto at = start + std::min(visible - 1, x * visible / ys.size());
level = std::clamp(samples[at] / 32768.0 * 2.5, -1.0, 1.0);
}
ys[x] = mid - core::round(level * half);
}
return ys;
}
std::string hex2(const int v)
{
return std::format("{:02X}", v & 0xFF);
}
}
sid_monitor_layout layout_sid_monitor(const recti bounds, const double scale, const int songs,
render::measure_context& source, const int detail, const int button_text,
const bool controls)
{
music_measure_context mc(source, scale);
const auto line_height = source.text_line_height(render::font_face::code);
const auto px = [scale](const double v)
{
return std::max(1, core::round(v * scale));
};
const auto lh = std::max(line_height, px(10));
// The stack's own inset within a panel, and its gap between them, which stays the column's whatever scale
// the monitor is drawn at.
const auto pad = px(ui::panel_element::content_padding);
const auto gap = core::round(ui::panel_element::spacing * source.scale_factor);
const auto flex_gap = static_cast<int>(std::round(ui::panel_element::spacing * source.scale_factor / scale));
sid_monitor_layout l;
l.compact = detail <= 0;
l.controls = controls;
// The monitor's panels fill the column's width, as every panel of the stack does.
const auto width = bounds.width();
l.panel = recti(bounds.left, bounds.top, bounds.right, bounds.top);
const auto x0 = l.panel.left;
const auto x1 = l.panel.right;
const auto y = l.panel.top;
auto root = std::make_shared<view_elements>();
root->flex_container.direction = flex_direction::column;
root->flex_container.wrap = flex_wrap::no_wrap;
root->flex_container.align_items = flex_align::stretch;
root->flex_container.padding = {0, 0};
root->flex_container.gap = {flex_gap, flex_gap};
const auto add_panel = [&](const sizei extent)
{
auto panel = std::make_shared<music_panel_element>(extent, true, flex_item::stretch);
root->add(panel);
return panel;
};
view_element_ptr tunes;
view_element_ptr output;
view_element_ptr filter;
view_element_ptr registers;
// The file's other tunes, in a panel of their own above the voices. The panel the monitor sits on names
// the file and the tune, so the monitor carries no heading of its own.
if (songs > 1 && controls)
{
const auto chip_w = std::max(px(30), lh * 2);
const auto chip_h = lh + px(6);
const auto chip_gap = px(4);
const auto label_w = px(72);
const auto top = y + pad;
const auto chips_left = x0 + pad + label_w + chip_gap;
const auto per_row = std::max(1, (x1 - pad - chips_left + chip_gap) / (chip_w + chip_gap));
const auto rows = (songs + per_row - 1) / per_row;
l.tunes_label = recti(x0 + pad, top, x0 + pad + label_w, top + chip_h);
if (rows <= 3)
{
for (int i = 0; i < songs; ++i)
{
const auto left = chips_left + (i % per_row) * (chip_w + chip_gap);
const auto chip_top = top + (i / per_row) * (chip_h + chip_gap);
l.tune_chips.emplace_back(left, chip_top, left + chip_w, chip_top + chip_h);
}
l.tunes = recti(x0, y, x1, top + rows * chip_h + (rows - 1) * chip_gap + pad);
}
else
{
// Too many to show at once: step through them instead.
l.previous_tune = recti(chips_left, top, chips_left + chip_w, top + chip_h);
l.tune_counter = recti(l.previous_tune.right + chip_gap, top, l.previous_tune.right + chip_gap + px(96),
top + chip_h);
l.next_tune = recti(l.tune_counter.right + chip_gap, top, l.tune_counter.right + chip_gap + chip_w,
top + chip_h);
l.tunes = recti(x0, y, x1, top + chip_h + pad);
}
tunes = add_panel(l.tunes.extent());
}
// The voices sit side by side when there is room, as turbo8bit's grid does, and stack when not. Mute and
// solo sit beside the gate and the note when they fit, and below them when not.
const auto inner = x1 - x0;
const auto min_voice_w = px(l.compact ? 150 : 190);
const auto columns = std::clamp((inner + gap) / (min_voice_w + gap), 1, 3);
const auto voice_w = (inner - (columns - 1) * gap) / columns;
const auto button_h = lh + px(4);
const auto button_w = std::max(px(40), button_text + px(12));
// The gate badge and a note such as C#-1, in the code font: about four line heights.
const auto gate_and_note = lh * 4 + px(20);
l.buttons_wrap = controls && gate_and_note + button_w * 2 + px(4) > voice_w - pad * 2;
const auto meter_rows = l.compact ? 2 : 3;
const auto voice_h = pad * 2 + (lh + px(9)) + (px(36) + px(8)) + meter_rows * (lh + px(6)) + button_h +
(l.buttons_wrap ? button_h + px(6) : 0);
auto voices = std::make_shared<view_elements>(flex_item::stretch);
voices->padding(0);
voices->margin(0);
voices->flex_container.gap = {flex_gap, flex_gap};
voices->flex_container.align_items = flex_align::stretch;
std::array<view_element_ptr, 3> voice_panels;
for (auto& voice : voice_panels)
{
flex_item_layout item;
item.grow = 1;
item.basis = l.compact ? 150 : 190;
voice = std::make_shared<music_panel_element>(sizei{min_voice_w, voice_h}, true, item);
voices->add(voice);
}
root->add(voices);
// The output, the filter and the register grid follow, each as wide as the column. Where the column is wide
// enough for two of them side by side at full size, as a landscape frame is, the output stands over the filter
// beside the register grid, which takes their height - or, without the grid, the two share a row. Drawn smaller
// to fit a short space, the monitor keeps the arrangement it has at full size. The flex tree places them, so
// only their heights are set here.
const auto wide = inner >= core::round(560 * source.scale_factor) * 2 + gap;
const auto beside_registers = wide && detail >= 4;
const auto paired = wide && detail == 3;
flex_item_layout half;
half.grow = 1;
half.basis = 560;
const auto add_group = [&](view_elements& into, const flex_direction direction, const flex_item_layout& item)
{
auto group = std::make_shared<view_elements>(item);
group->padding(0);
group->margin(0);
group->flex_container.direction = direction;
group->flex_container.wrap = flex_wrap::no_wrap;
group->flex_container.gap = {flex_gap, flex_gap};
group->flex_container.align_items = flex_align::stretch;
into.add(group);
return group;
};
const auto row = beside_registers || paired ? add_group(*root, flex_direction::row, flex_item::stretch) : nullptr;
const auto stack = beside_registers ? add_group(*row, flex_direction::column, half) : nullptr;
// A part across the column, or half of the row, or the stack's width.
const auto add_part = [&](const int height, const std::shared_ptr<view_elements>& into)
{
if (!into) return add_panel({inner, height});
const auto is_half = into == row;
auto panel = std::make_shared<music_panel_element>(sizei{is_half ? px(560) : inner, height}, true,
is_half ? half : flex_item::stretch);
into->add(panel);
return panel;
};
const auto upper = stack ? stack : row;
if (detail >= 2) output = add_part(pad * 2 + lh + px(8) + px(90), upper);
// The cutoff and resonance meters share the row with the six mode and routing badges where they fit,
// as on turbo8bit; a narrow column puts the badges on a row of their own.
l.filter_wraps = inner < px(560);
if (detail >= 3)
{
const auto badge_row_h = l.filter_wraps ? lh + px(8) + px(10) : 0;
filter = add_part(pad * 2 + lh + px(8) + (lh * 2 + px(18)) + badge_row_h + px(12) + px(24), upper);
}
if (detail >= 4)
{
const auto cell_h = lh * 2 + px(8);
registers = add_part(pad * 2 + lh + px(8) + register_rows * cell_h + (register_rows - 1) * px(4),
beside_registers ? row : nullptr);
}
l.height = root->measure(mc, width).cy;
l.panel.bottom = l.panel.top + l.height;
root->layout(mc, l.panel);
l.panels = root;
if (tunes)
{
const auto delta = tunes->bounds.top_left() - l.tunes.top_left();
l.tunes = tunes->bounds;
l.tunes_label = l.tunes_label.offset(delta);
for (auto& chip : l.tune_chips) chip = chip.offset(delta);
l.previous_tune = l.previous_tune.is_empty() ? recti{} : l.previous_tune.offset(delta);
l.next_tune = l.next_tune.is_empty() ? recti{} : l.next_tune.offset(delta);
l.tune_counter = l.tune_counter.is_empty() ? recti{} : l.tune_counter.offset(delta);
}
for (auto v = 0; v < 3; ++v)
{
const auto panel = voice_panels[v]->bounds;
l.voices[v] = panel;
if (!controls) continue;
if (l.buttons_wrap)
{
// A row of their own, shared between the two.
const auto half_w = (panel.width() - pad * 2 - px(4)) / 2;
l.mute[v] = recti(panel.left + pad, panel.bottom - pad - button_h, panel.left + pad + half_w,
panel.bottom - pad);
l.solo[v] = recti(panel.right - pad - half_w, l.mute[v].top, panel.right - pad, l.mute[v].bottom);
}
else
{
l.solo[v] = recti(panel.right - pad - button_w, panel.bottom - pad - button_h, panel.right - pad,
panel.bottom - pad);
l.mute[v] = l.solo[v].offset(-(button_w + px(4)), 0);
}
}
l.output = output ? output->bounds : recti{};
l.filter = filter ? filter->bounds : recti{};
l.registers = registers ? registers->bounds : recti{};
return l;
}
sid_monitor_layout fit_sid_monitor(const recti bounds, const double scale, const int songs, render::measure_context& mc,
const int button_text, double& used_scale, const bool controls)
{
const auto available = bounds.height();
const auto lay_out = [&](const recti at, const double s, const int detail)
{
return layout_sid_monitor(at, s, songs, mc, detail, button_text, controls);
};
// Smaller first, down to three quarters: text keeps its size, so the geometry cannot shrink much
// further before the labels crowd. Then fewer parts, the register grid first and the ADSR row last.
const auto smallest = scale * 0.75;
auto detail = 0;
used_scale = smallest;
for (int d = 4; d >= 0; --d)
{
if (lay_out(bounds, scale, d).height <= available)
{
detail = d;
used_scale = scale;
break;
}
if (lay_out(bounds, smallest, d).height <= available)
{
// Searched for, because the text keeps its size: the height is not in proportion to the scale,
// so a step in proportion to the shortfall still overflowed, and gave up a part - leaving a gap
// its size - wherever the space was a little short of it.
auto fits = smallest;
auto overflows = scale;
for (int i = 0; i < 10; ++i)
{
const auto mid = (fits + overflows) / 2.0;
if (lay_out(bounds, mid, d).height <= available) fits = mid;
else overflows = mid;
}
detail = d;
used_scale = fits;
break;
}
}
// At the top of the height it is given, which is more than it takes whenever a part or a step of scale did not
// fit: the monitor reports what it took, and the stack it stands in closes up behind it. One still too tall is
// cut off at its foot.
return lay_out(bounds, used_scale, detail);
}
std::array<float, sid_register_count> sid_register_activity(const sid_frame_state* frames, const size_t count)
{
std::array<float, sid_register_count> result = {};
if (count < 2) return result;
// A register glows when the tune gave it a new value, and the glow fades over the frames after.
for (auto f = 1_z; f < count; ++f)
{
const auto age = static_cast<float>(count - 1 - f);
const auto strength = std::max(0.0f, 1.0f - age / static_cast<float>(activity_frames));
for (int r = 0; r < sid_register_count; ++r)
{
if (frames[f].regs[r] != frames[f - 1].regs[r])
{
result[r] = std::max(result[r], strength);
}
}
}
return result;
}
sid_monitor_control::sid_monitor_control(view_state& state, display_state_ptr display) :
sid_monitor_control(std::move(display))
{
_state = &state;
}
sid_monitor_control::sid_monitor_control(display_state_ptr display) :
view_element(view_element_style::can_invoke | flex_item::media), _display(std::move(display))
{
// Its panels meet the column's edges, as the selection panel below them does.
padding(0);
margin(0);
}
void sid_monitor_control::visit_children(const std::function<void(const view_element_ptr&)>& visit) const
{
if (_panels) visit(_panels);
}
int sid_monitor_control::song_count() const
{
// The render knows once the player has opened the file; until then the index's track property, which
// the scan sets to the starting tune out of the tunes the file holds, is the same answer.
if (_display->_sid)
{
const auto songs = _display->_sid->details().songs;
if (songs > 1) return songs;
}
if (_display->_item1)
{
if (const auto md = _display->_item1->metadata())
{
if (md->track.y > 1) return md->track.y;
}
}
return 1;
}
sizei sid_monitor_control::measure(render::measure_context& mc, const int width_limit) const
{
const auto songs = song_count();
_measured_songs = songs;
const auto button_text = std::max(
mc.measure_text(tt.command_volume0, render::font_face::code, render::text_style::single_line, 10000).cx,
mc.measure_text(tt.sid_solo, render::font_face::code, render::text_style::single_line, 10000).cx);
const auto l = layout_sid_monitor(recti(0, 0, width_limit, 0), mc.scale_factor, songs, mc, 4, button_text,
_state != nullptr);
return {width_limit, l.height};
}
void sid_monitor_control::update_layout(render::measure_context& mc, const bool force) const
{
const auto line_height = mc.text_line_height(render::font_face::code);
const auto button_text = std::max(
mc.measure_text(tt.command_volume0, render::font_face::code, render::text_style::single_line, 10000).cx,
mc.measure_text(tt.sid_solo, render::font_face::code, render::text_style::single_line, 10000).cx);
const auto songs = song_count();
if (!force && _panels && _layout_bounds == bounds && _layout_scale == mc.scale_factor &&
_layout_line_height == line_height && _layout_button_width == button_text && _layout_songs == songs)
return;
_drawn = fit_sid_monitor(bounds, mc.scale_factor, songs, mc, button_text, _used_scale, _state != nullptr);
_panels = _drawn.panels;
_layout_bounds = bounds;
_layout_scale = mc.scale_factor;
_layout_line_height = line_height;
_layout_button_width = button_text;
_layout_songs = songs;
}
void sid_monitor_control::layout(render::measure_context& mc, const recti r)
{
view_element::layout(mc, r);
update_layout(mc, true);
// The panels take what they need of the height given, and the stack closes up behind them rather than
// leaving the rest as a gap between them and the panel below.
if (_drawn.height > 0 && _drawn.height < bounds.height())
{
bounds.bottom = bounds.top + _drawn.height;
_layout_bounds = bounds;
}
}
void sid_monitor_control::dispatch_event(const view_element_event& event)
{
if (event.type == view_element_event_type::invoke && _state)
{
_state->play(event.host);
}
}
view_controller_ptr sid_monitor_control::controller_from_location(const view_host_ptr& host, const pointi loc,
const pointi element_offset, hit_test_context& ctx)
{
if (!_state) return view_element::controller_from_location(host, loc, element_offset, ctx);
if (_panels)
{
if (auto controller = _panels->controller_from_location(host, loc, element_offset, ctx)) return controller;
}
const auto& l = _drawn;
const auto at = [&](const recti r)
{
return !r.is_empty() && r.offset(element_offset).contains(loc);
};
const auto click = [&](const recti r, std::function<void()> f)
{
return std::make_shared<sid_click_controller>(host, r.offset(element_offset), std::move(f));
};
for (auto i = 0_z; i < l.tune_chips.size(); ++i)
{
if (at(l.tune_chips[i]))
{
return click(l.tune_chips[i],
[state = _state, song = static_cast<int>(i) + 1] { state->select_sid_song(song); });
}
}
if (_display->_sid && (at(l.previous_tune) || at(l.next_tune)))
{
const auto details = _display->_sid->details();
const auto forward = at(l.next_tune);
const auto song = std::clamp(details.song + (forward ? 1 : -1), 1, details.songs);
return click(forward ? l.next_tune : l.previous_tune, [state = _state, song] { state->select_sid_song(song); });
}
for (int v = 0; v < 3; ++v)
{
// Acted on at release, and the panel can be rebuilt while the button is held - the tune ends, the
// folder refreshes - destroying this monitor. So the action holds nothing of the monitor: only the
// state, and the display it shows, weakly.
const auto toggle = [state = _state, weak_display = std::weak_ptr(_display), v, host](const bool solo)
{
const auto display = weak_display.lock();
if (!display || state->display_state() != display) return;
auto mix = display->_sid_mix;
if (solo) mix.solo = mix.solo == v ? -1 : v;
else mix.muted[v] = !mix.muted[v];
state->mix_sid_voices(mix);
host->frame()->invalidate();
};
if (at(l.mute[v])) return click(l.mute[v], [toggle] { toggle(false); });
if (at(l.solo[v])) return click(l.solo[v], [toggle] { toggle(true); });
}
return view_element::controller_from_location(host, loc, element_offset, ctx);
}
std::vector<recti> sid_monitor_control::keyboard_parts() const
{
if (!_state) return {};
const auto& l = _drawn;
std::vector<recti> parts;
const auto add = [&parts](const recti r)
{
if (!r.is_empty()) parts.emplace_back(r);
};
for (const auto& chip : l.tune_chips) add(chip);
if (_display->_sid)
{
add(l.previous_tune);
add(l.next_tune);
}
for (int v = 0; v < 3; ++v)
{
add(l.mute[v]);
add(l.solo[v]);
}
return parts;
}
void sid_monitor_control::render(render::draw_context& dc, const pointi element_offset) const
{
// Advances the session's clock, as the audio control does, so the monitor shows what is heard.
_display->update_for_present(dc.time_now);
const auto line_height = dc.text_line_height(render::font_face::code);
const auto logical = bounds.offset(element_offset);
_display->_sid_monitor_bounds = logical;
update_layout(dc);
const auto scale = _used_scale;
auto l = _drawn;
// A file whose tune count arrived after the layout was measured needs room for the tune selector.
if (song_count() != _measured_songs)
{
_measured_songs = song_count();
if (_state) _state->invalidate_view(view_invalid::view_layout);
}
for (auto* r : {&l.panel, &l.tunes, &l.tunes_label, &l.previous_tune, &l.next_tune, &l.tune_counter, &l.output,
&l.filter, &l.registers})
*r = r->is_empty() ? recti{} : r->offset(element_offset);
for (auto& r : l.tune_chips) r = r.offset(element_offset);
for (int v = 0; v < 3; ++v)
{
l.voices[v] = l.voices[v].offset(element_offset);
l.mute[v] = l.mute[v].offset(element_offset);
l.solo[v] = l.solo[v].offset(element_offset);
}
std::vector<queued_text> texts;
const painter p{dc, dc.colors.alpha, scale, texts};
// The last resort when even the voices do not fit: what is left over is cut off rather than drawn over the
// panel below.
const render::scoped_clip clip(dc, logical);
const auto lh = std::max(line_height, p.px(10));
const auto pad = p.px(ui::panel_element::content_padding);
const auto& sid = _display->_sid;
const auto details = sid ? sid->details() : sid_tune_details{};
const auto& mix = _display->_sid_mix;
const auto position = _display->playback_position(dc.time_now);
sid_frame_state state;
if (sid) sid->frame_at(position, state);
// A part that moves with the music has its badge, track or panel drawn here and is left to the vertex
// buffer, which draws over the backgrounds and under the text.
sid_monitor_motion::parts parts;
parts.thickness = p.px(2);
parts.column = p.px(1);
parts.inset = p.px(1);
// Each part's panel is drawn by the flex tree; the moving parts and the text go over them.
render_children(dc, element_offset);
if (!l.tunes.is_empty())
{
p.text(tt.prop_name_track, l.tunes_label, palette::text_muted());
for (auto i = 0_z; i < l.tune_chips.size(); ++i)
{
const auto current = static_cast<int>(i) + 1 == details.song;
p.badge(std::to_string(i + 1), l.tune_chips[i], current ? p.c(palette::accent()) : p.c(palette::raised),
current ? palette::text() : palette::text_muted());
}
if (!l.tune_counter.is_empty())
{
p.badge("<", l.previous_tune, p.c(palette::raised), palette::text_muted());
p.badge(">", l.next_tune, p.c(palette::raised), palette::text_muted());
p.text(std::format("{} / {}", details.song, details.songs), l.tune_counter, palette::text(), 1.0f,
render::text_style::single_line_center);
}
}
// The voices.
for (int v = 0; v < 3; ++v)
{
const auto& r = l.voices[v];
auto& part = parts.voices[v];
const voice_view voice(state, v);
const auto a = mix.is_silent(v) ? silenced_alpha : 1.0f;
const auto x0 = r.left + pad;
const auto x1 = r.right - pad;
auto y = r.top + pad;
const auto wave = sid_waveform_name(voice.control);
const auto wave_w = p.text_width(wave) + p.px(12);
p.text(str_format(tt.sid_voice_fmt.sv(), v + 1), recti(x0, y, x1 - wave_w, y + lh), palette::text(), a);
p.badge(wave, recti(x1 - wave_w, y - p.px(2), x1, y + lh + p.px(2)), p.c(palette::information(), a),
palette::text(), a);
y += lh + p.px(6);
p.dc.draw_rect(recti(x0, y, x1, y + 1), p.c(palette::line, a));
y += p.px(3);
part.shape = recti(x0, y, x1, y + p.px(36));
y = part.shape.bottom + p.px(8);
const auto label_w = p.px(40);
const auto value_w = p.px(50);
const auto meter_row = [&](const std::string_view label, const int value)
{
p.text(label, recti(x0, y, x0 + label_w, y + lh), palette::text_muted(), a);
const auto bar_h = p.px(12);
const auto bar_top = y + (lh - bar_h) / 2;
const recti track(x0 + label_w, bar_top, x1 - value_w, bar_top + bar_h);
p.fill(track, p.c(palette::raised, a), p.px(2));
p.text(std::to_string(value), recti(x1 - value_w, y, x1, y + lh), palette::text(), a,
render::text_style::single_line_far);
y += lh + p.px(6);
return track;
};
part.frequency = meter_row("FREQ", voice.frequency);
part.envelope = meter_row("ENV", voice.envelope);
// A:1 D:6 S:2 R:10, the letters muted.
if (!l.compact)
{
auto x = x0;
const std::pair<std::string_view, int> adsr[] = {{"A:", voice.attack},
{"D:", voice.decay},
{"S:", voice.sustain},
{"R:", voice.release}};
for (const auto& [name, value] : adsr)
{
const auto number = std::to_string(value);
const auto name_w = p.text_width(name);
const auto number_w = p.text_width(number);
p.text(name, recti(x, y, x + name_w, y + lh), palette::text_muted(), a);
p.text(number, recti(x + name_w, y, x + name_w + number_w, y + lh), palette::text(), a);
x += name_w + number_w + p.px(12);
}
y += lh + p.px(6);
}
// The gate, the note it plays, and the listening controls - beside them, or on the row below.
{
const auto inline_buttons = l.controls && !l.buttons_wrap;
const auto row_top = inline_buttons ? l.mute[v].top : y;
const auto row_bottom = row_top + lh + p.px(4);
const auto note_right = inline_buttons ? l.mute[v].left : x1;
const auto gate_w = p.text_width("GATE") + p.px(10);
part.gate = recti(x0, row_top, x0 + gate_w, row_bottom);
p.badge("GATE", part.gate, p.c(palette::raised, a), palette::text(), a);
const auto note = sid_note_name(voice.frequency, details.clock_hz);
p.text(note.empty() ? std::string("-") : note,
recti(part.gate.right + p.px(8), row_top, note_right, row_bottom), palette::text(), a);
if (l.controls)
{
p.badge(tt.command_volume0, l.mute[v], mix.muted[v] ? p.c(palette::red()) : p.c(palette::raised),
mix.muted[v] ? palette::text() : palette::text_muted());
p.badge(tt.sid_solo, l.solo[v], mix.solo == v ? p.c(palette::orange()) : p.c(palette::raised),
mix.solo == v ? palette::text() : palette::text_muted());
}
}
}
// What the chip puts out.
if (!l.output.is_empty())
{
const auto& r = l.output;
p.text(tt.sid_output, recti(r.left, r.top + pad, r.right, r.top + pad + lh), palette::text_muted(), 1.0f,
render::text_style::single_line_center);
parts.scope = recti(r.left + pad, r.top + pad + lh + p.px(8), r.right - pad, r.bottom - pad);
const auto mid = (parts.scope.top + parts.scope.bottom) / 2;
p.dc.draw_rect(recti(parts.scope.left, mid, parts.scope.right, mid + 1), p.c(palette::line));
}
// The filter and the volume.
if (!l.filter.is_empty())
{
const auto& r = l.filter;
const auto& regs = state.regs;
const auto cutoff = ((regs[0x16] << 3) | (regs[0x15] & 0x07)) & 0x7FF;
const auto resonance = regs[0x17] >> 4;
auto y = r.top + pad;
p.text(tt.sid_filter, recti(r.left, y, r.right, y + lh), palette::text_muted(), 1.0f,
render::text_style::single_line_center);
y += lh + p.px(8);
// CUTOFF and RES first, then the filter's modes and the voices it filters, wrapping below the meters
// when the panel is too narrow for one row.
const auto x0 = r.left + pad;
const auto x1 = r.right - pad;
const auto column_gap = p.px(24);
const auto badge_gap = p.px(6);
const auto badge_w = std::max(p.px(30), p.text_width("V3") + p.px(14));
const auto badges_w = badge_w * 6 + badge_gap * 4 + column_gap;
const auto bars_w = l.filter_wraps ? (x1 - x0 - column_gap) / 2
: std::max(p.px(60), (x1 - x0 - badges_w - column_gap * 2) / 2);
const auto parameter = [&](const int left, const std::string_view label, const int value)
{
const auto right = left + bars_w;
p.text(label, recti(left, y, right, y + lh), palette::text_muted());
const recti track(left, y + lh + p.px(4), right, y + lh + p.px(14));
p.fill(track, p.c(palette::raised), p.px(2));
p.text(std::to_string(value), recti(left, y + lh + p.px(18), right, y + lh * 2 + p.px(18)),
palette::text());
return track;
};
parts.cutoff = parameter(x0, "CUTOFF", cutoff);
parts.resonance = parameter(x0 + bars_w + column_gap, "RES", resonance);
auto x = l.filter_wraps ? x0 : x0 + (bars_w + column_gap) * 2;
const auto badge_top = l.filter_wraps ? y + lh * 2 + p.px(18) + p.px(10) : y + p.px(8);
constexpr std::string_view lamp_names[] = {"LP", "BP", "HP", "V1", "V2", "V3"};
for (auto i = 0_z; i < std::size(lamp_names); ++i)
{
// The modes, then the voices routed through the filter.
if (i == 3) x += column_gap - badge_gap;
parts.filter_lamps[i] = recti(x, badge_top, x + badge_w, badge_top + lh + p.px(8));
p.badge(lamp_names[i], parts.filter_lamps[i], p.c(palette::raised), palette::text());
x += badge_w + badge_gap;
}
y += lh * 2 + p.px(18) + p.px(6);
if (l.filter_wraps) y += lh + p.px(8) + p.px(10);
p.dc.draw_rect(recti(x0, y, x1, y + 1), p.c(palette::line));
y += p.px(6);
// Fifteen steps, lit green, then orange, then red.
const auto label_w = std::max(p.px(80), p.text_width(tt.sid_volume) + p.px(12));
p.text(tt.sid_volume, recti(x0, y, x0 + label_w, y + p.px(24)), palette::text_muted());
const auto bars_left = x0 + label_w;
const auto step_gap = p.px(3);
const auto step_w = std::max(1, (x1 - bars_left - step_gap * 14) / 15);
for (auto i = 0_z; i < parts.volume.size(); ++i)
{
const auto left = bars_left + static_cast<int>(i) * (step_w + step_gap);
parts.volume[i] = recti(left, y + p.px(2), left + step_w, y + p.px(22));
p.fill(parts.volume[i], p.c(palette::raised), p.px(2));
}
}
// The registers the tune writes, each highlighted briefly when it changes.
if (!l.registers.is_empty())
{
const auto& r = l.registers;
auto y = r.top + pad;
p.text(tt.sid_register_writes, recti(r.left, y, r.right, y + lh), palette::text_muted(), 1.0f,
render::text_style::single_line_center);
y += lh + p.px(8);
const auto x0 = r.left + pad;
const auto x1 = r.right - pad;
const auto cell_gap = p.px(4);
const auto cell_w = (x1 - x0 - cell_gap * (register_columns - 1)) / register_columns;
// Taller than they need where the grid stands beside the output and the filter, and takes their height.
const auto cell_h = std::max(lh * 2 + p.px(8),
(r.bottom - pad - y - cell_gap * (register_rows - 1)) / register_rows);
for (int i = 0; i < sid_register_count; ++i)