Music and crime fiction. They go so well together that it's become something of a cliche. You know the kind of thing: the lone detective who comes into his apartment late at night, gets a beer or bourbon and stares out of the window wracked by existential angst at the horror he's seen, all the while listening to cool jazz. And it's always cool jazz – never Chris Barber doing When The Saints Go Marching In. Same with Morse and his opera. Always dark and Wagnerian – never Pirates of Penzance. I know this is shorthand to show the detective is troubled about what he (usually he) has seen and what he should have done but, really, is it an accurate picture? And is it only a boy thing?

So what role does music play in the creation of crime fiction? Is there such a thing as a killer soundtrack? And does the music crime writers write to differ from that of other writers? I should know the answers. As well as being the Theakston's Old Peculier crime writing festival's reader in residence I've also been a professional crime novelist for over 15 years and, like most men in their 40s, an amateur musicologist.

These days crime writers are more likely to be seen at gigs than literary events. As well as passing on new books they've discovered, they'll be giving other writers mix CDs of new bands. I came across Lord Huron, Caitlin Rose and Night Beds that way. I'm pretty sure crime writers are more likely to be frustrated rock stars than any other genre of writers. In fact, Jo Nesbo actually is a rock star. As bestselling, award-winning novelist Mark Billingham, creator of Tom Thorne, said to me recently, "You remember when you and I were at a Richmond Fontaine gig a few years ago? We looked around and most of the crime writers in London were in that audience!"

In an average book of mine I'll probably name check Warren Zevon, Band of Horses, Wintersleep, Tom Waits, Midlake, Gillian Welch and plenty of others. Like many other writers, I give my lead character the same tastes as me so it's easy to use songs I know to create a kind of emotional shorthand while I'm working. The tone of the music seeps into and informs the writing. It can soundtrack a scene, create an atmosphere in a few sentences where whole paragraphs would have to be used otherwise. Elmore Leonard and George Pelecanos are the undisputed masters of this, giving a scene an immediate sense of time and place just by what they've got playing on the jukebox in the background of a scene and their characters' cultural responses to it.

So what about that killer soundtrack? "The only music I can listen to while writing is instrumental; otherwise, the lyrics get in the way," said Steve Mosby, the award-nominated, Leeds-based bestselling crime author of Still Bleeding, Back Flowers and most recently, Dark Room, "I often write in pubs, and have to tune out the background noise. At home, I find I listen to tracks in between bursts of writing – either as a reward, or for a break, and often to help conjure up an atmosphere. Often it's about association – for example, the soundtrack to the film Snowtown makes me feel bleak and nervous, and is perfect for darker material. But I remember listening to Radiohead's Optimistic over and over when I was struggling with one book, as the line "You can try the best you can/ The best you can is good enough" became a sort of mantra that helped me keep on writing."

I know what he means. I once played Tom Waits' Ruby's Arms on repeat as it had just the right melancholic tone and atmosphere for the scene I was working on. I was a wreck at the end of it, but the scene was bang on.

Mark Billingham has his own take. "Country is perfect music for crime fiction, I think. These songs are bleak, black stories but told in an entertaining and melodic fashion. I think the fact that Thorne loves this stuff says a lot about him. He relishes the bigoted reaction it provokes, as do I."

So does the music ever get in the way of the words? "No," says Cathi Unsworth, the critically acclaimed author of Bad Penny Blues, The Singer (a clue to how much she likes music in the title, I think) and, most recently, Weirdo. "I started my career in the Sounds newsroom with loud music blaring out all the time. I find it galvanises me and helps me to focus. The only bands I find impossible to write to are Led Zeppelin and the Cardiacs because I just want to start headbanging."

Unsworth also likes to subvert expectations of what a crime writer should listen to when they're engaged in the act of killing: "I want the music to reflect the horror of the killing, rather than get me in the mood to join in. Of all my books, Bad Penny Blues has the spookiest soundtrack, as innocent-seeming songs like You'll Never Walk Alone, She's Not There and You've Lost That Lovin' Feeling take on a whole new context in the setting of a phantom serial killer picking off working girls, walking among them, being known by them as someone they thought it was safe to go with. The songs weren't chosen at random, they were in the charts as the Jack the Stripper murders happened in real life, which makes it even scarier."

Juxtapositioning is everything for a crime writer. There's a song I've always wanted to use in that context: Wouldn't It Be Nice by the Beach Boys. It's so happy and optimistic it makes me cry every time I hear it. Obviously it would be the perfect soundtrack to a scene of total nihilistic devastation. So next time you pick up a crime novel and see music references just think: they're not just the writer showing off. They're integral to the story. They're necessary. I could talk about this all day but I've got a book to write. And more importantly, John Grant's got a new album out …

• Martyn Waites is the author of the Joe Donovan series and also writes as Tania Carver. His book, The Woman in Black: Angel of Death, a follow-up to Susan Hill's bestseller, will be published by Hammer, Random House in November 2013. Catch Martyn Waites, Mark Billingham, Steve Mosby and Cathi Unsworth at the Theakston's Old Peculier crime writing festival 2013, at The Old Swan Hotel, Harrogate, from 18-21 July. Other writers at the festival include Ian Rankin, Val McDermid, Kate Atkinson, Susan Hill, Ruth Rendell, Jeanette Winterson and Lee Child