-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replay gain (normalize volume)
- Loading branch information
Showing
15 changed files
with
240 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import 'dart:math' as math; | ||
|
||
class ReplayGainData { | ||
final double? trackGain, albumGain; | ||
final double? trackPeak, albumPeak; | ||
const ReplayGainData({ | ||
required this.trackGain, | ||
required this.trackPeak, | ||
required this.albumGain, | ||
required this.albumPeak, | ||
}); | ||
|
||
double? calculateGainAsVolume({double withRespectiveVolume = 0.75}) { | ||
final gainFinal = trackGain ?? albumGain; | ||
if (gainFinal == null) return null; | ||
final gainLinear = math.pow(10, gainFinal / 20).clamp(0.1, 1.0); | ||
return gainLinear * withRespectiveVolume; | ||
} | ||
|
||
static ReplayGainData? fromAndroidMap(Map map) { | ||
double? trackGainDB = ((map['replaygain_track_gain'] ?? map['REPLAYGAIN_TRACK_GAIN']) as String?)?._parseGainValue(); // "-0.515000 dB" | ||
double? albumGainDB = ((map['replaygain_album_gain'] ?? map['REPLAYGAIN_ALBUM_GAIN']) as String?)?._parseGainValue(); // "+0.040000 dB" | ||
|
||
trackGainDB ??= ((map['r128_track_gain'] ?? map['R128_TRACK_GAIN']) as String?)?._parseGainValueR128(); | ||
albumGainDB ??= ((map['r128_album_gain'] ?? map['R128_ALBUM_GAIN']) as String?)?._parseGainValueR128(); | ||
|
||
final trackPeak = ((map['replaygain_track_peak'] ?? map['REPLAYGAIN_TRACK_PEAK']) as String?)?._parsePeakValue(); | ||
final albumPeak = ((map['replaygain_album_peak'] ?? map['REPLAYGAIN_ALBUM_PEAK']) as String?)?._parsePeakValue(); | ||
|
||
final data = ReplayGainData( | ||
trackGain: trackGainDB, | ||
trackPeak: trackPeak, | ||
albumGain: albumGainDB, | ||
albumPeak: albumPeak, | ||
); | ||
if (data.trackGain == null && data.trackPeak == null && data.albumGain == null && data.albumPeak == null) return null; | ||
return data; | ||
} | ||
|
||
factory ReplayGainData.fromMap(Map<String, dynamic> map) { | ||
return ReplayGainData( | ||
trackGain: map['tg'], | ||
trackPeak: map['tp'], | ||
albumGain: map['ag'], | ||
albumPeak: map['ap'], | ||
); | ||
} | ||
|
||
Map<String, dynamic> toMap() { | ||
return <String, dynamic>{ | ||
"tg": trackGain, | ||
"tp": trackPeak, | ||
"ag": albumGain, | ||
"ap": albumPeak, | ||
}; | ||
} | ||
} | ||
|
||
extension _GainParser on String? { | ||
double? _parseGainValueR128() { | ||
final parsed = _parseGainValue(); | ||
return parsed == null ? null : (parsed / 256) + 5; | ||
} | ||
|
||
double? _parseGainValue() { | ||
var text = this; | ||
return text == null ? null : double.tryParse(text.replaceFirst(RegExp(r'[^\d.-]'), '')) ?? double.tryParse(text.split(' ').first); | ||
} | ||
|
||
double? _parsePeakValue() { | ||
var text = this; | ||
return text == null ? null : double.tryParse(text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.