1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-10-31 16:27:18 -04:00
OpenDiablo2/d2audio/sound_effect.go

55 lines
1022 B
Go
Raw Normal View History

2019-11-10 03:36:53 -05:00
package d2audio
2019-10-26 20:09:33 -04:00
import (
"log"
"github.com/OpenDiablo2/D2Shared/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2asset"
2019-10-26 20:09:33 -04:00
2019-11-10 03:36:53 -05:00
"github.com/hajimehoshi/ebiten/audio/wav"
2019-10-26 20:09:33 -04:00
"github.com/hajimehoshi/ebiten/audio"
)
type SoundEffect struct {
player *audio.Player
}
func CreateSoundEffect(sfx string, context *audio.Context, volume float64) *SoundEffect {
2019-10-26 20:09:33 -04:00
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() {
v.player.Rewind()
v.player.Play()
}
2019-10-27 12:48:25 -04:00
func (v *SoundEffect) Stop() {
v.player.Pause()
}