get rid of global SampleRate - remove time tracking from Ctrl and Take now takes number of samples

This commit is contained in:
faiface
2017-07-22 17:36:49 +02:00
parent ef04af564f
commit 69dc0a97fa
4 changed files with 9 additions and 35 deletions

View File

@@ -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,
}
}

18
ctrl.go
View File

@@ -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.

View File

@@ -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.

View File

@@ -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")
}