Created
September 10, 2016 04:18
-
-
Save tbogdala/d5b017c8af9b9e26a165c4ee872d1943 to your computer and use it in GitHub Desktop.
Simple OpenAL example for Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // OpenAL Test | |
| package main | |
| import ( | |
| "fmt" | |
| "io/ioutil" | |
| "time" | |
| openal "github.com/timshannon/go-openal/openal" | |
| ) | |
| func main() { | |
| // sets up OpenAL with default options | |
| device := openal.OpenDevice("") | |
| defer device.CloseDevice() | |
| context := device.CreateContext() | |
| defer context.Destroy() | |
| context.Activate() | |
| vendor := openal.GetVendor() | |
| // make sure things have gone well | |
| if err := openal.Err(); err != nil { | |
| fmt.Printf("Failed to setup OpenAL: %v\n", err) | |
| return | |
| } | |
| fmt.Printf("OpenAL vendor: %s\n", vendor) | |
| source := openal.NewSource() | |
| defer source.Pause() | |
| source.SetLooping(false) | |
| source.SetPosition(&openal.Vector{0.0, 0.0, -5.0}) | |
| soundBuffer := openal.NewBuffer() | |
| if err := openal.Err(); err != nil { | |
| fmt.Printf("OpenAL buffer creation failed: %v\n", err) | |
| return | |
| } | |
| // load a sound effect | |
| wavBytes, err := ioutil.ReadFile("assets/Randomize5.wav") | |
| if err != nil { | |
| fmt.Printf("Failed to load the sound effect: %v\n", err) | |
| return | |
| } | |
| soundBuffer.SetData(openal.FormatMono16, wavBytes, 44100) | |
| source.SetBuffer(soundBuffer) | |
| // play the sound | |
| source.Play() | |
| for source.State() == openal.Playing { | |
| // loop long enough to let the wave file finish | |
| time.Sleep(time.Millisecond * 10) | |
| } | |
| source.Delete() | |
| fmt.Println("Sound played!") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment