| @@ -1,7 +1,7 @@ | |||||
| package rds | package rds | ||||
| import ( | import ( | ||||
| "math" | |||||
| "math" | |||||
| ) | ) | ||||
| const ( | const ( | ||||
| @@ -29,6 +29,8 @@ func NewEncoder(cfg RDSConfig) (*Encoder, error) { | |||||
| if cfg.SampleRate <= 0 { | if cfg.SampleRate <= 0 { | ||||
| cfg.SampleRate = 48000 | cfg.SampleRate = 48000 | ||||
| } | } | ||||
| cfg.PS = normalizePS(cfg.PS) | |||||
| cfg.RT = normalizeRT(cfg.RT) | |||||
| bits := buildBits(cfg) | bits := buildBits(cfg) | ||||
| if len(bits) == 0 { | if len(bits) == 0 { | ||||
| @@ -2,6 +2,7 @@ package rds | |||||
| import ( | import ( | ||||
| "math" | "math" | ||||
| "strings" | |||||
| "testing" | "testing" | ||||
| ) | ) | ||||
| @@ -49,3 +50,18 @@ func TestEncoderReset(t *testing.T) { | |||||
| t.Fatalf("expected reset to replay initial sample: %v vs %v", sampleA, sampleB) | 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)) | |||||
| } | |||||
| } | |||||
| @@ -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 | |||||
| } | |||||