1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-18 02:16:23 -05:00
OpenDiablo2/d2core/d2audio/d2audio.go
2020-06-19 02:19:27 -04:00

62 lines
1.1 KiB
Go

package d2audio
import (
"errors"
)
var singleton AudioProvider
var (
ErrWasInit = errors.New("audio system is already initialized")
ErrNotInit = errors.New("audio system has not been initialized")
)
type SoundEffect interface {
Play()
Stop()
}
type AudioProvider interface {
PlayBGM(song string)
LoadSoundEffect(sfx string) (SoundEffect, error)
SetVolumes(bgmVolume, sfxVolume float64)
}
// CreateManager creates a sound provider
func Initialize(audioProvider AudioProvider) error {
verifyNotInit()
singleton = audioProvider
return nil
}
// PlayBGM plays an infinitely looping background track
func PlayBGM(song string) error {
verifyWasInit()
go func() {
singleton.PlayBGM(song)
}()
return nil
}
func LoadSoundEffect(sfx string) (SoundEffect, error) {
verifyWasInit()
return singleton.LoadSoundEffect(sfx)
}
func SetVolumes(bgmVolume, sfxVolume float64) {
verifyWasInit()
singleton.SetVolumes(bgmVolume, sfxVolume)
}
func verifyWasInit() {
if singleton == nil {
panic(ErrNotInit)
}
}
func verifyNotInit() {
if singleton != nil {
panic(ErrWasInit)
}
}