Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Created July 12, 2012 11:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderofsalvation/3097565 to your computer and use it in GitHub Desktop.
Save coderofsalvation/3097565 to your computer and use it in GitHub Desktop.
wavdir2sfz - create sfz soundfonts from directory containing wav audiofiles with notes in their filenames
#!/bin/bash
# Copyright 2012, Leon van Kammen (Coder of Salvation), All rights reserved.
#
# Native instrument comes with a huge library of sounds (The Maschine Library) for Maschine.
# Wouldnt it be nice if non-maschine owners can use these instruments?
# Lot of times when I see multisamples in directories, I'd like to have an .sfz soundfont for them
# (instead of playing it with some kind of massivegui vst/pluginplayer).
# A typical directory could look like :
#
# Samples/Instrument/Bass1/Bass1 c1.wav
# Samples/Instrument/Bass1/Bass1 c2.wav
# Samples/Instrument/Bass1/Bass1 e#3.wav
# Samples/Instrument/Bass1/Bass1 f4.wav
#
# With this tool you can easily create .sfz instruments from directories
#
# Usages: $ wavdir2sfz Samples/Instrument/Bass1 ../ > sfz/Bass1.sfz
# $ wavdir2sfz Samples/Instrument/Bass1 ../ | sed 's/\//\\/g' > sfz/Bass1_windows.sfz
#
# if you know your way with shellscript, its easy to recursively do this in batch
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY Coder of Salvation ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are those of the
# authors and should not be interpreted as representing official policies, either expressed
# or implied, of Coder of Salvation
#
indir="$1"
prefix="$2"
wavs=""
dir_instrument=""
notes=("c" "c#" "d" "d#" "e" "f" "f#" "g" "g#" "a" "a#" "b")
loadindexes=("|" "/" "-" "\\")
loadindex=0
detected=0
notemap=""
header='<group> lovel=1 hivel=127
ampeg_release=0.300000
'
_checkpaths(){
[ ! -d "$indir" ] && (echo "dir '$indir' not found"; exit )
dir_instrument=$(dirname "$indir")
}
_pump(){
size=${#loadindexes[@]}
((size=size-1))
if [[ $loadindex < $size ]]; then ((loadindex=loadindex+1)); else loadindex=0; fi
}
_notemap(){
echo "$1";exit
}
generate(){
_checkpaths
wavs=$(find "$indir" -name "*wav*")
printf "C0" > /tmp/notelast
current=""
echo "$header"
[ -f /tmp/notemap ] && rm /tmp/notemap
for j in {1..6}; do
for(( i = 0; i < "${#notes[@]}"; i++ )); do
printf "$wavs" | while read file; do
wav=$(basename "$file");
( tput el; printf "\r[${loadindexes[$loadindex]}] scanning notes...${notes[i]}$j "
if [[ ${wav,,} = *"${notes[i]}$j"* ]]; then
printf "$wav found!\n"; printf "$notemap\n${notes[i]^^}$j|$(cat /tmp/notelast)|$file" >> /tmp/notemap;
printf "${notes[i]^^}$j" > /tmp/notelast
fi
) 1>&2
done
_pump
done
done
tail -n+2 /tmp/notemap | while read line; do
note=$(echo "$line" | cut -d\| -f1)
last=$(echo "$line" | cut -d\| -f2)
file=$(echo "$line" | cut -d\| -f3)
printf "<region> sample=$prefix$file\nlokey=$last\nhikey=$note\npitch_keycenter=$note\n\n"
done
}
generate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment