How can noise be added to a signal to obtain a specific SNR?
Easiest way is to scale energy of noise. Nevertheless you must understand that for some files it is not that simple. Let's say that you have the recording of crying baby in silence. What is then your point of reference? Are you going to add white noise based on ratio between it's energy and average energy across the whole recording (silence+baby)? Or maybe you should calculate the energy in sliding window and then use only sound of interest as point of reference? That is why you should not add noise based on energy of your whole file. 

When it comes to some particular examples, then I suggest you to take a look here for mathematical derivation. If you are looking for functions, then please check out the MATLAB awgn function. Nevertheless if you want something even more sophisticated, then you might want to check the Audio Degradation Toolbox. You can find a dedicated routine for adding various types of noise with a given SNR.