Skip to content

Instantly share code, notes, and snippets.

@jeremywen
Last active February 15, 2025 23:35
Show Gist options
  • Save jeremywen/22c0fbe546d7777c15331cc570bc1bb1 to your computer and use it in GitHub Desktop.
Save jeremywen/22c0fbe546d7777c15331cc570bc1bb1 to your computer and use it in GitHub Desktop.
#!/bin/bash
########################################################################################
# Requires:
# shuf - install on a mac with `brew install coreutils`
# Args:
# -d | --source-dir source dir to copy wav files from
# -f | --find find a word ignoring case ie. kick
# -s | --samples number of samples to copy to a new random dir
# -r | --repeat number of times to create a random sample dir
# -x | --max-size maximum file size ie. 2MB
# --sfz create an sfz file from the random sample dir
#
# Examples:
# groove-box-utils.sh #copies 8 random samples to a new dir
# groove-box-utils.sh -s 32 -x 1MB
# open "$(groove-box-utils.sh -s 32 -x 1MB -f glass)"
#
# NOTE: shuf is from gnu utils
# NOTE: this bash script was created on mac os
########################################################################################
SRC_DIR='.'
MAX_SIZE='10M'
FIND_WORD='.'
SAMPLE_COUNT=8
REPEAT_COUNT=1
MAKE_SFZ=0
while [[ $# -gt 0 ]]; do
case $1 in
-d|--source-dir)
SRC_DIR="$2"; shift; shift
;;
-f|--find)
FIND_WORD="$2"; shift; shift
;;
-s|--samples)
SAMPLE_COUNT="$2"; shift; shift
;;
-r|--repeat)
REPEAT_COUNT="$2"; shift; shift
;;
-x|--max-size)
MAX_SIZE="$2"; shift; shift
;;
--sfz)
MAKE_SFZ=1; shift
;;
-*|--*)
echo "Unknown option $1"; exit 1
;;
esac
done
pushd "$SRC_DIR" > /dev/null
for (( i=1; i<=$REPEAT_COUNT; i++ ))
do
RND=$RANDOM
RD="$(pwd)/random_samples_$RND"
mkdir "$RD"
find . -type f -name "*.[wW][aA][vV]" -size -$MAX_SIZE | grep -i "$FIND_WORD" | shuf -n $SAMPLE_COUNT | tr "\n" "\0" | xargs -0 -I % cp % "$RD"
if [ $MAKE_SFZ -eq 1 ]; then
MIDI_KEY=36
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in "$RD"/*
do
echo "<region> key=$MIDI_KEY sample=$(basename $f)" >> "$RD/$RD.sfz"
let MIDI_KEY++
done
IFS=$SAVEIFS
fi
done
echo "$RD"
popd > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment