//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
//Functions for controlling the package sounds. Only include in the package.
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------

let soundObj = [];
let musicObj = [];
let music = [];

function GetMusicLoopPoints(name)
	{
	let time = [0,1];
	alternative(name)
		case("bgm_Title")
			{
			time = [10.666,64];
			}
		case("bgm_Score")
			{
			time = [0.2,54.15];
			}
		case("bgm_Boss")
			{
			time = [40.727,277.363];
			}
		case("bgm_Stage")
			{
			time = [10.666, 117.3333];
			}
	return time;
	}

function InitializeSound
	{
	let path = GetFilePathList(GetCurrentScriptDirectory ~ "../se/");
	ascent(i in 0..length(path))
		{
		soundObj = soundObj ~ [ObjSound_Create];
		ObjSound_Load(soundObj[i],path[i]);
		}
	path = GetFilePathList(GetCurrentScriptDirectory ~ "../bgm/");
	ascent(i in 0..length(path))
		{
		musicObj = musicObj ~ [ObjSound_Create];
		ObjSound_Load(musicObj[i],path[i]);
		}
	}
function SENameToNum(name)
	{
	name = GetCurrentScriptDirectory ~ "../se/" ~ name ~ ".wav";
	let path = GetFilePathList(GetCurrentScriptDirectory ~ "../se/");	
	let num = 0;
	ascent(i in 0..length(soundObj))
		{
		if(path[i] == name)
			{
			return i;
			}
		}
	return num;
	}
function BGMNameToNum(name)
	{
	name = GetCurrentScriptDirectory ~ "../bgm/" ~ name ~ ".ogg";
	let path = GetFilePathList(GetCurrentScriptDirectory ~ "../bgm/");	
	let num = -1;
	ascent(i in 0..length(musicObj))
		{
		if(path[i] == name)
			{
			return i;
			}
		}
	return num;
	}

task TSound(name,vol)
	{
	let num = SENameToNum(name);
	ObjSound_SetSoundDivision(soundObj[num], SOUND_SE);
	ObjSound_SetVolumeRate(soundObj[num],GetAreaCommonData("CONFIG","SOUND_VOLUME",100)*vol/100);
	ObjSound_Play(soundObj[num]);
	}
task TSoundStop(name)
	{
	let num = SENameToNum(name);
	ObjSound_Stop(soundObj[num]);
	}

task TMusic(name)
	{	
	let time = GetMusicLoopPoints(name);
	let num = BGMNameToNum(name);
	music = [];
	ObjSound_SetSoundDivision(musicObj[num], SOUND_BGM);
	ObjSound_SetVolumeRate(musicObj[num],0.8*GetAreaCommonData("CONFIG","MUSIC_VOLUME",100));
	ObjSound_SetLoopEnable(musicObj[num],true);
	ObjSound_SetLoopTime(musicObj[num],time[0],time[1]);
	ObjSound_Play(musicObj[num]);
	}
task TMusicStop(name)
	{
	TMusicStopA(BGMNameToNum(name));
	}
task TMusicStopA(num)
	{
	ObjSound_SetRestartEnable(musicObj[num],false);
	ObjSound_Stop(musicObj[num]);
	}
task TMusicFade(num,time)
	{
	TMusicFadeA(num,time);
	}
task TMusicFadeA(num,time)
	{
	let vol = ObjSound_GetVolumeRate(musicObj[num]);
	descent(t in 0..time)
		{
		ObjSound_SetVolumeRate(musicObj[num],t*vol/time);
		if(!ObjSound_IsPlaying(musicObj[num])){break;}
		yield;
		}
	TMusicStopA(num);
	ObjSound_SetVolumeRate(musicObj[num],GetAreaCommonData("CONFIG","MUSIC_VOLUME",1)*100);
	}
task TMusicVol(name,vol)
	{
	let num = BGMNameToNum(name);
	ObjSound_SetVolumeRate(musicObj[num],vol);
	}


task TMusicCurrentStop
	{
	ascent(num in 0..length(musicObj))
		{
		ObjSound_SetRestartEnable(musicObj[num],false);
		ObjSound_Stop(musicObj[num]);
		}
	}
task TMusicCurrentFade(time)
	{	
	ascent(num in 0..length(musicObj))
		{
		TMusicFadeA(num,time);
		}
	}


task TMusicPause
	{
	music = [];
	ascent(num in 0..length(musicObj))
		{
		if(ObjSound_IsPlaying(musicObj[num]))
			{
			music = music ~ [num];
			ObjSound_SetRestartEnable(musicObj[num],true);
			ObjSound_Stop(musicObj[num]);
			}
		}
	}
task TMusicResume
	{
	ascent(num in 0..length(music))
		{
		ObjSound_Play(musicObj[music[num]]);
		ObjSound_SetRestartEnable(musicObj[music[num]],false);
		}	
	music = [];	
	}