Beispiel #1
0
// 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)
			al.MakeContextCurrent(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")
				return
			}

			// copy the audio data into the buffer
			tag := &autag{}
			format := tag.audioFormat(sound)
			if format < 0 {
				log.Printf("au: error recognizing audio format")
				return
			}
			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)
				return
			}

			// Start playback and give enough time for the playback to finish.
			al.SourcePlay(source)
			time.Sleep(500 * time.Millisecond)
			return
		}
		log.Printf("au: error, failed to get a context")
	}
	log.Printf("au: error, failed to get a device")
}
Beispiel #2
0
// Shutdown closes down the openal library.  This is only expected to be called
// once by the engine when it is shutting down.
func (a *openal) Shutdown() {
	al.MakeContextCurrent(nil)
	if a.ctx != nil {
		al.DestroyContext(a.ctx)
	}
	if a.dev != nil {
		al.CloseDevice(a.dev)
	}
}
Beispiel #3
0
// Init runs the one time openal library initialization. It is only expected to
// be called once by the engine on startup.
func (a *openal) Init() (err error) {
	al.Init()
	if err = a.validate(); err != nil {
		return
	}

	// create an openal context for all sounds.
	if a.dev = al.OpenDevice(""); a.dev != nil {
		if a.ctx = al.CreateContext(a.dev, nil); a.ctx != nil {
			al.MakeContextCurrent(a.ctx)
			return // success
		}
	}
	return fmt.Errorf("Could not initialize openal audio")
}