you haven't yet tested if old_depth == depth, so this won't ramp properly.  Do the test for old_depth != depth.

Also our gain utilities already test for gain == 0.0 and gain == 1.0 so don't do that here at all.