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 (
|
2020-10-28 22:34:47 -04:00
|
|
|
"io"
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2020-06-28 19:31:10 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-11-21 05:33:22 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
2020-02-01 18:55:56 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio/wav"
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
const sampleRate = 44100
|
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
const logPrefix = "Ebiten Audio Provider"
|
|
|
|
|
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
|
|
|
// CreateAudio creates an instance of ebiten's audio provider
|
2020-11-21 05:33:22 -05:00
|
|
|
func CreateAudio(l d2util.LogLevel, am *d2asset.AssetManager) *AudioProvider {
|
2020-09-12 16:51:30 -04:00
|
|
|
result := &AudioProvider{
|
2020-09-20 17:52:01 -04:00
|
|
|
asset: am,
|
2020-09-12 16:51:30 -04:00
|
|
|
}
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
result.Logger = d2util.NewLogger()
|
|
|
|
result.Logger.SetLevel(l)
|
|
|
|
result.Logger.SetPrefix(logPrefix)
|
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
result.audioContext = audio.NewContext(sampleRate)
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
return result
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:51:30 -04:00
|
|
|
// AudioProvider represents a provider capable of playing audio
|
|
|
|
type AudioProvider struct {
|
2020-09-20 17:52:01 -04:00
|
|
|
asset *d2asset.AssetManager
|
2020-09-12 16:51:30 -04:00
|
|
|
audioContext *audio.Context // The Audio context
|
|
|
|
bgmAudio *audio.Player // The audio player
|
2020-10-28 22:34:47 -04:00
|
|
|
bgmStream *wav.Stream
|
2020-09-12 16:51:30 -04:00
|
|
|
lastBgm string
|
|
|
|
sfxVolume float64
|
|
|
|
bgmVolume float64
|
2020-11-21 05:33:22 -05:00
|
|
|
|
|
|
|
*d2util.Logger
|
2020-09-12 16:51:30 -04:00
|
|
|
}
|
|
|
|
|
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() {
|
2020-10-28 14:17:42 -04:00
|
|
|
eap.bgmAudio.Pause()
|
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-11-21 05:33:22 -05:00
|
|
|
eap.Fatal(err.Error())
|
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-09-20 17:52:01 -04:00
|
|
|
audioStream, err := eap.asset.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-10-28 22:34:47 -04:00
|
|
|
if _, err = audioStream.Seek(0, io.SeekStart); err != nil {
|
2020-11-21 05:33:22 -05:00
|
|
|
eap.Fatal(err.Error())
|
2020-10-28 22:34:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
eap.bgmStream, 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 {
|
2020-11-21 05:33:22 -05:00
|
|
|
eap.Fatal(err.Error())
|
2020-02-08 21:02:37 -05:00
|
|
|
}
|
2020-06-30 09:58:53 -04:00
|
|
|
|
2020-10-28 22:34:47 -04:00
|
|
|
s := audio.NewInfiniteLoop(eap.bgmStream, eap.bgmStream.Length())
|
2020-02-08 21:02:37 -05:00
|
|
|
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 {
|
2020-11-21 05:33:22 -05:00
|
|
|
eap.Fatal(err.Error())
|
2020-02-08 21:02:37 -05:00
|
|
|
}
|
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-10-28 14:17:42 -04:00
|
|
|
eap.bgmAudio.Play()
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-08-05 01:17:25 -04: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 16:17:26 -04:00
|
|
|
volume := eap.sfxVolume
|
|
|
|
if bgm {
|
|
|
|
volume = eap.bgmVolume
|
|
|
|
}
|
2020-08-05 01:17:25 -04:00
|
|
|
|
2020-09-12 16:51:30 -04:00
|
|
|
result := eap.createSoundEffect(sfx, eap.audioContext, loop)
|
2020-08-05 01:17:25 -04:00
|
|
|
|
|
|
|
result.volumeScale = volume
|
|
|
|
result.SetVolume(volume)
|
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
|
|
|
|
}
|
2020-09-12 16:51:30 -04: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{}
|
|
|
|
|
2020-11-03 07:54:15 -05:00
|
|
|
soundFile := "data/global/sfx/"
|
2020-09-12 16:51:30 -04:00
|
|
|
|
2020-09-20 17:52:01 -04:00
|
|
|
if _, exists := eap.asset.Records.Sound.Details[sfx]; exists {
|
|
|
|
soundEntry := eap.asset.Records.Sound.Details[sfx]
|
2020-09-12 16:51:30 -04:00
|
|
|
soundFile += soundEntry.FileName
|
|
|
|
} else {
|
|
|
|
soundFile += sfx
|
|
|
|
}
|
|
|
|
|
2020-09-20 17:52:01 -04:00
|
|
|
audioData, err := eap.asset.LoadFileStream(soundFile)
|
2020-09-12 16:51:30 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2020-11-03 07:54:15 -05:00
|
|
|
audioData, err = eap.asset.LoadFileStream("data/global/music/" + sfx)
|
2020-09-12 16:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := wav.Decode(context, audioData)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-11-21 05:33:22 -05:00
|
|
|
eap.Fatal(err.Error())
|
2020-09-12 16:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-11-21 05:33:22 -05:00
|
|
|
eap.Fatal(err.Error())
|
2020-09-12 16:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
result.player = player
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|