It must be the nexus of setting and soundtrack, but there is nothing on Earth quite like the Festival of Gnawa. By the middle of the first night, as you walk through Essaouira's Medina you feel as if you're in some great interconnected sound resonator: the bass of the ghimbri, the magical instrument of Morocco's Gnawa masters, and the high, frenzied clack of the krakeb percussion gusting with the wind over riad roof terraces and down narrow, crowded streets pressed with tens of thousands of people moving to and from one of the half-dozen open-air stages scattered through the old town.
Once a beautiful, crumbling and quiet destination for hippies, travellers and surfers - it's the "wind city" of north Africa - things have changed in Essaouira over the last decade, and much of it is down to the impact of the Gnawa Festival, a free annual event spread over four days at the end of June that transforms this beautiful, languid port of some 70,000 people into one of Morocco's biggest musical gatherings, with an estimated half a million attendees last year.
The festival was launched in 1998 and for its 12th edition the organisers have had their work cut out in battling the knock-on effects of a worldwide recession. Aside from the late-night lilas, the intimate musical ceremonies held in a handful of Essaouira's most beautiful riads, the festival programme is completely free, and the scrabble for sponsorship in a climate of global austerity has resulted in a tighter budget and focus in the festival programming: less international stars, more great Gnawa Malaams descending upon Essaouira from across Morocco. Still, this is good news if you like your Gnawa undiluted, straight from the source.
Western jazzmen still come, of course - the fusion of Gnawa with jazz has been a cornerstone for the festival's founders. This year, international guests included the American saxophonist Donald Harrison, whose new swing style blends the form's classic rhythms with the architecture of funk, hip-hop and R&B. With New Orleans ' Congo Nation, the Mardi Gras troupe blending African-American with Native American forms, Harrison headlined the big stage in Place Moulay Hassan by the port on Friday night with the mighty Malaam Mahomed Kouyou from Marrakech, who helped steal the show last year with a blistering end-of-set collaboration with the jazz giant Wayne Shorter. This year, Harrison and Congo Nation achieved similar levels of on-the-spot synthesis between their own music and the often thunderous wall of sound and movement the Gnawa can achieve.
For many aficionados, Gnawa is one of the world's root musical forms, the one ingredient that mixes with all the others. Which can explain why such an ancient folk tradition has found its constituency within the contemporary sphere. Moroccan teenagers chant the names of Gnawa Malaams as if they were football stars, past guests have included the London-based Moroccan musician U-Cef and Asian Dub Foundation, and this year Arrested Development brought their own brand of hip-hop to the Place Moulay Hassan.
World-music fans also get their fill, with Toumani Diabate and Bassekou Kouyate delighting audiences last year, while this year's west African artists included the Senegalese kora player Solo Cissokho and the Malian singer Babani Koné. Elsewhere, Nass el Ghiwane, the Moroccan band once described by Martin Scorsese as "the Rolling Stones of Africa", delivered a brilliant acoustic set on the beach stage. Political in terms of asserting their own culture in the face of increasing global homogeneity, the audience's reaction to this band confirmed the relevance of that message today.
The music's roots can be found in the Sudan and West Africa, in peoples inveigled into slavery and service in the Morocco of centuries past and who became known as Gnawa. Indeed, the beautiful blue and whitewashed streets of Essaouira's medina were designed by a French architect in the 18th century and built by Gnawa who had been shackled, bound and brought in by the boatload. It is even said that the distinctive head dance evolved because it was the only part of the body that the slaves could move.
Over its 12 years, this festival has achieved a huge amount in changing the perception of Gnawa in Morocco. Malaam El Gadari, who now lives in Italy and performed with Moroccan DJ Hak'x on the beach stage and later at Chez Kabir with a more intimate set, sees the impact of the festival on Gnawa culture as "gaining not only recognition but respect". "Musicians who come to the festival to play with us describe many similarities to Gnawa in their own approach to music and rhythms," he adds. "From Korea, Argentina, from all over the world. Whether it's a lila or a concert in Paris or Genoa, the process, it is the same. What is different is that each culture interprets the chords and the colours in its own way. We Gnawa see our music in terms of colours, not in terms of simplicity or complexity. It's not intellectual for us, but a feeling."
One of this year's festival's innovations has been the launch of an artist-in-residence programme to replace the ad hoc jam sessions between European and American jazz musicians and the Gnawa. The pairing of the 25-strong West German Radio Orchestra with Malaam Hamid el Kasri and an eagerly anticipated appearance from Rai superstar Khaled, was a prime example, with the orchestra's brassy backdrop meshing with a Gnawa repertoire.
When Khaled finally appeared in the early hours of the morning, the intensity of his stage presence was palpable even from a distance. There was no mistaking the charisma and sense of direct, heart-to-heart communication he has with his audience. "When I'm on stage I'm always shivering," he said earlier. "When I sing to people I can't forget their faces. I keep looking at the faces of the young people and they definitely need something. I love that I can help bring them what they need."
During a set that included an ebullient, Gnawa-powered version of his international hit Didi, Khaled contributed a peculiarly deep, string of vocal injections to El Kasri's singing, looking over the crowd as if he couldn't believe his eyes. When he finally took up the reins and unleashed his soaring voice, it made an intoxicating mix with el Kasri's broiling, interweaving Gnawa rhythms. Each year, there is one moment that epitomises the magic of this unique musical gathering casts. This year, it was sitting on the terrace of the Rencontre restaurant as the African sun sank into the Atlantic. From the floor below erupted the sound of ecstatic female voices and a dense, pounding percussion. We clambered downstairs to find the floor below occupied by the Hoara of Taroudant - a group of Berber women from the High Atlas mountains - performing off-festival around the medina.
Within the confines of the restaurant, their joyous noise was overwhelming. Electrifying ululations sinking deeper than an archaeological dig and a woman with a blue tattoo on her chin squatting in the middle of it all, cracking out rhythms on a metal tea tray. The big stars on the big stages may provide thrilling spectacles, but it's these unexpected, intimate moments of musical revelation that make you vow to return to Essaouira again before the big stages are even broken down.