2019-10-26 20:09:33 -04:00
|
|
|
package Sound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/audio/wav"
|
|
|
|
|
2019-11-02 17:38:39 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/Common"
|
2019-10-26 20:09:33 -04:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/audio"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SoundEffect struct {
|
|
|
|
player *audio.Player
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateSoundEffect(sfx string, fileProvider Common.FileProvider, context *audio.Context, volume float64) *SoundEffect {
|
|
|
|
result := &SoundEffect{}
|
2019-11-01 00:21:39 -04:00
|
|
|
var soundFile string
|
|
|
|
if _, exists := Common.Sounds[sfx]; exists {
|
|
|
|
soundEntry := Common.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()
|
|
|
|
}
|