removed unwanted bar now with waveforms, major updates
This commit is contained in:
@@ -134,3 +134,57 @@ enum WaveformBatch {
|
||||
progress(state);return state
|
||||
}
|
||||
}
|
||||
|
||||
/// Admission control is read on workers; starting playback reduces future admissions.
|
||||
final class WaveformWorkload: @unchecked Sendable {
|
||||
static let shared = WaveformWorkload()
|
||||
private let lock = NSCondition()
|
||||
private var playing = Set<UUID>()
|
||||
private var active = 0
|
||||
private var allowed: Int { playing.isEmpty ? min(2,max(1,ProcessInfo.processInfo.activeProcessorCount/2)) : 1 }
|
||||
func setPlaying(_ active: Bool,id: UUID) { lock.lock();defer { lock.unlock() };if active { playing.insert(id) } else { playing.remove(id) };lock.broadcast() }
|
||||
var workers: Int { lock.lock();defer { lock.unlock() };return allowed }
|
||||
var playbackActive: Bool { lock.lock();defer { lock.unlock() };return !playing.isEmpty }
|
||||
func acquire(cancelled: () -> Bool) throws {
|
||||
lock.lock();defer { lock.unlock() }
|
||||
while active >= allowed {
|
||||
if cancelled() { throw CancellationError() }
|
||||
_ = lock.wait(until:Date().addingTimeInterval(0.02))
|
||||
}
|
||||
if cancelled() { throw CancellationError() };active += 1
|
||||
}
|
||||
func release() { lock.lock();active -= 1;lock.broadcast();lock.unlock() }
|
||||
}
|
||||
extension WaveformBatch {
|
||||
static func runConcurrent(songs: [Song],locations: [MediaLocation],cache: URL,force: Bool = false,limit: @escaping @Sendable () -> Int = { WaveformWorkload.shared.workers },progress: (WaveformJobProgress) -> Void = { _ in }) async -> WaveformJobProgress {
|
||||
var ids = Set<Int64>(),groups = [[Song]](),indices = [String:Int]()
|
||||
for song in songs where ids.insert(song.id).inserted {
|
||||
let key = URL(fileURLWithPath:song.audio).standardizedFileURL.path
|
||||
if let index = indices[key] { groups[index].append(song) }
|
||||
else { indices[key] = groups.count;groups.append([song]) }
|
||||
}
|
||||
var state = WaveformJobProgress(total:ids.count),next = 0,active = 0,lastReport = Date.distantPast
|
||||
await withTaskGroup(of:WaveformJobProgress.self) { group in
|
||||
while next < groups.count || active > 0 {
|
||||
if Task.isCancelled { group.cancelAll();state.cancelled = true }
|
||||
let allowed = min(2,max(1,limit()))
|
||||
while !Task.isCancelled,active < allowed,next < groups.count {
|
||||
let tracks = groups[next];next += 1;active += 1
|
||||
state.current = tracks.first?.title ?? "Preparing waveforms…"
|
||||
group.addTask(priority:.utility) {
|
||||
run(songs:tracks,locations:locations,cache:cache,force:force,cancelled:{Task.isCancelled})
|
||||
}
|
||||
}
|
||||
guard active > 0,let result = await group.next() else { break }
|
||||
active -= 1
|
||||
state.processed += result.processed;state.generated += result.generated;state.cached += result.cached
|
||||
state.skipped += result.skipped;state.issues += result.issues;state.cancelled = state.cancelled || result.cancelled
|
||||
state.messages.append(contentsOf:result.messages.prefix(max(0,100-state.messages.count)))
|
||||
if Date().timeIntervalSince(lastReport) >= 0.12 { progress(state);lastReport = Date() }
|
||||
}
|
||||
}
|
||||
state.finished = true
|
||||
state.current = state.cancelled ? "Cancelled · completed waveforms kept" : state.issues > 0 ? "Waveform analysis finished with issues" : "Waveforms ready"
|
||||
progress(state);return state
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user