Skip to content

Instantly share code, notes, and snippets.

@RomanHargrave
Created May 18, 2023 05:15
Show Gist options
  • Save RomanHargrave/3bf33ef40600c1fca3716122ff639a8d to your computer and use it in GitHub Desktop.
Save RomanHargrave/3bf33ef40600c1fca3716122ff639a8d to your computer and use it in GitHub Desktop.
#!/usr/bin/rakudo
use XML::XPath;
use TagLibC;
enum Format <MP3Gain>;
class Loudness {
has Real $.dbfs is readonly;
has Real $.db is readonly;
has Real $.amplitude is readonly;
}
class Measurement {
has Loudness $.integrated is readonly;
has Loudness $.momentary is readonly;
has Loudness $.shortterm-maximum is readonly;
has Loudness $.sample-peak is readonly;
has Loudness $.true-peak is readonly;
}
class Track {
has Measurement $.measurement is readonly;
has Str $.file is readonly;
}
sub lift_measurement(XML::Element $e) {
my $measurements = $e.nodes.map: -> $_ {
next unless $_ ~~ XML::Element;
next if .name eq 'track';
(.name, Loudness.new(|<dbfs db amplitude>.map(-> $n {$n => .attribs{$n} if .attribs{$n}}).Map>>.Real))
};
Measurement.new(|$measurements.Map)
}
sub MAIN(
Bool :o(:$terse) = True, #= Terse output
Str :s(:$skiptag) = 's', #= mp3gain compat, ignored
Int :d(:$target) = 0, #= Reference level, modify suggested gain by level, mp3gain compat, ignored
Bool :k(:$noclip) = True, #= mp3gain compat, ignored presently
Bool :c(:$nowarn) = True, #= mp3gain compat, ingored presently
Format :f(:$format) = MP3Gain, #= output format
*@files # Files to process
) {
my $bs1770 = run(|<bs1770gain --replaygain --track-tags --album-tags --suppress-progress --xml -imstp -->, |@files, :out);
my $report = XML::XPath.new(xml => $bs1770.out.slurp: :close);
my $album = lift_measurement($report.find('/bs1770gain'));
my $tracks = $report.find('/bs1770gain/track').Array.map: -> $_ {
Track.new(file => .attribs<file>, measurement => lift_measurement($_));
}
if ($format == MP3Gain) {
# peak = sample-peak.amplitude
#
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment