Competing lattice fluctuations and magnetic excitations in CuO