The “speaker” is actually not a speaker. It is really a piezo element.
About the code, I think these might be helpful: There is a section in Sparkfun Inventors Kit that covers it. Here is the guide. Here is the code. You will need to find project 11 in the code .zip and in the guide.