Example #1
// au demonstrates basic audio library (vu/audio/al) capabilities.
// It checks that OpenAL is installed and the bindings are working
// by loading and playing a sound.
func au() {
	al.Init() // map the bindings to the OpenAL dynamic library.

	// open the default device.
	if dev := al.OpenDevice(""); dev != nil {
		defer al.CloseDevice(dev)

		// create a context on the device.
		if ctx := al.CreateContext(dev, nil); ctx != nil {
			defer al.MakeContextCurrent(nil)
			defer al.DestroyContext(ctx)

			// create buffers and sources
			var buffer, source uint32
			al.GenBuffers(1, &buffer)
			al.GenSources(1, &source)

			// read in the audio data.
			sound := &data.Sound{}
			loader := data.NewLoader()
			loader.Load("bloop", &sound)
			if sound == nil {
				log.Printf("au: error loading audio file", "bloop")

			// copy the audio data into the buffer
			tag := &autag{}
			format := tag.audioFormat(sound)
			if format < 0 {
				log.Printf("au: error recognizing audio format")
			al.BufferData(buffer, int32(format), al.Pointer(&(sound.AudioData[0])), int32(sound.DataSize), int32(sound.Frequency))

			// attach the source to a buffer.
			al.Sourcei(source, al.BUFFER, int32(buffer))

			// check for any audio library errors that have happened up to this point.
			if err := al.GetError(); err != 0 {
				log.Printf("au: OpenAL error ", err)

			// Start playback and give enough time for the playback to finish.
			time.Sleep(500 * time.Millisecond)
		log.Printf("au: error, failed to get a context")
	log.Printf("au: error, failed to get a device")
Example #2
// BindSound loads the raw audio data into the sound buffer.
func (a *openal) BindSound(s *data.Sound) (err error) {
	if alerr := al.GetError(); alerr != al.NO_ERROR {
		log.Printf("openal:bindSound need to find and fix prior error %X", alerr)

	// create the sound buffer and copy the audio data into the buffer
	var format int32
	if format, err = a.format(s); err == nil {
		al.GenBuffers(1, &(s.Buffer))
		al.BufferData(s.Buffer, format, al.Pointer(&(s.AudioData[0])), int32(s.DataSize), int32(s.Frequency))
		if alerr := al.GetError(); alerr != al.NO_ERROR {
			err = fmt.Errorf("Failed binding sound %s", s.Name)