hey, there is an already existing thread on PLL Phase locked loops
and there is also a C++ Ugen from @nathan GitHub - nhthn/pll
One thing you should be aware of when dealing with single sample feedback which is needed for a Phased-Locked-Loop (PLL) is, that in all creative coding environments including (SC, Max, PD) samples are handled in chunks called blocks or vectors for efficiency reasons. So in these environments it can be challenging or simply not possible to implement single-sample feedback. But you could try out LocalIn / LocalOut which sets up a feedback loop according to the blocksize, which is per default 64 samples.