From c142c3eb0af616f37546976d0f08df9d516a801e Mon Sep 17 00:00:00 2001 From: faiface Date: Mon, 3 Jul 2017 19:07:30 +0200 Subject: [PATCH] add StreamerFunc helper type --- audio/interface.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/audio/interface.go b/audio/interface.go index 395548c..d426beb 100644 --- a/audio/interface.go +++ b/audio/interface.go @@ -36,3 +36,22 @@ type Streamer interface { // following calls. Stream(samples [][2]float64) (n int, ok bool) } + +// StreamerFunc is a Streamer created by simply wrapping a streaming function (usually a closure, +// which encloses a time tracking variable). This sometimes simplifies creating new streamers. +// +// Example: +// +// noise := StreamerFunc(func(samples [][2]float64) (n int, ok bool) { +// for i := range samples { +// samples[i][0] = rand.Float64()*2 - 1 +// samples[i][1] = rand.Float64()*2 - 1 +// } +// return len(samples), true +// }) +type StreamerFunc func(samples [][2]float64) (n int, ok bool) + +// Stream calls the wrapped streaming function. +func (sf StreamerFunc) Stream(samples [][2]float64) (n int, ok bool) { + return sf(samples) +}