I'm amazed by all the answers here suggesting the use of FFT, given that FFT isn't generally precise enough for pitch detection. It can be, but only with an impractically large FFT window. For example, in order to determine the fundamental with 1/100th of a semi-tone accuracy (which is about what you need for accurate pitch detection) when the fundamental is around concert A (440 Hz), you need an FFT window with 524,288 elements. 1024 is a much more typical FFT size - the computation time become progressively worse the larger the window.
I have to identify the fundamental pitch of WAV files in my software synthesizer (where a "miss" is immediately audible as an out-of-tune instrument) and I've found that autocorrelation does by far the best job. Basically, I iterate through each note in the 12-tone scale over an 8-octave range, compute the frequency and the wavelength of each note, and then perform an autocorrelation using that wavelength as the lag (an autocorrelation is where you measure the correlation between a set of data and the same set of data offset by some lag amount).
The note with the highest autocorrelation score is thus roughly the fundamental pitch. I then "hone in" on the true fundamental by iterating from one semi-tone down to one semi-tone up by 1/1000ths of a semi-tone, to find the local peak autocorrelation value. This method works very accurately, and more importantly it works for a wide variety of instrument files (strings, guitar, human voices etc.).
This process is extremely slow, however, especially for long WAV files, so it could not be used as is for a realtime application. However, if you used FFT to get a rough estimate of the fundamental, and then used autocorrelation to zero in on the true value (and you were content with being less accurate then 1/1000th of a semi-tone, which is absurdly over-accurate) you would have a method which was both relatively fast and extremely accurate.