Files
sizzletracker-android/ios/app/Host/Screens/Mix/MixScreen.swift
T
ReactorcoremeltdownandClaude Opus 5.5 64e21e0bcc iOS 0.6.0: milestone 5 (recording, MIDI, input bindings, themes, bundles, backups)
- Master recording (MasterSink ring + writer thread) and the sample recorder
- CoreMIDI in/out: sound module, clock send/follow, virtual "Sizzle" ports;
  platform-free parser/module/clock in core (midi_test)
- Hardware keyboard and gamepads via GameController; rebindable keyboard,
  gamepad and MIDI CC actions; per-parameter MIDI learn
- Colour themes (.szt), project bundles (.szgz), backup & restore;
  ZIP in core with zlib (zip_test against jar and unzip)
- Persistent app settings; audio pauses in the background unless sound module

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

221 lines
9.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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 VStack(spacing: 0) {
RecordToolbar(session: session)
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)
}
}
/// Android's RecordToolbar: arm the master recording (it starts on Play or an
/// external MIDI clock Start), the FX tail after the stop, and the last take. Takes
/// are saved in Sizzle's Recordings folder (Files app); SHARE sends the last one.
private struct RecordToolbar: View {
let session: SizzleSession
var body: some View {
let recording = session.masterRecording
let last = session.lastRecordingURL
let status: String = recording ? "recording…"
: session.recordArmed ? "armed — starts on play or MIDI clock start"
: last.map { "saved \($0.lastPathComponent)" } ?? "master → WAV (Files › Sizzle › Recordings)"
MixKit.HScroll {
HStack(spacing: 6) {
MixKit.Button(label: recording ? "● REC" : "● ARM", active: session.recordArmed && !recording, danger: recording) {
session.recordArmed = !session.recordArmed
}
MixKit.Dropdown(label: "TAIL", options: [0, 1, 2, 3, 4], selected: session.recordTailBars,
optionLabel: { "\($0) bar" + ($0 == 1 ? "" : "s") },
onSelect: { session.recordTailBars = $0; AppSettings.shared.recordTailBars = $0 })
if let url = last, !recording {
MixKit.Button(label: "SHARE") { ShareSheet.present(url) }
}
Text(status).font(Theme.mono(11)).foregroundColor(Theme.dim).lineLimit(1)
}
.padding(6)
}
.frame(maxWidth: .infinity, alignment: .leading)
.background(Theme.surface)
}
}
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)
if AppSettings.shared.vuMeters { 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))
}
}
}
}
}
}