From 69dc0a97facbe9c6cf40f20afbbafe2f76d9ea45 Mon Sep 17 00:00:00 2001 From: faiface Date: Sat, 22 Jul 2017 17:36:49 +0200 Subject: [PATCH] get rid of global SampleRate - remove time tracking from Ctrl and Take now takes number of samples --- compositors.go | 11 +++-------- ctrl.go | 18 ++---------------- interface.go | 6 ------ speaker/speaker.go | 9 ++++----- 4 files changed, 9 insertions(+), 35 deletions(-) diff --git a/compositors.go b/compositors.go index 04a5de7..f67992d 100644 --- a/compositors.go +++ b/compositors.go @@ -1,18 +1,13 @@ package beep -import ( - "math" - "time" -) - -// Take returns a Streamer which streams s for at most d duration. +// Take returns a Streamer which streams at most n samples from s. // // The returned Streamer propagates s's errors throught Err. -func Take(d time.Duration, s Streamer) Streamer { +func Take(n int, s Streamer) Streamer { return &take{ s: s, currSample: 0, - numSamples: int(math.Ceil(d.Seconds() * float64(SampleRate))), + numSamples: n, } } diff --git a/ctrl.go b/ctrl.go index 0400d5b..aaae96e 100644 --- a/ctrl.go +++ b/ctrl.go @@ -1,8 +1,6 @@ package beep -import "time" - -// Ctrl allows for pausing and tracking a Streamer. +// Ctrl allows for pausing a Streamer. // // Wrap a Streamer in a Ctrl. // @@ -12,11 +10,6 @@ import "time" // // ctrl.Paused = true // -// And we can check how much has already been streamed. Position is not incremented when the Ctrl is -// paused. -// -// fmt.Println(ctrl.Position) -// // To completely stop a Ctrl before the wrapped Streamer is drained, just set the wrapped Streamer // to nil. // @@ -30,14 +23,9 @@ import "time" // speaker.Lock() // ctrl.Paused = true // speaker.Unlock() -// // ... -// speaker.Lock() -// fmt.Println(ctrl.Position) -// speaker.Unlock() type Ctrl struct { Streamer Streamer Paused bool - Position time.Duration } // Stream streams the wrapped Streamer, if not nil. If the Streamer is nil, Ctrl acts as drained. @@ -52,9 +40,7 @@ func (c *Ctrl) Stream(samples [][2]float64) (n int, ok bool) { } return len(samples), true } - n, ok = c.Streamer.Stream(samples) - c.Position += time.Duration(n) * time.Second / time.Duration(SampleRate) - return n, ok + return c.Streamer.Stream(samples) } // Err returns the error of the wrapped Streamer, if not nil. diff --git a/interface.go b/interface.go index ee648fe..18841de 100644 --- a/interface.go +++ b/interface.go @@ -2,12 +2,6 @@ package beep import "time" -// SampleRate is the number of audio samples a Streamer should produce per one second of audio. -// -// This value should be set at most once before using audio package. It is safe to assume that this -// value does not change during runtime. -var SampleRate = 48000 - // Streamer is able to stream a finite or infinite sequence of audio samples. type Streamer interface { // Stream copies at most len(samples) next audio samples to the samples slice. diff --git a/speaker/speaker.go b/speaker/speaker.go index 18356c0..a479a18 100644 --- a/speaker/speaker.go +++ b/speaker/speaker.go @@ -19,13 +19,12 @@ var ( done chan struct{} ) -// Init initializes audio playback through speaker. Must be called before using this package. The -// value of beep.SampleRate must be set (or left to the default) before calling this function. +// Init initializes audio playback through speaker. Must be called before using this package. // // The bufferSize argument specifies the length of the speaker's buffer. Bigger bufferSize means // lower CPU usage and more reliable playback. Lower bufferSize means better responsiveness and less // delay. -func Init(bufferSize time.Duration) error { +func Init(sampleRate int, bufferSize time.Duration) error { mu.Lock() defer mu.Unlock() @@ -36,14 +35,14 @@ func Init(bufferSize time.Duration) error { mixer = beep.Mixer{} - numSamples := int(math.Ceil(bufferSize.Seconds() * float64(beep.SampleRate))) + numSamples := int(math.Ceil(bufferSize.Seconds() * float64(sampleRate))) numBytes := numSamples * 4 samples = make([][2]float64, numSamples) buf = make([]byte, numBytes) var err error - player, err = oto.NewPlayer(int(beep.SampleRate), 2, 2, numBytes) + player, err = oto.NewPlayer(sampleRate, 2, 2, numBytes) if err != nil { return errors.Wrap(err, "failed to initialize speaker") }