Question about using gains on AudioBuffer

I’m making a basic sampler, each sample has their own envelope that can be adjusted in real time, as standard on most sample players. I know I can apply gain ramps for things like attack and release, but what happens when the user adjusts their settings? The code looks destructive, surely there is no record being kept of the original data value which has now been changed. Do I need to keep a record of my gain ramp, then apply the inverse before I apply a different gain ramp?

Also, are all gain ramps linear? What if I wanted to support a knee? Should I just do it manually? 1 sample at a time based on my own function?

Applying a gain is destructive.

I’m not sure if there is a “right” answer for what you want to achieve. You could keep your original audio data unchanged and overwrite a fresh section of it each time a user changes a parameter.

In that case what I’ll do is reserve twice as much memory as I need each time a sample is loaded, initialise the other 1/2 with the original data, then re-initialise with my gain ramps each time the user does something new.