Computational music analysis from first principles