In-universe, the explanation is simple: The Hobbit was Tolkien's translation into English of the original material from Common Speech, since the plot device is that both The Hobbit and LOTR are the writings of Frodo and Bilbo, that he (Tolkien) had access to and translated.
The Common Speech, as the language of the Hobbits and their narratives, has inevitably been turned into modern English .
In the process the difference between the varieties observable in the use of the Westron has been lessened.
Some attempt has been made to represent these varieties by variations in the kind of English used ...
( LOTR:
The Return of the King, Appendix F, "II.
On Translation" )
As such, it's a common thing with the translators to employ idiomatic translations instead of literal - and as a linguist, Tolkien surely was familiar with translation techniques.
As other answers noted, both Tolkien himself, as well as his intended audience (English speaking people of 20th century) knew what "like drums and guns" sounded - probably a lot better than whatever idiom was used by Bilbo in the Common language.
So, this is a very valid approach to the text.
scifi.stackexchange.com/users/976/DVK-on-Ahch-To
One has indeed personally to come under the shadow of war to feel fully its oppression; but as the years go by it seems now often forgotten that to be caught in youth by 1914 was no less hideous an experience than to be involved in 1939 and the following years.
By 1918, all but one of my close friends were dead.
—
J.R.R. Tolkien, foreword to The Lord of the Rings Even though it was a translation from the original
, I am pretty sure this was only an image of speaking to make the modern people imagine how the roar really sounded like in his mind.
J.R.R. Tolkien’s service in the British Army during World War I may have influenced his fiction.
More here: JRR Tolkien and World War
I J.R.R. Tolkien in uniform, 1916.
scifi.stackexchange.com/users/38436/Rocket
I doubt this is what Tolkien had in mind, but the OED gives several obsolete meanings of "gun" which would make sense in a world with no firearms but does possess "blasting fire".
The first is used to describe any large engine of war.
1689
R. Milward Selden's Table-talk 30
The word Gun was in use in England for an Engine to cast a thing from a man, long before there was any Gun-powder found out.
This would cover the tossing part, but not the roar.
A better fit is used to describe a blasting device.
1753
Chambers's Cycl.
Suppl.
Gun is also a name given by the miners, to an instrument used in cleaving rocks with gunpowder.
It is an iron cylinder..
having..
a hole drilled through it to communicate with the inside of the hole in the rock.
scifi.stackexchange.com/users/1441/Schwern
A etymology search shows gun to come from gunne meaning war.
mid-14c., gunne "an engine of war that throws rocks, arrows or other missiles," probably a shortening of woman's name Gunilda, found in Middle English gonnilde "cannon" and in an Anglo-Latin reference to a specific gun from a 1330 munitions inventory of Windsor Castle ("...una magna balista de cornu quae Domina Gunilda ...
"), from Old NorseGunnhildr, woman's name, from gunnr + hildr, both meaning "war, battle.
"
First element from PIE *gwhen- "to strike, kill" (see bane); for second, see Hilda.
A key meaning is siege engine, aka catapult, ballista, or trebuchet, whose munitions produce a large roar or crash on impact, and would be recognizable to middle earth inhabitants.
scifi.stackexchange.com/users/None/
Gunpowder hadn't really taken hold in ME at this point but it was used at Helm's Deep by the forces of Saruman to breach the wall.
This is opinion based of course
but one could reasonably suspect that the word was used for the readers benefit in describing Beorn's assault on the platoon of Bolg.
scifi.stackexchange.com/users/31828/Omar Devon Little
'The Hobbit' is written in a very different style to LotR. When he was writing 'The Hobbit', Tolkien didn't yet have the details of Middle-Earth and its history worked out, nor had he yet adopted the flowing, almost archaic style of writing that still characterises fantasy literature to this day.
He probably used the word because it was what came to his mind as a good description, without thinking about whether it fitted with the sort of world he was creating.
scifi.stackexchange.com/users/31394/Rand al'Thor
In Chapter XVIII -  The Return Journey  of the book ' The Hobbit ', Tolkien uses the word  gun .   'The roar of his voice was like drums and  guns ; and he tossed  wolves and goblins from his path like straws and feathers'   As far as I know guns are not used as a weapon in Middle-earth and I don't remember them being mentioned neither in  The Hobbit  nor in  LotR .  Was this a mistake on Tolkien's part or is there another explanation?
The use of the word "gun" in The Hobbit
scifi.stackexchange.com
<the-hobbit><tolkiens-legendarium>
scifi.stackexchange.com/questions/76397
scifi.stackexchange.com/users/3542/bobbyalex
Both Tolkien and his intended audience (English speaking people of 20th century) knew what "like drums and guns" sounded like. Tolkien was translating Frodo and Bilbo's "Common Speech" into English. The word gun was probably employed by Tolkien because it is descriptive and worked well as a translation of the original. Furthermore, Tolkien’s time in the army probably influenced his choice to use the word gun regardless of the context. 
He probably used the word because it was what came to his mind as a good description, without thinking about whether it fitted with the sort of world he was creating.
The word gun was probably employed by Tolkien because it is descriptive and worked well as a translation of the original. Tolkien’s time in the army probably influenced his use of the word gun. 