Files
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

170 lines
11 KiB
Markdown

# Porting Sizzle to iOS/iPadOS
The whole Android app (`../app`, ~20k lines of Kotlin) as a native iOS/iPadOS 15+
app. Target hardware includes the iPad mini 4 (A8, 2 GB RAM). Decided 2026-11-15:
- **Interface:** native, SwiftUI with UIKit where SwiftUI is too slow on an A8
(the tracker grid). Written fresh, following the Android screens.
- **Engine, model and file formats:** C++ in `core/`, each part checked against
the Kotlin app's own output (reference audio and reference files), like the
effects in 0.1.x. Swift talks to the core through an Objective-C++ facade.
- **Plugins:** the AUv3 extension keeps shipping Sizzle's devices to other hosts;
the app itself hosts the same code in-process (no per-plugin process cost, no
dependence on the system plugin registry).
The Kotlin app stays the reference. When it changes, the reference files are
regenerated (`make -C ios golden`) and the C++ follows.
## Map: Android → iOS
| Android (Kotlin) | Lines | iOS | How it's checked |
|---|---|---|---|
| `audio/Effects.kt`, `AmbienceReverb.kt` | 1,150 | `core/src/fx` (done in 0.1.0) | reference audio per effect |
| `audio/SynthVoice.kt`, `SynthPatch.kt` | 500 | `core/src/synth` | reference audio per voice |
| `frasil/` (Morph Pad) | 950 | `core/src/frasil` | reference audio per voice |
| `wavetable/` | 730 | `core/src/wavetable` | reference audio per voice, table data |
| `echofield/`, `audio/GrainSources.kt` (Grain Cloud) | 1,100 | `core/src/echofield` | reference audio per voice |
| `audio/SampleVoice.kt`, `SampleStore.kt`, `SoundFontLoader.kt` | 570 | `core/src/sampler` | reference audio with generated samples / test SF2 |
| `model/` | 1,360 | `core/src/model` | via file round trips and full-song renders |
| `audio/AudioEngine.kt` (sequencer, mixer, routing, arp, clock) | 1,680 | `core/src/engine` | full-song renders (`samples/*.sng`, generated songs) |
| `io/` (.sng, projects, presets, bundles, backups) | 1,800 | `core/src/io` | Kotlin-written files parsed and re-written byte-identical |
| `input/` (MIDI in/out, clock, gamepad, key bindings) | 1,350 | CoreMIDI, GameController (Obj-C++/Swift) | unit tests on the parsers; on device |
| `playback/` (background service, sound module) | 430 | AVAudioSession, background audio | on device |
| `ui/` + `AppViewModel` | 8,000 | `app/` SwiftUI | on device / simulator screenshots |
## Milestones
Each milestone ends with a build that runs on the iPad.
1. **Instruments in C++.** Synth, Morph Pad, Wavetable, Grain Cloud, then Sampler
and SF2/XI, each matching Kotlin voice renders. Plus AUv3 instruments (`aumu`)
and the in-app host's first form: a device list with a keyboard, so each one
can be played on the iPad and measured on the A8.
2. **Model, files and engine.** Project model, .sng / project / preset formats
(byte-identical round trips with Kotlin-written files), and the sequencer and
mixer engine (full-song renders match Kotlin). MIDI effects (arp, transposer,
LFO) come with the engine.
3. **App shell and tracker.** Tabs (Tracker, Mix, Toolbox, Setup), transport,
pattern grid, arrangement roll, keyboards, song load/save. Audio through
AVAudioEngine; background audio.
4. **Toolbox and mixer.** Device editors (cards from the DeviceLayouts data, then
the bespoke editors), presets, mixer with FX routing.
5. **Everything else.** MIDI in/out and clock sync, gamepad and key bindings,
recording, sample recorder, sharing/import, backups, themes, settings.
## Progress
- 2026-11-15: all six instruments' sound is in C++ and matches Kotlin bit for bit
(Synth, Morph Pad, Wavetable, Grain Cloud, Sampler, SF2/XI), plus the 12 factory
wavetables, the sample store and the WAV/SF2/XI readers (`core/tests/sampler_test`).
`make -C ios core-test`: 61 reference scenarios + 16 sampler tests.
- 2026-11-15, 0.2.0: the instruments as AUv3 (`aumu`) on `sizzle::Instrument` (the
engine's per-channel voice pool), checked on the Mac against the C++ instruments
with sample-accurate MIDI (`make -C ios au-test`). The app hosts every device
in-process (`SizzleHost`, manufacturer 'Tsyl'), so it works without the system
plugin registry, and benchmarks each device on the device it runs on.
Measured on the iPad mini 4 (A8, iOS 15.8.8), 0.3.0's Benchmark: 10 s of 44.1 kHz
audio per device, share of one core (instruments with an 8-note chord held,
effects on noise, stereo):
| Device | Cost | Device | Cost |
|---|---|---|---|
| Ambience Reverb | 9.3% | Morph Pad (8 notes) | 9.6% |
| Tape Delay | 1.5% | Wavetable (8 notes) | 6.1% |
| 10-Band EQ | 1.4% | Synth (8 notes) | 5.0% |
| Tape Engine | 1.1% | Grain Cloud (8 notes) | 1.1% |
| Mix Tightener | 0.9% | SF2/XI loader (8 notes) | 0.8% |
| Bitcrusher | 0.5% | Sampler (8 notes) | 0.4% |
| Filter | 0.3% | | |
| Granular Glitch | 0.2% | | |
All fourteen together come to about 38% of one core; the A8 has two. The C++ DSP is
cheap enough for the whole suite on this iPad. What made AUM struggle with a few
AUv3s is the per-plugin process round trip, which the in-app host avoids.
- 2026-11-15: the engine (`core/src/engine`, `sizzle::engine::Engine`): AudioEngine.kt's
sequencer, arrangement playlist with loop regions, note-offs, arpeggiator,
transposer, MIDI LFO, mixer, FX chains and master limiter. `core/tests/engine_test`
renders the four reference songs (demo, mix, waltz, midifx) and matches Kotlin
(demo and midifx bit for bit; mix and waltz within 1e-5, from the Tape Engine's
known one-ulp difference fed through the rest of the song). Not ported: audio
output (the app calls `fillBlock`), WAV writing (`MasterSink`), CoreMIDI (the
engine fills the outgoing queue, `pollMidiOut`), effect scope/playhead views (done in 0.5.0: Granular Glitch).
Decisions: the voice pools are built in AudioEngine's field order from
`EngineGlobals` (clock and seed counters); swapped FX chains and playlists are
freed by a later main-thread rebuild once the audio thread has finished a block;
the live note calls touch voices directly like the Kotlin, so the app must call
them on the audio thread (or serialise them with it). The MIDI LFO writes its
value into the target slot as text every block, like Kotlin; that goes through
`kotlinFloatToChars` (no allocation).
- 2026-11-16: file formats (`core/src/io`, `sizzle::io`): `ProjectIo` (.szg),
`SngFormat` (.sng) and `PresetIo` (.szp), ported from io/*.kt with Kotlin's
string semantics (`io/KotlinText`: lineSequence, Unicode trim, toIntOrNull with
Unicode digits, toFloatOrNull with Java's parseFloat grammar). `core/tests/io_test`
re-writes every reference file byte for byte, except [TOOLBOX] pairs (Kotlin
writes ConcurrentHashMap order; compared as sets), and checks .szg -> .sng / .szp.
`model::kotlinFloatToString` is JDK 17's FloatingDecimal ported line for line and
was compared with JDK 17.0.14 on all 2^32 float bit patterns. Kotlin's own lossy
spots, kept as is: PresetIo.import drops NAME (fill() overwrites it); a .sng
`track` channel comes from the mixer, which import leaves alone; names with `;`,
values with `,` or `=` break a [TOOLBOX] line. `Params` caps keys at 23 bytes,
values at 31 and slots at 96 keys, so a long sample path would be cut short.
- 2026-11-16, 0.6.0 (milestone 5): recording, MIDI, input bindings, themes,
bundles, backups, settings. Decisions:
- Master recording: `SizzleSession+Record` implements the engine's `MasterSink`
(MasterRecorder.kt: SPSC float ring, a writer thread, header patched at the
end). Takes go to Documents/Recordings (no folder picker on iOS; Documents is
visible in Files). Sample recording uses AVAudioRecorder; the audio session
is play-and-record only while recording, and the engine restarts itself on
the configuration change that causes.
- MIDI: `core/include/sizzle/Midi.hpp` holds the platform-free parts
(MidiByteParser, MidiSoundModule, the clock follower; `midi_test`).
`SizzleMidi` is the CoreMIDI side. Notes from the MIDI threads go through a
second note queue to the audio thread (pushes serialised by the module's
lock), never through the main thread. The send thread reads the engine's
`isPlaying` / `pollMidiOut` and the tempo without the song lock. Source keys
are manufacturer|model|name (CoreMIDI's unique id is used only within a run).
- Input: InputAction / InputRouter / InputBindings / InputController port
Android's input package; HardwareInput uses GameController's GCKeyboard and
GCController, so no responder-chain plumbing is needed (keys are ignored
while a text field has focus). Keyboard codes are HID usages, so binding
maps don't carry over from Android (they never could: Android key codes).
- Themes: `Theme` reads a `Palette`; the app rebuilds its view tree when the
theme changes. The plugins always draw the default theme.
- ZIP: `core/src/io/Zip` (zlib): reads through the central directory (so
java.util.zip's data descriptors are fine), writes sizes in local headers
(so ZipInputStream reads ours); `zip-test` checks both against `jar` and
`unzip`. Bundles keep the Android layout and store samples under their
original ids. Backups hold songs/themes/presets/samples, autosave.szg and
settings.txt (AppSettings' key=value text); restore unpacks aside first,
then swaps the folders in and applies everything at once (no restart).
- Not done: App Group file access for the plugins; per-device output choice
(iOS routes audio; Setup shows the route and the system picker); MIDI 2.0 /
MIDIEventList.
Notes for the engine port (from the voice ports):
- Kotlin seeds every new FrasilVoice and GrainCloud from global counters, and every
SynthVoice builds a FrasilVoice. The C++ engine owns one `frasil::FrasilSeeds`,
one `echofield::GrainSeeds` and one `frasil::FrasilClock`, and builds its voice
pools in AudioEngine's order, or full-song renders won't match.
- `synth::SynthPatch` has an id standing in for Kotlin's object identity
(`setPatch` skips retuning for the same patch): reuse the patch built per block,
don't rebuild it.
- `sampler::SampleStore`: the audio thread calls `quiescent()` once per block;
voices pin samples. The Grain Cloud's custom-sample pointer isn't pinned yet.
## Rules for ports
- Translate line for line and keep Kotlin's types (`Float` → `float`, `Double` →
`double`); use `core/src/fx/KotlinMath.hpp` for kotlin.math semantics;
`uint32_t` for xorshift; port `java.util.Random` exactly where it is seeded.
- No allocation or locks on the audio path; allocate in constructors / `prepare`.
- A port is done when its reference scenarios pass (`make -C ios core-test`).
Never loosen the tolerance; find the precision difference instead.
- Global Kotlin state (e.g. `FrasilClock`) becomes explicit state passed in or
owned by the engine, and the reference files record its starting value.