// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI /// The Mix Tightener's hand-built editor (Android's MixTightenerEditor): four /// vertical faders, bottom = off, top = full. /// - Strength: the master amount of the EQ → compressor → limiter chain (a parameter). /// - EQ / Comp / Limit: per-stage trims (`eqAmt`, `compAmt`, `limAmt`). They aren't /// ParamSpecs, so they go through the data source's hidden values (song slot or AU /// state); each stage's amount in the DSP is Strength × its trim. A missing trim /// reads as 1, like MixTightener.cpp. @MainActor enum MixTightenerBespoke { static func body(_ model: DeviceModel) -> AnyView? { AnyView(MixTightenerEditorView(model: model)) } static let trims: Set = ["eqAmt", "compAmt", "limAmt"] /// A trim's value (0..1). AUDataSource doesn't forward hidden values yet, so the /// AU is asked directly. static func trim(_ model: DeviceModel, _ key: String) -> Float { let text: String? if let au = (model.data as? AUDataSource)?.au { text = au.hiddenValue(forKey: key) } else { text = model.data?.hiddenValue(key) } return Swift.min(1, Swift.max(0, text.flatMap { Float($0.trimmingCharacters(in: .whitespaces)) } ?? 1)) } static func setTrim(_ model: DeviceModel, _ key: String, _ v: Float) { let text = String(Swift.min(1, Swift.max(0, v))) if let au = (model.data as? AUDataSource)?.au { au.setHiddenValue(text, forKey: key) } else { model.data?.setHiddenValue(text, key: key) } } } private struct MixTightenerEditorView: View { @ObservedObject var model: DeviceModel /// Hidden values aren't published by the model: bumped after a trim write so the /// faders re-read them (presets and parameter changes re-render via the model). @State private var trimRevision = 0 var body: some View { VStack(spacing: 10) { Text("Preset-driven EQ → 3-band compressor → limiter. Pick a preset, then set the master Strength and trim each stage:") .font(Theme.mono(11)).foregroundColor(Theme.dim).multilineTextAlignment(.center) .fixedSize(horizontal: false, vertical: true) HStack(spacing: 8) { fader("strength", "STRENGTH") fader("eqAmt", "EQ") fader("compAmt", "COMP") fader("limAmt", "LIMIT") } .frame(height: 268) Text("Bottom = off · Top = full. Each stage = Strength × its fader.") .font(Theme.mono(10)).foregroundColor(Theme.dim).multilineTextAlignment(.center) .fixedSize(horizontal: false, vertical: true) } .padding(.top, 4) .frame(maxWidth: .infinity) } private func fader(_ key: String, _ label: String) -> some View { let hidden = MixTightenerBespoke.trims.contains(key) let value = hidden ? MixTightenerBespoke.trim(model, key) : Swift.min(1, Swift.max(0, model.value(key))) return MixTightenerFader(label: label, value: value) { v in if hidden { MixTightenerBespoke.setTrim(model, key, v); trimRevision &+= 1 } else { model.set(key, v) } } } } /// One labelled vertical fader: tap or drag to set (absolute position). private struct MixTightenerFader: View { let label: String let value: Float let onChange: (Float) -> Void var body: some View { VStack(spacing: 4) { Text(label).font(Theme.mono(11)).foregroundColor(Theme.accent).lineLimit(1).minimumScaleFactor(0.7) Text("\(Int((value * 100).rounded()))%").font(Theme.mono(12)).foregroundColor(Theme.text) GeometryReader { g in let h = g.size.height let fill = h * CGFloat(Swift.max(0.001, value)) ZStack(alignment: .bottom) { Rectangle().fill(Theme.background) Rectangle().fill(Theme.accent.opacity(0.30)).frame(height: fill) Rectangle().fill(Theme.accent).frame(height: 3).offset(y: -Swift.max(0, fill - 3)) } .overlay(Rectangle().stroke(Theme.grid, lineWidth: 1)) .contentShape(Rectangle()) .gesture(DragGesture(minimumDistance: 0).onChanged { d in onChange(Float(1 - Swift.min(1, Swift.max(0, d.location.y / Swift.max(1, h))))) }) } } .frame(maxWidth: .infinity) } }