From a67403af0418e43ca1cdc3b6f2d4507b9174a9aa Mon Sep 17 00:00:00 2001 From: Jan Svabenik Date: Thu, 2 Apr 2026 22:45:09 +0200 Subject: [PATCH] feat: normalize RDS PS and radiotext fields --- internal/rds/encoder.go | 4 +++- internal/rds/encoder_test.go | 16 ++++++++++++++++ internal/rds/normalize.go | 21 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 internal/rds/normalize.go diff --git a/internal/rds/encoder.go b/internal/rds/encoder.go index 1a62279..854a746 100644 --- a/internal/rds/encoder.go +++ b/internal/rds/encoder.go @@ -1,7 +1,7 @@ package rds import ( - "math" + "math" ) const ( @@ -29,6 +29,8 @@ func NewEncoder(cfg RDSConfig) (*Encoder, error) { if cfg.SampleRate <= 0 { cfg.SampleRate = 48000 } + cfg.PS = normalizePS(cfg.PS) + cfg.RT = normalizeRT(cfg.RT) bits := buildBits(cfg) if len(bits) == 0 { diff --git a/internal/rds/encoder_test.go b/internal/rds/encoder_test.go index 964acee..0b5c8b2 100644 --- a/internal/rds/encoder_test.go +++ b/internal/rds/encoder_test.go @@ -2,6 +2,7 @@ package rds import ( "math" + "strings" "testing" ) @@ -49,3 +50,18 @@ func TestEncoderReset(t *testing.T) { t.Fatalf("expected reset to replay initial sample: %v vs %v", sampleA, sampleB) } } + +func TestNormalizePS(t *testing.T) { + got := normalizePS("radiox") + if got != "RADIOX " { + t.Fatalf("unexpected PS: %q", got) + } +} + +func TestNormalizeRT(t *testing.T) { + long := strings.Repeat("a", 80) + got := normalizeRT(long) + if len(got) != 64 { + t.Fatalf("unexpected RT length: %d", len(got)) + } +} diff --git a/internal/rds/normalize.go b/internal/rds/normalize.go new file mode 100644 index 0000000..1dfeb7d --- /dev/null +++ b/internal/rds/normalize.go @@ -0,0 +1,21 @@ +package rds + +import "strings" + +func normalizePS(ps string) string { + ps = strings.ToUpper(ps) + if len(ps) > 8 { + ps = ps[:8] + } + if len(ps) < 8 { + ps = ps + strings.Repeat(" ", 8-len(ps)) + } + return ps +} + +func normalizeRT(rt string) string { + if len(rt) > 64 { + rt = rt[:64] + } + return rt +}