mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-09-05 19:14:14 -04:00
60 lines
1.0 KiB
Go
60 lines
1.0 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()
|
|
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)
|
|
}
|
|
}
|