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 NewConfiguredToneSource(sampleRate, leftHz, rightHz, amplitude float64) *ToneSource {
    src := NewToneSource(sampleRate)
    src.LeftFreq = leftHz
    src.RightFreq = rightHz
    src.Amplitude = amplitude
    return src
}

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