Created
March 9, 2016 21:38
-
-
Save Necktrox/9fe6461c53a89de7f52f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local map_setSoundProperty = {["SampleRate"] = 1, ["Tempo"] = 2, ["Pitch"] = 3, ["Reversed"] = 4} | |
function setSoundProperty(sound, property, value) | |
-- Only handle sound elements | |
if (not isElement(sound) or getElementType(sound) ~= "sound") then return end | |
-- Get the current properties | |
local properties = { getSoundProperties(sound) } | |
-- Get the index by using the property parameter | |
local index = map_setSoundProperty[property] | |
-- Apply the value if there is an index for it | |
if (index) then properties[index] = tonumber(value) end | |
-- Return the property-success boolean | |
return setSoundProperties(sound, unpack(properties)) | |
end | |
function applyDopplerEffectToSound(sound) | |
-- Get the receiver (use localPlayer if we have no vehicle) | |
local receiver = { e = getPedOccupiedVehicle(localPlayer) } | |
if (not receiver.e) then receiver.e = localPlayer end | |
-- Get the sender (abort if it's invalid or if it equals to the receiver) | |
local sender = { e = getElementAttachedTo(sound) } | |
if (not sender.e or sender.e == receiver.e) then return end | |
-- Receiver information | |
receiver.p = { getElementPosition(receiver.e) } | |
receiver.v = { getElementVelocity(receiver.e) } | |
-- Sender information | |
sender.p = { getElementPosition(sender.e) } | |
sender.v = { getElementVelocity(sender.e) } | |
-- Distance calculation (abort if there is no distance) | |
local dx, dy, dz, d = getDistanceBetweenPoints(receiver.p, sender.p) | |
if (d < 1) then return end | |
-- Calculate velocity dotproduct for receiver and sender | |
sender.vs = getVelocityDotproduct(sender.v, dx, dy, dz, d) | |
receiver.vr = getVelocityDotproduct(receiver.v, dx, dy, dz, d) | |
-- Calculate pitch | |
local p = ((1 + receiver.vr) / (1 + sender.vs)) * (4.5 * math.max(0, sender.vs - receiver.vr)) | |
setSoundProperty(sound, "Pitch", p) | |
end | |
function getDistanceBetweenPoints(pointA, pointB) | |
local x, y, z = math.abs(pointB[1] - pointA[1]), math.abs(pointB[2] - pointA[2]), math.abs(pointB[3] - pointA[3]) | |
local distance = getDistanceBetweenPoints3D(pointA[1], pointA[2], pointA[3], unpack(pointB)) | |
return x, y, z, distance | |
end | |
function getVelocityDotproduct(point, dx, dy, dz, d) | |
if (d == 0) then return 1 end | |
return (math.abs(point[1] * dx) + math.abs(point[2] * dy) + math.abs(point[3] * dz)) / d | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment