Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

can I map input values to an output range?

In the context of an music amplitude bar, how would I go about mapping my values that currently go from 0 to 1, into a different output range, say 5 to 10, or even 10 to 1. If this is possible let me know.

I have put the code below. Any advice is greatly appreciated!

--wait for Sound
local Sound = game.workspace:WaitForChild("Sound") 
local Players = game:GetService("Players")
 
-- wait for local player PlayerGui
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
 
-- create a ScreenGui
local screenGui = Instance.new("ScreenGui", playerGui)
 
-- create a holder for our bar
local frame = Instance.new("Frame", screenGui)
frame.AnchorPoint = Vector2.new(0.5,0.5)
frame.Position = UDim2.new(0.5, 0, 0.5, 0)
frame.Size = UDim2.new(1, 0, 0.05, 0)
 
-- create a bar
local bar = Instance.new("Frame", frame)
bar.Position = UDim2.new(0, 0, 0, 0)
bar.Size = UDim2.new(1, 0, 1, 0)
bar.BackgroundColor3 = Color3.new(0, 1, 0)
local Baseplate = workspace.Baseplate
 
-- define a max loudness
local maxLoudness = 1000
 
-- animate the amplitude bar
while true do
	local amplitude = math.clamp(Sound.PlaybackLoudness / maxLoudness, 0, 1)
	bar.Size = UDim2.new(amplitude, 0, 1, 0)
	bar.BackgroundColor3= Color3.new(amplitude, 0 , 0.1)
	Baseplate.Transparency = amplitude
	print(amplitude)
	wait()
end

2 answers

✓ Accepted answer

Assuming that you want the distribution of the numbers to stay the same, you can convert the range from 0 to 1 to any other range with addition, multiplication, and negation.

local amplitude1 = amplitude * 2 -- 0 to 2
local amplitude2 = amplitude * 5 + 5 -- 5 to 10
local amplitude3 = -amplitude * 9 + 10 -- 10 to 1

2 comments

0

Looks promising! if this works I will accept your answer.

0

your method works, but I have found a universal formula for this answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.