Skip to content

armgit / nota

Public Imported from GitHub
0

Nota — a free DAW for macOS, Windows and Linux. No subscription, no account — just open it and make music.

16.0 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (c) 2026 Egor Khindikaynen (Nota). See LICENSES/ for license terms.
//
// The Modular Scope node (specs/047): a CV monitor whose trace can be resized by the grip
// under it or popped out into its own window, with a time window (ms, or beats when
// synced), a vertical zoom, a rising-edge trigger and a freeze. The window, sync, zoom,
// trigger and trace height are modulator fields saved with the project; freeze is a view
// state shared by the node and its window. The maths lives in ScopeTrace.

using System;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Threading;
using Nota.Application;
using Nota.Application.Localization;

namespace Nota.App;

public sealed partial class ModularView
{
    // A frozen Scope keeps the history it had when Freeze went on, so the node and its window
    // (rebuilt on every chip change) both draw the held trace.
    private readonly Dictionary<(int Track, int Mod), ScopeSnapshot> _scopeFrozen = new();
    internal sealed record ScopeSnapshot(float[] Values, float[] Dts, int Count);
    private readonly Dictionary<(int Track, int Mod), ScopeWindow> _scopeWindows = new();
    private bool _rebuildQueued;

    internal bool ScopeFrozen(int trackId, int modId) => _scopeFrozen.ContainsKey((trackId, modId));
    internal ScopeSnapshot? ScopeHeld(int trackId, int modId) => _scopeFrozen.GetValueOrDefault((trackId, modId));

    /// <summary>A new or reopened project reuses track and modulator ids: drop the held traces
    /// and close the Scope windows, which belong to the old project.</summary>
    public void ForgetScopes()
    {
        _scopeFrozen.Clear();
        foreach (var w in new List<ScopeWindow>(_scopeWindows.Values)) w.Close();
        _scopeWindows.Clear();
    }

    private void SetScopeFrozen(int trackId, int modId, bool on)
    {
        if (!on) { _scopeFrozen.Remove((trackId, modId)); return; }
        var v = new float[ScopeTrace.HistoryLength]; var dt = new float[ScopeTrace.HistoryLength];
        int n = _engine.ModulatorScopeTimed(trackId, modId, v, dt);
        _scopeFrozen[(trackId, modId)] = new ScopeSnapshot(v, dt, Math.Max(0, n));
    }

    // Rebuild on the next dispatcher turn: safe from inside a live-follow tick, which
    // Rebuild would otherwise clear while it runs.
    private void QueueRebuild()
    {
        if (_rebuildQueued) return;
        _rebuildQueued = true;
        Dispatcher.UIThread.Post(() => { _rebuildQueued = false; Rebuild(); });
    }

    // Scope: a CV monitor — one CV input (patch a cable in) drives an oscilloscope; the
    // incoming CV also passes straight through to CV-out.
    private void BuildScopeView(GNode n, int modId)
    {
        var col = new StackPanel();
        var headBorder = NodeHeader(n);
        col.Children.Add(headBorder);
        col.Children.Add(PortsBlock(n));

        int t = _trackId; var e = _engine;
        var viz = new ScopeViz(e, t, modId, () => ScopeHeld(t, modId));
        _liveKnobs.Add(viz.Update);
        // Unwired, the scope says what it can watch: a modulator's CV, patched into its input. It
        // doesn't tap audio — a first-time user tried to hang it off a filter's output (#261). The hint
        // stands in for the trace, which would only draw a flat line through it.
        Control body = viz;
        if ((int)Math.Round(e.ModulatorGet(t, modId, 10)) < 0)
        {
            body = new TextBlock
            {
                Text = Loc.T("modular.scope.watches_cv_drag_a_modulators"),
                FontSize = 10, Foreground = Text3, TextWrapping = TextWrapping.Wrap, TextAlignment = TextAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(8, 0),
            };
        }
        double h0 = Math.Clamp(e.ModulatorGet(t, modId, ScopeTrace.FieldHeight), ScopeTrace.MinHeight, ScopeTrace.MaxHeight);
        var vizBox = new Border { Height = h0, Margin = new Thickness(10, 0, 10, 0), Child = body };
        col.Children.Add(new Border
        {
            Child = new StackPanel { Children = { vizBox, ScopeGrip(vizBox, t, modId) } },
            BorderBrush = Divider, BorderThickness = new Thickness(0, 1, 0, 0), Padding = new Thickness(0, 8, 0, 0),
        });
        col.Children.Add(ScopeControls(t, modId, _liveKnobs.Add, QueueRebuild));
        WrapFrame(n, col, headBorder);
    }

