All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad is His slave and Messenger.
There is no doubt that it is not permissible for a Muslim to go to any place where there is evil unless it is for a valid reason, like denying the evil that the people are doing there and the like. Therefore, it is not permissible for you to go to a gym where there is music. Putting headphones into your ears does not make it permissible for you to go to that gym because of the existence of that evil.
As regards the contract that you have signed with that gym, if you can break it and get your money back, then this is good, otherwise you should seek the reward of Allaah for it and may Allaah compensate you with something better, as whoever leaves something for the sake of Allaah [out of fear of Allaah], Allaah will compensate him with something better. Allaah says (what means): {And whoever fears Allaah - He will make for him a way out. And will provide for him from where he does not expect.} [Quran 65:2-3]
For more benefit on the ruling of gyms that contain mixing and music, please refer to Fatwa 90256.
Allaah Knows best.