2019-11-10 03:36:53 -05:00
|
|
|
package d2audio
|
2019-10-25 20:28:14 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2common/d2interface"
|
2019-11-10 03:36:53 -05:00
|
|
|
|
2019-10-25 20:28:14 -04:00
|
|
|
"github.com/hajimehoshi/ebiten/audio"
|
|
|
|
"github.com/hajimehoshi/ebiten/audio/wav"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Manager provides sound
|
|
|
|
type Manager struct {
|
2019-11-10 03:36:53 -05:00
|
|
|
fileProvider d2interface.FileProvider
|
2019-10-25 20:28:14 -04:00
|
|
|
audioContext *audio.Context // The Audio context
|
|
|
|
bgmAudio *audio.Player // The audio player
|
2019-10-26 00:26:48 -04:00
|
|
|
lastBgm string
|
2019-10-26 20:09:33 -04:00
|
|
|
sfxVolume float64
|
|
|
|
bgmVolume float64
|
2019-10-25 20:28:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateManager creates a sound provider
|
2019-11-10 03:36:53 -05:00
|
|
|
func CreateManager(fileProvider d2interface.FileProvider) *Manager {
|
2019-10-25 20:28:14 -04:00
|
|
|
result := &Manager{
|
|
|
|
fileProvider: fileProvider,
|
|
|
|
}
|
2019-10-26 20:09:33 -04:00
|
|
|
audioContext, err := audio.NewContext(44100)
|
2019-10-25 20:28:14 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
result.audioContext = audioContext
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// PlayBGM plays an infinitely looping background track
|
|
|
|
func (v *Manager) PlayBGM(song string) {
|
2019-10-26 00:26:48 -04:00
|
|
|
if v.lastBgm == song {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.lastBgm = song
|
2019-10-27 21:35:16 -04:00
|
|
|
if song == "" && v.bgmAudio != nil && v.bgmAudio.IsPlaying() {
|
|
|
|
_ = v.bgmAudio.Pause()
|
|
|
|
return
|
|
|
|
}
|
2019-10-25 20:28:14 -04:00
|
|
|
go func() {
|
|
|
|
if v.bgmAudio != nil {
|
2019-10-26 20:09:33 -04:00
|
|
|
err := v.bgmAudio.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2019-10-25 20:28:14 -04:00
|
|
|
}
|
|
|
|
audioData := v.fileProvider.LoadFile(song)
|
|
|
|
d, err := wav.Decode(v.audioContext, audio.BytesReadSeekCloser(audioData))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-26 23:59:27 -04:00
|
|
|
s := audio.NewInfiniteLoop(d, d.Length())
|
2019-10-25 20:28:14 -04:00
|
|
|
v.bgmAudio, err = audio.NewPlayer(v.audioContext, s)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-26 20:09:33 -04:00
|
|
|
v.bgmAudio.SetVolume(v.bgmVolume)
|
2019-10-25 20:28:14 -04:00
|
|
|
// Play the infinite-length stream. This never ends.
|
2019-10-26 20:09:33 -04:00
|
|
|
err = v.bgmAudio.Rewind()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = v.bgmAudio.Play()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-10-25 20:28:14 -04:00
|
|
|
}()
|
|
|
|
}
|
2019-10-26 20:09:33 -04:00
|
|
|
|
|
|
|
func (v *Manager) LoadSoundEffect(sfx string) *SoundEffect {
|
|
|
|
result := CreateSoundEffect(sfx, v.fileProvider, v.audioContext, v.sfxVolume)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Manager) SetVolumes(bgmVolume, sfxVolume float64) {
|
|
|
|
v.sfxVolume = sfxVolume
|
|
|
|
v.bgmVolume = bgmVolume
|
|
|
|
}
|