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

ContentProvider:PreloadAsync(workspace.Scripts.MainModule:GetDescendants())
function SetSounds(sounds)
	ContentProvider:PreloadAsync(workspace.Scripts.MainModule:GetDescendants())
	LocalSounds = sounds
end

function PlayNoteSound(note, source, range, sounds, vol, SpecialPiano)	

	local pitch = 1
	if note > 61 then
		pitch = 1.059463 ^ (note - 61)
	elseif note < 1 then
		pitch = 1.059463 ^ (-(1 - note))
	end
	note = note > 61 and 61 or note < 1 and 1 or note
	local note2 = (note - 1)%12 + 1	-- Which note? (1-12)
	local octave = math.ceil(note/12) -- Which octave?
	local offset = (16 * (octave - 1) + 8 * (1 - note2%2))
	local sound = math.ceil(note2/2) -- Which audio?
	
	local audio = Instance.new("Sound", SoundFolder) -- Create the audio
	audio.SoundId = "https://roblox.com/asset/?id="..((sounds and sounds[sound]) or LocalSounds[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.01 then
			audio:remove()
			return
		end
		audio.Volume = volume * vol
	else
		audio.Volume = Volume;
	end
	if fade then
		delay(0.75, function()
			for i = 1, 5 do
				audio.Volume = audio.Volume * 0.75
				wait(0.1)
			end
			audio:Stop()
			audio:Destroy()
		end)
	end
	audio.TimePosition = offset + ((SpecialPiano or specialPiano) and 0.04 or (octave - .9)/15) -- set the time position
	audio.Pitch = pitch
	audio:Play() -- Play the audio
	
	table.insert(ExistingSounds, 1, audio)
	
	if #ExistingSounds >= 15 then
		ExistingSounds[15]:Stop() -- limit the number of playing sounds!
		ExistingSounds[15] = nil
	end
	delay(5, function() audio:Stop() audio:remove() end ) -- remove the audio in 4 seconds, enough time for it to play
end
