I can only relate to babysitting my nieces. So I was able to tell the difference upon them crying out of pain (dont think too bad of me, but they were twins and perfectly able to "hurt" themselves without real purpose by biting each other or pulling at each other hairs from now and then and other stuff ^^) which was very sudden and loud and had its own noise or if it was out of frustration/protest (so no real need, but something didnt go the way the wanted, like when a small plushie falls out of the sidecaged bed, and they are unable to reach it again).
The third different kind of crying, the "I feel unhappy out of the need for something/ I am not comfortable." I was basically able to tell, but upon the certain topic that made them unhappy, I simply had to do a checklist, so I couldnt separate tell by noise the difference about "Too hot, too cold, pampers wet, thirsty, bored, ..." Normally by checking that (feeling temperature at the face, seeking for sweat, feeling for cold fingers/toes, doing the smell-test at the pampers, offering something to drink/eat, offering entertainment/activity, ....) so I was able to find the "problem", but sometimes however they simply stayed crying (if one of them was crying the other one felt often affected too after a certain time - please god, I dont want twins. XD ), and then I simply swayed them slowly in the arms, until they normally slept at least after an hour or so. I felt bad, when I couldnt find the "problem", but my aunt told me, that this would happen to her from now and then as well, that they sometimes simply cry, without any recognizable reason, and that it would be ok, as long as its no "pain-caused" crying or goes on for hours and hours.