1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-10 06:16:27 -05:00
OpenDiablo2/d2core/d2audio/ebiten/ebiten_sound_effect.go

53 lines
1.0 KiB
Go
Raw Normal View History

package ebiten
2019-10-26 20:09:33 -04:00
import (
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
2019-10-26 20:09:33 -04:00
"github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/wav"
2019-10-26 20:09:33 -04:00
)
type SoundEffect struct {
2019-10-26 20:09:33 -04:00
player *audio.Player
}
func CreateSoundEffect(sfx string, context *audio.Context, volume float64) *SoundEffect {
result := &SoundEffect{}
var soundFile string
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.LoadFile(soundFile)
if err != nil {
panic(err)
}
2019-10-26 20:09:33 -04:00
d, err := wav.Decode(context, audio.BytesReadSeekCloser(audioData))
if err != nil {
log.Fatal(err)
}
player, err := audio.NewPlayer(context, d)
if err != nil {
log.Fatal(err)
}
player.SetVolume(volume)
result.player = player
return result
}
func (v *SoundEffect) Play() {
2019-10-26 20:09:33 -04:00
v.player.Rewind()
v.player.Play()
}
2019-10-27 12:48:25 -04:00
func (v *SoundEffect) Stop() {
2019-10-27 12:48:25 -04:00
v.player.Pause()
}