Skip to content

Instantly share code, notes, and snippets.

@tsointsoin
Last active July 5, 2023 19:43
Show Gist options
  • Select an option

  • Save tsointsoin/91c5b5be75fba7db00b29626e64bba10 to your computer and use it in GitHub Desktop.

Select an option

Save tsointsoin/91c5b5be75fba7db00b29626e64bba10 to your computer and use it in GitHub Desktop.
Bash script to prepare samples for Bastle Instruments Microgranny - Needs the following utilities : detox, ffmpeg and sox
#!/bin/bash
#
# Prepare samples for microgranny:
# - Remove all metadata from wav files in folders
# - Convert wav to mono/16bit/22kHz
# - Remove silent parts at beginning and end
# - Normalize to -0.1dB
#
# Usage:
# Put this script in a folder with your samples (they can be in subfolders), chmod +x
# and execute ./prepare_microgranny.sh
# All .wav files will be treated, detox will take care of spaces and such things
# Be aware that THIS SCRIPT MAKES NO BACKUP, so work on copies
#
# Do we have what we need ?
if ! [[ $(which sox) ]]; then
echo "sox executable not found on system, exiting"
exit 0
fi
if ! [[ $(which ffmpeg) ]]; then
echo "ffmpeg executable not found on system, exiting"
exit 0
fi
if ! [[ $(which detox) ]]; then
echo "detox executable not found on system, exiting"
exit 0
fi
# Use sox to convert samples to 16bit/22050Hz
# Original samples are deleted
#
convert_16b22khz () {
filename="$1"
sample_rate=$(sox --i -r $filename)
sample_depth=$(sox --i -p $filename)
if [[ "$sample_depth" != 16 || "$sample_rate" != 22050 ]]; then
newname=${filename%.wav}_s.wav
sox $filename -c 1 -r 22050 -b 16 $newname
rm $filename
mv $newname $filename
echo "Sample "$filename" converted to mono 16bit/22kHz"
else
echo "Not converting "$filename
fi
}
# Use sox to normalize to -0.1dB
#
normalize () {
filename="$1"
newname=${filename%.wav}_s.wav
sox $filename $newname norm -0.1
mv $newname $filename
echo "Sample "$filename" normalized"
}
# Use sox to strip silence from begining and end of the sample
# /!\ If your sample length is less than 0.002s (2ms) it will be erased
# This value is kept low to preserve single wave samples (used for example by the wavetable LFO)
#
remove_silence () {
filename="$1"
newname=${filename%.wav}_s.wav
sox $filename $newname silence 1 0.002 -60d reverse silence 1 0.002 -60d reverse
rm $filename
mv $newname $filename
echo "Silence removed from sample "$filename
}
# Use ffmpeg to remove metadata from samples
# Original samples are deleted
#
remove_metadata () {
filename="$1"
newname=${filename%.wav}_s.wav
ffmpeg -hide_banner -loglevel warning -y -i $filename -map_metadata -1 -codec copy $newname
rm $filename
mv $newname $filename
echo "Metadata removed from sample "$filename
}
export -f remove_silence
export -f convert_16b22khz
export -f remove_metadata
export -f normalize
detox -r *
# Just comment what you don't need
#
find . -type f -name "*.wav" -exec bash -c 'remove_silence "$0"' {} \;
find . -type f -name "*.wav" -exec bash -c 'convert_16b22khz "$0"' {} \;
find . -type f -name "*.wav" -exec bash -c 'remove_metadata "$0"' {} \;
find . -type f -name "*.wav" -exec bash -c 'normalize "$0"' {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment