2020-01-31 23:18:11 -05:00
|
|
|
package ebiten
|
2019-10-26 20:09:33 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/audio"
|
2020-01-31 23:18:11 -05:00
|
|
|
"github.com/hajimehoshi/ebiten/audio/wav"
|
2020-07-23 12:56:50 -04:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2019-10-26 20:09:33 -04:00
|
|
|
)
|
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
// SoundEffect represents an ebiten implementation of a sound effect
|
2020-02-01 21:51:49 -05:00
|
|
|
type SoundEffect struct {
|
2020-07-30 16:17:26 -04:00
|
|
|
player *audio.Player
|
|
|
|
volumeScale float64
|
2019-10-26 20:09:33 -04:00
|
|
|
}
|
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
// CreateSoundEffect creates a new instance of ebiten's sound effect implementation.
|
2020-07-30 16:17:26 -04:00
|
|
|
func CreateSoundEffect(sfx string, context *audio.Context, volume float64, loop bool) *SoundEffect {
|
2020-02-01 21:51:49 -05:00
|
|
|
result := &SoundEffect{}
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-07-30 16:17:26 -04:00
|
|
|
soundFile := "/data/global/sfx/"
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2019-11-10 08:51:02 -05:00
|
|
|
if _, exists := d2datadict.Sounds[sfx]; exists {
|
|
|
|
soundEntry := d2datadict.Sounds[sfx]
|
2020-07-30 16:17:26 -04:00
|
|
|
soundFile += soundEntry.FileName
|
2019-11-01 00:21:39 -04:00
|
|
|
} else {
|
2020-07-30 16:17:26 -04:00
|
|
|
soundFile += sfx
|
2019-11-01 00:21:39 -04:00
|
|
|
}
|
2019-12-24 01:48:45 -05:00
|
|
|
|
2020-07-30 16:17:26 -04:00
|
|
|
audioData, err := d2asset.LoadFileStream(soundFile)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
audioData, err = d2asset.LoadFileStream("/data/global/music/" + sfx)
|
|
|
|
}
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-07-30 16:17:26 -04:00
|
|
|
d, err := wav.Decode(context, audioData)
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2019-10-26 20:09:33 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-07-30 16:17:26 -04:00
|
|
|
var player *audio.Player
|
|
|
|
|
|
|
|
if loop {
|
|
|
|
s := audio.NewInfiniteLoop(d, d.Length())
|
|
|
|
player, err = audio.NewPlayer(context, s)
|
|
|
|
} else {
|
|
|
|
player, err = audio.NewPlayer(context, d)
|
|
|
|
}
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2019-10-26 20:09:33 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-07-30 16:17:26 -04:00
|
|
|
result.volumeScale = volume
|
2019-10-26 20:09:33 -04:00
|
|
|
player.SetVolume(volume)
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2019-10-26 20:09:33 -04:00
|
|
|
result.player = player
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2019-10-26 20:09:33 -04:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-07-30 16:17:26 -04:00
|
|
|
func (v *SoundEffect) SetVolume(volume float64) {
|
|
|
|
v.player.SetVolume(volume * v.volumeScale)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *SoundEffect) IsPlaying() bool {
|
|
|
|
return v.player.IsPlaying()
|
|
|
|
}
|
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
// Play plays the sound effect
|
2020-02-01 21:51:49 -05:00
|
|
|
func (v *SoundEffect) Play() {
|
2020-06-30 09:58:53 -04:00
|
|
|
err := v.player.Rewind()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v.player.Play()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-10-26 20:09:33 -04:00
|
|
|
}
|
2019-10-27 12:48:25 -04:00
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
// Stop stops the sound effect
|
2020-02-01 21:51:49 -05:00
|
|
|
func (v *SoundEffect) Stop() {
|
2020-06-30 09:58:53 -04:00
|
|
|
err := v.player.Pause()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-10-27 12:48:25 -04:00
|
|
|
}
|