A theory of emergent quantum mechanics