Selaa lähdekoodia

feat: add reusable tone source for offline pipeline

tags/v0.3.0-pre
Jan Svabenik 1 kuukausi sitten
vanhempi
commit
29955093e3
3 muutettua tiedostoa jossa 53 lisäystä ja 7 poistoa
  1. +37
    -0
      internal/audio/source.go
  2. +12
    -0
      internal/audio/source_test.go
  3. +4
    -7
      internal/offline/generator.go

+ 37
- 0
internal/audio/source.go Näytä tiedosto

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

import (
"math"
)

type ToneSource struct {
LeftFreq float64
RightFreq float64
SampleRate float64
LeftPhase float64
RightPhase float64
Amplitude float64
}

func NewToneSource(sampleRate float64) *ToneSource {
return &ToneSource{
LeftFreq: 1000,
RightFreq: 1600,
SampleRate: sampleRate,
Amplitude: 0.4,
}
}

func (s *ToneSource) NextFrame() Frame {
left := s.Amplitude * math.Sin(2*math.Pi*s.LeftPhase)
right := s.Amplitude * math.Sin(2*math.Pi*s.RightPhase)
s.LeftPhase += s.LeftFreq / s.SampleRate
s.RightPhase += s.RightFreq / s.SampleRate
if s.LeftPhase >= 1 {
s.LeftPhase -= math.Floor(s.LeftPhase)
}
if s.RightPhase >= 1 {
s.RightPhase -= math.Floor(s.RightPhase)
}
return NewFrame(Sample(left), Sample(right))
}

+ 12
- 0
internal/audio/source_test.go Näytä tiedosto

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

import "testing"

func TestToneSourceNextFrame(t *testing.T) {
src := NewToneSource(48000)
a := src.NextFrame()
b := src.NextFrame()
if a == b {
t.Fatal("expected varying frames")
}
}

+ 4
- 7
internal/offline/generator.go Näytä tiedosto

@@ -55,16 +55,13 @@ func (g *Generator) GenerateFrame(duration time.Duration) *output.CompositeFrame
})
rdsSamples := rdsEnc.Generate(samples)

leftFreq := 1000.0
rightFreq := 1600.0
stereoCarrierFreq := 38000.0
source := audio.NewToneSource(sampleRate)

for i := 0; i < samples; i++ {
t := float64(i) / sampleRate
left := audio.Sample(0.4 * math.Sin(2*math.Pi*leftFreq*t))
right := audio.Sample(0.4 * math.Sin(2*math.Pi*rightFreq*t+math.Pi/3))
comps := stereoEncoder.Encode(audio.NewFrame(left, right))
stereoDSB := comps.Stereo * math.Sin(2*math.Pi*stereoCarrierFreq*t)
in := source.NextFrame()
comps := stereoEncoder.Encode(in)
stereoDSB := comps.Stereo * math.Sin(2*math.Pi*38000.0*t)
rdsValue := 0.0
if g.cfg.RDS.Enabled && i < len(rdsSamples) {
rdsValue = rdsSamples[i]


Loading…
Peruuta
Tallenna