A mass on a spring obeys m x'' = -k x - c v + F0 cos(wt). Multiplying by v and integrating gives the energy ledger:
E_mech(t) = E_mech(0) + W_motor - H, where W_motor = integral F v dt and friction heat H = integral c v^2 dt. Energy is never created: large motion near resonance only means the motor has been feeding it for a long time.
Steady state: A = F0 / sqrt((k - m w^2)^2 + (c w)^2), resonance at w0 = sqrt(k/m), quality factor Q = sqrt(km)/c. Average motor power equals the heat rate: P = c w^2 A^2 / 2.
The "free-energy gadget" is modelled as negative friction -beta v. It looks like a gain, but its own meter (W_gadget = integral beta v^2 dt) shows it must be supplied from somewhere. If beta exceeds c the motion grows without bound only until the (unmodelled) supply runs out; real devices break first.
Simplifications: linear spring, viscous friction, point mass, no air radiation or spring mass. The same ledger idea applies to every machine: a perpetual-motion claim is always an unmetered input.