# -*- mode: sh -*-
#
# *SpHEAR Project
#
# make_reference_drc_filter: create an inverse equalization filter based
#                            on the recorded reference microphone signal
#
#
# Copyright 2016-2017, Fernando Lopez-Lezcano, All Rights Reserved
# nando@ccrma.stanford.edu
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

# we assume DRC is installed, see:
# http://drc-fir.sourceforge.net/
#
DRC_BASE="../drc-3.2.1"

# we use the full reference microphone recording as input
REFERENCE="../data/current/ref-all/reference-001-1.wav"

# raw pcm output file base name
OUTFILE="drc-filter"

# calibration data directory
CAL_DIR="../data/current/cal"

# configuration file (based on the "minimal" stock preset)
CONFIG="drc/minimal.drc"

# normalize reference soundfile and convert to a format that DRC likes
REFDRC=`echo ${REFERENCE}|sed "s|.wav|-norm.raw|"`
sox --norm=-3 ${REFERENCE} ${REFDRC}

${DRC_BASE}/source/drc --BCInFile ${REFDRC} --PSOutFile ${CAL_DIR}/${OUTFILE}.pcm ${CONFIG} \
	   --PSPointsFile drc/flat.txt \
	   --MCPointsFile drc/mics/emm-6-5850/emm-6-5850-cal.txt

# convert the raw PCM output to a wav file that octave can understand
sox -e floating-point -b 32 -r 48000 -c 1 -t raw ${CAL_DIR}/${OUTFILE}.pcm ${CAL_DIR}/${OUTFILE}.wav

echo "filter written to ${CAL_DIR}/${OUTFILE}.wav"
echo "done!"
