1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-09-16 00:08:29 -04:00
OpenDiablo2/d2audio/sound_effect.go

51 lines
1.0 KiB
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"
2019-11-10 03:36:53 -05:00
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
2019-11-10 08:51:02 -05:00
"github.com/OpenDiablo2/OpenDiablo2/d2data/d2datadict"
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
}
2019-11-10 03:36:53 -05:00
func CreateSoundEffect(sfx string, fileProvider d2interface.FileProvider, 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 := fileProvider.LoadFile(soundFile)
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()
}