Bläddra i källkod

feat: add basic wav rate adaptation for offline and sim paths

tags/v0.4.0-pre
Jan Svabenik 1 månad sedan
förälder
incheckning
9c74eb2bd3
5 ändrade filer med 51 tillägg och 2 borttagningar
  1. +1
    -0
      docs/README.md
  2. +8
    -1
      internal/app/sim.go
  3. +28
    -0
      internal/audio/resample.go
  4. +13
    -0
      internal/audio/resample_test.go
  5. +1
    -1
      internal/offline/generator.go

+ 1
- 0
docs/README.md Visa fil

@@ -15,6 +15,7 @@
Current no-hardware sources:
- generated stereo tones via config
- 16-bit PCM WAV file input via `audio.inputPath`
- basic sample-rate adaptation for WAV sources into the composite generation path

### Tone configuration



+ 8
- 1
internal/app/sim.go Visa fil

@@ -52,5 +52,12 @@ func RunSimulatedTransmit(cfg cfgpkg.Config, outPath string, duration time.Durat
if err := backend.Flush(context.Background()); err != nil {
return "", err
}
return fmt.Sprintf("simulated transmit: backend=%s output=%s duration=%s", backend.Info().Name, outPath, duration), nil
return fmt.Sprintf("simulated transmit: backend=%s output=%s duration=%s input=%s", backend.Info().Name, outPath, duration, inputLabel(cfg)), nil
}

func inputLabel(cfg cfgpkg.Config) string {
if cfg.Audio.InputPath != "" {
return cfg.Audio.InputPath
}
return "tones"
}

+ 28
- 0
internal/audio/resample.go Visa fil

@@ -0,0 +1,28 @@
package audio

type ResampledSource struct {
src *WAVSource
ratio float64
position float64
}

func NewResampledSource(src *WAVSource, targetSampleRate float64) *ResampledSource {
ratio := 1.0
if src != nil && src.SampleRate > 0 && targetSampleRate > 0 {
ratio = float64(src.SampleRate) / targetSampleRate
}
return &ResampledSource{src: src, ratio: ratio}
}

func (s *ResampledSource) NextFrame() Frame {
if s.src == nil || len(s.src.frames) == 0 {
return NewFrame(0, 0)
}
idx := int(s.position) % len(s.src.frames)
frame := s.src.frames[idx]
s.position += s.ratio
for s.position >= float64(len(s.src.frames)) {
s.position -= float64(len(s.src.frames))
}
return frame
}

+ 13
- 0
internal/audio/resample_test.go Visa fil

@@ -0,0 +1,13 @@
package audio

import "testing"

func TestResampledSource(t *testing.T) {
src := &WAVSource{frames: []Frame{NewFrame(0.1, 0.1), NewFrame(0.2, 0.2)}, SampleRate: 48000}
rs := NewResampledSource(src, 96000)
a := rs.NextFrame()
b := rs.NextFrame()
if a == (Frame{}) || b == (Frame{}) {
t.Fatal("expected frames")
}
}

+ 1
- 1
internal/offline/generator.go Visa fil

@@ -31,7 +31,7 @@ func NewGenerator(cfg cfgpkg.Config) *Generator {
func (g *Generator) sourceFor(sampleRate float64) frameSource {
if g.cfg.Audio.InputPath != "" {
if src, err := audio.LoadWAVSource(g.cfg.Audio.InputPath); err == nil {
return src
return audio.NewResampledSource(src, sampleRate)
}
}
return audio.NewConfiguredToneSource(sampleRate, g.cfg.Audio.ToneLeftHz, g.cfg.Audio.ToneRightHz, g.cfg.Audio.ToneAmplitude)


Laddar…
Avbryt
Spara