I think using Engine.setLatency dynamically can be problematic.
Instead, after the maximum delay value caused by pitchshift is set in Engine.setLatency, the difference can be balanced dynamically with a delay node according to the parameter values that give less delay.