At Luke 1: 46-55 we see Blessed Virgin Mary singing a song of praise, now known as the Magnificat, while greeting her cousin Elizabeth. I wish to know if the song of Mary was a spontaneous utterance, or was it an integration of prayers from the old Jewish scripts and the Psalms ? What do the teachings of Catholic Church say on the source(s) of the Magnificat ?
Mary knew Holy Scriptures very well, and her Magnificat canticle was inspired by the Holy Ghost.
During her Visitation with St. Elizabeth,
Mary, too, was inspired by the Holy Spirit. She improvised the poem known to us as the Magnificat,27 from the opening word of its Latin version. It is charged with reminiscences of the psalms and other writings of the Old Testament, showing that Mary's mind was steeped in Holy Writ. In the first strophe (Lk. 1:46-50), she meditates with restrained enthusiasm upon the mercy, power, and holiness of God, who had chosen her for so great a dignity. She explains in the second strophe (vv. 51-53) that God is wont to exalt the weak and depose the proud. In the third strophe (vv. 54-55), she praises God's fidelity in fulfilling through His Son the promises made to Abraham and his posterity.
source: Fr. Michael J. Gruenthaner, S.J., "Mary in the New Testament", pp. 89-90 of Juniper B. Carol, O.F.M, Mariology (vol. 1), 1955.