The Mix record bar, the Sampler waveform page, the LFO target picker and the audition keyboards read the session but had no changing inputs, so SwiftUI skipped redrawing them until something else (rotation) forced it. They now observe MixSongWatch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
270 lines
12 KiB
Swift
270 lines
12 KiB
Swift
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||
|
||
import SwiftUI
|
||
|
||
/// Sampler pads: 16 pads from C2 (MIDI 36), keys "padN" (sample id), "padNS" /
|
||
/// "padNE" (slice 0..1), "padNV" (volume, 0.8 by default).
|
||
enum SamplerPads {
|
||
static let baseNote = 36
|
||
static let defaultVolume: Float = 0.8
|
||
static func note(_ pad: Int) -> Int { baseNote + pad }
|
||
}
|
||
|
||
/// SamplerEditor's detail area for the selected pad: its source buttons, then a
|
||
/// swipeable pair of pages (waveform with slice markers / the 16 pad volumes).
|
||
struct SamplerDetail: View {
|
||
let slot: Int
|
||
let pad: Int
|
||
let onSelectPad: (Int) -> Void
|
||
let onImport: () -> Void
|
||
@State private var page = 0
|
||
@ObservedObject private var recorder = SampleRecorder.shared
|
||
|
||
var body: some View {
|
||
let session = SizzleSession.shared
|
||
let assigned = !session.value(ofSlot: slot, key: "pad\(pad)").isEmpty
|
||
let oneShot = session.value(ofSlot: slot, key: "oneShot") == "On"
|
||
VStack(alignment: .leading, spacing: 8) {
|
||
// One-shot (the whole Sampler): note-offs don't cut the pads.
|
||
HStack(spacing: 8) {
|
||
MixKit.Button(label: "ONE-SHOT", active: oneShot) {
|
||
session.setValue(oneShot ? "Off" : "On", ofSlot: slot, key: "oneShot")
|
||
}
|
||
Text(oneShot ? "Pads play their whole slice; note-offs are ignored."
|
||
: "Pads stop on note-off (key release, ===).")
|
||
.font(Theme.mono(11)).foregroundColor(Theme.dim).lineLimit(2)
|
||
}
|
||
Text("Pad \(pad + 1) · \(MixKit.pitchName(SamplerPads.note(pad)))").font(Theme.mono(13)).foregroundColor(Theme.accent)
|
||
MixKit.HScroll {
|
||
HStack(spacing: 8) {
|
||
MixKit.Button(label: "IMPORT WAV", action: onImport)
|
||
if recorder.isRecording {
|
||
MixKit.Button(label: "■ STOP REC", active: true) {
|
||
recorder.stopInto(slot: slot, pad: pad)
|
||
session.setNumber(0, ofSlot: slot, key: "pad\(pad)S")
|
||
session.setNumber(1, ofSlot: slot, key: "pad\(pad)E")
|
||
}
|
||
} else {
|
||
MixKit.Button(label: "● RECORD") { recorder.start() }
|
||
}
|
||
if assigned {
|
||
MixKit.Button(label: "✂ CLIP") { _ = session.trimPad(pad, ofSlot: slot) }
|
||
MixKit.Button(label: "NORMALIZE") { _ = session.normalizePad(pad, ofSlot: slot) }
|
||
MixKit.Button(label: "CLEAR", danger: true) { session.clearPad(pad, ofSlot: slot) }
|
||
}
|
||
}
|
||
}
|
||
Group {
|
||
if page == 0 {
|
||
WaveformPage(slot: slot, pad: pad)
|
||
} else {
|
||
VStack(alignment: .leading, spacing: 6) {
|
||
Text("Pad volumes (drag a slider; tap selects that pad):").font(Theme.mono(11)).foregroundColor(Theme.dim)
|
||
PadVolumeBank(slot: slot, selected: pad, onSelect: onSelectPad)
|
||
}
|
||
}
|
||
}
|
||
.frame(maxWidth: .infinity, minHeight: 200, maxHeight: 200, alignment: .top)
|
||
.contentShape(Rectangle())
|
||
.simultaneousGesture(DragGesture(minimumDistance: 30).onEnded { g in
|
||
// Swipe between the pages (the drags inside them are vertical or marker drags).
|
||
guard abs(g.translation.width) > abs(g.translation.height) * 2, abs(g.translation.width) > 80 else { return }
|
||
page = g.translation.width < 0 ? 1 : 0
|
||
})
|
||
HStack(spacing: 8) {
|
||
ForEach(0..<2, id: \.self) { i in
|
||
Circle().fill(i == page ? Theme.accent : Theme.grid)
|
||
.frame(width: i == page ? 10 : 7, height: i == page ? 10 : 7)
|
||
.frame(width: 14, height: 14)
|
||
.contentShape(Rectangle())
|
||
.onTapGesture { page = i }
|
||
}
|
||
}
|
||
.frame(maxWidth: .infinity)
|
||
}
|
||
}
|
||
}
|
||
|
||
/// The selected pad's waveform; drag near a marker to move the slice start / end.
|
||
private struct WaveformPage: View {
|
||
/// Redraw when the song changes: these inputs alone never change, so SwiftUI
|
||
/// would otherwise keep showing the old state.
|
||
@ObservedObject private var watch = MixSongWatch.shared
|
||
let slot: Int
|
||
let pad: Int
|
||
@State private var dragging = -1
|
||
|
||
var body: some View {
|
||
let session = SizzleSession.shared
|
||
let key = "pad\(pad)"
|
||
let peaks = PeakCache.peaks(slot: slot, key: key)
|
||
let start = CGFloat(Float(session.value(ofSlot: slot, key: key + "S")) ?? 0)
|
||
let end = CGFloat(Float(session.value(ofSlot: slot, key: key + "E")) ?? 1)
|
||
let frames = session.sampleFrames(ofSlot: slot, key: key)
|
||
VStack(alignment: .leading, spacing: 6) {
|
||
GeometryReader { g in
|
||
let w = g.size.width, h = g.size.height
|
||
ZStack(alignment: .topLeading) {
|
||
Theme.surface
|
||
if let p = peaks { WaveformShape(peaks: p).stroke(Theme.text, lineWidth: 1) }
|
||
Theme.accent.opacity(0.14).frame(width: max(0, (end - start) * w), height: h).offset(x: start * w)
|
||
Theme.accent.frame(width: 3, height: h).offset(x: start * w - 1.5)
|
||
MixKit.playhead.frame(width: 3, height: h).offset(x: end * w - 1.5)
|
||
}
|
||
.clipped()
|
||
.contentShape(Rectangle())
|
||
.gesture(DragGesture(minimumDistance: 1)
|
||
.onChanged { d in
|
||
guard w > 0 else { return }
|
||
if dragging < 0 {
|
||
let f0 = min(max(d.startLocation.x / w, 0), 1)
|
||
dragging = abs(f0 - start) <= abs(f0 - end) ? 0 : 1
|
||
}
|
||
let f = Float(min(max(d.location.x / w, 0), 1))
|
||
session.setNumber(f, ofSlot: slot, key: key + (dragging == 0 ? "S" : "E"))
|
||
}
|
||
.onEnded { _ in dragging = -1 })
|
||
}
|
||
.frame(height: 150)
|
||
Text(frames == 0 ? "Pad empty — Import a WAV to assign it."
|
||
: "\(frames) frames @ \(session.sampleRate(ofSlot: slot, key: key)) Hz · drag the markers, then CLIP to trim")
|
||
.font(Theme.mono(10)).foregroundColor(Theme.dim)
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 16 vertical pad-volume sliders, 8 wide and 2 tall like the pad grid.
|
||
private struct PadVolumeBank: View {
|
||
let slot: Int
|
||
let selected: Int
|
||
let onSelect: (Int) -> Void
|
||
|
||
var body: some View {
|
||
VStack(spacing: 6) {
|
||
ForEach(0..<2, id: \.self) { row in
|
||
HStack(spacing: 4) {
|
||
ForEach(0..<8, id: \.self) { col in
|
||
PadVolumeSlider(slot: slot, pad: row * 8 + col, isSelected: selected == row * 8 + col, onSelect: onSelect)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private struct PadVolumeSlider: View {
|
||
let slot: Int
|
||
let pad: Int
|
||
let isSelected: Bool
|
||
let onSelect: (Int) -> Void
|
||
|
||
var body: some View {
|
||
let session = SizzleSession.shared
|
||
let vol = CGFloat(Float(session.value(ofSlot: slot, key: "pad\(pad)V")) ?? SamplerPads.defaultVolume)
|
||
let assigned = !session.value(ofSlot: slot, key: "pad\(pad)").isEmpty
|
||
let border = isSelected ? MixKit.playhead : (assigned ? Theme.accent : Theme.grid)
|
||
VStack(spacing: 1) {
|
||
GeometryReader { g in
|
||
let h = g.size.height
|
||
ZStack(alignment: .bottom) {
|
||
Theme.surface
|
||
(assigned ? Theme.accent : Theme.grid).opacity(0.55).frame(height: min(max(vol, 0), 1) * h)
|
||
}
|
||
.overlay(Rectangle().stroke(border, lineWidth: isSelected ? 2 : 1))
|
||
.contentShape(Rectangle())
|
||
.gesture(DragGesture(minimumDistance: 0).onChanged { d in
|
||
if !isSelected { onSelect(pad) }
|
||
guard h > 0 else { return }
|
||
let raw = Float(min(max(1 - d.location.y / h, 0), 1))
|
||
let v = abs(raw - SamplerPads.defaultVolume) < 0.03 ? SamplerPads.defaultVolume : raw
|
||
session.setNumber(v, ofSlot: slot, key: "pad\(pad)V")
|
||
})
|
||
}
|
||
.frame(height: 76)
|
||
Text(MixKit.pitchName(SamplerPads.note(pad))).font(Theme.mono(8)).foregroundColor(assigned ? Theme.text : Theme.dim)
|
||
}
|
||
.frame(maxWidth: .infinity)
|
||
}
|
||
}
|
||
|
||
/// SamplerPadBank: the playable 4x4 pads pinned under the editor (pad 0 = C2 top
|
||
/// left). A pad sounds the instant it's touched and becomes the selected one.
|
||
struct SamplerPadBank: View {
|
||
let slot: Int
|
||
let selected: Int
|
||
let onSelect: (Int) -> Void
|
||
|
||
var body: some View {
|
||
VStack(spacing: 4) {
|
||
ForEach(0..<4, id: \.self) { row in
|
||
HStack(spacing: 4) {
|
||
ForEach(0..<4, id: \.self) { col in
|
||
PadCell(slot: slot, pad: row * 4 + col, isSelected: selected == row * 4 + col, onSelect: onSelect)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
.padding(6)
|
||
.background(Theme.background)
|
||
}
|
||
}
|
||
|
||
private struct PadCell: View {
|
||
let slot: Int
|
||
let pad: Int
|
||
let isSelected: Bool
|
||
let onSelect: (Int) -> Void
|
||
@State private var down = false
|
||
|
||
var body: some View {
|
||
let session = SizzleSession.shared
|
||
let assigned = !session.value(ofSlot: slot, key: "pad\(pad)").isEmpty
|
||
let note = SamplerPads.note(pad)
|
||
let border = isSelected ? MixKit.playhead : (assigned ? Theme.accent : Theme.grid)
|
||
let fill = isSelected ? Theme.accent.opacity(0.28) : (assigned ? Theme.accent.opacity(0.12) : Theme.surface)
|
||
ZStack {
|
||
fill
|
||
VStack(spacing: 1) {
|
||
Text(MixKit.pitchName(note)).font(Theme.mono(10)).foregroundColor(assigned ? Theme.text : Theme.dim)
|
||
Text(assigned ? "●" : "+").font(Theme.mono(10)).foregroundColor(assigned ? Theme.accent : Theme.dim)
|
||
}
|
||
}
|
||
.overlay(Rectangle().stroke(border, lineWidth: isSelected ? 2 : 1))
|
||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||
.contentShape(Rectangle())
|
||
.gesture(DragGesture(minimumDistance: 0)
|
||
.onChanged { _ in
|
||
// On touch-down, like a drum pad; a one-shot, so no note-off (as Android).
|
||
guard !down else { return }
|
||
down = true
|
||
onSelect(pad)
|
||
session.auditionSlot(slot, note: note, on: true)
|
||
}
|
||
.onEnded { _ in down = false })
|
||
}
|
||
}
|
||
|
||
/// SoundFontEditor's loader part: LOAD SF2 / XI, what's loaded, and its waveform.
|
||
/// (Loop, crossfade, volume and envelope are on the cards below.)
|
||
struct SoundFontDetail: View {
|
||
let slot: Int
|
||
let onImport: () -> Void
|
||
|
||
var body: some View {
|
||
let session = SizzleSession.shared
|
||
let frames = session.sampleFrames(ofSlot: slot, key: "sfSample")
|
||
let root = Int(Float(session.value(ofSlot: slot, key: "sfRoot")) ?? 60)
|
||
VStack(alignment: .leading, spacing: 8) {
|
||
MixKit.Button(label: "LOAD SF2 / XI", action: onImport)
|
||
Text(frames == 0 ? "No instrument loaded — load a .sf2 or .xi file."
|
||
: "Loaded: \(frames) frames @ \(session.sampleRate(ofSlot: slot, key: "sfSample")) Hz · root \(MixKit.pitchName(root))")
|
||
.font(Theme.mono(10)).foregroundColor(frames == 0 ? Theme.dim : Theme.text)
|
||
if frames > 0, let peaks = PeakCache.peaks(slot: slot, key: "sfSample") {
|
||
WaveformShape(peaks: peaks).stroke(Theme.text, lineWidth: 1)
|
||
.frame(height: 96).background(Theme.surface)
|
||
}
|
||
}
|
||
}
|
||
}
|