Files
sizzletracker-android/ios/app/Host/Screens/Mix/MixScreen.swift
T
ReactorcoremeltdownandClaude Opus 5.5 962cfd1e2b iOS 0.4.0: Sizzle's Tracker, Mix, Toolbox and Setup tabs on the C++ engine
Ported from the Android tabs by parallel agents (each screen with its own
SizzleSession category); slot editors use the card editor with a preset
library; songs load/save in the Android project format.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:32:30 +01:00

186 lines
8.0 KiB
Swift

// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
import SwiftUI
/// The second tab (Android's MixerScreen): four channel strips, one per tracker
/// track. Each picks an instrument and up to four effects from the toolbox, sets a
/// MIDI channel, and has a volume fader with a live meter, a limiter and mute/solo.
/// Long-pressing an instrument or effect opens its settings in the Toolbox.
struct MixScreen: View {
@EnvironmentObject var app: AppModel
var body: some View { MixBody(app: app) }
}
/// Watches the song only (not the transport), so playback doesn't redraw the strips;
/// the meters redraw themselves at display rate.
private struct MixBody: View {
let app: AppModel
@ObservedObject private var watch = MixSongWatch.shared
var body: some View {
let session = app.session
let kinds = Dictionary(SizzleSession.deviceTypes.map { ($0.type, $0.isInstrument) }, uniquingKeysWith: { a, _ in a })
var instruments: [Int] = [], effects: [Int] = []
for i in 0..<16 {
let t = session.type(ofSlot: i)
guard !t.isEmpty, let inst = kinds[t] else { continue }
if inst { instruments.append(i) } else { effects.append(i) }
}
let names = (0..<16).map { session.name(ofSlot: $0) }
return GeometryReader { geo in
let landscape = geo.size.width > geo.size.height
HStack(spacing: 6) {
ForEach(0..<4, id: \.self) { ch in
ChannelStrip(ch: ch, landscape: landscape, instruments: instruments, effects: effects, names: names,
open: { ToolboxNav.open($0, app: app, returnTab: .mix) })
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.padding(6)
}
.background(Theme.background)
}
}
private struct ChannelStrip: View {
let ch: Int
let landscape: Bool
let instruments: [Int]
let effects: [Int]
let names: [String]
let open: (Int) -> Void
private var session: SizzleSession { SizzleSession.shared }
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text("CH \(ch + 1)").font(Theme.mono(13)).foregroundColor(Theme.accent)
if landscape {
// Routing left (scrolls if short), levels right: the fader and
// LIM / M / S stay visible in the short height.
HStack(spacing: 6) {
SnapScroll { VStack(alignment: .leading, spacing: 4) { routing } }
.frame(maxWidth: .infinity, maxHeight: .infinity)
VStack(alignment: .leading, spacing: 4) { levels }
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
} else {
routing
levels
}
}
.padding(6)
.background(Theme.surface)
}
private func label(_ i: Int) -> String { i < 0 ? "—" : "\(i + 1):\(names[i])" }
@ViewBuilder private var routing: some View {
let inst = session.instrumentSlot(ofChannel: ch)
MixKit.SectionLabel(text: "Instrument")
MixKit.Dropdown(label: "", options: [-1] + instruments, selected: inst, fill: true, optionLabel: label,
onLongPress: { if inst >= 0 { open(inst) } },
onSelect: { session.setInstrumentSlot($0, ofChannel: ch) })
MixKit.SectionLabel(text: "FX 1-4")
ForEach(0..<4, id: \.self) { i in
let fx = session.fxSlot(i, ofChannel: ch)
MixKit.Dropdown(label: "", options: [-1] + effects, selected: fx, fill: true, optionLabel: label,
onLongPress: { if fx >= 0 { open(fx) } },
onSelect: { session.setFxSlot(i, to: $0, ofChannel: ch) })
}
MixKit.SectionLabel(text: "MIDI Ch")
HStack(spacing: 0) {
MixKit.RepeatButton(label: "-") {
session.setMidiChannel(max(0, session.midiChannel(ofChannel: ch) - 1), ofChannel: ch)
}
Text("\(session.midiChannel(ofChannel: ch))").font(Theme.mono(12)).foregroundColor(Theme.text)
.lineLimit(1).frame(width: 28)
MixKit.RepeatButton(label: "+") {
session.setMidiChannel(min(16, session.midiChannel(ofChannel: ch) + 1), ofChannel: ch)
}
}
}
@ViewBuilder private var levels: some View {
MixKit.SectionLabel(text: "Volume")
VolumeFader(ch: ch, value: session.volume(ofChannel: ch)) { session.setVolume($0, ofChannel: ch) }
.frame(maxWidth: .infinity, maxHeight: .infinity)
MixKit.Button(label: "LIM", active: session.hasLimiter(ch), fill: true) {
session.setChannel(ch, limiter: !session.hasLimiter(ch))
}
HStack(spacing: 4) {
MixKit.Button(label: "M", active: session.isMuted(ch), fill: true) { session.setChannel(ch, muted: !session.isMuted(ch)) }
MixKit.Button(label: "S", active: session.isSolo(ch), fill: true) { session.setChannel(ch, solo: !session.isSolo(ch)) }
}
}
}
/// Android's VerticalFader: the set level fills from the bottom with a thumb line;
/// tap or drag to set, snapping to the default (0.8) within ±0.03. The bus meter
/// rises inside the fill, green to amber to red over the track's height, all red
/// while the bus clips. Only the meter layer redraws each frame.
private struct VolumeFader: View {
let ch: Int
let value: Float
let onChange: (Float) -> Void
private let defaultVolume: Float = 0.8
var body: some View {
GeometryReader { g in
let h = g.size.height
let vol = CGFloat(min(max(value, 0), 1))
let top = h - vol * h
ZStack(alignment: .top) {
Theme.background
Rectangle().fill(Theme.accent.opacity(0.30)).frame(height: vol * h).offset(y: top)
MeterLayer(ch: ch, volume: vol)
Rectangle().fill(Theme.accent).frame(height: 3).offset(y: min(max(top - 1.5, 0), max(h - 3, 0)))
}
.frame(width: g.size.width, height: h, alignment: .top)
.clipped()
.overlay(Rectangle().stroke(Theme.grid, lineWidth: 1))
.contentShape(Rectangle())
.gesture(DragGesture(minimumDistance: 0).onChanged { d in
guard h > 0 else { return }
let raw = min(max(Float(1 - d.location.y / h), 0), 1)
onChange(abs(raw - defaultVolume) < 0.03 ? defaultVolume : raw)
})
}
}
}
/// The live meter: a Canvas in a display-rate TimelineView, reading the engine's
/// peak and clip flag directly (no state, nothing else redraws).
private struct MeterLayer: View {
let ch: Int
let volume: CGFloat
var body: some View {
if Snapshot.active {
Color.clear
} else {
TimelineView(.animation) { _ in
let session = SizzleSession.shared
let level = CGFloat(min(max(session.meter(ofChannel: ch), 0), 1))
let clipped = session.clippedChannel(ch)
let vol = volume
Canvas { ctx, size in
let frac = min(level, vol)
guard frac > 0.001 else { return }
let h = size.height, mh = frac * h
let rect = Path(CGRect(x: 0, y: h - mh, width: size.width, height: mh))
if clipped {
ctx.fill(rect, with: .color(MixKit.danger))
} else {
let g = Gradient(stops: [
.init(color: MixKit.playhead, location: 0), .init(color: MixKit.playhead, location: 0.65),
.init(color: Theme.accent, location: 0.85), .init(color: MixKit.danger, location: 1),
])
ctx.fill(rect, with: .linearGradient(g, startPoint: CGPoint(x: 0, y: h), endPoint: .zero))
}
}
}
}
}
}