Пример #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 != 0 {
		defer al.CloseDevice(dev)

		// create a context on the device.
		if ctx := al.CreateContext(dev, nil); ctx != 0 {
			defer al.MakeContextCurrent(0)
			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.
			ldr := load.NewLoader()
			wh, data, err := ldr.Wav("bloop")
			if err != nil {
				log.Printf("au: error loading audio file %s %s", "bloop", err)
				return
			}

			// copy the audio data into the buffer
			tag := &autag{}
			format := tag.audioFormat(wh)
			if format < 0 {
				log.Printf("au: error recognizing audio format")
				return
			}
			al.BufferData(buffer, int32(format), al.Pointer(&(data[0])), int32(wh.DataSize), int32(wh.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 openAlErr := al.GetError(); openAlErr != 0 {
				log.Printf("au: OpenAL error %d", openAlErr)
				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")
}
Пример #2
0
// Init runs the one time openal library initialization. It is 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 != 0 {
		if a.ctx = al.CreateContext(a.dev, nil); a.ctx != 0 {
			al.MakeContextCurrent(a.ctx)
			return // success
		}
	}
	return fmt.Errorf("openal audio init failed.")
}