Quantum Monte Carlo study of the dynamic structure factor in the gas and crystal phase of hard-sphere bosons