// 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 } } }