There are two sources of sound from the discharge of a firearm. One is the "bang" created by the burning powder as it pushes the bullet out of the barrel. The other is the mini sonic boom (the "crack" that you hear) created by the bullet moving faster than the speed of sound.
It is possible to create a sound that is equal, but of opposite polarity that can theoretically cancel an unwanted sound. The "anti sound" source would have to be very close to the sound source. There was an "anti sound" automobile muffler patented, and prototypes made, a dozen or so years or so, but it's lifespan wasn't too good. The transducer was inside the muffler.
This technique could be used to cancel the "bang". Legal issues notwithstanding so can a suppressor (silencer). Both options would require a separate device for each firearm, and the "anti sound" technique (if even possible) would require different "anti sounds" for each type of ammo in a given gun.
The "crack" is a different issue. How do you use sound to cancel a sound source that is moving faster than the speed of sound? The bullet will outrun your "anti sound." I believe that this issue is not solvable in this manner, or certainly not cost effective if it is possible.
Subsonic ammo is a possibility in some guns. I have fired a .22 pistol with subsonic ammo and a suppressor (class 3 permitted), and all I could hear was the action clicking as the old round was ejected and the fresh one loaded.