----------------------------------
------------FUNCTIONS-------------
----------------------------------


function PreloadAudio(sounds)
	for i, v in pairs(sounds) do
		ContentProvider:Preload("rbxassetid://"..v)
	end
end
function SetSounds(sounds)
	PreloadAudio(sounds)
	LocalSounds = sounds
end
function PlayNoteSound(note, source, range, sounds)
	local SoundList = sounds or LocalSounds
	
	local note2 = (note - 1)%12 + 1	-- Which note? (1-12)
	
	local octave = math.ceil(note/12) -- Which octave?
	
	local sound = math.ceil(note2/2) -- Which audio?
	
	local offset = 16 * (octave - 1) + 8 * (1 - note2%2) -- How far in audio?
	
	local audio = Instance.new("Sound", SoundFolder) -- Create the audio
	audio.SoundId = "rbxassetid://"..SoundList[sound] -- Give its sound
	
	if source then
		local a = 1/range^2
		local distance = (game.Workspace.CurrentCamera.CoordinateFrame.p - source).magnitude
		local volume = -a*distance^2 + 1
		if volume < 0.05 then
			audio:remove()
			return
		end
		audio.Volume = volume
	end
	audio.TimePosition = offset + (octave - .9)/15 -- set the time position
	audio:Play() -- Play the audio
	
	table.insert(ExistingSounds, 1, audio)
	if #ExistingSounds >= 10 then
		ExistingSounds[10]:Stop() -- limit the number of playing sounds!
		ExistingSounds[10] = nil
	end
	
	delay(4, function() audio:Stop() audio:remove() end ) -- remove the audio in 4 seconds, enough time for it to play
end
