surge-synthesizer / surge

Synthesizer plug-in (previously released as Vember Audio Surge)
https://surge-synthesizer.github.io/
GNU General Public License v3.0
3.14k stars 400 forks source link

Add a patch prelude for formula modulators #7736

Open blancoberg opened 3 months ago

blancoberg commented 3 months ago

Is your feature request related to a problem? Please describe! All custom functions, tables, and variables in the formula modulator have to be declared in the init() function of each note, even though it would be much more convenient if they were already declared upon preset load.

Describe the solution you'd like: Create a "patch prelude." It's similar to the prelude section, but it is editable and saved within the preset. Everything declared in that section should be accessible globally by all formula modulators, just like the regular prelude.

This would make it much more efficient to create custom classes and handle large tables without risking lag on each note.

nuoun commented 3 months ago

Not exactly a "prelude style" editor but this is already possible using the "surge" table which loads the prelude, everything added to this table is shared globally among all Lua instances. I'm currently suggesting to add an actual "global" table in #7727 (wip) that also gets cleared every new function environment (so on Lua apply / patch load) so stuff added here can get garbage collected rather than just keep adding to it until Surge exits.

baconpaul commented 3 months ago

Yeah!

there’s a ui part to avoid accidental sharing which is another tab in the editor for code which is inserted into that global scope and which is stored at a patch as opposed to modulator level and is shared across all modulators but you are correct the mechanics at a lua state level are there today. Neat!!

blancoberg commented 3 months ago

Would it be possible to get access to setmetatable() in the patch prelude? As I understand it, creating classes using that method is more efficient that the alternative methods

nuoun commented 3 months ago

I think we could add this but there could be an issue that it's used to overwrite existing functions which will break Surge's Lua until it restarts. This is currently also possible by modifying the math table which I suppose is just as bad so I don't see why not unless we really want to lock stuff down.

baconpaul commented 3 months ago

Can we intercept it to stop that from happening?

nuoun commented 3 months ago

We could create a new math table for the sandbox. Right now we're giving the user direct access to the global math table which means one could set math.sin to nil (or worse a function with an infinite loop) and if math.sin is called anywhere else in Surge afterwards it would use the modified version. Luckily, this should be very easy to fix as we're already looping through the math table.

nuoun commented 2 months ago

7743 adds setmetatable (and more) and is available in the current nightly, it's very neat to have:

https://www.lua.org/pil/16.1.html https://gist.github.com/oatmealine/655c9e64599d0f0dd47687c1186de99f

Thanks for the suggestion! I will look into the editable prelude and see what is exactly needed for it.

blancoberg commented 2 months ago

Incredible! It is indeed very useful.

and It's going to be real powerful together with the editable prelude.