Last summer, a carefully chosen group of producers was given a challenging task – to complete Tim, the third album by the late Swedish DJ and producer Avicii, AKA Tim Bergling. Released this week, the album was roughly two-thirds finished when Bergling killed himself on 20 April 2018 aged 28.

His was the defining voice of EDM, the bombastic, Technicolor dance music that has dominated the charts in this decade. His 2013 song Wake Me Up, with soul singer Aloe Blacc, spent 26 weeks at No 1 on the US Billboard dance singles chart and was for a time the most-streamed song in Spotify history. Bergling became one of the world’s highest-paid DJs and collaborated with Madonna, Coldplay and Nile Rogers, who called him “one of the greatest natural melody writers I’ve ever worked with”.

As Bergling’s fame intensified, so did his problems. After being hospitalised in 2014, he was diagnosed with acute pancreatitis caused by excessive drinking. He later had surgery to remove his gallbladder, was prescribed drugs (including the addictive opioid Percocet) for physical pain, depression and anxiety, yet continued his gruelling schedule.

By 2016, he’d had enough. He fired his manager and retired from touring. “I know I am blessed to be able to travel all around the world,” he said at the time, “but I have too little left for the life of a real person behind the artist.” In 2017, he released a documentary, Avicii: True Stories. In the final scene, Nina Simone’s Feeling Good blares out as Bergling sunbathes on a tropical beach. It was during this period that he began working on Tim.

Previous posthumous records, such as Aaliyah’s I Care 4 U, Eazy-E’s Str8 Off tha Streetz of Muthaphukkin Compton and Joy Division’s Closer, have served as more than just albums; they are memorials to their creators. And the task of finishing the work of a person who dies in shocking circumstances presents obvious difficulties – especially when the creator was a producer, not a vocalist: how can you honour their sonic intentions?

In the centre of Stockholm are Kinglet Studios, run by Carl Falk, and, a few streets away, Studio Gottefar, occupied by Vincent Pontare and Salem Al Fakir, AKA the duo Vargas and Lagola. These producers, who considered Bergling both a collaborator and a good friend, completed six songs on Tim. It is the most nakedly pop-sounding of all of his albums, edging away from the buzzsaw riffs typical of EDM towards gentler melodies with romantic rather than hedonistic lyrics.

Sitting at his desk in Kinglet, Falk has ordered meatballs for lunch (“What a cliche,” he says. “I never normally do this”). He co-wrote One Direction’s What Makes You Beautiful and Nicki Minaj’s Starships, which he modestly calls his “poor attempt to do a Swedish House Mafia song”. It was Bergling’s father, Klas, who approached him to complete the album. “I remember it being not awkward, but definitely strange,” he says softly. “Like he was a messenger of Tim’s ... ‘I have a proposal for you, but I’m still his dad.’”

The Bergling family have been working to create a legacy for their son. This year, Klas gave a keynote speech at Ibiza’s biggest dance music conference, the International Music Summit, about mental health in the industry, and the family launched the Tim Bergling Foundation, using Bergling’s immense personal wealth to work with charities dedicated to suicide prevention and mental health issues. The album is part of this legacy – all net proceeds will go the foundation – but despite the positives, it has still been an emotional process for those charged with finishing the songs.

When Falk met Bergling in 2010, he was struck by his innocent approach to a genre rife with cynicism. “With these [EDM] guys, you never know if they’re actually the people behind the music,” he says. “But you could tell instantly with him.” They met up at Kinglet, played around on the guitar and piano, and Falk was sold. “I would think: ‘He’s so full of ideas, how does he ever finish a song?’” Bergling would spend hours on a simple melody, and was stubborn. “If you wanted to change something he’d done, you’d better have the best argument as to why. But he also learned to trust me.”

Carl Falk. Photograph: Ibl/REX/Shutterstock

Klas approached Falk to finish three songs – Ain’t a Thing, Bad Reputation and Fades Away – which he and Bergling had worked on together. “There was never an ego with him,” says Falk. “As long as I do something that betters the song, I’m going to go for it.” He sits at the keyboard, explaining the DNA of Bergling’s sound with teacherly warmth: Bergling’s signatures are simple, repetitive melodies that mix major and minor chords within the same melody line. The major keys are “like Schlager hooks” – referring to the Germanic Europop tradition – and the minor keys come from country and blues, loaded with longing. When Bergling and Falk initially collaborated, they spoke about sampling south-east Asian and West Indian instrumentation. For the rhythmic steel-drum fill sounds on Bad Reputation, Falk drew direct inspiration from Helt Seriöst, a song by the Swedish rapper Kaliffa that Bergling loved.

Falk says that Bergling would “recycle” clips from older songs, meaning he could do the same, even using parts for Bad Reputation from Bergling’s mega-hit Levels. Falk also knew that Bergling wanted to work with a live orchestra, so he programmed versions of his synth riffs for a string arranger, who interpreted them with an orchestra for the album’s last song, Fades Away.

Falk has few moral or ethical qualms about the process. “When someone close to you passes, the emptiness becomes a mission,” he says. “He told me that this was the first time he’d enjoyed making music in years, so I had to make this as good as I possibly could to honour him and our friendship.”

At Gottefar, Pontare stands tall and broad, with long blond hair, a thick beard and extravagant silver rings on each finger. He nods and hums while Al Fakir speaks with jittery enthusiasm. They began working with Bergling in 2012 and co-wrote and produced his breakthrough singles Hey Brother and Waiting for Love. They also worked on three songs for Tim – Piece of Mind, Tough Love, and Excuse Me Mr Sir – all co-written with Bergling.

Al Fakir is keen to emphasise that the making of Tim was a happy time for the shy star. “Tim getting out of that bad place, exercising and meditating,” he says. “He was strong. Everyone could see it.” His sentences trail off when we talk about his death, and whether listening to Tim is a darker or more revealing experience than they had anticipated. They felt Bergling had the same energy and focus during the writing of Tim as during the sessions for his debut album, True. As with Falk, Al Fakir and Pontare evince typical Scandinavian discretion when talking about their friend, preferring gentle diplomacy to emotional outpourings. “There are still more questions than answers, because we’d seen him in a good place,” says Al Fakir.

Still, they echo Falk’s impressions: that Bergling was brimming with melodic ideas, and cared more for detail than overall effect. “He was explaining how he wanted the ad libs at the end of the chorus to sound before we’d written the chorus,” laughs Pontare. “We used to say that he’d choose the china for the kitchen before the house was built.” In the years between his retirement from performing and his death, Bergling spent much of his time in Los Angeles, where Pontare and Al Fakir often joined him in his home studio. Drawing on his success with country crossover on Wake Me Up, Bergling was curious about incorporating 90s Californian rock motifs into Tim; Red Hot Chili Peppers, Beck and Crazy Town’s 1999 skater rock-rap hit Butterfly were references.

Vincent Pontare, left, and Salem Al Fakir, AKA Vargas and Lagola. Photograph: Chris Pizzello/Invision/AP

Pontare has fond memories of writing lyrics with Bergling while sitting on his balcony, talking about his recovery. Piece of Mind is about “a society that’s moving too fast” and is a song that the duo struggled to listen to after his death, while Pontare says that Excuse Me Mr Sir is about the “corporate world of dance music, and how one person clashes with that”. Listening to the songs after Bergling’s death, they find that they illuminate his state of mind; how the DJ felt pressured to live up to the outwardly glamorous lifestyle of his profession, and how young fans might cope with having seen their musical hero struggle.

Their roles in the posthumous production process focused largely on arrangement, editing and finishing vocal recordings. For the romantic duet Tough Love, they spoke with the Bergling family and agreed they would not seek out any new vocalists to feature. “I don’t know how much your heart must wear out, as a parent, doing this,” says Al Fakir. The duo re-recorded their guide vocals from the demo with Pontare and his wife, the Swedish singer Agnes, to keep it as close to the last Bergling-approved version as possible.

They say that they found the strength to finish the sessions by speaking with the other producers involved over their months of shared work, and have become closer to the Bergling family in the process. “Even the interviews talking about the process are part of the therapy for us,” says Al Fakir. Most importantly, “[Bergling] loved these songs, and wanted them to come out,” he adds. “That’s the sad part. He’s not here, and I feel like he’d just started something.”

Tim by Avicii is out today. In the UK, Samaritans can be contacted on 116 123 or email jo@samaritans.org. You can contact the mental health charity Mind by calling 0300 123 3393 or visiting mind.org.uk.