    // The bar under the trace: drag it to make the trace taller or shorter (76–320 px).
    private Control ScopeGrip(Border box, int t, int modId)
    {
        var line = new Border
        {
            Width = 28, Height = 2, CornerRadius = NotaRadius.Cell, Background = NotaPalette.Seam,
            HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center,
        };
        var grip = new Border { Height = 9, Background = Brushes.Transparent, Cursor = new Cursor(StandardCursorType.SizeNorthSouth), Child = line };
        ToolTip.SetTip(grip, Loc.T("modular.scope.drag_to_resize_the_trace"));
        bool drag = false; double y0 = 0, h0 = 0;
        grip.PointerEntered += (_, _) => line.Background = Text3;
        grip.PointerExited += (_, _) => { if (!drag) line.Background = NotaPalette.Seam; };
        grip.PointerPressed += (_, ev) =>
        {
            drag = true; y0 = ev.GetPosition(this).Y; h0 = box.Height;
            ev.Pointer.Capture(grip); ev.Handled = true;
        };
        grip.PointerMoved += (_, ev) =>
        {
            if (!drag) return;
            double dy = (ev.GetPosition(this).Y - y0) / Math.Max(0.05, _scale);   // screen px → node px
            box.Height = Math.Round(Math.Clamp(h0 + dy, ScopeTrace.MinHeight, ScopeTrace.MaxHeight));
            ev.Handled = true;
        };
        grip.PointerReleased += (_, ev) =>
        {
            if (!drag) return;
            drag = false; ev.Pointer.Capture(null); line.Background = NotaPalette.Seam;
            _engine.ModulatorSet(t, modId, ScopeTrace.FieldHeight, (float)box.Height);
            Changed?.Invoke();
            ev.Handled = true;
        };
        return grip;
    }

