According to the Catholic Church, was Mary's Magnificat a spontaneous utterance? 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., &quot;Mary in the New Testament&quot;, pp. 89-90 of Juniper B. Carol, O.F.M, Mariology (vol. 1), 1955.
Magnificat (Lk. 1) / Hannah (1 Sam.)  parallels
Lk. 1:46: My soul doth magnify the Lord.
1 Sam. 2:1: My heart hath rejoiced in the Lord, and my horn is exalted in my God: my mouth is enlarged over my enemies: because I have joyed in thy salvation.
Lk. 1:48: he hath regarded the humility (ταπείνωσιν, humilitatem) of his handmaid
1 Sam. 1:11: thou wilt behold the affliction (ταπείνωσιν, afflictionem) of thy servant
(DRC doesn't have &quot;affliction&quot;.)
Lk. 1:52: He hath put down the mighty from their seat and hath exalted the humble.(cf. Ps. 109:1: &quot;…make thy enemies thy footstool.&quot;)
1 Sam. 2:6: The Lord killeth and maketh alive, he bringeth down to hell, and bringeth back again.
Lk. 1:53: He hath filled the hungry with good things: and the rich he hath sent empty away.
1 Sam. 2:7: The Lord maketh poor and maketh rich, he humbleth and he exalteth:
courtesy: The Biblical Doctrine of Virginity by Lucien Legrand, M.E.P., p. 124
From the article on 1 &amp; 2 Kings (1 &amp; 2 Samuel) by H. McKay, O.F.M., in A Catholic Commentary on Holy Scripture (ref:68.5):

§ f 2:1–10 Anna’s Hymn of Thankgiving—This is sometimes called ‘the Magnificat of the OT’. Both Mary’s song and Anna’s canticle are poetry and prophecy. St Luke’s narrative (Lk 1:46–55) shows how deeply Anna’s words had affected our Lady’s mind. But when they pass through her soul, they catch something of the fragrance of her personality and are clothed with her own quiet restraint and selfless humility. In the present context, the leading idea seems to be the praise of Yahweh who has given deliverance to Anna. The proud and boastful must be silent before him, for he weighs all their thoughts and actions. In his sovereign government of the world he can reverse the positions of weak and strong, mighty and lowly. This great moral principle of his rule will receive its complete fulfilment in the judgement of the world and the exaltation of Yahweh’s anointed King.

Our Lady, being very knowledgeable of Holy Scripture, certainly was thinking of Hannah's barrenness / misery and how happy she was conceiving a child, Samuel.