#!/bin/sh
# Prepares a sample.h file
# sed not used, should just werk (TM) everywhere
# Usage: ./samplify.sh my_sample.wav
# Or you can install samplify.sh in your ~/bin and call it
# from anywhere you like

# Convert original soundfile and create sample.h
# A temp file is needed otherwise xxd is unable to generate 
# sample length information?
# Need to fix that later...
sox "${1}" -t u8 -c 1 -r 8000 /tmp/sample.wav
xxd -i /tmp/sample.wav > sample.h
rm /tmp/sample.wav

# Extract sample length
SAMPLE_LEN=$(tail -1 sample.h | cut -d ' ' -f 5 | tr -d ';')

# Inject SAMPLE_LEN in new file header
NEW_HEADER="#define SAMPLE_RATE 8000
const int sound_length=${SAMPLE_LEN};
const unsigned char sound_data[] PROGMEM= {"

# Remove first line of sample.h
echo "$(tail -n +2 sample.h)" > sample.h

# Prepend new header to sample.h
echo "$(echo "${NEW_HEADER}"; cat sample.h)" > sample.h
