149 lines
3 KiB
Go
149 lines
3 KiB
Go
//go:build cgo
|
|
|
|
package replaygain
|
|
|
|
import (
|
|
"fmt"
|
|
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
|
|
"git.gammaspectra.live/S.O.N.G/Kirika/audio/format/guess"
|
|
"git.gammaspectra.live/S.O.N.G/Kirika/test"
|
|
"os"
|
|
"path"
|
|
"testing"
|
|
)
|
|
|
|
func TestReplayGainNormalization24(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fp, err := os.Open(test.TestSingleSample24)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
defer fp.Close()
|
|
source, err := guess.GetDecoder("flac").Open(fp)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
source = NewNormalizationFilter(5).Process(source)
|
|
|
|
gain, peak, err := GetTrackReplayGain(source)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
expect := "-0.033242 dB, 0.627992"
|
|
result := fmt.Sprintf("%f dB, %f", gain, peak)
|
|
if result != expect {
|
|
t.Errorf("Wrong ReplayGain %s != %s", result, expect)
|
|
}
|
|
}
|
|
|
|
func TestReplayGain24(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fp, err := os.Open(test.TestSingleSample24)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
defer fp.Close()
|
|
source, err := guess.GetDecoder("flac").Open(fp)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
gain, peak, err := GetTrackReplayGain(source)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
expect := "-11.590850 dB, 1.000000"
|
|
result := fmt.Sprintf("%f dB, %f", gain, peak)
|
|
if result != expect {
|
|
t.Errorf("Wrong ReplayGain %s != %s", result, expect)
|
|
}
|
|
}
|
|
|
|
func TestReplayGain16(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fp, err := os.Open(test.TestSingleSample16)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
defer fp.Close()
|
|
source, err := guess.GetDecoder("flac").Open(fp)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
gain, peak, err := GetTrackReplayGain(source)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
expect := "-7.748202 dB, 0.942137"
|
|
result := fmt.Sprintf("%f dB, %f", gain, peak)
|
|
if result != expect {
|
|
t.Errorf("Wrong ReplayGain %s != %s", result, expect)
|
|
}
|
|
}
|
|
|
|
func TestAlbumReplayGain(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
location := test.TestSampleLocations[1]
|
|
entries, err := os.ReadDir(location)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
var sources []audio.Source
|
|
for _, f := range entries {
|
|
if path.Ext(f.Name()) == ".flac" {
|
|
fullPath := path.Join(location, f.Name())
|
|
|
|
fp, err := os.Open(fullPath)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
defer fp.Close()
|
|
decoders, err := guess.GetDecoders(fp, fullPath)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
source, err := guess.Open(fp, decoders)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
sources = append(sources, source)
|
|
}
|
|
}
|
|
|
|
albumGain, albumPeak, trackGains, trackPeaks, err := GetAlbumReplayGain(sources)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
expect := "-8.539780 dB, 0.970916, [-7.748202 -9.020742 -9.192557 -8.015611 -8.303189 -9.071835 -9.356090 -8.114885 -7.760444 -7.317174 -8.339798 -8.906274 -8.001073], [0.942137 0.970916 0.970916 0.970916 0.942106 0.970916 0.970916 0.965606 0.970916 0.970916 0.942137 0.970916 0.942137]"
|
|
result := fmt.Sprintf("%f dB, %f, %f, %f", albumGain, albumPeak, trackGains, trackPeaks)
|
|
if result != expect {
|
|
t.Errorf("Wrong ReplayGain %s != %s", result, expect)
|
|
}
|
|
}
|