Branchwell

PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / BundledSoundtrack.swift

1import Foundation
2
3enum BundledSoundtrack {
4 static let identifier = "pulsecanvas"
5
6 static var url: URL? {
7 let candidates = [
8 Bundle.module.url(forResource: "PulseCanvas", withExtension: "mp3", subdirectory: "NativeAssets/Audio"),
9 Bundle.module.url(forResource: "PulseCanvas", withExtension: "mp3", subdirectory: "Resources/Audio"),
10 URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
11 .appendingPathComponent("Sources/PulseCanvas/Resources/Audio/PulseCanvas.mp3")
12 ]
13 return candidates.compactMap { $0 }.first { FileManager.default.fileExists(atPath: $0.path) }
14 }
15
16 static func identifier(for soundtrack: URL?) -> String? {
17 guard let soundtrack, let bundled = url,
18 soundtrack.standardizedFileURL == bundled.standardizedFileURL else { return nil }
19 return identifier
20 }
21
22 /// A bundled track uses a stable project reference, independent of where
23 /// the application is installed on this or another Mac.
24 static func resolve(identifier: String?, externalURL: URL?) throws -> URL? {
25 guard let identifier else { return externalURL }
26 guard identifier == Self.identifier else { throw CocoaError(.fileReadCorruptFile) }
27 guard let url else { throw CocoaError(.fileReadNoSuchFile) }
28 return url
29 }
30}