Skip to content

Instantly share code, notes, and snippets.

@rlamana
Created May 30, 2013 11:30
Show Gist options
  • Save rlamana/5677248 to your computer and use it in GitHub Desktop.
Save rlamana/5677248 to your computer and use it in GitHub Desktop.
Convert a SoundFont + MIDI file to a CAF audio file. Based on https://github.com/neonichu/Core-Audio-Samples
#!/bin/sh
#
## Convert a SoundFont + MIDI file to a CAF audio file.
##
## needs:
## fluidsynth, sox (installable via brew)
## afconvert (part of OS X)
#
if [ "$#" -lt 2 ]
then
echo "Usage: `basename $0` [SoundFont] [MIDI]"
exit 1
fi
SOUNDFONT="$1"
MIDI="$2"
OUT="`basename \"$SOUNDFONT\" .sf2`.wav"
# Play MIDI file using the given SoundFont
fluidsynth -nli -F "temp.raw" "$SOUNDFONT" "$MIDI" >/dev/null
# Convert raw output to WAV
sox -r 44100 -2 -c 2 -s "temp.raw" "temp.wav"
# Trim the WAV to one second
sox "temp.wav" "$OUT" trim 0.0 1.0
# Convert the WAV to CAF
afconvert -f caff -d LEI16 "$OUT"
# Cleanup
rm -f temp.raw temp.wav "$OUT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment