#include "../sound.txt"
#include "../music.txt"

let soundObj = [];
let musicObj = [];
let music = [];
	
task TSound(numb,vol)
	{
	LoadCommonDataAreaA2("CONFIG",GetCurrentScriptDirectory ~ "../data/CONFIG");
	ObjSound_SetSoundDivision(soundObj[numb], SOUND_SE);
	ObjSound_SetVolumeRate(soundObj[numb],GetAreaCommonData("CONFIG","SOUND_VOLUME",1)*vol);
	ObjSound_Play(soundObj[numb]);
	}
task TSoundStop(numb)
	{
	ObjSound_Stop(soundObj[numb]);
	}

task TMusic(num)
	{
	music = [];
	LoadCommonDataAreaA2("CONFIG",GetCurrentScriptDirectory ~ "../data/CONFIG");
	num = BGMNameToNumb(num);	
	ObjSound_SetSoundDivision(musicObj[num], SOUND_BGM);
	ObjSound_SetVolumeRate(musicObj[num],GetAreaCommonData("CONFIG","MUSIC_VOLUME",1)*100);
	ObjSound_Play(musicObj[num]);
	}
task TMusicStop(num)
	{
	num = BGMNameToNumb(num);
	ObjSound_SetRestartEnable(musicObj[num],false);
	ObjSound_Stop(musicObj[num]);
	}
task TMusicFade(num,time)
	{
	num = BGMNameToNumb(num);
	descent(t in 0..time)
		{
		ObjSound_SetVolumeRate(musicObj[num],t*GetAreaCommonData("CONFIG","MUSIC_VOLUME",1)*100/time);
		if(!ObjSound_IsPlaying(musicObj[num]))
			{
			break;
			}
		yield;
		}
	TMusicStop(BGMNumbToName(num));
	ObjSound_SetVolumeRate(musicObj[num],GetAreaCommonData("CONFIG","MUSIC_VOLUME",1)*100);
	}
task TMusicVol(num,vol)
	{
	num = BGMNameToNumb(num);
	ObjSound_SetVolumeRate(musicObj[num],vol);
	}

task TMusicCurrentStop
	{
	ascent(num in 0..18)
		{
		ObjSound_SetRestartEnable(musicObj[num],false);
		ObjSound_Stop(musicObj[num]);
		}
	}
task TMusicCurrentFade(time)
	{	
	ascent(num in 0..18)
		{
		TMusicFade(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 = [];	
	}

function InitializeSound
	{
	ascent(i in 0..56)
		{
		soundObj = soundObj ~ [ObjSound_Create];
		let path = GetCurrentScriptDirectory ~ "../se/" ~ ToString(SENumbToName(i)) ~ ".wav";
		ObjSound_Load(soundObj[i],path);	
		}
	}

function InitializeMusic
	{
	ascent(i in 0..18)
		{
		let name = BGMNumbToName(i);
		musicObj = musicObj ~ [ObjSound_Create];
		let path = GetCurrentScriptDirectory ~ "../bgm/bgm_" ~ IntToString(name) ~ ".ogg";
		ObjSound_Load(musicObj[i],path);

		ObjSound_SetRestartEnable(musicObj[i],false);
		ObjSound_SetLoopEnable(musicObj[i],true);
		let time = GetBGMLoopTime(name);
		ObjSound_SetLoopTime(musicObj[i],time[0],time[1]);
		}
	}