feat: engine audio code
This commit is contained in:
parent
1799422591
commit
5860aa69d6
6 changed files with 170 additions and 0 deletions
35
engine/audio/default_manger.go
Normal file
35
engine/audio/default_manger.go
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
package audio
|
||||||
|
|
||||||
|
var defaultManager = Manager{}
|
||||||
|
|
||||||
|
func LoadLibrary(library *Library) {
|
||||||
|
defaultManager.Load(library)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Play(id SoundID) {
|
||||||
|
defaultManager.Play(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func PlayLooped(id SoundID) {
|
||||||
|
defaultManager.PlayLooped(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Stop(id SoundID) {
|
||||||
|
defaultManager.Stop(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsPlaying(id SoundID) bool {
|
||||||
|
return defaultManager.IsPlaying(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Pause() {
|
||||||
|
defaultManager.Pause()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Resume() {
|
||||||
|
defaultManager.Resume()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Update() {
|
||||||
|
defaultManager.Update()
|
||||||
|
}
|
||||||
11
engine/audio/init.go
Normal file
11
engine/audio/init.go
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package audio
|
||||||
|
|
||||||
|
import rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
rl.InitAudioDevice()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Exit() {
|
||||||
|
rl.CloseAudioDevice()
|
||||||
|
}
|
||||||
16
engine/audio/library.go
Normal file
16
engine/audio/library.go
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
package audio
|
||||||
|
|
||||||
|
import rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
|
||||||
|
// Library is a map of SoundID's and sound data.
|
||||||
|
type Library []rl.Sound
|
||||||
|
|
||||||
|
func (l Library) Unload() {
|
||||||
|
for _, stream := range l {
|
||||||
|
rl.UnloadSound(stream)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l Library) Get(id SoundID) *rl.Sound {
|
||||||
|
return &l[id]
|
||||||
|
}
|
||||||
11
engine/audio/load.go
Normal file
11
engine/audio/load.go
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package audio
|
||||||
|
|
||||||
|
import (
|
||||||
|
rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
)
|
||||||
|
|
||||||
|
func LoadFromMemory(typ string, data []byte) rl.Sound {
|
||||||
|
wave := rl.LoadWaveFromMemory(typ, data, int32(len(data)))
|
||||||
|
defer rl.UnloadWave(wave)
|
||||||
|
return rl.LoadSoundFromWave(wave)
|
||||||
|
}
|
||||||
93
engine/audio/manager.go
Normal file
93
engine/audio/manager.go
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
package audio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"slices"
|
||||||
|
|
||||||
|
rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
)
|
||||||
|
|
||||||
|
type audio struct {
|
||||||
|
id SoundID
|
||||||
|
snd rl.Sound
|
||||||
|
}
|
||||||
|
|
||||||
|
type Manager struct {
|
||||||
|
library *Library
|
||||||
|
active []audio
|
||||||
|
paused bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *Manager) Load(library *Library) {
|
||||||
|
sm.library = library
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *Manager) play(id SoundID, looping bool) {
|
||||||
|
snd := sm.library.Get(id)
|
||||||
|
|
||||||
|
alias := rl.LoadSoundAlias(*snd)
|
||||||
|
alias.Stream.Buffer.Looping = looping
|
||||||
|
|
||||||
|
sm.active = append(sm.active, audio{
|
||||||
|
id: id,
|
||||||
|
snd: alias,
|
||||||
|
})
|
||||||
|
|
||||||
|
rl.PlaySound(alias)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *Manager) Play(id SoundID) {
|
||||||
|
sm.play(id, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *Manager) PlayLooped(id SoundID) {
|
||||||
|
sm.play(id, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *Manager) Stop(id SoundID) {
|
||||||
|
sm.active = slices.DeleteFunc(sm.active, func(a audio) bool {
|
||||||
|
if a.id == id {
|
||||||
|
rl.StopSound(a.snd)
|
||||||
|
rl.UnloadSoundAlias(a.snd)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm Manager) IsPlaying(id SoundID) bool {
|
||||||
|
return rl.IsSoundPlaying(*sm.library.Get(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pause all active sounds
|
||||||
|
func (sm *Manager) Pause() {
|
||||||
|
sm.paused = true
|
||||||
|
for _, audio := range sm.active {
|
||||||
|
rl.PauseSound(audio.snd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resume all active sounds.
|
||||||
|
func (sm *Manager) Resume() {
|
||||||
|
for _, audio := range sm.active {
|
||||||
|
rl.ResumeSound(audio.snd)
|
||||||
|
}
|
||||||
|
sm.paused = false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *Manager) Update() {
|
||||||
|
if sm.paused {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
active := sm.active[:0]
|
||||||
|
|
||||||
|
for _, audio := range sm.active {
|
||||||
|
if !rl.IsSoundPlaying(audio.snd) {
|
||||||
|
rl.UnloadSoundAlias(audio.snd)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
active = append(active, audio)
|
||||||
|
}
|
||||||
|
|
||||||
|
sm.active = active
|
||||||
|
}
|
||||||
4
engine/audio/sound_id.go
Normal file
4
engine/audio/sound_id.go
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
package audio
|
||||||
|
|
||||||
|
type SoundID byte
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue