Purring is caused by the vibration of the cat’s vocal cords. When air is taken into the lungs, it passes through the voice box that contains the vocal cords.
Purring is of course under the control of the cat. When it wishes to express its contentment, the vocal cords are allowed to vibrate as the air passes in and out of the lungs during breathing.
When the cat is asleep, or if it does not want to purr for some reason, the passage of air does not affect the vocal cords and no purr is produced.