how the game picks a track
Music in Terraria is state driven rather than scripted. The game looks at where you are and what is happening, picks the matching track, and loops it until one of those changes. There are well over a hundred of them, so nearly every biome and event has its own. All of it is client side: what plays for you is decided by your position rather than the server's, so in multiplayer two people standing in different biomes are hearing different music, and the volume slider only touches your own game.
keeping a track
The Music Box records whatever is playing and turns into a placeable copy of it. Once placed, it plays that track wherever it sits, which is how a base or a single room gets a permanent theme regardless of the biome it was built in. Short of replacing files, it is the only way to hear a track outside the conditions that normally trigger it.
On desktop, resource packs can swap any track for a file of your own, which covers everything the boxes cannot.
the otherworld soundtrack
When Terraria: Otherworld was canceled, its soundtrack survived. Twenty seven of those tracks were folded into the main game with the 1.4 update, where by default they take over from the usual music in any drunk world. Visiting one of those worlds once also gives the Party Girl a Music option in her dialogue window, and that unlock is permanent across every character and world you own, not only the one that granted it. From then on you can run the alternate soundtrack anywhere.
what the swap actually changes
Every track gets replaced, but there are fewer of them, so some places share. Anywhere without a direct equivalent falls back to the closest match, which means the Underground Jungle plays the surface jungle theme rather than getting one of its own. Names are reused from the originals, which would make the boxes ambiguous, so the alternate ones are called Otherworldly Music Box to keep them apart in storage.
otherworld music in multiplayer
The toggle is per player and does not travel. On a shared server only the people who have unlocked the option and turned it on hear the alternate soundtrack. Everyone else gets the normal one in the same place at the same moment. The boxes are the exception: an Otherworldly box plays for everybody in earshot whether or not they have ever visited a drunk world. Leave the world and your soundtrack reverts on its own.
who wrote it
Most of the soundtrack is the work of Scott Lloyd Shelly of Resonance Array, which is why it holds together across more than a decade of updates. The exceptions are all crossovers or one-offs. Klei Entertainment wrote the Deerclops theme to go with the fight it arrived in. The Torch God's track came from Prosthetic Orchestra. The Day Remix was done by Xenon and DSniper.
two odd histories
The Towers appeared on the second soundtrack album a full two years before it played in the game, arriving with 1.3.0.1. It had been written for a final boss fight that was redesigned in the meantime, which is why an album track existed with nowhere to go. The Martian Madness theme came out earlier still, released as a teaser for 1.3 before anyone knew what the event attached to it would be.

