1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-18 10:26:02 -05:00
OpenDiablo2/d2core/d2audio/ebiten/ebiten_sound_effect.go

142 lines
2.7 KiB
Go
Raw Normal View History

package ebiten
2019-10-26 20:09:33 -04:00
import (
"log"
"math"
2019-10-26 20:09:33 -04:00
"github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/wav"
2020-07-23 12:56:50 -04:00
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
2019-10-26 20:09:33 -04:00
)
type panStream struct {
audio.ReadSeekCloser
pan float64 // -1: left; 0: center; 1: right
}
func NewPanStreamFromReader(src audio.ReadSeekCloser) *panStream {
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 {
lc := int16(float64(int16(p[i])|int16(p[i+1])<<8) * ls)
rc := int16(float64(int16(p[i+2])|int16(p[i+3])<<8) * rs)
p[i] = byte(lc)
p[i+1] = byte(lc >> 8)
p[i+2] = byte(rc)
p[i+3] = byte(rc >> 8)
}
return
}
2020-06-30 09:58:53 -04:00
// SoundEffect represents an ebiten implementation of a sound effect
type SoundEffect struct {
player *audio.Player
volumeScale float64
panStream *panStream
2019-10-26 20:09:33 -04:00
}
2020-06-30 09:58:53 -04:00
// CreateSoundEffect creates a new instance of ebiten's sound effect implementation.
func CreateSoundEffect(sfx string, context *audio.Context, volume float64, loop bool) *SoundEffect {
result := &SoundEffect{}
2020-06-30 09:58:53 -04:00
soundFile := "/data/global/sfx/"
2020-06-30 09:58:53 -04:00
2019-11-10 08:51:02 -05:00
if _, exists := d2datadict.Sounds[sfx]; exists {
soundEntry := d2datadict.Sounds[sfx]
soundFile += soundEntry.FileName
} else {
soundFile += sfx
}
audioData, err := d2asset.LoadFileStream(soundFile)
if err != nil {
audioData, err = d2asset.LoadFileStream("/data/global/music/" + sfx)
}
2020-06-30 09:58:53 -04:00
if err != nil {
panic(err)
}
d, err := wav.Decode(context, audioData)
2020-06-30 09:58:53 -04:00
2019-10-26 20:09:33 -04:00
if err != nil {
log.Fatal(err)
}
var player *audio.Player
if loop {
s := audio.NewInfiniteLoop(d, d.Length())
result.panStream = NewPanStreamFromReader(s)
player, err = audio.NewPlayer(context, result.panStream)
} else {
result.panStream = NewPanStreamFromReader(d)
player, err = audio.NewPlayer(context, result.panStream)
}
2020-06-30 09:58:53 -04:00
2019-10-26 20:09:33 -04:00
if err != nil {
log.Fatal(err)
}
2020-06-30 09:58:53 -04:00
result.volumeScale = volume
2019-10-26 20:09:33 -04:00
player.SetVolume(volume)
2020-06-30 09:58:53 -04:00
2019-10-26 20:09:33 -04:00
result.player = player
2020-06-30 09:58:53 -04:00
2019-10-26 20:09:33 -04:00
return result
}
func (v *SoundEffect) SetPan(pan float64) {
v.panStream.pan = pan
}
func (v *SoundEffect) SetVolume(volume float64) {
v.player.SetVolume(volume * v.volumeScale)
}
func (v *SoundEffect) IsPlaying() bool {
return v.player.IsPlaying()
}
2020-06-30 09:58:53 -04:00
// Play plays the sound effect
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
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
}