92 lines
4.6 KiB
Swift
92 lines
4.6 KiB
Swift
import SwiftUI
|
|
import AVFoundation
|
|
|
|
/// CDG follows consumed source frames, so tempo adjustment preserves graphics timing.
|
|
@MainActor final class Player: ObservableObject {
|
|
@Published var image: CGImage?
|
|
@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() { engine.attach(node); engine.attach(effect) }
|
|
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: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
|
|
engine.mainMixerNode.outputVolume = Float(volume)
|
|
engine.prepare(); try engine.start()
|
|
schedule(at:0,play:true); startTimer(); tick()
|
|
} catch { stop(); throw error }
|
|
}
|
|
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() {
|
|
generation = UUID(); timer?.invalidate(); timer = nil; node.stop(); engine.stop()
|
|
engine.disconnectNodeOutput(node); engine.disconnectNodeOutput(effect)
|
|
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() {
|
|
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()
|
|
}
|
|
}
|