Ver código fonte

feat: normalize RDS PS and radiotext fields

tags/v0.3.0-pre
Jan Svabenik 1 mês atrás
pai
commit
a67403af04
3 arquivos alterados com 40 adições e 1 exclusões
  1. +3
    -1
      internal/rds/encoder.go
  2. +16
    -0
      internal/rds/encoder_test.go
  3. +21
    -0
      internal/rds/normalize.go

+ 3
- 1
internal/rds/encoder.go Ver arquivo

@@ -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 {


+ 16
- 0
internal/rds/encoder_test.go Ver arquivo

@@ -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))
}
}

+ 21
- 0
internal/rds/normalize.go Ver arquivo

@@ -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
}

Carregando…
Cancelar
Salvar