Luckily enough for us musicians music isn't an exact science in the way your question looks at it. You have to add in Tempo, Rhythm and Key to the equation as well as just the actual notes played, there are many songs which share very similar melody lines but sound very different due to being played in totally different genres. As to the last part of your question when will we run out of new tunes......Unless we run out of original songwriters and music purely becomes a medium for cover versions and rip-offs by DJ's nicking bits from loads of different records just to make a pastiche then I hope we'll be ok for a good few years yet.