1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-16 12:35:22 +00:00
OpenDiablo2/d2core/d2audio/ebiten/ebiten_sound_effect.go

53 lines
1.0 KiB
Go
Raw Normal View History

package ebiten
2019-10-27 00:09:33 +00:00
import (
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
2019-10-27 00:09:33 +00:00
"github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/wav"
2019-10-27 00:09:33 +00:00
)
type SoundEffect struct {
2019-10-27 00:09:33 +00:00
player *audio.Player
}
func CreateSoundEffect(sfx string, context *audio.Context, volume float64) *SoundEffect {
result := &SoundEffect{}
var soundFile string
2019-11-10 13:51:02 +00: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-27 00:09:33 +00: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-27 00:09:33 +00:00
v.player.Rewind()
v.player.Play()
}
2019-10-27 16:48:25 +00:00
func (v *SoundEffect) Stop() {
2019-10-27 16:48:25 +00:00
v.player.Pause()
}