removed Stream interface, can now use filters to get similar functionality

This commit is contained in:
DataHoarder 2022-07-29 12:57:31 +02:00
parent 9ece89c3e6
commit 1581d6813d
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk

View file

@ -1,99 +0,0 @@
package audio
type Stream struct {
source Source
channel chan []float32
samplesProcessed int
buffer []float32
blockSize int
}
func NewStream(source Source, blockSize int) *Stream {
return &Stream{
source: source,
blockSize: blockSize,
}
}
func (s *Stream) GetChannels() int {
return s.source.GetChannels()
}
func (s *Stream) GetSampleRate() int {
return s.source.GetSampleRate()
}
func (s *Stream) GetSamplesProcessed() int {
return s.samplesProcessed
}
func (s *Stream) GetAsChannel() chan float32 {
newChannel := make(chan float32)
go func() {
defer close(newChannel)
for {
v, more := s.Get()
if !more {
return
}
newChannel <- v
s.samplesProcessed++
}
}()
return newChannel
}
func (s *Stream) GetAsBlockChannel() chan []float32 {
newChannel := make(chan []float32)
go func() {
defer close(newChannel)
for buf := range s.source.ToFloat32().GetBlocks() {
s.buffer = append(s.buffer, buf...)
for len(s.buffer) >= s.blockSize*s.source.GetChannels() {
newChannel <- s.buffer[0 : s.blockSize*s.source.GetChannels()]
s.samplesProcessed += s.blockSize * s.source.GetChannels()
s.buffer = s.buffer[s.blockSize*s.source.GetChannels():]
}
}
if len(s.buffer) > 0 {
newChannel <- s.buffer
s.samplesProcessed += len(s.buffer)
}
s.buffer = nil
}()
return newChannel
}
func (s *Stream) Get() (float32, bool) {
if s.channel == nil {
s.channel = s.source.ToFloat32().GetBlocks()
}
var more bool
if len(s.buffer) == 0 {
s.buffer, more = <-s.channel
if !more {
return 0, false
}
}
f := s.buffer[0]
s.buffer = s.buffer[1:]
s.samplesProcessed++
return f, true
}
func (s *Stream) AdvanceSeconds(seconds float64) bool {
stopAt := s.secondsIndex(seconds)
for i := 0; i < stopAt; i++ {
_, more := s.Get()
if !more {
return false
}
}
return true
}
func (s *Stream) secondsIndex(seconds float64) int {
return int(seconds * float64(s.source.GetSampleRate()))
}