A wave propagation method with constrained transport for ideal and shallow water magnetohydrodynamics