John Legend was holed up in a London recording studio with Paul Epworth, at work on his next album (due in May), when he was inspired to pen a song for Quentin Tarantino's Django Unchained soundtrack.

"I had been reading about the film, very early on when it was in development, and I knew I wanted to write something for it," Legend told Rolling Stone. "But he didn't ask me to write it.

Still, Django's soundtrack is the first time Tarantino has collaborated with artists to produce original songs for a film. The list includes tracks by Jamie Foxx and Rick Ross, Anthony Hamilton and Elayna Boynton, Ennio Morricone and Elisa, and a James Brown and Tupac mashup, plus a smattering of archive-dives (like one from Jim Croce) that come from the director's own collection, complete with pops and scratches.

"When word got out what the movie was about, we discovered that it was actually inspiring artists to write songs with soul that underscored the love story and the revenge story," Django's music supervisor, Mary Ramos, said by email.

Foxx brought Ross to the set to write "100 Coffins," Hamilton and Boynton were inspired by the trailers, and Ramos' crew dug through the Tupac vaults and teamed up with the James Brown estate to produce a new song. Frank Ocean even wrote what Tarantino called "a fantastic ballad," but it didn't work with the film's final cut.

"There just wasn't a scene where it could be really featured," Ramos said. "Quentin didn't want to cheapen the song by just shoehorning it in, and wouldn't use it unless it could be really be heard, and used effectively to really amplify the emotional content of a scene."

In Legend's sweaty, soulful "Who Did That to You," hardcore R&B fans will recognize the grimy, funky lead-in from the Mighty Hannibal's 1967 single "The Right to Love You."

"The samples and different musical elements that Paul was bringing in, it just felt like we were in a creative space to create something that would be cool for Quentin," Legend said. "The sample had some of the flavor of the kind of music that I associate with Tarantino movies. It's really cool, it's almost campy, and it's got a sort of vintage to it."

Over a down-home organ groove, Legend offers a darker, grittier take than we usually hear from the silky crooner, vowing, "My wrath will come down like the cold rain and there won't be no shelter, no place you can go."

"The writing is pretty straightforward, and it is pretty spot-on in terms of the themes of the film," Legend said. "It's about retribution, it's about avenging your lover's honor, it's about a desire to find your love and exact retribution on whoever harmed her, which obviously fits perfectly with the plot of Django." (Foxx told Ross to rap about the same ideas for "100 Black Coffins.")

"A lot of my undergrad studies were about reading about American history and African American history, and America's long struggle with racism and how we deal with race and how important that struggle has been to the forming of this country," said Legend, a University of Pennsylvania grad. "These are things that I have thought about for years."

But in Hollywood, what's a good idea without an angle? "We heard from an inside source that [Tarantino] really likes listening to music on cassette in his car," Legend said. "So we converted the digital material into a cassette and I wrote him a note and I said, 'I wrote this song for your film, wanted to know if you would use it.'"

At last week's premiere, the technophobic Tarantino told a packed house at the Ziegfeld in New York, he received the tape with two months left in the shoot. "If I had gotten a link or something to plug into my computer, I would have thrown that shit away, because I don't know how to do that shit, but I got a cassette tape, and that I know how to play." The director ended up deploying the song in one of the film's most climatic scenes.

When Legend saw the completed film for the first time recently, he was pleased to find "they used it as a central narrative thrust of the film. He's on the horse and heading in back to the plantation, to get his girl. It's all building up to the goal that Django has of liberating his wife and exacting revenge on whoever was in the way of that happening. And as he was gearing up to do that, the song comes on and he's here to exact retribution."