By the HRTF, the Head Related Transfer Function.
This is, in mathematical terms, the function that is applied to a sound wave, which shapes it depending on where it comes from. It affects different frequencies differently, and it’s slightly different for everybody.
Your brain notices slight differences in timing and frequencies between your left and right ear, and also the reflections and dampening of your ear shape, which is different depending on where sound comes from. From that, it creates an intuitive understanding of where sound is coming from. This has to be learned.
Latest Answers