DAWExporter.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import Foundation
  2. /// Protocol for all DAW export formats.
  3. protocol DAWExporter {
  4. /// Unique identifier for this export format.
  5. static var formatID: String { get }
  6. /// Display name for UI.
  7. static var formatName: String { get }
  8. /// File extension(s) for the export.
  9. static var fileExtension: String { get }
  10. /// Export a playlist to the specified URL.
  11. static func export(playlist: Playlist, to url: URL, options: ExportOptions) throws
  12. }
  13. /// Options for DAW export.
  14. struct ExportOptions {
  15. /// Whether to copy audio files alongside the session file.
  16. var copyAudioFiles: Bool = true
  17. /// Target sample rate for the session (nil = use original).
  18. var targetSampleRate: Double? = nil
  19. /// Target bit depth for the session.
  20. var targetBitDepth: Int = 24
  21. /// Include cue points as markers in the DAW session.
  22. var includeCuePoints: Bool = true
  23. /// Include crossfade information.
  24. var includeCrossfades: Bool = true
  25. /// Base directory for relative audio file paths.
  26. var audioFilesRelativePath: String = "Audio Files"
  27. /// Template for renaming copied files. Nil = keep original filenames.
  28. var fileNameTemplate: String? = nil
  29. /// Downloaded cloud track files, mapped by Track.id to local temp URL.
  30. /// Used by the export pipeline to include cloud tracks that were downloaded before export.
  31. var downloadedFiles: [UUID: URL] = [:]
  32. static let `default` = ExportOptions()
  33. /// Returns the effective file URL for a track, considering downloaded cloud files.
  34. func effectiveFileURL(for track: Track) -> URL? {
  35. if let downloaded = downloadedFiles[track.id] {
  36. return downloaded
  37. }
  38. guard track.hasLocalFile else { return nil }
  39. return track.fileURL
  40. }
  41. }
  42. /// Central exporter that dispatches to format-specific exporters.
  43. struct MixExporter {
  44. enum ExportFormat: String, CaseIterable, Identifiable {
  45. case audition = "audition"
  46. case cueSheet = "cue"
  47. case dawProject = "dawproject"
  48. case edl = "edl"
  49. case m3u = "m3u"
  50. var id: String { rawValue }
  51. var name: String {
  52. switch self {
  53. case .audition: return "Adobe Audition Session"
  54. case .cueSheet: return "Cue Sheet (.cue)"
  55. case .dawProject: return "DAWproject (Open Standard)"
  56. case .edl: return "Edit Decision List (EDL)"
  57. case .m3u: return "M3U Playlist"
  58. }
  59. }
  60. var fileExtension: String {
  61. switch self {
  62. case .audition: return "sesx"
  63. case .cueSheet: return "cue"
  64. case .dawProject: return "dawproject"
  65. case .edl: return "edl"
  66. case .m3u: return "m3u"
  67. }
  68. }
  69. var description: String {
  70. switch self {
  71. case .audition:
  72. return "Adobe Audition multitrack session with markers and crossfades"
  73. case .cueSheet:
  74. return "Standard cue sheet format, compatible with many audio tools"
  75. case .dawProject:
  76. return "Open DAW exchange format (Bitwig, PreSonus, REAPER)"
  77. case .edl:
  78. return "CMX 3600 Edit Decision List for professional DAWs"
  79. case .m3u:
  80. return "Simple playlist format for basic file list export"
  81. }
  82. }
  83. }
  84. /// Export a playlist in the specified format.
  85. static func export(
  86. playlist: Playlist,
  87. format: ExportFormat,
  88. to url: URL,
  89. options: ExportOptions = .default
  90. ) throws {
  91. // Wrap output in a parent folder named after the session file
  92. let baseName = url.deletingPathExtension().lastPathComponent
  93. let wrapperDir = url.deletingLastPathComponent().appendingPathComponent(baseName, isDirectory: true)
  94. try FileManager.default.createDirectory(at: wrapperDir, withIntermediateDirectories: true)
  95. let wrappedURL = wrapperDir.appendingPathComponent(url.lastPathComponent)
  96. // Copy audio files if requested
  97. if options.copyAudioFiles {
  98. let audioDir = wrapperDir
  99. .appendingPathComponent(options.audioFilesRelativePath)
  100. let entries = playlist.sortedEntries
  101. try copyAudioFiles(entries: entries, to: audioDir, options: options)
  102. }
  103. switch format {
  104. case .audition: try AuditionExporter.export(playlist: playlist, to: wrappedURL, options: options)
  105. case .cueSheet: try CueSheetExporter.export(playlist: playlist, to: wrappedURL, options: options)
  106. case .dawProject: try DAWProjectExporter.export(playlist: playlist, to: wrappedURL, options: options)
  107. case .edl: try EDLExporter.export(playlist: playlist, to: wrappedURL, options: options)
  108. case .m3u: try M3UExporter.export(playlist: playlist, to: wrappedURL, options: options)
  109. }
  110. }
  111. private static func copyAudioFiles(entries: [PlaylistEntry], to directory: URL, options: ExportOptions) throws {
  112. let fm = FileManager.default
  113. try fm.createDirectory(at: directory, withIntermediateDirectories: true)
  114. let totalTracks = entries.count
  115. for (index, entry) in entries.enumerated() {
  116. guard let track = entry.track,
  117. let source = options.effectiveFileURL(for: track) else { continue }
  118. let ext = source.pathExtension
  119. let destName: String
  120. if let template = options.fileNameTemplate {
  121. let baseName = FileNameTemplate.generate(
  122. template: template,
  123. track: track,
  124. playlistIndex: index,
  125. totalTracks: totalTracks
  126. )
  127. destName = "\(baseName).\(ext)"
  128. } else {
  129. destName = source.lastPathComponent
  130. }
  131. let dest = directory.appendingPathComponent(destName)
  132. if !fm.fileExists(atPath: dest.path) {
  133. try fm.copyItem(at: source, to: dest)
  134. }
  135. }
  136. }
  137. }