Skip to content

Commit

Permalink
#12 Reading Replay Gain metadata from Cue sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-venugopal committed Aug 21, 2024
1 parent b5975ce commit b626f0c
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 22 deletions.
Binary file not shown.
8 changes: 8 additions & 0 deletions Source/Core/LibCue/LibCueMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ class LibCueMapper {
metadata.genre = cd.genre
metadata.date = cd.date

metadata.replayGain = .init(albumGain: cd.replayGain_albumGain,
albumPeak: cd.replayGain_albumPeak)

return metadata
}

Expand All @@ -89,6 +92,11 @@ class LibCueMapper {
metadata.genre = cd.genre
metadata.date = cd.date

metadata.replayGain = .init(trackGain: track.replayGain_trackGain,
trackPeak: track.replayGain_trackPeak,
albumGain: cd.replayGain_albumGain,
albumPeak: cd.replayGain_albumPeak)

metadata.artist = track.performer
metadata.title = track.title
metadata.composer = track.composer
Expand Down
33 changes: 28 additions & 5 deletions Source/Core/LibCue/Wrappers/LibCueCD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,27 @@ class LibCueCD {

let file: URL

let rems: [LibCueREM]
lazy var date: String? = rems.first(where: {$0.type == .date})?.value
let rems: [LibCueREMType: LibCueREM]

lazy var date: String? = rems[.date]?.value

lazy var replayGain_albumGain: Float? = {

if let gainStr = self.rems[.replayGain_albumGain]?.value.lowerCasedAndTrimmed().removingOccurrences(of: "db") {
return Float(gainStr)
}

return nil
}()

lazy var replayGain_albumPeak: Float? = {

if let gainStr = self.rems[.replayGain_albumPeak]?.value.trim() {
return Float(gainStr)
}

return nil
}()

let cdTexts: [LibCueCDText]
lazy var title: String? = cdTexts.first(where: {$0.pti == .title})?.value
Expand Down Expand Up @@ -67,12 +86,16 @@ class LibCueCD {

if let remPtr = cd_get_rem(cdPtr) {

self.rems = LibCueREMType.allCases.compactMap {remType in
LibCueREM(type: remType, pointer: remPtr)
var map: [LibCueREMType: LibCueREM] = [:]

LibCueREMType.allCases.forEach {remType in
map[remType] = LibCueREM(type: remType, pointer: remPtr)
}

self.rems = map

} else {
self.rems = []
self.rems = [:]
}
}
}
35 changes: 26 additions & 9 deletions Source/Core/LibCue/Wrappers/LibCueREM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@ class LibCueREM {

enum LibCueREMType: CaseIterable {

// REM_DATE, /* date of cd/track */
// REM_REPLAYGAIN_ALBUM_GAIN,
// REM_REPLAYGAIN_ALBUM_PEAK,
// REM_REPLAYGAIN_TRACK_GAIN,
// REM_REPLAYGAIN_TRACK_PEAK,
// REM_END /* terminating REM (for stepping through REMs) */

case date, end
case date, replayGain_albumGain, replayGain_albumPeak, replayGain_trackGain, replayGain_trackPeak, end

private static var mappings: [RemType: LibCueREMType] = [

REM_DATE: .date,
REM_REPLAYGAIN_ALBUM_GAIN: .replayGain_albumGain,
REM_REPLAYGAIN_ALBUM_PEAK: .replayGain_albumPeak,
REM_REPLAYGAIN_TRACK_GAIN: .replayGain_trackGain,
REM_REPLAYGAIN_TRACK_PEAK: .replayGain_trackPeak,
REM_END: .end
]

Expand All @@ -46,7 +43,27 @@ enum LibCueREMType: CaseIterable {
}

var remType: RemType {
return self == .date ? REM_DATE : REM_END

switch self {

case .date:
return REM_DATE

case .replayGain_albumGain:
return REM_REPLAYGAIN_ALBUM_GAIN

case .replayGain_albumPeak:
return REM_REPLAYGAIN_ALBUM_PEAK

case .replayGain_trackGain:
return REM_REPLAYGAIN_TRACK_GAIN

case .replayGain_trackPeak:
return REM_REPLAYGAIN_TRACK_PEAK

case .end:
return REM_END
}
}
}

Expand Down
32 changes: 27 additions & 5 deletions Source/Core/LibCue/Wrappers/LibCueTrack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,26 @@ class LibCueTrack {
let preGap: Double?
let postGap: Double?

let rems: [LibCueREM]
lazy var date: String? = rems.first(where: {$0.type == .date})?.value
let rems: [LibCueREMType: LibCueREM]
lazy var date: String? = rems[.date]?.value

lazy var replayGain_trackGain: Float? = {

if let gainStr = self.rems[.replayGain_trackGain]?.value.lowerCasedAndTrimmed().removingOccurrences(of: "db") {
return Float(gainStr)
}

return nil
}()

lazy var replayGain_trackPeak: Float? = {

if let gainStr = self.rems[.replayGain_trackPeak]?.value.trim() {
return Float(gainStr)
}

return nil
}()

let cdTexts: [LibCueCDText]
lazy var title: String? = cdTexts.first(where: {$0.pti == .title})?.value
Expand Down Expand Up @@ -80,12 +98,16 @@ class LibCueTrack {

if let remPtr = track_get_rem(pointer) {

self.rems = LibCueREMType.allCases.compactMap {remType in
LibCueREM(type: remType, pointer: remPtr)
var map: [LibCueREMType: LibCueREM] = [:]

LibCueREMType.allCases.forEach {remType in
map[remType] = LibCueREM(type: remType, pointer: remPtr)
}

self.rems = map

} else {
self.rems = []
self.rems = [:]
}
}
}
4 changes: 3 additions & 1 deletion Source/Core/Playlist/ImportExport/FileSystemPlaylist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class CueSheetMetadata: Codable {
var date: String?
var discID: String?
var comment: String?


var replayGain: ReplayGain?

// Track-specific info

var title: String?
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/TrackIO/Model/ReplayGain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import Foundation

struct ReplayGain {
struct ReplayGain: Codable {

let trackGain: Float?
let trackPeak: Float?
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/TrackIO/Model/Track.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class Track: Hashable, PlaylistItem, PlayableItem {
auxiliaryMetadata[theKey] = MetadataEntry(format: .other, key: theKey, value: value)
}

self.replayGain = metadata.replayGain
self.replayGain = metadata.replayGain ?? cueSheetMetadata?.replayGain
}

private func correctChapterTimes() {
Expand Down

0 comments on commit b626f0c

Please sign in to comment.