You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MicroDexed/addon/tools/wav2c.sh

140 lines
2.8 KiB

#!/bin/bash
#
# Converter for WAV to C Header
# Used for MicroDexed sampler
#
# (c) 2021 Holger Wirtz
#
SOX=`which sox`
XXD=`which xxd`
TMP="/tmp/wav2c"
AUDIO_BLOCK_SIZE=128
DRUMSET_H="drumset.h"
function cleanexit()
{
#rm -rf "${TMP}"
echo -n ""
}
trap cleanexit EXIT
if [ -z "${SOX}" ]
then
echo "Cannot find 'sox' in your PATH." >&2
exit 100
fi
if [ -z "${XXD}" ]
then
echo "Cannot find 'xxd' in your PATH." >&2
exit 101
fi
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-c|--config)
CONFIG="$2"
shift # past argument
shift # past value
;;
-w|--wavs)
WAV_DIR="$2"
shift # past argument
shift # past value
;;
# -t|--test)
# TEST="true"
# shift # past argument
# ;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
if [ ! -f "${CONFIG}" ]
then
echo "Cannot find configuration file." >&2
exit 200
fi
if [ ! -d "${WAV_DIR}" ]
then
echo "Cannot find folder for WAV files." >&2
exit 201
fi
if (( `ls "${WAV_DIR}" | wc -l` == 0 ))
then
echo "Cannot find any file in ${WAV_DIR}." >&2
exit 202
fi
mkdir -p "${TMP}"
rm -f "${DRUMSET_H}"
while IFS= read -r l
do
declare -A sample
if [[ "${l:0:1}" == "#" ]]
then
continue
fi
IFS=',' read -ra samplecfg <<< "${l}"
sample['class']="${samplecfg[0]}"
sample['midi_note']="${samplecfg[1]}"
sample['name']="${samplecfg[2]}"
sample['shortname']="${samplecfg[3]}"
sample['pitch']="${samplecfg[4]}"
sample['pan']="${samplecfg[5]}"
sample['vol_max']="${samplecfg[6]}"
sample['vol_min']="${samplecfg[7]}"
sample['reverb_send']="${samplecfg[8]}"
sample['filename']="${samplecfg[9]}"
if [ -f "${WAV_DIR}/${sample['filename']}" ]
then
file "${WAV_DIR}/${sample['filename']}" | grep -i -q "WAVE audio"
if [ "${?}" == 0 ]
then
basename=`echo "${sample['filename']}" | cut -d'.' -f1`
sox "${WAV_DIR}/${sample['filename']}" -c 1 -b 16 -L "${TMP}/${basename}.raw"
xxd -i "${TMP}/${basename}.raw" > "${TMP}/${basename}.h"
sample['len']=`grep "^unsigned int" "${TMP}/${basename}.h" | cut -d"=" -f2 | sed 's/\s*\([0-9]\+\);/\1/'`
fill=`expr "${sample['len']}" % "${AUDIO_BLOCK_SIZE}"`
echo "PROGMEM const int8_t ${sample['name']}[] = {" >> "${DRUMSET_H}"
grep "^ " "${TMP}/${basename}.h" >> "${DRUMSET_H}"
if (( "${fill}" > 0 ))
then
echo -n "," >> "${DRUMSET_H}"
fill_counter=0
for i in $(seq 1 "${fill}")
do
echo -n "0x00" >> "${DRUMSET_H}"
let fill_counter+=1
if (( "${fill_counter}" >= 8 ))
then
echo "" >> "${DRUMSET_H}"
fill_counter=0
else
echo -n ", " >> "${DRUMSET_H}"
fi
done
fi
echo "};" >> /tmp/b
fi
else
echo "File \'${WAV_DIR}/${sample['filename']}\' does not exits." >&2
fi
done < "${CONFIG}"