2020-06-30 13:58:53 +00:00
|
|
|
// Package ebiten contains ebiten's implementation of the audio interface
|
2020-02-01 04:18:11 +00:00
|
|
|
package ebiten
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2020-09-12 20:51:30 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
2020-06-28 23:31:10 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-02-01 23:55:56 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-02-01 04:18:11 +00:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/audio"
|
2020-07-13 13:05:04 +00:00
|
|
|
"github.com/hajimehoshi/ebiten/audio/wav"
|
2020-02-01 04:18:11 +00:00
|
|
|
)
|
|
|
|
|
2020-06-30 13:58:53 +00:00
|
|
|
const sampleRate = 44100
|
|
|
|
|
2020-07-13 13:05:04 +00:00
|
|
|
var _ d2interface.AudioProvider = &AudioProvider{} // Static check to confirm struct conforms to interface
|
|
|
|
|
2020-06-30 13:58:53 +00:00
|
|
|
// CreateAudio creates an instance of ebiten's audio provider
|
2020-09-12 20:51:30 +00:00
|
|
|
func CreateAudio(am *d2asset.AssetManager) (*AudioProvider, error) {
|
|
|
|
result := &AudioProvider{
|
|
|
|
assetManager: am,
|
|
|
|
}
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-01 04:18:11 +00:00
|
|
|
var err error
|
2020-06-30 13:58:53 +00:00
|
|
|
result.audioContext, err = audio.NewContext(sampleRate)
|
|
|
|
|
2020-02-01 04:18:11 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-01 04:18:11 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-09-12 20:51:30 +00:00
|
|
|
// AudioProvider represents a provider capable of playing audio
|
|
|
|
type AudioProvider struct {
|
|
|
|
assetManager *d2asset.AssetManager
|
|
|
|
audioContext *audio.Context // The Audio context
|
|
|
|
bgmAudio *audio.Player // The audio player
|
|
|
|
lastBgm string
|
|
|
|
sfxVolume float64
|
|
|
|
bgmVolume float64
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:58:53 +00:00
|
|
|
// PlayBGM loads an audio stream and plays it in the background
|
2020-02-02 02:51:49 +00:00
|
|
|
func (eap *AudioProvider) PlayBGM(song string) {
|
2020-02-01 04:18:11 +00:00
|
|
|
if eap.lastBgm == song {
|
|
|
|
return
|
|
|
|
}
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-01 04:18:11 +00:00
|
|
|
eap.lastBgm = song
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-01 04:18:11 +00:00
|
|
|
if song == "" && eap.bgmAudio != nil && eap.bgmAudio.IsPlaying() {
|
|
|
|
_ = eap.bgmAudio.Pause()
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-01 04:18:11 +00:00
|
|
|
return
|
|
|
|
}
|
2020-02-09 02:02:37 +00:00
|
|
|
|
|
|
|
if eap.bgmAudio != nil {
|
|
|
|
err := eap.bgmAudio.Close()
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-01 04:18:11 +00:00
|
|
|
if err != nil {
|
2020-02-09 02:02:37 +00:00
|
|
|
log.Panic(err)
|
2020-02-01 04:18:11 +00:00
|
|
|
}
|
2020-02-09 02:02:37 +00:00
|
|
|
}
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-09-12 20:51:30 +00:00
|
|
|
audioStream, err := eap.assetManager.LoadFileStream(song)
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-09 02:02:37 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-06-27 06:49:27 +00:00
|
|
|
d, err := wav.Decode(eap.audioContext, audioStream)
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-09 02:02:37 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-09 02:02:37 +00:00
|
|
|
s := audio.NewInfiniteLoop(d, d.Length())
|
|
|
|
eap.bgmAudio, err = audio.NewPlayer(eap.audioContext, s)
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-09 02:02:37 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-09 02:02:37 +00:00
|
|
|
eap.bgmAudio.SetVolume(eap.bgmVolume)
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-09 02:02:37 +00:00
|
|
|
// Play the infinite-length stream. This never ends.
|
|
|
|
err = eap.bgmAudio.Rewind()
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-09 02:02:37 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-09 02:02:37 +00:00
|
|
|
err = eap.bgmAudio.Play()
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-09 02:02:37 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-02-01 04:18:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 05:17:25 +00:00
|
|
|
// LoadSound loads a sound affect so that it canb e played
|
|
|
|
func (eap *AudioProvider) LoadSound(sfx string, loop, bgm bool) (d2interface.SoundEffect, error) {
|
2020-07-30 20:17:26 +00:00
|
|
|
volume := eap.sfxVolume
|
|
|
|
if bgm {
|
|
|
|
volume = eap.bgmVolume
|
|
|
|
}
|
2020-08-05 05:17:25 +00:00
|
|
|
|
2020-09-12 20:51:30 +00:00
|
|
|
result := eap.createSoundEffect(sfx, eap.audioContext, loop)
|
2020-08-05 05:17:25 +00:00
|
|
|
|
|
|
|
result.volumeScale = volume
|
|
|
|
result.SetVolume(volume)
|
2020-06-30 13:58:53 +00:00
|
|
|
|
2020-02-01 04:18:11 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:58:53 +00:00
|
|
|
// SetVolumes sets the volumes of the audio provider
|
2020-02-02 02:51:49 +00:00
|
|
|
func (eap *AudioProvider) SetVolumes(bgmVolume, sfxVolume float64) {
|
2020-02-01 04:18:11 +00:00
|
|
|
eap.sfxVolume = sfxVolume
|
|
|
|
eap.bgmVolume = bgmVolume
|
|
|
|
}
|
2020-09-12 20:51:30 +00:00
|
|
|
|
|
|
|
// createSoundEffect creates a new instance of ebiten's sound effect implementation.
|
|
|
|
func (eap *AudioProvider) createSoundEffect(sfx string, context *audio.Context,
|
|
|
|
loop bool) *SoundEffect {
|
|
|
|
result := &SoundEffect{}
|
|
|
|
|
|
|
|
soundFile := "/data/global/sfx/"
|
|
|
|
|
|
|
|
if _, exists := d2datadict.Sounds[sfx]; exists {
|
|
|
|
soundEntry := d2datadict.Sounds[sfx]
|
|
|
|
soundFile += soundEntry.FileName
|
|
|
|
} else {
|
|
|
|
soundFile += sfx
|
|
|
|
}
|
|
|
|
|
|
|
|
audioData, err := eap.assetManager.LoadFileStream(soundFile)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
audioData, err = eap.assetManager.LoadFileStream("/data/global/music/" + sfx)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := wav.Decode(context, audioData)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var player *audio.Player
|
|
|
|
|
|
|
|
if loop {
|
|
|
|
s := audio.NewInfiniteLoop(d, d.Length())
|
|
|
|
result.panStream = newPanStreamFromReader(s)
|
|
|
|
player, err = audio.NewPlayer(context, result.panStream)
|
|
|
|
} else {
|
|
|
|
result.panStream = newPanStreamFromReader(d)
|
|
|
|
player, err = audio.NewPlayer(context, result.panStream)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result.player = player
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|