Files
sizzletracker-android/ios/app/Host/Bespoke/SlotDataSource+Sources.swift
T
ReactorcoremeltdownandClaude Opus 5.5 435305e42a iOS 0.5.0: hand-built editors (Tape Delay, Tape Engine, Mix Tightener, Granular scope, Wavetable tables, Grain Cloud source)
Effect visualisation through the C++ effect, engine and AU; custom wavetables and
grain sources load and persist with the song; the app's slot editor hosts the
file picker for bespoke views.

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

45 lines
2.0 KiB
Swift

// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
import Foundation
/// The Wavetable's tables and the Grain Cloud's sources for a toolbox slot of the
/// song, and loading your own (SizzleSession+Sources).
extension SlotDataSource: SampleLengthSource {
func wavetableFrames(osc: Int) -> (frames: Int, size: Int, data: [Float])? {
var frames = 0, size = 0
guard let d = session.wavetable(ofSlot: slot, osc: osc, frames: &frames, size: &size) else { return nil }
return (frames, size, WavetableBespoke.floats(d))
}
func samplePeaks(key: String, buckets: Int) -> [Float]? {
session.samplePeaks(ofSlot: slot, key: key, buckets: buckets).map(WavetableBespoke.floats)
}
func sampleSeconds(key: String) -> Double? {
let s = session.sampleSeconds(ofSlot: slot, key: key)
return s > 0 ? s : nil
}
/// "wavetable" (LOAD TABLE) or "grainSource" (the Grain Cloud's LOAD WAV): the
/// picked file (security-scoped, at most 64 MB) into this slot.
func loadFile(_ url: URL, kind: String) -> String? {
guard kind == "wavetable" || kind == "grainSource" else { return "Not available here yet." }
let scoped = url.startAccessingSecurityScopedResource()
defer { if scoped { url.stopAccessingSecurityScopedResource() } }
let maxBytes = 64 * 1024 * 1024
let size = (try? url.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0
guard size <= maxBytes, let data = try? Data(contentsOf: url), data.count <= maxBytes else {
return "Couldn't read the file, or it's larger than 64 MB."
}
let name = url.deletingPathExtension().lastPathComponent
do {
if kind == "wavetable" { try session.loadWavetableData(data, name: name, intoSlot: slot) }
else { try session.loadGrainSourceData(data, name: name, intoSlot: slot) }
return nil
} catch {
return error.localizedDescription
}
}
}