Nothing user-visible changes, except that the Android app is half the size and both are lighter on the battery. R8 is on for release builds (21.5 MB -> 10.1 MB); only the two JNI classes are pinned by name, since the app reflects over nothing of its own, and `make dist` keeps the mapping beside the APK. The Kotlin DSP that core/ replaced in 0.37.0 was still compiled into every APK with nothing calling it — it moves to the test source set, where it belongs as the reference the C++ is checked against — and the golden tests' TestVoice wrappers leave core/src for core/tests/voices, so neither app compiles them either. Speed: channel gain, mute/solo and the FX chain are read once per audio block instead of once per frame in Engine::fillBlock (both platforms, bit-identical — all 61 reference scenarios still match); the transport pollers idle at 100 ms (Android) and 10 Hz (iOS) instead of polling flat out while a song is stopped, woken by any transport call; ProjectIo.save builds into one pre-sized buffer and a burst of edits is coalesced into one engine sync; the Kotlin grain sources and factory wavetables are built when their editor opens rather than at launch, where they duplicated ~6 MB the engine already renders in core/; and Xcode builds the DSP core at -O2, the level the golden tests measure, not -Os. Fix: `make lint` failed on a manifest asking for ACCESS_FINE_LOCATION without ACCESS_COARSE_LOCATION (the BLE MIDI scan on Android 8-11), so `make check` passes again. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
100 lines
3.9 KiB
Swift
100 lines
3.9 KiB
Swift
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||
|
||
import Combine
|
||
import SwiftUI
|
||
|
||
/// The app's UI state around the open song (SizzleSession): the iOS counterpart
|
||
/// of the Android app's AppViewModel. Screens read the song through `session`
|
||
/// and re-render when `revision` changes; transport state is polled at display
|
||
/// rate while playing.
|
||
@MainActor
|
||
final class AppModel: ObservableObject {
|
||
enum Tab: Int, CaseIterable { case tracker, mix, toolbox, setup }
|
||
|
||
let session = SizzleSession.shared
|
||
/// The app's instance (the snapshot tool makes its own).
|
||
static let main = AppModel()
|
||
|
||
/// Bumped whenever the song changes (any edit, load, transport change).
|
||
@Published private(set) var revision = 0
|
||
@Published var tab: Tab = .tracker
|
||
|
||
// Transport, polled 30×/s while playing (see pollTransport).
|
||
@Published private(set) var isPlaying = false
|
||
@Published private(set) var currentLine = 0
|
||
@Published private(set) var currentBeat = 0
|
||
|
||
// Tracker cursor (Android: cursorLine / cursorTrack / column).
|
||
enum Column: Int { case note, velocity, channel }
|
||
@Published var cursorTrack = 0
|
||
@Published var cursorLine = 0
|
||
@Published var cursorColumn: Column = .note
|
||
|
||
// On-screen keyboard (Android: kbdChannel / kbdOctave / kbdVelocity).
|
||
// Channel and octave persist (AppSettings), like Android's.
|
||
@Published var kbdChannel = AppSettings.shared.kbdChannel { didSet { AppSettings.shared.kbdChannel = kbdChannel } }
|
||
@Published var kbdOctave = AppSettings.shared.kbdOctave { didSet { AppSettings.shared.kbdOctave = kbdOctave } }
|
||
@Published var kbdVelocity = 127
|
||
|
||
/// The toolbox slot whose editor is open, if any.
|
||
@Published var editingSlot: Int?
|
||
|
||
private var observer: NSObjectProtocol?
|
||
private var timer: Timer?
|
||
|
||
init() {
|
||
observer = NotificationCenter.default.addObserver(forName: .SizzleSessionDidChange, object: nil, queue: .main) { [weak self] _ in
|
||
// SizzleSession's -edit: announces transport calls too, so this is also
|
||
// where the poll rate catches up: however playback was started — the
|
||
// tracker's PLAY button, a gamepad, a MIDI binding — the display-rate
|
||
// ticks begin in the same turn, not up to a tick later.
|
||
Task { @MainActor in self?.revision += 1; self?.pollTransport() }
|
||
}
|
||
// The timer fires on the main run loop, so the body is already on the main
|
||
// actor: assumeIsolated instead of `Task { @MainActor in … }`, which hopped
|
||
// through an allocation and a scheduler turn 30 times a second forever.
|
||
// Ticks while stopped only watch for isPlaying, so they run at 10×/s.
|
||
startTimer(hz: Self.idleHz)
|
||
}
|
||
|
||
private static let playingHz = 30.0
|
||
private static let idleHz = 10.0
|
||
|
||
private func startTimer(hz: Double) {
|
||
timer?.invalidate()
|
||
timerHz = hz
|
||
let t = Timer(timeInterval: 1.0 / hz, repeats: true) { [weak self] _ in
|
||
MainActor.assumeIsolated { self?.pollTransport() }
|
||
}
|
||
RunLoop.main.add(t, forMode: .common)
|
||
timer = t
|
||
}
|
||
|
||
private var timerHz = 0.0
|
||
|
||
private func pollTransport() {
|
||
let playing = session.isPlaying
|
||
if playing != isPlaying { isPlaying = playing }
|
||
if playing {
|
||
let line = session.currentLine, beat = session.currentBeat
|
||
if line != currentLine { currentLine = line }
|
||
if beat != currentBeat { currentBeat = beat }
|
||
}
|
||
// Follow the playhead at display rate; idle at a third of that, which is what
|
||
// a stopped song needs to notice someone else starting it.
|
||
let want = playing ? Self.playingHz : Self.idleHz
|
||
if want != timerHz { startTimer(hz: want) }
|
||
}
|
||
|
||
func playPause() {
|
||
session.isPlaying ? session.pause() : session.play()
|
||
pollTransport()
|
||
}
|
||
|
||
func stop() {
|
||
session.stop()
|
||
pollTransport()
|
||
}
|
||
}
|