2019-11-10 03:36:53 -05:00
|
|
|
package d2audio
|
2019-10-26 20:09:33 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2data/d2datadict"
|
2019-12-21 20:53:18 -05:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func CreateSoundEffect(sfx string, context *audio.Context, volume float64) *SoundEffect {
|
2019-10-26 20:09:33 -04:00
|
|
|
result := &SoundEffect{}
|
2019-11-01 00:21:39 -04:00
|
|
|
var soundFile string
|
2019-11-10 08:51:02 -05:00
|
|
|
if _, exists := d2datadict.Sounds[sfx]; exists {
|
|
|
|
soundEntry := d2datadict.Sounds[sfx]
|
2019-11-01 00:21:39 -04:00
|
|
|
soundFile = soundEntry.FileName
|
|
|
|
} else {
|
|
|
|
soundFile = sfx
|
|
|
|
}
|
2019-12-21 20:53:18 -05:00
|
|
|
audioData := d2asset.MustLoadFile(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()
|
|
|
|
}
|