I have now moved a response to this to the Tutorials Category as i felt it a more appropriate place to post it.
Basically you can find a scale (and chords) to harmonise a melody once the melody has been detected. You then have to fit the timing of the chords to the melody…