#!/bin/bash

SOUNDFONT="${MIDI2FLAC_GMFONT:-/usr/share/soundfonts/FluidR3_GM.sf2}"
SAMPLERATE=44100
SAMPLEFORMAT="s16"

if [[ -e "$1" ]]; then
    MIDI="$1"
elif [[ -e "$1.mid" ]]; then
    MIDI="$1.mid"
fi

if [[ -e "${MIDI%.*}.fluid" ]]; then
    OPTS="${MIDI%.*}.fluid"
elif [[ -e "midi2flac.fluid" ]]; then
    OPTS="midi2flac.fluid"
fi

TMP="${TMP:-/tmp}"
AUDIO_TMP="$(mktemp "$TMP/midi2flac-XXXXXX.wav")"
BASENAME="${MIDI##*/}"
FLAC="${BASENAME%.*}.flac"

fluidsynth -nil \
    -r ${SAMPLERATE} \
    -O ${SAMPLEFORMAT} \
    -F "$AUDIO_TMP" \
    ${OPTS:+-f $OPTS} \
    "$SOUNDFONT" \
    "$MIDI" && \
flac -f -o "$FLAC" "$AUDIO_TMP" && \
rm -f "$AUDIO_TMP" && \
echo "FLAC output file written to '$FLAC'."
