Im making a game in Golang using SDL2 binding here : https://github.com/veandco/go-sdl2
The problem is, I added code to play audio and when I try, the audio sound is messed up. you can tell it actually played something because of the duration of the sound, and it got triggered at the right event. But the sound itself is messed up.
Here is my code :
package SDL
import (
"io/ioutil"
"log"
"github.com/veandco/go-sdl2/mix"
)
var AUDIOS map[string]*mix.Chunk
func init() {
AUDIOS = make(map[string]*mix.Chunk)
AUDIOS["point_normal"] = LoadAudio("sound/effects/point_normal.wav")
}
func LoadAudio(path string) *mix.Chunk {
if err := mix.OpenAudio(44100, mix.DEFAULT_FORMAT, 2, 4096); err != nil {
log.Fatalf("%s
", err.Error())
}
// Load entire WAV data from file
data, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("%s
", err.Error())
}
// Load WAV from data (memory)
chunk, err := mix.QuickLoadWAV(data)
if err != nil {
log.Fatalf("%s
", err.Error())
}
defer chunk.Free()
return chunk
}
func CloseAudio() {
mix.CloseAudio()
}
And here is the sound I try to play :
https://opengameart.org/sites/default/files/point_normal.wav
Can someone see anything wrong with my code?
I don't know the sound library you're using, but these lines look suspicious to me:
defer chunk.Free()
return chunk
The returned "chunk" will have Free() called on it. Is that really what you intended?