Read the three parts of this article.
https://web.archive.org/web/20210308142542/http://www.uchobby.com/index.php/2007/11/11/arduino-sound-part-1/