module gedo.audio.sound;



private import gedo.file.path;
private import gedo.math.vector;
private import gedo.audio.gal;
private import gedo.system.cache;



public class Sound
{
public:
	static void initialize(string[] args)
	{
		GAL_initialize(0, null);
	}


	static void quit()
	{
		GAL_quit();
	}


	static Sound opCall(string path)
	{
		if(!_cache)
		{
			_cache = new typeof(_cache);
		}

		return _cache.get(path,

				(out uint size)
				{
					Sound s = new Sound(path);

					size = GAL_soundSize(s._sound);

					return s;
				});
	}


	static void clearCache()
	{
		if(_cache)
		{
			_cache.clear();
		}
	}


	bool isPlayable()
	{
		return (_sound !is null);
	}


private:
	GAL_Sound* _sound;


	this(string path)
	{
		_sound = GAL_createSound(.gedo.file.path.toStringz(path));
	}


	~this()
	{
		GAL_releaseSound(_sound);
	}
}



public class Source
{
public:
	static Source opCall()
	{
		static Source temp = null;
		if(!temp)
		{
			temp = new Source();
		}

		return temp;
	}


	this()
	{
		_source = GAL_createSource();
	}


	~this()
	{
		GAL_releaseSource(_source);
	}


	void play(Sound sound)
	{
		if(sound.isPlayable)
		{
			GAL_setSource(_source, sound._sound);

			GAL_playSource(_source);
		}
	}


	void stop()
	{
		GAL_stopSource(_source);
	}


private:
	GAL_Source* _source;
}



private Cache!(Sound, 1<<20) _cache;
