I recently had a problem with an extremely loud, machine gun type sound in my water pipes whenever someone turned on the hot water too quickly. I would also hear a loud bang noise when any toilet was flushed, when the water would stop flowing in. At first it was fairly random when it would happen but as time went on it became more and more evident, to the point where I was finally able to track it down to the cause.
The culprit was an automatic shutoff valve that I had installed on the supply side of my water heater about a year and a half ago. The valve was a solenoid shutoff valve with a rubber flapper inside that would seal off the supply when actuated. For some reason it became defective and would attempt to shutoff the supply and then open to allow flow again, only it did this very rapidly in succession, which gave off the sound of a machine gun in my pipes, and I'm not exaggerating about the sound of a machine gun... it was that loud.
When I called the manufacture I was told that they had experienced this problem with this type valve and had since gone to a motorized ball valve. They were extremely nice and shipped me a new shutoff assembly for free, which has corrected the problem completely. It also corrected the banging problem I would hear whenever a toilet was flushed... that really surprised me. I thought for sure the two problems were unrelated.
Anyway, I just wanted to share this experience in the hopes of helping someone else who maybe experiencing the same problem. If you are and you have one of these valves installed, remove it and see if your problem goes away.
Cheers... Randy