2020-01-31 23:18:11 -05:00
|
|
|
package ebiten
|
2019-10-26 20:09:33 -04:00
|
|
|
|
|
|
|
import (
|
2020-07-30 21:50:27 -04:00
|
|
|
"math"
|
2019-10-26 20:09:33 -04:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/audio"
|
|
|
|
)
|
|
|
|
|
2020-07-30 21:50:27 -04:00
|
|
|
type panStream struct {
|
|
|
|
audio.ReadSeekCloser
|
|
|
|
pan float64 // -1: left; 0: center; 1: right
|
|
|
|
}
|
|
|
|
|
2020-08-05 01:17:25 -04:00
|
|
|
const (
|
|
|
|
bitsPerByte = 8
|
|
|
|
)
|
|
|
|
|
|
|
|
func newPanStreamFromReader(src audio.ReadSeekCloser) *panStream {
|
2020-07-30 21:50:27 -04:00
|
|
|
return &panStream{
|
|
|
|
ReadSeekCloser: src,
|
|
|
|
pan: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *panStream) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = s.ReadSeekCloser.Read(p)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ls := math.Min(s.pan*-1+1, 1)
|
|
|
|
rs := math.Min(s.pan+1, 1)
|
|
|
|
|
|
|
|
for i := 0; i < len(p); i += 4 {
|
2020-08-05 01:17:25 -04:00
|
|
|
lc := int16(float64(int16(p[i])|int16(p[i+1])<<bitsPerByte) * ls)
|
|
|
|
rc := int16(float64(int16(p[i+2])|int16(p[i+3])<<bitsPerByte) * rs)
|
2020-07-30 21:50:27 -04:00
|
|
|
|
|
|
|
p[i] = byte(lc)
|
2020-08-05 01:17:25 -04:00
|
|
|
p[i+1] = byte(lc >> bitsPerByte)
|
2020-07-30 21:50:27 -04:00
|
|
|
p[i+2] = byte(rc)
|
2020-08-05 01:17:25 -04:00
|
|
|
p[i+3] = byte(rc >> bitsPerByte)
|
2020-07-30 21:50:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
// SoundEffect represents an ebiten implementation of a sound effect
|
2020-02-01 21:51:49 -05:00
|
|
|
type SoundEffect struct {
|
2020-07-30 16:17:26 -04:00
|
|
|
player *audio.Player
|
|
|
|
volumeScale float64
|
2020-07-30 21:50:27 -04:00
|
|
|
panStream *panStream
|
2019-10-26 20:09:33 -04:00
|
|
|
}
|
|
|
|
|
2020-08-05 01:17:25 -04:00
|
|
|
// SetPan sets the audio pan, left is -1.0, center is 0.0, right is 1.0
|
2020-07-30 21:50:27 -04:00
|
|
|
func (v *SoundEffect) SetPan(pan float64) {
|
|
|
|
v.panStream.pan = pan
|
|
|
|
}
|
|
|
|
|
2020-08-05 01:17:25 -04:00
|
|
|
// SetVolume ets the volume
|
2020-07-30 16:17:26 -04:00
|
|
|
func (v *SoundEffect) SetVolume(volume float64) {
|
|
|
|
v.player.SetVolume(volume * v.volumeScale)
|
|
|
|
}
|
|
|
|
|
2020-08-05 01:17:25 -04:00
|
|
|
// IsPlaying returns a bool for whether or not the sound is currently playing
|
2020-07-30 16:17:26 -04:00
|
|
|
func (v *SoundEffect) IsPlaying() bool {
|
|
|
|
return v.player.IsPlaying()
|
|
|
|
}
|
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
// Play plays the sound effect
|
2020-02-01 21:51:49 -05:00
|
|
|
func (v *SoundEffect) Play() {
|
2020-06-30 09:58:53 -04:00
|
|
|
err := v.player.Rewind()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v.player.Play()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-10-26 20:09:33 -04:00
|
|
|
}
|
2019-10-27 12:48:25 -04:00
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
// Stop stops the sound effect
|
2020-02-01 21:51:49 -05:00
|
|
|
func (v *SoundEffect) Stop() {
|
2020-06-30 09:58:53 -04:00
|
|
|
err := v.player.Pause()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-10-27 12:48:25 -04:00
|
|
|
}
|