Our Rabbis taught: One should not stand up to say Tefillah while immersed in sorrow, or idleness,
or laughter, or chatter, or frivolity, or idle talk, but only while still rejoicing in the performance of a mitzva.
Similarly a man before taking leave of his fellow should not finish off with ordinary conversation, or joking, or frivolity, or idle talk, but while rejoicing on the performance of a mitzva [גירסת הגר"א]. For so we find with the early prophets that they concluded their harangues with words of praise and comfort.
We move to a similar gemara in Psachim [117a].
Our Rabbis taught: As for all the songs and praises to which David gave utterance in the Book of
Psalms etc. 'To David, a psalm’ intimates that the Shechinah rested upon him and then he uttered [that] song; ‘a psalm of david’ intimates that he [first] uttered [that particular] psalm and then the Shechinah rested upon him. This teaches you that the Shechinah rests [upon man] neither in indolence nor in gloom nor in frivolity nor in levity, nor in vain pursuits, but only in rejoicing on the performance of a mitzva, for it is said, ‘but now bring me a minstrel [מנגן]. And it came to
pass, when the minstrel played, that he hand of the haShem came upon him'.
Rav Yehudah said in Rav's name: And it is likewise so in a matter of halachah.
R. Nachman said: And it is likewise so for a good dream.
We must understand why in connection with davening in our sugya in Brachos we bring the example of taking leave of his fellow with simcha and in Psachim we do not. We also omit the notion of השראת השכינה and דבר הלכה and חלום טוב in Brachos while we mention them in Psachim.
The answer is that the two sugyos are talking about two distinct types of dvar mitzva. In Psachim the gemara later says the learning should be opened with a מילתא דבדיחותא
- humor - which is not inherently a mitzva but is for the sake of a mitzva. To achieve השראת השכינה or to learn a דבר הלכה or to get a חלום טוב it is a mitzva to arouse himself to a state of joy with something that naturally makes one happy.
The gemara in Brachos in contrast is talking about a simcha which is inherently a mitzva as is clear from Rashi who writes אלא מתוך שמחה -- כגון דברי תנחומין של תורה.
This would also explain why in Pesachim says שמחה של דבר מצוה [unlike the gemara in Brachos which just says שמחה של מצוה] - one says something that brings to simcha of mitzva [such a music for nevua] but is not a mitzva per se.
ספר מרבה עמידה מאת מו"ר הרה"ג ר' יעקב דוד המניק שליט"א סי' כב
[Note - Why taking leave of your friend requires an inherent mitzva eludes me...]