I want to increase the volume of some MP3 files that I have. Is there a way to do this in a lossless way (without re-compressing the MP3 file and hence decreasing its quality)?
Yes. You can do this. The technique is used by a number of programs and the levels are calculated by an algorithm called ReplayGain. Mp3 volume level data can be adjusted losslessly, just as a JPEG picture can be losslessly rotated.
There are two methods
- Calculate the volume level of the file and add a new tag, letting compatible programs adjust volume on the fly
- Calculate the volume level and adjust a multiplier level in the mp3 data.
From HydrogenAudio: Implementations:
In the metadata method, information on both types of ReplayGain (Track Gain and Album Gain) can be stored. The volume-change information can be very precise. If audio data was also changed, the metadata can contain "undo" info. Not all audio players/decoders know how to read and use ReplayGain information stored in metadata. And there's no standard for where and how ReplayGain info is stored; each implementation uses different formats and puts the info in different locations.
In the audio data method, the file's actual audio data is modified so that its natural/default playback volume is at the target level. In this scenario, only one type of ReplayGain (Track Gain or Album Gain) can be applied. If no "undo" info is saved somewhere, it may not be possible to restore the original audio data. Limitations of the audio file format may prevent precise (finely tuned) gain adjustments with this method. For example, MP3 and AAC files can only be losslessly modified in 1.5 dB steps. Depending on the audio file format, the process may also be lossy in the sense that it could irreversibly push a signal above the format's maximum amplitude (resulting in clipping) or below the minimum (resulting in silence).
The second method does alter your file, but because the underlying data is not modified it loses no quality, hence the adjustment can be done losslessly. It is not generally recommended for your main collection as it is modifying the files from their original state, but it can be very useful for portable media players.
A program called mp3gain can do the second option and reversibly adjust the volume of mp3s. It does absolutely no re-encoding and simply adjusts magnitude values in the mp3 file itself to change the volume. From HydrogenAudio: ReplayGain
If modifying the gain, it always modifies the global gain fields in the MP3 audio data. It can add somewhat precise metadata, including undo info
Due to the way mp3 files are encoded the adjustment is limited to 1.5dB steps, which is usually enough to get close.
Foobar2k also includes this functionality and can apply the changes to the files.
Interesting. Could you explain the first option? Are these special ID3 tags?– LPChipJun 5, 2016 at 11:51
2In the past I have tested one of the programs which claimed to do lossless rotation of a JPEG file. Decompressing a version which had been rotated by 90 degrees 4 times produced the same output as decompressing the original. However decompressing a version which had been rotated by 90 degrees only once did not produce the same result as decompressing the original and rotating by 90 degrees. So 90 degree rotation of a JPEG file was not entirely lossless, and the root cause turns out to be certain asymmetries in the JPEG specification itself.– kasperdJun 5, 2016 at 15:31
7@kasperd that is quite interesting in that it shows that in rotation the actual encoded image data is 100% preserved, otherwise continuing to rotate would never result in the same file, but that there is a subsequent process in the image reconstruction that is sensitive to the data rotation. It's not entirely surprising given the computations involved, but your original compressed data is in fact unharmed by the process and can be recovered. Hence the rotation itself is not loosing any data and is therefore lossless. It is the decoder reacting differently to the rotated data in this case.– Mokubai ♦Jun 5, 2016 at 18:33
2(I think jpeg rotations are lossless only if the size of the image in either direction is a multiple of 8 pixels) Jun 6, 2016 at 10:12
1@Strawberry: The data for a JPG always has an integer number of tiles in both directions, but the file includes dimensions that need not be a multiple of that. If one were to rotate the data in the tiles when the dimensions weren't a multiple of the time size, portions of tiles that hadn't been in view would become visible, and some portions that had been visible would be preserved but hidden. Repeating the operation three more times would bring the hidden data back into view.– supercatJun 6, 2016 at 15:53
In my opinion, Replaygain is not an answer to this question. It is not increasing the volume of the content, just adding a tag to tell players (only players which support the tag) to do their own volume adjustment at playback time. In particular, it's not compatible with all players, and not suited for all uses of mp3s (such as concatenating them into a single stream, etc.).
On the other hand, in theory it's possible to scale all the DCT coefficients without decoding and reencoding (i.e. without reversing quantization and DCT then redoing them), only redoing the Huffman coding (which is lossless). Back when I worked on MPlayer I believe someone had a utility which did this, but I've lost track of the name.
Yes, it's called replay gain, the principle is very simple, and works rather well. Basically, software "listens" to your track, and determines how much it would need to be amplified as to sound "normal". The information is written in mp3tag, separate from the data.
Compatible player then uses that data to automatically amplify the volume as to make all the tracks be reproduced at the same loudness. I would suggest to try http://www.foobar2000.org/ for this exercise. By right-clicking the track you can scan it, and in the program settings you need to set to use replay-gain info.
After downloading Audacity (free app) https://sourceforge.net/projects/audacity/, choose File/Import/Audio, navigate to your mp3 file, click on it and choose open. You will see a two-channel wave form.
From the drop down at top click on Edit/Select/All. While the file is selected choose Effects from the drop down list at top and choose Normalize. Just click "ok" from the window that opens. The defaults are usually sufficient. Click the green play triangle from the visible radio buttons to test your edited file. You can work with many other effects such as the Equalization, Amplify, Bass & Treble etc. When satisfied with the results choose File/Export Audio and choose the "save as type" you prefer, usually .WAV or .MP3.
I've had to do this with many weak files I've received.