    // Window + zoom knobs, then the trigger and freeze chips. Shared by the node and its
    // window: `addTick` registers the live follow, `refresh` rebuilds the host when a
    // chip changes (or the other copy changed one).
    internal Control ScopeControls(int t, int modId, Action<Action> addTick, Action refresh)
    {
        var e = _engine;
        bool sync = e.ModulatorGet(t, modId, ScopeTrace.FieldSync) > 0.5f;
        bool trig = e.ModulatorGet(t, modId, ScopeTrace.FieldTrigger) > 0.5f;
        bool frozen = ScopeFrozen(t, modId);
        int lastBeat = ScopeTrace.BeatSteps.Length - 1;

        // Window: milliseconds on a log knob, or musical steps when synced.
        int wField = sync ? ScopeTrace.FieldWindowBeats : ScopeTrace.FieldWindowMs;
        double ToFrac(float v) => sync ? ScopeTrace.BeatIndex(v) / (double)lastBeat : ScopeTrace.MsToKnob(v);
        float FromFrac(double f) => sync ? ScopeTrace.BeatSteps[(int)Math.Round(Math.Clamp(f, 0, 1) * lastBeat)] : (float)ScopeTrace.KnobToMs(f);
        string Fmt(float v) => sync ? ScopeTrace.BeatsLabel(v) : ScopeTrace.MsLabel(v);
        float w0 = e.ModulatorGet(t, modId, wField);
        var wValue = MonoValue(Fmt(w0));
        // Full travel either way; double-click goes back to 1 s (or 4 beats).
        var wKnob = new Knob(ToFrac(w0))
        {
            Accent = true, Width = 30, Height = 30,
            Default = sync ? ToFrac((float)ScopeTrace.DefaultWindowBeats) : ScopeTrace.MsToKnob(ScopeTrace.DefaultWindowMs),
        };
        wKnob.Format = f => Fmt(FromFrac(f));
        wKnob.ValueChanged += f => { float v = FromFrac(f); e.ModulatorSet(t, modId, wField, v); wValue.Text = Fmt(v); };
        ValueEntry.Attach(wKnob, Loc.T("modular.scope.window"), wValue);
        var syncChip = ToggleLabel(sync ? Loc.T("modular.sync") : "ms", sync ? Loc.T("modular.scope.the_window_is_in_beats")
                                                             : Loc.T("modular.scope.the_window_is_in_milliseconds"),
            () => { e.ModulatorSet(t, modId, ScopeTrace.FieldSync, sync ? 0f : 1f); Changed?.Invoke(); refresh(); });
        var windowCell = new StackPanel
        {
            Spacing = 0, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center,
            Children = { syncChip, wKnob, wValue },
        };

        // Zoom: 1× / 2× / 4× / 8× on a stepped knob.
        int last = ScopeTrace.Zooms.Length - 1;
        int ZoomAt(double f) => ScopeTrace.Zooms[(int)Math.Round(Math.Clamp(f, 0, 1) * last)];
        float z0 = e.ModulatorGet(t, modId, ScopeTrace.FieldZoom);
        var zValue = MonoValue(ScopeTrace.ZoomLabel(ScopeTrace.Zooms[ScopeTrace.ZoomIndex(z0)]));
        var zKnob = new Knob(ScopeTrace.ZoomIndex(z0) / (double)last, 0) { Accent = true };
        zKnob.Format = f => ScopeTrace.ZoomLabel(ZoomAt(f));
        zKnob.ValueChanged += f => { int z = ZoomAt(f); e.ModulatorSet(t, modId, ScopeTrace.FieldZoom, z); zValue.Text = ScopeTrace.ZoomLabel(z); };
        ValueEntry.Attach(zKnob, Loc.T("modular.scope.zoom"), zValue);
        var zoomCell = KnobCell(Loc.T("modular.scope.zoom"), zKnob, zValue);

        addTick(() =>
        {
            // Another copy (node ↔ window) switched a chip: rebuild to match.
            if ((e.ModulatorGet(t, modId, ScopeTrace.FieldSync) > 0.5f) != sync
                || (e.ModulatorGet(t, modId, ScopeTrace.FieldTrigger) > 0.5f) != trig
                || ScopeFrozen(t, modId) != frozen) { refresh(); return; }
            if (!wKnob.Dragging)
            {
                float cur = e.ModulatorGet(t, modId, wField); double f = ToFrac(cur);
                if (Math.Abs(f - wKnob.Value) > 1e-3) { wKnob.Value = f; wValue.Text = Fmt(cur); }
            }
            if (!zKnob.Dragging)
            {
                float cur = e.ModulatorGet(t, modId, ScopeTrace.FieldZoom); double f = ScopeTrace.ZoomIndex(cur) / (double)last;
                if (Math.Abs(f - zKnob.Value) > 1e-3) { zKnob.Value = f; zValue.Text = ScopeTrace.ZoomLabel(ScopeTrace.Zooms[ScopeTrace.ZoomIndex(cur)]); }
            }
        });

        var chips = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 3, Margin = new Thickness(9, 0), VerticalAlignment = VerticalAlignment.Center };
        void SetTrig(bool on) { e.ModulatorSet(t, modId, ScopeTrace.FieldTrigger, on ? 1f : 0f); Changed?.Invoke(); refresh(); }
        var free = SegChip(Loc.T("modular.free"), !trig, () => SetTrig(false));
        ToolTip.SetTip(free, Loc.T("modular.scope.free_running_the_newest_value"));
        var rise = SegChip(Loc.T("modular.scope.rise"), trig, () => SetTrig(true));
        ToolTip.SetTip(rise, Loc.T("modular.scope.trigger_start_the_trace_where"));
        var freeze = SegChip(Loc.T("common.freeze"), frozen, () => { SetScopeFrozen(t, modId, !frozen); refresh(); });
        ToolTip.SetTip(freeze, frozen ? Loc.T("modular.scope.back_to_the_live_trace") : Loc.T("modular.scope.hold_the_trace_while_the"));
        freeze.Margin = new Thickness(8, 0, 0, 0);
        chips.Children.Add(free); chips.Children.Add(rise); chips.Children.Add(freeze);

        return new StackPanel
        {
            Margin = new Thickness(0, 4, 0, 0),
            Children =
            {
                KnobGrid(windowCell, zoomCell),
                new Border { Height = 34, Child = chips, BorderBrush = Divider, BorderThickness = new Thickness(0, 1, 0, 0) },
            },
        };
    }

    // The Scope's two knobs side by side, as a device card's knob cells.
    private static Control KnobCell(string name, Knob knob, TextBlock value)
    {
        knob.Width = Knob.SizeSecondary; knob.Height = Knob.SizeSecondary;
        var cell = DeviceCardKit.KnobCell(name, knob, value, 50);
        cell.HorizontalAlignment = HorizontalAlignment.Center;
        cell.VerticalAlignment = VerticalAlignment.Center;
        return cell;
    }

    private static Grid KnobGrid(params Control[] cells)
    {
        var grid = new Grid { ColumnDefinitions = new ColumnDefinitions("*,*"), RowDefinitions = new RowDefinitions("56") };
        for (int i = 0; i < cells.Length; i++)
        {
            var cell = new Border { Child = cells[i], BorderBrush = Divider, BorderThickness = new Thickness(i % 2 == 1 ? 1 : 0, 0, 0, 0) };
            Grid.SetColumn(cell, i % 2);
            grid.Children.Add(cell);
        }
        return grid;
    }

    // Open (or bring forward) the Scope's own window.
    private void OpenScopeWindow(int t, int modId, Control anchor)
    {
        if (_scopeWindows.TryGetValue((t, modId), out var open)) { open.Activate(); return; }
        var w = new ScopeWindow(this, _engine, t, modId, Loc.F("modular.scope.scope", modId, TrackName(t)));
        _scopeWindows[(t, modId)] = w;
        w.Closed += (_, _) => _scopeWindows.Remove((t, modId));
        // After the click has finished (the button opens on release), owned by the window that
        // holds the node, so it stays in front of it.
        var owner = TopLevel.GetTopLevel(anchor) as Window;
        Dispatcher.UIThread.Post(() => { w.ShowOver(owner); w.Activate(); }, DispatcherPriority.Background);
    }

    internal sealed class ScopeViz : Control
    {
        private readonly IAudioEngine _e;
        private readonly int _t, _mod;
        private readonly Func<ScopeSnapshot?> _held;
        private readonly float[] _v = new float[ScopeTrace.HistoryLength], _dt = new float[ScopeTrace.HistoryLength];
        private readonly double[] _xs = new double[ScopeTrace.HistoryLength];
        private readonly float[] _ys = new float[ScopeTrace.HistoryLength];
        private int _n, _zoom = 1;
        private bool _triggered;

        public ScopeViz(IAudioEngine e, int trackId, int modId, Func<ScopeSnapshot?> held)
        {
            _e = e; _t = trackId; _mod = modId; _held = held;
            IsHitTestVisible = false; ClipToBounds = true;
        }

        public void Update()
        {
            _zoom = ScopeTrace.Zooms[ScopeTrace.ZoomIndex(_e.ModulatorGet(_t, _mod, ScopeTrace.FieldZoom))];
            // Frozen: lay out the held history, so window, zoom and trigger still apply to it.
            var held = _held();
            float[] v = held?.Values ?? _v, dt = held?.Dts ?? _dt;
            int m = held?.Count ?? _e.ModulatorScopeTimed(_t, _mod, _v, _dt);
            double win = ScopeTrace.WindowSeconds(_e.ModulatorGet(_t, _mod, ScopeTrace.FieldSync) > 0.5f,
                _e.ModulatorGet(_t, _mod, ScopeTrace.FieldWindowMs), _e.ModulatorGet(_t, _mod, ScopeTrace.FieldWindowBeats), _e.Bpm);
            bool trig = _e.ModulatorGet(_t, _mod, ScopeTrace.FieldTrigger) > 0.5f;
            _n = ScopeTrace.Layout(v.AsSpan(0, m), dt.AsSpan(0, m), win, trig, _xs, _ys, out _triggered);
            InvalidateVisual();
        }

        public override void Render(DrawingContext ctx)
        {
            double w = Bounds.Width, h = Bounds.Height;
            if (w <= 0 || h <= 0) return;
            ctx.DrawRectangle(ScopeBg, null, new Rect(0, 0, w, h), NotaRadius.ValueBarValue, NotaRadius.ValueBarValue);
            double mid = h / 2;
            var grid = new Pen(BorderInner, 1);
            for (int i = 1; i < 4; i++) { double x = Math.Round(w * i / 4) + 0.5; ctx.DrawLine(grid, new Point(x, 2), new Point(x, h - 2)); }
            ctx.DrawLine(grid, new Point(0, mid), new Point(w, mid));
            if (_triggered) ctx.DrawRectangle(Brass, null, new Rect(0, mid - 4, 2, 8));   // the trigger point
            if (_n < 2) return;
            double scale = _zoom / ScopeTrace.Range;
            var geo = new StreamGeometry();
            using (var g = geo.Open())
            {
                for (int i = 0; i < _n; i++)
                {
                    double v = Math.Clamp(_ys[i] * scale, -1, 1);
                    var p = new Point(_xs[i] * w, mid - v * (mid - 2));
                    if (i == 0) g.BeginFigure(p, false); else g.LineTo(p);
                }
            }
            ctx.DrawGeometry(null, new Pen(Brass, 1.5), geo);
        }
    }
}