Skip to content

Instantly share code, notes, and snippets.

@agusterol
Last active September 14, 2023 15:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agusterol/a935b466af090fe074281843d3274289 to your computer and use it in GitHub Desktop.
Save agusterol/a935b466af090fe074281843d3274289 to your computer and use it in GitHub Desktop.
Simple script for resampling a drumgimzo kit using sox
#! /bin/bash
#
# Simple script for resampling a drumgimzo kit using sox
#
# Author: Agus Terol - agus3985@gmail.com
# License: GPLv3 - https://www.gnu.org/licenses/gpl-3.0.html
#
function print_usage
{
echo "DGResample - Simple script for resampling a drumgimzo kit using sox"
echo ""
echo "Usage: dg_resample.sh [-h|-v] INPUT_DIR OUTPUT_DIR [SAMPLE_RATE]"
echo ""
echo " INPUT_DIR directory that contains the drumgizmo kit to be resampled"
echo " OUTPUT_DIR directory where the resampled drumgizmo kit is going to saved"
echo " SAMPLE_RATE (optional) sample rate parameter to be passed to sox."
echo " Default value is 48000."
echo ""
echo " -h, --help print this help"
echo " -v, --verbose print verbose messages"
echo ""
echo "Author: Agus Terol - agus3985@gmail.com"
echo "License: GPLv3 - https://www.gnu.org/licenses/gpl-3.0.html"
}
# Arguments parsing
if [ "$#" -eq 0 -o "$1" = "-h" -o "$1" = "--help" ]
then
print_usage $0
exit 0
fi
VERBOSE=0
MKDIR_VERBOSE_FLAGS=""
CP_VERBOSE_FLAGS=""
SOX_VERBOSE_FLAGS=""
if [ "$1" = "-v" -o "$1" = "--verbose" ]
then
VERBOSE=1
MKDIR_VERBOSE_FLAGS="-v"
CP_VERBOSE_FLAGS="-v"
SOX_VERBOSE_FLAGS="-S"
shift
fi
if [ "$#" -lt 2 ]
then
echo "ERROR: Incorrect arguments" >&2
print_usage $0
exit 1
fi
INPUT_DIR=$1
OUTPUT_DIR=$2
SAMPLE_RATE=48000
if [ ! -z "$3" ]
then
SAMPLE_RATE=$3
fi
# Check input directory exists
if [ ! -d "$INPUT_DIR" ]
then
echo "ERROR: Directory '$INPUT_DIR' does not exist!" >&2
exit 1
fi
# Check output directory does not exist
if [ -d "$OUTPUT_DIR" ]
then
echo "ERROR: Directory '$OUTPUT_DIR' exists!" >&2
exit 1
fi
# Check sox exists in PATH
WHICH_SOX=$(which sox)
if [ -z "$WHICH_SOX" ]
then
echo "ERROR: Command 'sox' not found. Please install it and/or be sure it is in PATH" >&2
exit 1
fi
IFS=$'\n'
# Replicate folder structure
for i in $(find "$INPUT_DIR" -type d)
do
mkdir $MKDIR_VERBOSE_FLAGS -p "$OUTPUT_DIR/$(realpath --relative-to="$INPUT_DIR" $i)"
done
# Copy non .wav files
for i in $(find "$INPUT_DIR" -type f -not -iname "*.wav")
do
cp $CP_VERBOSE_FLAGS "$i" "$OUTPUT_DIR/$(realpath --relative-to="$INPUT_DIR" $i)"
done
# Resample .wav files to $SAMPLE_RATE
for i in $(find "$INPUT_DIR" -iname "*.wav")
do
o="$OUTPUT_DIR/$(realpath --relative-to="$INPUT_DIR" $i)"
sox $SOX_VERBOSE_FLAGS "$i" "$o" rate -v -s -L $SAMPLE_RATE
if [ $VERBOSE -eq 1 ]
then
sox --info "$o"
fi
done
# Modify XML kits definitions for modifiying or adding samplerate="$SAMPLE_RATE" attribute to the drumkit element
for i in $(find "$OUTPUT_DIR" -maxdepth 1 -type f -iname "*.xml")
do
grep -q "<drumkit" "$i"
if [ $? -eq 0 ]
then
# If this XML file has a drumkit element...
grep -q "samplerate" "$i"
if [ $? -eq 0 ]
then
# ...and if it has already a samplerate attribute, then modify it
sed -i "s/\(<drumkit.*samplerate=\"\).*\(\".*>\)/\1$SAMPLE_RATE\2/" "$i"
else
# ...and if it does not have a samplerate attribute yet, then add it
sed -i "s/\(<drumkit .*\)>/\1 samplerate=\"$SAMPLE_RATE\">/" "$i"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment