Sama, sacred listening, can carry the heart toward God when the intention is pure
Rather than ruling on the contested question of music's permissibility once and for all, al-Ghazali argues that the very same sacred listening can either intensify a heart already oriented toward God or inflame the desires of one that is not.
Book 18 of the Ihya, on sama and wajd, enters directly into a live legal and theological controversy of al-Ghazali's own time over whether listening to music is permitted in Islam at all, and rather than settling the question with a blanket ruling, he argues that the effect of sama depends heavily on who is listening and with what intention. For someone whose heart is already oriented toward God, sama can intensify and clarify existing devotion, acting almost like a catalyst on a state already present; for someone whose heart is dominated by worldly desire, the very same music and poetry can just as easily inflame that desire instead, so the practice is not safe or beneficial by default. Al-Ghazali specifies practical safeguards in the chapter, appropriate content, a genuine spiritual setting rather than mere entertainment, and honesty about one's own motive for attending, reflecting his broader method of treating even devotional practices as double-edged, capable of forming or corrupting character depending on how they are approached.
Ihya Ulum al-Din