The tricky part would be maintaining the equalization + delay matching in a changing environment - imagine a person suddenly walks in between you and the speaker that's playing the music.
Unless your correction system can respond in real-time, you've accidentally created an audio-frequency bistatic radar :P
It's a very interesting problem. I'm worried about echos and crappy speakers distortion, but it looks totally posible.
I have wanted this at the speaker level, be able to cancel environmental noise coming through eg a window. A library of cancellation patterns, delivered through an aptly named sound bar.
Here is one approach that works: block/reduce the majority of sound physically. And then use s microphone with an adaptive filterbank to let in only the sounds you want to have. This is how hearing protection with speech passthrough works.
Masking with a static noise source can also help.
A. Time Domain
Fingerprinting a song introduces latency because the song has to play long enough to get an adequate sample for the fingerprint.
What would the headphones do before a match is established?
B. Space Domain
Canceling requires a waveform 180 degrees out of phase. It needs to be about the same size as the song. If you stream that waveform, there’s latency and mechanical rights issues. If it is stored locally, you need space for all-the-songs.
C. Music is bigger than Texas
Remixes, covers, recordings of live performances add complexity to fingerprinting and increase the size of the phased wave database.
Good luck.
Back in 2018 I spent a week or two messing around with this idea, and produced a hacky proof-of-concept.[1] It's not intended for real-time or production use, I just made a prototype to see if it could be done.
The README explains the method: once the contaminating song is identified, it syncs up the recordings in time with a correlation analysis, adjusts for frequency-dependent gain effects, then subtracts the undesired content.
Warning: I'm not an audio engineer, the output sound quality is NOT good! This was just a toy project in my early days of learning to code. I assume there are much better ways to approach this that would yield significantly better results.
[1] https://github.com/mitchellpkt/tracksubtract