package projectzombie.audio;

import static org.lwjgl.openal.ALC10.ALC_DEFAULT_DEVICE_SPECIFIER;
import static org.lwjgl.openal.ALC10.alcCloseDevice;
import static org.lwjgl.openal.ALC10.alcCreateContext;
import static org.lwjgl.openal.ALC10.alcDestroyContext;
import static org.lwjgl.openal.ALC10.alcGetString;
import static org.lwjgl.openal.ALC10.alcMakeContextCurrent;
import static org.lwjgl.openal.ALC10.alcOpenDevice;

import org.lwjgl.openal.AL;
import org.lwjgl.openal.ALC;
import org.lwjgl.openal.ALCCapabilities;
import org.lwjgl.openal.ALCapabilities;

public class AudioEngine
{
	String deviceName;
	long device, context;
	ALCCapabilities alcCapabilities;
	ALCapabilities alCapabilities;
	
	public void init()
	{
		// Get the device name
		deviceName = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER);
		device = alcOpenDevice(deviceName);
		
		if(device == 0) {
			System.err.println("OpenAL device is null");
		}
		
		// Create the context
		int[] attributes = {0};
		context = alcCreateContext(device, attributes);
		alcMakeContextCurrent(context);
		
		// Get the capabilities
		alcCapabilities = ALC.createCapabilities(device);
		alCapabilities = AL.createCapabilities(alcCapabilities);
		
		// Display some info
		if(alCapabilities.OpenAL11) {
			System.out.println("OpenAL 1.1 supported");
		}
		
		else if(alCapabilities.OpenAL10) {
			System.out.println("OpenAL 1.0 supported");
		}
		
		else {
			System.err.println("No OpenAL capability found");
		}
	}
	
	public void destroy()
	{
		// Close the context and the device
		alcDestroyContext(context);
		alcCloseDevice(device);
	}
}
