go-opengl-pixel/audio/speaker/speaker.go

112 lines
2.4 KiB
Go
Raw Normal View History

2017-07-06 14:44:34 -05:00
package speaker
import (
"math"
"sync"
"time"
"github.com/faiface/pixel/audio"
"github.com/hajimehoshi/oto"
"github.com/pkg/errors"
)
var (
mu sync.Mutex
streamer audio.Streamer
samples [][2]float64
buf []byte
2017-07-06 14:44:34 -05:00
player *oto.Player
)
// Init initializes audio playback through speaker. Must be called before using this package. The
// value of audio.SampleRate must be set (or left to the default) before calling this function.
//
// 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.
2017-07-06 14:44:34 -05:00
func Init(bufferSize time.Duration) error {
mu.Lock()
defer mu.Unlock()
2017-07-06 14:44:34 -05:00
if player != nil {
panic("already called Init")
2017-07-06 14:44:34 -05:00
}
numSamples := int(math.Ceil(bufferSize.Seconds() * audio.SampleRate))
numBytes := numSamples * 4
var err error
player, err = oto.NewPlayer(int(audio.SampleRate), 2, 2, numBytes)
if err != nil {
return errors.Wrap(err, "failed to initialize speaker")
}
samples = make([][2]float64, numSamples)
buf = make([]byte, numBytes)
go func() {
for {
update()
}
}()
2017-07-06 14:44:34 -05:00
return nil
}
// Lock locks the speaker. While locked, speaker won't pull new data from the playing Stramers. Lock
// if you want to modify any currently playing Streamers to avoid race conditions.
func Lock() {
mu.Lock()
}
// Unlock unlocks the speaker. Call after modifying any currently playing Streamer.
func Unlock() {
mu.Unlock()
}
2017-07-06 14:44:34 -05:00
// Play starts playing the provided Streamer through the speaker.
func Play(s audio.Streamer) {
mu.Lock()
2017-07-06 14:44:34 -05:00
streamer = s
mu.Unlock()
2017-07-06 14:44:34 -05:00
}
// update pulls new data from the playing Streamers and sends it to the speaker. Blocks until the
2017-07-06 14:44:34 -05:00
// data is sent and started playing.
func update() {
2017-07-06 15:34:22 -05:00
// pull data from the streamer, if any
2017-07-06 14:44:34 -05:00
n := 0
if streamer != nil {
var ok bool
mu.Lock()
2017-07-06 14:44:34 -05:00
n, ok = streamer.Stream(samples)
mu.Unlock()
2017-07-06 14:44:34 -05:00
if !ok {
streamer = nil
}
}
// convert samples to bytes
for i := range samples[:n] {
for c := range samples[i] {
val := samples[i][c]
if val < -1 {
val = -1
}
if val > +1 {
val = +1
}
valInt16 := int16(val * (1<<15 - 1))
2017-07-06 14:44:34 -05:00
low := byte(valInt16 % (1 << 8))
high := byte(valInt16 / (1 << 8))
buf[i*4+c*2+0] = low
buf[i*4+c*2+1] = high
}
}
// fill the rest with silence
for i := n * 4; i < len(buf); i++ {
buf[i] = 0
}
2017-07-06 15:26:20 -05:00
// send data to speaker
2017-07-06 14:44:34 -05:00
player.Write(buf)
}