Clone function?

So I’m trying to setup a definedParameter that will be attached to a menu.
Using this code, just as an experiment.

function doIt()
   print(envS)
end

defineParameter("envS", nil, 1, {"Amp Env",  "Filter Env",  "Pitch Env",  "User Env"}, doIt)

When attached to a menu the entries are correct, however when selecting an entry
it just returns the index#. How can it be setup so that it returns the actual string?

What I’m trying to do is mod your “Copy/Restore” code so that I can select which envelope type I’m copying/restoring.

defineParameter("CopyEnv", nil, false, function() copyEnvelope("User Env") end)
defineParameter("RestoreEnv", nil, false, function() restoreEnvelope("User Env") end)
defineParameter{name = "DataCopied", default = false, persistent = true, automatable = false, readOnly = true}

zone = this.parent:getZone()

function copyEnvelope(envType)
	if CopyEnv then
		envelopePoints = zone:getParameter(envType..".EnvelopePoints")
		--sustainIndex = zone:getParameter(envType..".SustainIndex")
		mode = zone:getParameter(envType..".Mode")

		DataCopied = true
		
		wait(50)
		CopyEnv = false
	end
end

function restoreEnvelope(envType)
	if RestoreEnv and DataCopied then
		zone:setParameter(envType..".EnvelopePoints", envelopePoints)
		--zone:setParameter(envType..".SustainIndex", sustainIndex)
		zone:setParameter(envType..".Mode", mode)
		
		wait(50)
		RestoreEnv = false
	end
end

Another question is, how can I reuse the copy/resetore functions?
I experimented by just duplicating the functions and changing the parameters (envelopePoints & mode) to (envelopePoints2, mode2)…, etc., which works, but I’d rather just reuse if possible so as to make the script more tidy, because I’m probably going to go with around 12 or so.

Thank you,

Create a table with strings and then use the index returned by the parameter.

envNames = {"Amp Env", "Filter Env", "Pitch Env", "User Env"}

defineParameter("CopyEnv", nil, false, function() copyEnvelope() end)
defineParameter("RestoreEnv", nil, false, function() restoreEnvelope() end)
defineParameter{name = "DataCopied", default = false, persistent = false, automatable = false, readOnly = true}
defineParameter("Ens", nil, 1, envNames, function() DataCopied = false end)

zone = this.parent:getLayer():getZone()

function copyEnvelope()
	local envType = envNames[Ens]
	if CopyEnv then
		envelopePoints = zone:getParameter(envType..".EnvelopePoints")
		sustainIndex = zone:getParameter(envType..".SustainIndex")
		mode = zone:getParameter(envType..".Mode")

		DataCopied = true
		
		wait(50)
		CopyEnv = false
	end
end

function restoreEnvelope()
	local envType = envNames[Ens]
	if RestoreEnv and DataCopied then
		zone:setParameter(envType..".EnvelopePoints", envelopePoints)
		zone:setParameter(envType..".SustainIndex", sustainIndex)
		zone:setParameter(envType..".Mode", mode)
		
		wait(50)
		RestoreEnv = false
	end
end

Hi misohoza,

It’s close but not quite what I was looking to do.
Something like this…, which obviously doesn’t work, but maybe gives you an idea.

defineParameter("CopyEnv", nil, false, function() copyEnvelope(envNames[Ens]) end)
defineParameter("RestoreEnv", nil, false, function() restoreEnvelope(envNames[Ens]) end)

This way for example if I selected “User Env” from the menu and then click copy and I leave the menu as is, then click restore it would restore to “User Env”…, or if I was to make a different menu selection, say “Pitch Env” then click restore it would restore the “User Env” to the “Pitch Env”…, hope that makes sense?

Delete the callback from the Ens parameter.
(function () DataCopied = false end)

It should do what you want. But you can get errors if you try to copy pitch or user envelope to amp or filter envelope and the copied envelope has negative values.

YAY! SUCCESS!!! Thank you so much misohoza.

You don’t happen to know it there is a “feature request” thread? There are just a couple of improvements I’d like to see if the devs would be willing to implement.

  1. Maybe a choice of one or more smoothing algorithms for around the points.
  2. More points. 128 is so limiting. It’s weird because you can actually force the envelope to have 512 and even save it, but things like copy/paste from one env to another won’t work and neither does trying to manipulate the table via lua.
  3. In loop mode it would be nice to be able to set loop start to the first point, or in Shaper mode the ability to unlink the first and last points.