let GlobalMusic = [];
let GlobalMusicStr = [];
let GlobalMusicLoop = [];
let GlobalSound = [];
let GlobalSoundStr = [];
	CreateCommonDataArea("BGM");
	CreateCommonDataArea("OST");
function LoadAllSound{
	ClearAreaCommonData("BGM");
	if(!LoadCommonDataAreaA1("BGM")){ CreateCommonDataArea("BGM");}
	CreateSound("SShot1",GetCurrentScriptDirectory()~"ZUN/se_kira00.ogg");
	CreateSound("SShot2",GetCurrentScriptDirectory()~"ZUN/se_tan00.ogg");
	CreateSound("SShot3",GetCurrentScriptDirectory()~"ZUN/se_tan01.ogg");
	CreateSound("SShot4",GetCurrentScriptDirectory()~"ZUN/se_tan02.ogg");
	CreateSound("SBoon1",GetCurrentScriptDirectory()~"ZUN/se_boon01.ogg");
	CreateSound("SBoon2",GetCurrentScriptDirectory()~"ZUN/se_boon00.ogg");
	CreateSound("SExplode1",GetCurrentScriptDirectory()~"ZUN/se_enep02.ogg");
	CreateSound("SCharge1",GetCurrentScriptDirectory()~"ZUN/se_ch02.ogg");
	CreateSound("SCharge2",GetCurrentScriptDirectory()~"ZUN/se_ch00.ogg");
	CreateSound("SSlash1",GetCurrentScriptDirectory()~"ZUN/se_slash.ogg");
	CreateSound("SDead1",GetCurrentScriptDirectory()~"ZUN/se_enep00.ogg");
	CreateSound("SNote1",GetCurrentScriptDirectory()~"ZUN/se_lgods1.ogg");
	CreateSound("SNote2",GetCurrentScriptDirectory()~"ZUN/se_lgods2.ogg");
	CreateSound("SNote3",GetCurrentScriptDirectory()~"ZUN/se_lgods3.ogg");
	CreateSound("SHeal1",GetCurrentScriptDirectory()~"ZUN/se_heal.ogg");
	CreateSound("SLaser1",GetCurrentScriptDirectory()~"ZUN/se_lazer02.ogg");
	CreateSound("SLaser2",GetCurrentScriptDirectory()~"ZUN/se_lazer01.ogg");
	CreateSound("SFam1",GetCurrentScriptDirectory()~"ZUN/se_option.ogg");
	CreateSound("SSpell1",GetCurrentScriptDirectory()~"ZUN/se_cat00.ogg");
	CreateSound("SSpell2",GetCurrentScriptDirectory()~"ZUN/se_cardget.ogg");
	CreateSound("SSpell3",GetCurrentScriptDirectory()~"ZUN/se_fault.ogg");
	CreateSound("STimeout1",GetCurrentScriptDirectory()~"ZUN/se_timeout.ogg");
}

function LoadAllMusic{
	CreateCommonDataArea("OST");
	CreateMusic("MBoss1",GetCurrentScriptDirectory()~"bgm/boss.ogg",0,0);
}
function CreateSound(let str,let path){
	let objSound = ObjSound_Create();
	ObjSound_Load(objSound,path);
	ObjSound_SetSoundDivision(objSound, SOUND_SE);
	ObjSound_SetRestartEnable(objSound, true);
	ObjSound_SetVolumeRate(objSound, 90);
	GlobalSound = GlobalSound ~ [objSound];
	GlobalSoundStr = GlobalSoundStr ~ [str]; 
	SetAreaCommonData("BGM","Sound",GlobalSound);
	SetAreaCommonData("BGM","SoundStr",GlobalSoundStr); 
}

function CreateMusic(let str,let path,start,end){
	let objSound = ObjSound_Create();
	ObjSound_Load(objSound,path);
	ObjSound_SetSoundDivision(objSound, SOUND_BGM);
	ObjSound_SetRestartEnable(objSound, true);
	ObjSound_SetLoopEnable(objSound, true);
	ObjSound_SetLoopTime(objSound, start, end);
	ObjSound_SetVolumeRate(objSound, 100);
	GlobalMusic = GlobalMusic ~ [objSound];
	GlobalMusicStr = GlobalMusicStr ~ [str]; 
	GlobalMusicLoop = GlobalMusicLoop ~ [[start,end]];
	SetAreaCommonData("OST","Sound",GlobalMusic);
	SetAreaCommonData("OST","SoundStr",GlobalMusicStr); 
	SetAreaCommonData("OST","SoundLoop",GlobalMusicLoop);
}
function PlayMusic(SEobj){
	let id = -1;
	let Sounds = GetAreaCommonData("OST", "Sound", [-1]);
	let SoundString = GetAreaCommonData("OST", "SoundStr", [-1]);
	ascent(i in 0..length(SoundString)){
		if(SEobj==SoundString[i]){id = i; break;}
	}
	if(id==-1){ 
		RaiseError("Music \"" ~ SEobj ~ "\" does not exist.");
	} else {
		ObjSound_Play(Sounds[id]);
	}
}
task DelaySE(SEobj,w,v,p){
	loop(w){yield;}
	let id = -1;
	let Sounds = GetAreaCommonData("BGM", "Sound", [-1]);
	let SoundString = GetAreaCommonData("BGM", "SoundStr", [-1]);
	ascent(i in 0..length(SoundString)){
		if(SEobj==SoundString[i]){id = i; break;}
	}
	if(id==-1){ 
		RaiseError("Sound \"" ~ SEobj ~ "\" does not exist.");
	} else {
		ObjSound_SetVolumeRate(Sounds[id], 90+v);
		ObjSound_SetPanRate(Sounds[id], p);
		ObjSound_Play(Sounds[id]);
	}
}