Skip to content

Commit

Permalink
Deduplicate code
Browse files Browse the repository at this point in the history
uklotzde committed Jul 31, 2022
1 parent a607187 commit f377ded
Showing 2 changed files with 43 additions and 56 deletions.
90 changes: 38 additions & 52 deletions src/track/taglib/trackmetadata_common.cpp
Original file line number Diff line number Diff line change
@@ -14,13 +14,16 @@ namespace {
Logger kLogger("TagLib");

bool parseReplayGainGain(
ReplayGain* pReplayGain,
const QString& dbGain) {
DEBUG_ASSERT(pReplayGain);

bool isRatioValid = false;
double ratio = ReplayGain::ratioFromString(dbGain, &isRatioValid);
if (isRatioValid) {
gsl::not_null<ReplayGain*> pReplayGain,
const QString& dbGain,
bool resetIfEmpty) {
if (resetIfEmpty && dbGain.trimmed().isEmpty()) {
pReplayGain->resetRatio();
return true;
}
bool isValid = false;
double ratio = ReplayGain::ratioFromString(dbGain, &isValid);
if (isValid) {
// Some applications (e.g. Rapid Evolution 3) write a replay gain
// of 0 dB even if the replay gain is undefined. To be safe we
// ignore this special value and instead prefer to recalculate
@@ -32,20 +35,23 @@ bool parseReplayGainGain(
}
pReplayGain->setRatio(ratio);
}
return isRatioValid;
return isValid;
}

bool parseReplayGainPeak(
ReplayGain* pReplayGain,
const QString& strPeak) {
DEBUG_ASSERT(pReplayGain);

bool isPeakValid = false;
const CSAMPLE peak = ReplayGain::peakFromString(strPeak, &isPeakValid);
if (isPeakValid) {
gsl::not_null<ReplayGain*> pReplayGain,
const QString& strPeak,
bool resetIfEmpty) {
if (resetIfEmpty && strPeak.trimmed().isEmpty()) {
pReplayGain->resetPeak();
return true;
}
bool isValid = false;
const CSAMPLE peak = ReplayGain::peakFromString(strPeak, &isValid);
if (isValid) {
pReplayGain->setPeak(peak);
}
return isPeakValid;
return isValid;
}

} // anonymous namespace
@@ -101,60 +107,40 @@ bool parseBpm(
}

bool parseTrackGain(
TrackMetadata* pTrackMetadata,
gsl::not_null<TrackMetadata*> pTrackMetadata,
const QString& dbGain,
bool resetIfEmpty) {
DEBUG_ASSERT(pTrackMetadata);

ReplayGain* pReplayGain = pTrackMetadata->refTrackInfo().ptrReplayGain();
if (resetIfEmpty && dbGain.trimmed().isEmpty()) {
pReplayGain->resetRatio();
return true;
}
return parseReplayGainGain(pReplayGain, dbGain);
return parseReplayGainGain(pTrackMetadata->refTrackInfo().ptrReplayGain(),
dbGain,
resetIfEmpty);
}

bool parseTrackPeak(
TrackMetadata* pTrackMetadata,
gsl::not_null<TrackMetadata*> pTrackMetadata,
const QString& strPeak,
bool resetIfEmpty) {
DEBUG_ASSERT(pTrackMetadata);

ReplayGain* pReplayGain = pTrackMetadata->refTrackInfo().ptrReplayGain();
if (resetIfEmpty && strPeak.trimmed().isEmpty()) {
pReplayGain->resetPeak();
return true;
}
return parseReplayGainPeak(pReplayGain, strPeak);
return parseReplayGainPeak(pTrackMetadata->refTrackInfo().ptrReplayGain(),
strPeak,
resetIfEmpty);
}

#if defined(__EXTRA_METADATA__)
bool parseAlbumGain(
TrackMetadata* pTrackMetadata,
gsl::not_null<TrackMetadata*> pTrackMetadata,
const QString& dbGain,
bool resetIfEmpty) {
DEBUG_ASSERT(pTrackMetadata);

ReplayGain* pReplayGain = pTrackMetadata->refAlbumInfo().ptrReplayGain();
if (resetIfEmpty && dbGain.trimmed().isEmpty()) {
pReplayGain->resetRatio();
return true;
}
return parseReplayGainGain(pReplayGain, dbGain);
return parseReplayGainGain(pTrackMetadata->refAlbumInfo().ptrReplayGain(),
dbGain,
resetIfEmpty);
}

bool parseAlbumPeak(
TrackMetadata* pTrackMetadata,
gsl::not_null<TrackMetadata*> pTrackMetadata,
const QString& strPeak,
bool resetIfEmpty) {
DEBUG_ASSERT(pTrackMetadata);

ReplayGain* pReplayGain = pTrackMetadata->refAlbumInfo().ptrReplayGain();
if (resetIfEmpty && strPeak.trimmed().isEmpty()) {
pReplayGain->resetPeak();
return true;
}
return parseReplayGainPeak(pReplayGain, strPeak);
return parseReplayGainPeak(pTrackMetadata->refAlbumInfo().ptrReplayGain(),
strPeak,
resetIfEmpty);
}
#endif // __EXTRA_METADATA__

9 changes: 5 additions & 4 deletions src/track/taglib/trackmetadata_common.h
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
#include <QFlags>
#include <QImage>
#include <QString>
#include <gsl/pointers>

#if defined(__EXTRA_METADATA__)
#include "util/quuid.h"
@@ -91,7 +92,7 @@ inline QString formatTrackGain(
}

bool parseTrackGain(
TrackMetadata* pTrackMetadata,
gsl::not_null<TrackMetadata*> pTrackMetadata,
const QString& dbGain,
bool resetIfEmpty);

@@ -102,7 +103,7 @@ inline QString formatTrackPeak(
}

bool parseTrackPeak(
TrackMetadata* pTrackMetadata,
gsl::not_null<TrackMetadata*> pTrackMetadata,
const QString& strPeak,
bool resetIfEmpty);

@@ -113,7 +114,7 @@ inline QString formatAlbumGain(
}

bool parseAlbumGain(
TrackMetadata* pTrackMetadata,
gsl::not_null<TrackMetadata*> pTrackMetadata,
const QString& dbGain,
bool resetIfEmpty);

@@ -123,7 +124,7 @@ inline QString formatAlbumPeak(
}

bool parseAlbumPeak(
TrackMetadata* pTrackMetadata,
gsl::not_null<TrackMetadata*> pTrackMetadata,
const QString& strPeak,
bool resetIfEmpty);
#endif // __EXTRA_METADATA__

0 comments on commit f377ded

Please sign in to comment.