Skip to content

Commit

Permalink
Album RG scanner op implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-venugopal committed Aug 24, 2024
1 parent e309186 commit 71bb926
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 33 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AVFReplayGainScanner: EBUR128LoudnessScannerProtocol {
private let sampleRate: Double
private let totalSamples: AVAudioFramePosition

private let ebur128: EBUR128State
let ebur128: EBUR128State

private(set) var isCancelled: Bool = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class ReplayGainAlbumScannerOperation: Operation {
private var scanners: [EBUR128LoudnessScannerProtocol] = []
private let completionHandler: (ReplayGainAlbumScannerOperation, EBUR128AlbumAnalysisResult?) -> Void

let eburs: ConcurrentArray<EBUR128State> = .init()
let results: ConcurrentArray<EBUR128TrackAnalysisResult> = .init()

// -------------------------------------------------------------------------------------------------------------------

// MARK: - NSOperation Overrides
Expand All @@ -36,11 +39,27 @@ class ReplayGainAlbumScannerOperation: Operation {
self.files = files
self.completionHandler = completionHandler

// self.scanner = file.isNativelySupported ?
// try AVFReplayGainScanner(file: file) :
// try FFmpegReplayGainScanner(file: file)

super.init()

for file in self.files {

addDependency(BlockOperation {

do {

let scanner: EBUR128LoudnessScannerProtocol = file.isNativelySupported ?
try AVFReplayGainScanner(file: file) :
try FFmpegReplayGainScanner(file: file)

self.eburs.append(scanner.ebur128)
let result = try scanner.scan()
self.results.append(result)

} catch {
NSLog("EBUR128 analysis of file '\(file.path)' failed. Error: \((error as? EBUR128Error)?.description ?? error.localizedDescription)")
}
})
}
}

override func start() {
Expand All @@ -53,19 +72,16 @@ class ReplayGainAlbumScannerOperation: Operation {
_isExecuting = true
didChangeValue(forKey: "isExecuting")

DispatchQueue.global(qos: .userInitiated).async {

var result: EBUR128AlbumAnalysisResult? = nil
var result: EBUR128AlbumAnalysisResult? = nil

do {
result = try EBUR128State.computeAlbumLoudnessAndPeak(with: eburs.array, andTrackResults: results.array)
} catch {

// do {
// result = try self.scanner.scan()
// } catch {
// NSLog("EBUR128 analysis of file '\(self.file.path)' failed. Error: \((error as? EBUR128Error)?.description ?? error.localizedDescription)")
// }

self.completionHandler(self, result)
self.finish()
}

self.completionHandler(self, result)
self.finish()
}

private func finish() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ protocol EBUR128LoudnessScannerProtocol {

var file: URL {get}

var ebur128: EBUR128State {get}

init(file: URL) throws

func scan() throws -> EBUR128TrackAnalysisResult
Expand All @@ -40,7 +42,7 @@ class ReplayGainScanner {
print("ReplayGainScanner.init() read \(self.cache.count) cache entries")
}

func scan(forFile file: URL, _ completionHandler: @escaping (ReplayGain?) -> Void) throws {
func scan(forFile file: URL, _ completionHandler: @escaping (ReplayGain?) -> Void) {

cancelOngoingScan()

Expand All @@ -54,7 +56,7 @@ class ReplayGainScanner {

// Cache miss, initiate a scan

scanOp = try ReplayGainTrackScannerOperation(file: file) {[weak self] finishedScanOp, ebur128Result in
scanOp = ReplayGainTrackScannerOperation(file: file) {[weak self] finishedScanOp, ebur128Result in

// A previously scheduled scan op may finish just before being cancelled. This check
// will prevent rogue completion handler execution.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,16 @@ class ReplayGainUnitDelegate: EffectsUnitDelegate<ReplayGainUnit>, ReplayGainUni

private func analyze(file: URL) {

do {
_isScanning.setTrue()
Messenger.publish(.Effects.ReplayGainUnit.scanInitiated)
_isScanning.setTrue()
Messenger.publish(.Effects.ReplayGainUnit.scanInitiated)

replayGainScanner.scan(forFile: file) {[weak self] (replayGain: ReplayGain?) in

try replayGainScanner.scan(forFile: file) {[weak self] (replayGain: ReplayGain?) in

guard let strongSelf = self else {return}

strongSelf.unit.replayGain = replayGain
strongSelf._isScanning.setFalse()

Messenger.publish(.Effects.ReplayGainUnit.scanCompleted)
}
guard let strongSelf = self else {return}

} catch {
strongSelf.unit.replayGain = replayGain
strongSelf._isScanning.setFalse()

_isScanning.setFalse()
Messenger.publish(.Effects.ReplayGainUnit.scanCompleted)
}
}
Expand Down

0 comments on commit 71bb926

Please sign in to comment.