2022-02-22 18:30:48 +00:00
|
|
|
package audio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Sink interface {
|
|
|
|
Process(source Source)
|
|
|
|
}
|
|
|
|
|
|
|
|
type NullSink struct {
|
|
|
|
Sink
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNullSink() *NullSink {
|
|
|
|
return &NullSink{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NullSink) Process(source Source) {
|
2022-07-20 17:02:31 +00:00
|
|
|
if float32Source, ok := source.(*Float32Source); ok {
|
|
|
|
for range float32Source.GetBlocks() {
|
2022-02-22 18:30:48 +00:00
|
|
|
|
2022-07-20 17:02:31 +00:00
|
|
|
}
|
|
|
|
} else if int16Source, ok := source.(*Int16Source); ok {
|
|
|
|
for range int16Source.GetBlocks() {
|
|
|
|
|
|
|
|
}
|
|
|
|
} else if int32Source, ok := source.(*Int32Source); ok {
|
|
|
|
for range int32Source.GetBlocks() {
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for range source.ToFloat32().GetBlocks() {
|
|
|
|
|
|
|
|
}
|
2022-02-22 18:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ForwardSink struct {
|
|
|
|
Sink
|
|
|
|
Target Sink
|
|
|
|
SamplesRead int64
|
|
|
|
Duration time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewForwardSink(target Sink) *ForwardSink {
|
|
|
|
return &ForwardSink{
|
|
|
|
Target: target,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ForwardSink) GetDuration() time.Duration {
|
|
|
|
return time.Duration(atomic.LoadInt64((*int64)(&n.Duration)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ForwardSink) GetSamplesRead() int64 {
|
|
|
|
return atomic.LoadInt64(&n.SamplesRead)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ForwardSink) Process(source Source) {
|
2022-07-19 08:36:22 +00:00
|
|
|
processor := NewFloat32Source(source.GetSampleRate(), source.GetChannels())
|
2022-02-22 18:30:48 +00:00
|
|
|
go func() {
|
2022-07-19 08:36:22 +00:00
|
|
|
defer processor.Close()
|
|
|
|
for block := range source.ToFloat32().GetBlocks() {
|
|
|
|
atomic.AddInt64((*int64)(&n.Duration), int64((time.Second*time.Duration(len(block)/source.GetChannels()))/time.Duration(source.GetSampleRate())))
|
|
|
|
atomic.AddInt64(&n.SamplesRead, int64(len(block)/source.GetChannels()))
|
|
|
|
processor.IngestFloat32(block)
|
2022-02-22 18:30:48 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
n.Target.Process(processor)
|
|
|
|
}
|