CueSheetExporter.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import Foundation
  2. /// Exports a playlist as a standard CUE sheet (.cue).
  3. /// CUE sheets are widely supported by CD burning software, virtual CD drives,
  4. /// and many audio tools including Audacity, foobar2000, and various DAWs.
  5. struct CueSheetExporter: DAWExporter {
  6. static let formatID = "cue"
  7. static let formatName = "Cue Sheet"
  8. static let fileExtension = "cue"
  9. static func export(playlist: Playlist, to url: URL, options: ExportOptions) throws {
  10. let entries = playlist.sortedEntries
  11. var lines: [String] = []
  12. lines.append("REM Generated by MixBoard")
  13. lines.append("REM Date: \(ISO8601DateFormatter().string(from: Date()))")
  14. lines.append("TITLE \"\(playlist.name)\"")
  15. var timelinePosition: TimeInterval = 0
  16. for (index, entry) in entries.enumerated() {
  17. guard let track = entry.track,
  18. let fileURL = options.effectiveFileURL(for: track) else { continue }
  19. let trackNumber = String(format: "%02d", index + 1)
  20. let fileName = fileURL.lastPathComponent
  21. let relativePath = "\(options.audioFilesRelativePath)/\(fileName)"
  22. lines.append("FILE \"\(relativePath)\" WAVE")
  23. lines.append(" TRACK \(trackNumber) AUDIO")
  24. lines.append(" TITLE \"\(track.title)\"")
  25. if !track.artist.isEmpty {
  26. lines.append(" PERFORMER \"\(track.artist)\"")
  27. }
  28. // INDEX 01 is the start time in MM:SS:FF format (frames = 1/75 second)
  29. let indexTime = formatCueTime(timelinePosition)
  30. lines.append(" INDEX 01 \(indexTime)")
  31. // Add cue points as INDEX entries
  32. if options.includeCuePoints {
  33. for (cpIndex, cuePoint) in track.cuePoints.sorted().enumerated() {
  34. // INDEX 02+ for additional cue points
  35. let cpTime = formatCueTime(timelinePosition + cuePoint.timestamp - entry.startOffset)
  36. lines.append(" REM CUE \(cuePoint.name.isEmpty ? cuePoint.type.rawValue : cuePoint.name)")
  37. if cpIndex < 98 { // CUE format supports INDEX 00-99
  38. lines.append(" INDEX \(String(format: "%02d", cpIndex + 2)) \(cpTime)")
  39. }
  40. }
  41. }
  42. // Add BPM as a remark
  43. if let bpm = track.bpm {
  44. lines.append(" REM BPM \(String(format: "%.1f", bpm))")
  45. }
  46. if let key = track.musicalKey {
  47. lines.append(" REM KEY \(key)")
  48. }
  49. timelinePosition += entry.effectiveDuration
  50. if index + 1 < entries.count {
  51. timelinePosition -= entries[index + 1].crossfadeDuration
  52. }
  53. }
  54. let content = lines.joined(separator: "\n") + "\n"
  55. try content.write(to: url, atomically: true, encoding: .utf8)
  56. }
  57. // MARK: - CUE Time Format
  58. /// Convert seconds to CUE time format MM:SS:FF where FF = frames (1/75 second).
  59. private static func formatCueTime(_ seconds: TimeInterval) -> String {
  60. let totalSeconds = max(0, seconds)
  61. let minutes = Int(totalSeconds) / 60
  62. let secs = Int(totalSeconds) % 60
  63. let frames = Int((totalSeconds - Double(Int(totalSeconds))) * 75)
  64. return String(format: "%02d:%02d:%02d", minutes, secs, frames)
  65. }
  66. }