Skip to content
Redox edited this page Sep 28, 2024 · 5 revisions

File Format

.mp3 is the ideal format to use for sounds you intend to ship with your Garry's Mod addon.

It's an efficient and widely supported file format. You suffer a bit of quality loss, but it's usually imperceptible. Especially for sounds you usually use in Garry's Mod (short sounds that are played in the background, like gunshots, reload sounds, button presses, etc.)

Note: Looping .mp3 sounds is much trickier (if not impossible?) than .wav files, so it's recommended not to convert any sounds you intend to loop.

Converting from .wav to .mp3

If you're working with an addon that already uses .wav files, you can run a few commands to convert all of them to optimized .mp3 files and reduce the overall size by ~20x.

We now have a python script for this, it can be found here

This guide was made for Ubuntu, you may have to modify it to work with your system.

Prerequisites

sudo apt update
sudo apt install sox libsox-fmt-mp3

Now, in your sound/ dir:

# Finds all .wav files and converts them to a 22.05kHz single-channel .mp3 file in the same directory
# Volume is reduced to 70% to prevent clipping
find . -iname "*.wav" -exec sh -c 'sox -v 0.7 $1 -r 22050 -c 1 $(dirname $1)/$(basename $1 .wav).mp3' sh {} \;

Manually verify that a new .mp3 file with the same name exists alongside all of your .wav files.

Then, remove all of the .wav files:

rm -rfv ./**/*.wav

And finally, change all references to your old .wav files to .mp3

This isn't a perfect process. You may have to do some manual code editing.

If all of your sounds are in a sub-dir for your addon, i.e. sound/my_addon/, you can do something like:

grep -rl "my_addon/.*\.wav" . | xargs sed -i 's/\(my_addon\/.*\)\.wav/\1.mp3/g'

This will replace all sound names with the associated .mp3. Replacements would look like:

- ent:EmitSound( "my_addon/example_sound.wav" )
+ ent:EmitSound( "my_addon/example_sound.mp3" )

---

- ENT.RunningSound = "my_addon/running_sounds/example.wav"
+ ENT.RunningSound = "my_addon/running_sounds/example.mp3"

But it won't be perfect. If you were doing something like this:

local runningSoundPrefix = "my_addon/running_sounds/"
ENT.RunningSound = runningSoundPrefix .. "example.wav"

This command would miss it.

Depending on how many internal/externals sounds you use, it might be easier to convert all .wav to .mp3 and then manually fix anything it gets wrong:

grep -rl "\.wav" . | xargs sed -i 's/\.wav/\.mp3/g'