Generation of terahertz waves by a current in magnetic junctions