Broadband on-chip terahertz spectroscopy