Files
KMix_Pro/backups/before-kjmix-20261103-100337/KJamsClone/Player.swift
T
2026-11-03 12:53:05 +00:00

278 lines
16 KiB
Swift

import SwiftUI
import AVFoundation
import CryptoKit
/// CDG follows consumed source frames, so tempo adjustment preserves graphics timing.
@MainActor final class Player: ObservableObject {
@Published var image: CGImage?
@Published private(set) var waveform = [Float]()
@Published private(set) var waveformBands = [WaveformBands]()
@Published var lowGain: Double = 0 { didSet { updateTone() } }
@Published var midGain: Double = 0 { didSet { updateTone() } }
@Published var highGain: Double = 0 { didSet { updateTone() } }
@Published var filter: Double = 0 { didSet { updateTone() } }
@Published var toneEnabled = true { didSet { updateTone() } }
@Published private(set) var leftLevel: Float = 0
@Published private(set) var rightLevel: Float = 0
private let tone = AVAudioUnitEQ(numberOfBands:4)
private let meter = AudioLevelMeter()
private var metering = false
@Published private(set) var waveformLoading = false
@Published private(set) var waveformUnavailable = false
@Published private(set) var waveformFromCache = false
@Published private(set) var waveformSaved = false
private let waveformCacheDirectory: URL
private var audioURL: URL?
private var waveformTask: Task<Void, Never>?
private var waveformGeneration = UUID()
@Published var playing = false
@Published var time: Double = 0
@Published var duration: Double = 0
@Published var title = "Select a song"
@Published var volume: Double = 0.8 { didSet { engine.mainMixerNode.outputVolume = Float(volume) } }
@Published var repeating = false
@Published var pitch: Double = 0 { didSet { effect.pitch = Float(pitch * 100) } }
@Published var tempo: Double = 1 { didSet { effect.rate = Float(tempo) } }
@Published var graphicsOffset: Double = 0
private(set) var currentSongID: Int64?
var onFinished: (() -> Void)?
private let engine = AVAudioEngine()
private let node = AVAudioPlayerNode()
private let effect = AVAudioUnitTimePitch()
private var file: AVAudioFile?
private var anchor: Double = 0
private var generation = UUID()
private var cdg = Data()
private var decoder = CDGDecoder()
private var timer: Timer?
init(waveformCacheDirectory: URL? = nil) {
self.waveformCacheDirectory = waveformCacheDirectory ?? FileManager.default.urls(for:.applicationSupportDirectory,in:.userDomainMask)[0].appendingPathComponent("KJamsClone/Waveforms")
engine.attach(node);engine.attach(effect);engine.attach(tone);updateTone()
}
func reanalyseWaveform() { if let audioURL { analyseWaveform(audioURL,force:true) } }
func resetTone() { lowGain = 0; midGain = 0; highGain = 0; filter = 0; toneEnabled = true }
private func updateTone() {
AudioTone.configure(tone,sampleRate:file?.processingFormat.sampleRate ?? 44100,low:lowGain,mid:midGain,high:highGain,filter:filter,enabled:toneEnabled)
}
func load(_ song: Song, preference: SongPreference = .init()) throws {
stop()
do {
let loaded = try AVAudioFile(forReading:URL(fileURLWithPath:song.audio))
let graphics = try song.cdg.map { try Data(contentsOf:URL(fileURLWithPath:$0)) } ?? Data()
guard loaded.length > 0 else { throw LibraryError.message("Audio file is empty") }
engine.connect(node,to:effect,format:loaded.processingFormat)
engine.connect(effect,to:tone,format:loaded.processingFormat)
engine.connect(tone,to:engine.mainMixerNode,format:loaded.processingFormat)
file = loaded; cdg = graphics; decoder = CDGDecoder()
duration = Double(loaded.length)/loaded.processingFormat.sampleRate
title = song.title; currentSongID = song.id
pitch = preference.pitch; tempo = preference.tempo
updateTone()
let meter = self.meter
engine.mainMixerNode.installTap(onBus:0,bufferSize:1024,format:nil) { buffer, _ in meter.consume(buffer) }
metering = true
engine.mainMixerNode.outputVolume = Float(volume)
engine.prepare(); try engine.start()
schedule(at:0,play:true); startTimer(); tick()
audioURL = URL(fileURLWithPath:song.audio)
analyseWaveform(audioURL!)
} catch { stop(); throw error }
}
private func analyseWaveform(_ url: URL, force: Bool = false) {
waveformTask?.cancel()
let token = UUID();waveformGeneration = token;waveformLoading = true;waveformUnavailable = false
let directory = waveformCacheDirectory
waveformTask = Task.detached(priority:.utility) { [weak self] in
let result: Result<AudioWaveform.Analysis,Error>
var cached = false,saved = false
do {
if !force,let analysis = try? WaveformCache.load(audio:url,directory:directory) {
result = .success(analysis);cached = true;saved = true
} else {
let analysis = try AudioWaveform.analyse(url:url,cancelled:{ Task.isCancelled })
if !Task.isCancelled { saved = (try? WaveformCache.save(analysis,audio:url,directory:directory)) != nil }
result = .success(analysis)
}
} catch { result = .failure(error) }
guard !Task.isCancelled else { return }
let loadedFromCache = cached,analysisSaved = saved
await MainActor.run { [weak self] in
guard let self,self.waveformGeneration == token else { return }
self.waveformLoading = false;self.waveformFromCache = loadedFromCache;self.waveformSaved = analysisSaved
switch result {
case .success(let analysis):self.waveform = analysis.levels;self.waveformBands = analysis.bands
case .failure:self.waveformUnavailable = true
}
}
}
}
private func schedule(at seconds: Double, play: Bool) {
guard let file else { return }
generation = UUID(); let token = generation
node.stop()
let rate = file.processingFormat.sampleRate
let start = min(file.length,max(0,AVAudioFramePosition(seconds*rate)))
anchor = Double(start)/rate; time = anchor
guard start < file.length else { playing = false; render(); return }
node.scheduleSegment(file,startingFrame:start,frameCount:AVAudioFrameCount(min(file.length-start,Int64(UInt32.max))),at:nil,completionCallbackType:.dataPlayedBack) { [weak self] _ in
Task { @MainActor in
guard let self, self.generation == token else { return }
if self.repeating { self.schedule(at:0,play:true) }
else {
self.playing = false; self.time = self.duration; self.render()
self.timer?.invalidate(); self.timer = nil; self.onFinished?()
}
}
}
if play { node.play() }; playing = play; render()
}
private func startTimer() {
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval:1.0/30,repeats:true) { [weak self] _ in Task { @MainActor in self?.tick() } }
}
func toggle() {
guard file != nil else { return }
if playing { tick(); node.pause(); playing = false }
else if time >= duration { schedule(at:0,play:true); startTimer() }
else { node.play(); playing = true; startTimer() }
}
func stop() {
waveformTask?.cancel(); waveformTask = nil; waveformGeneration = UUID()
waveform = [];waveformBands = [];waveformLoading = false;waveformUnavailable = false;waveformFromCache = false;waveformSaved = false;audioURL = nil
generation = UUID(); timer?.invalidate(); timer = nil; node.stop(); engine.stop()
if metering { engine.mainMixerNode.removeTap(onBus:0); metering = false }
meter.reset(); leftLevel = 0; rightLevel = 0
engine.disconnectNodeOutput(node); engine.disconnectNodeOutput(effect); engine.disconnectNodeOutput(tone)
file = nil; playing = false; time = 0; anchor = 0; duration = 0; image = nil; currentSongID = nil; title = "Select a song"
}
func seek(_ value: Double) {
guard file != nil, value.isFinite else { return }
schedule(at:min(max(value,0),duration),play:playing); startTimer()
}
private func render() { decoder.render(cdg,at:max(0,time+graphicsOffset)); image = cdg.isEmpty ? nil : decoder.image() }
private func tick() {
let levels = meter.snapshot()
leftLevel = playing ? levels.0 : 0; rightLevel = playing ? levels.1 : 0
if playing, let file, let renderTime = node.lastRenderTime, let playerTime = node.playerTime(forNodeTime:renderTime) {
time = min(duration,max(0,anchor+Double(playerTime.sampleTime)/file.processingFormat.sampleRate))
}
render()
}
}
/// Streams fixed-size PCM chunks into a bounded RMS envelope, independent of playback.
/// A separate AVAudioFile keeps analysis away from the player's scheduling/read position.
enum AudioWaveform {
struct Analysis: Codable, Sendable { var levels: [Float]; var bands: [WaveformBands] }
static func read(url: URL, bins: Int = 4096, cancelled: () -> Bool = { false }) throws -> [Float] {
try analyse(url:url,bins:bins,cancelled:cancelled).levels
}
static func analyse(url: URL, bins: Int = 4096, cancelled: () -> Bool = { false }) throws -> Analysis {
guard bins > 0 else { return .init(levels:[],bands:[]) }
let file = try AVAudioFile(forReading: url, commonFormat: .pcmFormatFloat32, interleaved: false)
guard file.length > 0, let buffer = AVAudioPCMBuffer(pcmFormat: file.processingFormat, frameCapacity: 16384) else { return .init(levels:[],bands:[]) }
var energy = [Double](repeating: 0, count: bins)
var counts = [Int](repeating: 0, count: bins)
var position: Int64 = 0
let channels = Int(file.processingFormat.channelCount)
let rate = file.processingFormat.sampleRate
let lowAlpha = exp(-2*Double.pi*250/rate), highAlpha = exp(-2*Double.pi*2500/rate)
var lowState = [Double](repeating:0,count:channels), highState = lowState
var lows = [Double](repeating:0,count:bins), mids = lows, highs = lows
while position < file.length {
if cancelled() { throw CancellationError() }
try file.read(into: buffer, frameCount: AVAudioFrameCount(min(16384, file.length-position)))
let frames = Int(buffer.frameLength)
guard frames > 0, let data = buffer.floatChannelData else { break }
for frame in 0..<frames {
let bin = min(bins-1, Int((position+Int64(frame))*Int64(bins)/file.length))
var sum: Double = 0
for channel in 0..<channels {
let sample = Double(data[channel][frame])
if sample.isFinite {
sum += sample * sample
lowState[channel] = lowAlpha*lowState[channel]+(1-lowAlpha)*sample
highState[channel] = highAlpha*highState[channel]+(1-highAlpha)*sample
let low = lowState[channel], mid = highState[channel]-low, high = sample-highState[channel]
lows[bin] += low*low; mids[bin] += mid*mid; highs[bin] += high*high
}
}
energy[bin] += sum / Double(max(channels,1)); counts[bin] += 1
}
position += Int64(frames)
}
let rms = zip(energy, counts).map { $1 > 0 ? sqrt($0/Double($1)) : 0 }
let peak = rms.max() ?? 0
let levels = peak > 0 ? rms.map { Float(pow(min(1,$0/peak),0.7)) } : [Float](repeating:0,count:bins)
let bands = (0..<bins).map { i in WaveformBands(low:Float(sqrt(lows[i])),mid:Float(sqrt(mids[i])),high:Float(sqrt(highs[i]))) }
return .init(levels:levels,bands:bands)
}
}
struct WaveformBands: Codable, Sendable {
var low: Float
var mid: Float
var high: Float
}
/// Four real audio filters; the filter knob is low-pass left, bypass at centre, high-pass right.
enum AudioTone {
static func configure(_ eq: AVAudioUnitEQ, sampleRate: Double, low: Double, mid: Double, high: Double, filter: Double, enabled: Bool) {
eq.bypass = !enabled
let frequencies = [160.0,1000.0,6000.0]
let types: [AVAudioUnitEQFilterType] = [.lowShelf,.parametric,.highShelf]
for i in 0..<3 {
let band = eq.bands[i];band.filterType = types[i];band.frequency = Float(min(frequencies[i],sampleRate*0.4))
band.bandwidth = 1;band.gain = Float(min(12,max(-12,[low,mid,high][i])));band.bypass = false
}
let value = min(1,max(-1,filter))
let band = eq.bands[3];band.bypass = abs(value) < 0.01;band.bandwidth = 0.7
band.filterType = value < 0 ? .lowPass : .highPass
let frequency = value < 0 ? 20000*pow(0.004,-value) : 20*pow(400,value)
band.frequency = Float(min(sampleRate*0.45,max(20,frequency)))
}
}
/// The audio callback only updates a tiny locked snapshot; UI reads it on the existing clock.
final class AudioLevelMeter: @unchecked Sendable {
private let lock = NSLock()
private var levels: (Float,Float) = (0,0)
func consume(_ buffer: AVAudioPCMBuffer) {
guard let data = buffer.floatChannelData else { return }
let channels = Int(buffer.format.channelCount), frames = Int(buffer.frameLength)
var left: Float = 0, right: Float = 0
for i in 0..<frames {
left = max(left,abs(data[0][i]));right = max(right,abs(data[min(1,channels-1)][i]))
}
lock.lock();levels = (left,right);lock.unlock()
}
func snapshot() -> (Float,Float) { lock.lock();defer { lock.unlock() };return levels }
func reset() { lock.lock();levels = (0,0);lock.unlock() }
}
/// Persistent analysis cache: file path, size, modification time and analyser version form its key.
/// Invalid or obsolete entries are treated as misses, never as playback errors.
enum WaveformCache {
static func location(audio: URL,directory: URL) throws -> URL {
let values = try FileManager.default.attributesOfItem(atPath:audio.path)
let stamp = (values[.modificationDate] as? Date)?.timeIntervalSince1970 ?? 0
let size = (values[.size] as? NSNumber)?.int64Value ?? 0
let signature = "rgb-rms-v2-4096|\(audio.standardizedFileURL.path)|\(size)|\(stamp)"
let key = SHA256.hash(data:Data(signature.utf8)).map { String(format:"%02x",$0) }.joined()
return directory.appendingPathComponent(key+".waveform")
}
static func load(audio: URL,directory: URL) throws -> AudioWaveform.Analysis {
let url = try location(audio:audio,directory:directory)
let data = try Data(contentsOf:url)
guard data.count < 4_000_000 else { throw LibraryError.message("Invalid waveform cache") }
let analysis = try PropertyListDecoder().decode(AudioWaveform.Analysis.self,from:data)
guard analysis.levels.count == 4096,analysis.bands.count == 4096,
analysis.levels.allSatisfy({ $0.isFinite && $0 >= 0 && $0 <= 1 }),
analysis.bands.allSatisfy({ $0.low.isFinite && $0.mid.isFinite && $0.high.isFinite && $0.low >= 0 && $0.mid >= 0 && $0.high >= 0 }) else { throw LibraryError.message("Invalid waveform cache") }
return analysis
}
static func save(_ analysis: AudioWaveform.Analysis,audio: URL,directory: URL) throws {
try FileManager.default.createDirectory(at:directory,withIntermediateDirectories:true)
let encoder = PropertyListEncoder();encoder.outputFormat = .binary
try encoder.encode(analysis).write(to:location(audio:audio,directory:directory),options:.atomic)
}
}