2020-06-30 09:58:53 -04:00
|
|
|
// Package ebiten contains ebiten's implementation of the audio interface
|
2020-01-31 23:18:11 -05:00
|
|
|
package ebiten
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2020-06-28 19:31:10 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-02-01 18:55:56 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-01-31 23:18:11 -05:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/audio"
|
2020-07-13 09:05:04 -04:00
|
|
|
"github.com/hajimehoshi/ebiten/audio/wav"
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
const sampleRate = 44100
|
|
|
|
|
2020-07-13 09:05:04 -04:00
|
|
|
var _ d2interface.AudioProvider = &AudioProvider{} // Static check to confirm struct conforms to interface
|
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
// AudioProvider represents a provider capable of playing audio
|
2020-02-01 21:51:49 -05:00
|
|
|
type AudioProvider struct {
|
2020-01-31 23:18:11 -05:00
|
|
|
audioContext *audio.Context // The Audio context
|
|
|
|
bgmAudio *audio.Player // The audio player
|
|
|
|
lastBgm string
|
|
|
|
sfxVolume float64
|
|
|
|
bgmVolume float64
|
|
|
|
}
|
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
// CreateAudio creates an instance of ebiten's audio provider
|
2020-02-01 21:51:49 -05:00
|
|
|
func CreateAudio() (*AudioProvider, error) {
|
|
|
|
result := &AudioProvider{}
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
var err error
|
2020-06-30 09:58:53 -04:00
|
|
|
result.audioContext, err = audio.NewContext(sampleRate)
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
// PlayBGM loads an audio stream and plays it in the background
|
2020-02-01 21:51:49 -05:00
|
|
|
func (eap *AudioProvider) PlayBGM(song string) {
|
2020-01-31 23:18:11 -05:00
|
|
|
if eap.lastBgm == song {
|
|
|
|
return
|
|
|
|
}
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
eap.lastBgm = song
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
if song == "" && eap.bgmAudio != nil && eap.bgmAudio.IsPlaying() {
|
|
|
|
_ = eap.bgmAudio.Pause()
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
return
|
|
|
|
}
|
2020-02-08 21:02:37 -05:00
|
|
|
|
|
|
|
if eap.bgmAudio != nil {
|
|
|
|
err := eap.bgmAudio.Close()
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
if err != nil {
|
2020-02-08 21:02:37 -05:00
|
|
|
log.Panic(err)
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
2020-02-08 21:02:37 -05:00
|
|
|
}
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-06-27 02:49:27 -04:00
|
|
|
audioStream, err := d2asset.LoadFileStream(song)
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-06-27 02:49:27 -04:00
|
|
|
d, err := wav.Decode(eap.audioContext, audioStream)
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
s := audio.NewInfiniteLoop(d, d.Length())
|
|
|
|
eap.bgmAudio, err = audio.NewPlayer(eap.audioContext, s)
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
eap.bgmAudio.SetVolume(eap.bgmVolume)
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
// Play the infinite-length stream. This never ends.
|
|
|
|
err = eap.bgmAudio.Rewind()
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
err = eap.bgmAudio.Play()
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
// LoadSoundEffect loads a sound affect so that it canb e played
|
2020-06-28 19:31:10 -04:00
|
|
|
func (eap *AudioProvider) LoadSoundEffect(sfx string) (d2interface.SoundEffect, error) {
|
2020-01-31 23:18:11 -05:00
|
|
|
result := CreateSoundEffect(sfx, eap.audioContext, eap.sfxVolume) // TODO: Split
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
// SetVolumes sets the volumes of the audio provider
|
2020-02-01 21:51:49 -05:00
|
|
|
func (eap *AudioProvider) SetVolumes(bgmVolume, sfxVolume float64) {
|
2020-01-31 23:18:11 -05:00
|
|
|
eap.sfxVolume = sfxVolume
|
|
|
|
eap.bgmVolume = bgmVolume
|
|
|
|
}
|