latest updates
This commit is contained in:
@@ -79,3 +79,58 @@ enum LibraryScanner {
|
||||
progress(state);return state
|
||||
}
|
||||
}
|
||||
|
||||
struct WaveformJobProgress: Sendable {
|
||||
var total: Int
|
||||
var processed = 0
|
||||
var generated = 0
|
||||
var cached = 0
|
||||
var skipped = 0
|
||||
var issues = 0
|
||||
var current = "Preparing waveforms…"
|
||||
var messages = [String]()
|
||||
var cancelled = false
|
||||
var finished = false
|
||||
}
|
||||
/// Prepares existing library media without importing, playing or modifying track metadata.
|
||||
enum WaveformBatch {
|
||||
static func run(songs: [Song],locations: [MediaLocation],cache: URL,force: Bool = false,cancelled: () -> Bool = { false },progress: (WaveformJobProgress) -> Void = { _ in }) -> WaveformJobProgress {
|
||||
var accessed = [URL]()
|
||||
for location in locations {
|
||||
var url = URL(fileURLWithPath:location.path)
|
||||
if let bookmark = location.bookmark { var stale = false;if let resolved = try? URL(resolvingBookmarkData:bookmark,options:[.withSecurityScope,.withoutUI],relativeTo:nil,bookmarkDataIsStale:&stale) { url = resolved } }
|
||||
if url.startAccessingSecurityScopedResource() { accessed.append(url) }
|
||||
}
|
||||
defer { for url in accessed { url.stopAccessingSecurityScopedResource() } }
|
||||
var ids = Set<Int64>(),analysedPaths = Set<String>()
|
||||
let tracks = songs.filter{ids.insert($0.id).inserted}
|
||||
var state = WaveformJobProgress(total:tracks.count),lastReport = Date.distantPast
|
||||
for song in tracks {
|
||||
if cancelled() { state.cancelled = true;break }
|
||||
state.current = song.title
|
||||
if song.audio.hasPrefix("showscreen:") { state.skipped += 1;state.processed += 1;continue }
|
||||
let url = URL(fileURLWithPath:song.audio),path = url.standardizedFileURL.path
|
||||
let scoped = url.startAccessingSecurityScopedResource()
|
||||
do {
|
||||
if (!force || analysedPaths.contains(path)),(try? WaveformCache.load(audio:url,directory:cache)) != nil { state.cached += 1 }
|
||||
else {
|
||||
let value = try AudioWaveform.analyse(url:url,cancelled:cancelled)
|
||||
guard value.levels.count == 4096,value.bands.count == 4096 else { throw LibraryError.message("No audio samples available for waveform analysis") }
|
||||
if cancelled() { throw CancellationError() }
|
||||
try WaveformCache.save(value,audio:url,directory:cache);state.generated += 1
|
||||
}
|
||||
analysedPaths.insert(path)
|
||||
} catch is CancellationError {
|
||||
if scoped { url.stopAccessingSecurityScopedResource() };state.cancelled = true;break
|
||||
} catch {
|
||||
state.issues += 1
|
||||
if state.messages.count < 100 { state.messages.append(song.title+": "+error.localizedDescription) }
|
||||
}
|
||||
if scoped { url.stopAccessingSecurityScopedResource() }
|
||||
state.processed += 1
|
||||
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