There are a couple of possibilites that come to mind with your description. The first is Kennel Cough
. This is a harsh cough that usually results in bringing up foamy fluid which the dog often swallows back again. Kennel Cough is due to a combination of bacteria and viruses and can sound like your dog will cough itself to death
. Treatment aimed at the bacterial component of the infection. If not treated, the cough usually lasts about 7-10 days. In some cases, it can progress into a much more serious pneumonia.
Here's a link, including video of a dog with Kennel Cough:
The second possibility is a condition called Tracheal Collapse. The cartilage that forms the trachea (wind pipe) weakens as the dog ages and folds over on itself, which causes the dog to cough as it tries to get air into the lungs. This is a problem that is much more common in small breed dogs, and although it can't be cured, it can be controlled with medication.
Here's further information:
The third possiblity is that your dog is going into heart failure. Often there is a past history of a heart murmur and as the heart weakens, fluid builds up in the lungs which causes the coughing. Since this cough came on suddenly, and you didn't mention a heart problem, this is a less likely possibility.
Since your dog is 12 years old, it would be wise to have him checked out by your veterinarian. Older dogs may not get over an episode of Kennel Cough, for example, as easily as a young dog.
I hope this has been helpful; let me know if you have further questions.