How is an mp3 file compressed?
The MP3 file takes up less space but loses information from the original recording, so it is a lossy compression. The question is, what is the algorithm for scrapping those details of music? How are they removed from the recording? Don’t they really matter and we don’t perceive those losses?
MP3 and auditory masking
The algorithm for MP3 compression eliminates details of the original music based on the phenomenon of the sound masking of our sense of hearing, a psychoacoustic phenomenon so daily that surely many will not have paid attention before, and that it is necessary to know to understand the MP3 .

Imagine that we are talking to someone on the street, a car passes by and suddenly we stop hearing our interlocutor. Why have we stopped hearing the other person? If we had recorded this situation with a microphone we would see that both sounds, the voice and the car, would have been perfectly recorded …
This phenomenon occurs because there are situations in which our sense of hearing gives prominence to one sound and ignores another if both are simultaneous, what is called sound masking, and that depends on well-defined causes that can be summarized as follows.
A sound can mask another when they reach the ear simultaneously depending on their relative frequencies and volumes. As seen in the figure, at the loudest sound our ear creates a new limit of hearing or masking at that time. If another simultaneous sound is under that frequency environment, we will not perceive it.

Temporary masking
When there is a sound of sufficient power to be masking, there are moments before and after that we will not perceive other sounds, depending on how closely they are in time and their relative volume, with the behavior represented in the figure. As you can see, a sound can be masked whether it occurs immediately after the masking, or if it occurs before!
The MP3 compression algorithm
When we perform an MP3 compression, the coding algorithm divides the music into a multitude of short-lived fragments. Each of these fragments are analyzed individually in many frequency bands, to be able to detect if in any of them there is any masking sound that is masking sounds of the other bands of the fragment, and therefore are inaudible or expendable. In that case, what you will do is encode that fragment with fewer bits than the original fragment, so resolution of the more subtle details (those details that have been dispensable) will be lost and the background noise of the fragment will increase.
The amount of bit reduction for that fragment will depend on the quality sought in the encoding. If we set it to high quality, it will reduce the resolution of the fragment only just enough so that the new background noise is still masked by the masking sound that was detected in that fragment.
Therefore, and according to the masking theory, no change will be perceived after the resolution reduction: neither by the loss of the details that were already originally masked, nor by the new background noise, which will remain imperceptible by also maintaining below that masking sound detected.
After this process, the fragment could have been encoded with fewer bits, occupying less information than the original. Once this attempt at bit reduction has been repeated with all the multitude of fragments into which the original file had been divided, the song is reconstructed and a compressed file is obtained that will now take up less space.
In addition to this masking-based coding, finally an “Huffman” arithmetic coding is applied to the resulting bits, similar to that performed in a “.zip” compression. This process will not entail additional quality losses.
Sound quality in MP3 files
The sound quality of the compression depends on the size that we want the compressed song to occupy, therefore the bitrate we indicate when performing the compression. If we choose a high bitrate, the algorithm will not be forced to eliminate much information, so it will eliminate really inaudible details according to the masking curves. But if we want the file to take up less space and choose a lower bitrate, the algorithm will have to be more drastic overcoming the most imperceptible masking curves, and it will be inevitable that the loss of information will be noticed.
For example, in the most common 128 kbps MP3s a few years ago, the quality is significantly lower than the original for most people, if a direct comparison is made. On the other hand, an MP3 file with the maximum bitrate of 320 kbps hardly loses information, and is practically indistinguishable from the original in most cases.