From 1581d6813da75add6086ecd090916ade2feefbf1 Mon Sep 17 00:00:00 2001 From: WeebDataHoarder <57538841+WeebDataHoarder@users.noreply.github.com> Date: Fri, 29 Jul 2022 12:57:31 +0200 Subject: [PATCH] removed Stream interface, can now use filters to get similar functionality --- audio/stream.go | 99 ------------------------------------------------- 1 file changed, 99 deletions(-) delete mode 100644 audio/stream.go diff --git a/audio/stream.go b/audio/stream.go deleted file mode 100644 index 0f1f33f..0000000 --- a/audio/stream.go +++ /dev/null @@ -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())) -}