Skip to content

Instantly share code, notes, and snippets.

@prosper00
Last active July 10, 2023 15:59
Show Gist options
  • Save prosper00/49a4f19622b815f65e065e2ea803736f to your computer and use it in GitHub Desktop.
Save prosper00/49a4f19622b815f65e065e2ea803736f to your computer and use it in GitHub Desktop.
bash script to convert sound file to C/C++ header
/* Adventure Kid Waveforms (AKWF) converted for use with Teensy Audio Library
*
* Adventure Kid Waveforms(AKWF) Open waveforms library
* https://www.adventurekid.se/akrt/waveforms/adventure-kid-waveforms/
*
* This code is in the public domain, CC0 1.0 Universal (CC0 1.0)
* https://creativecommons.org/publicdomain/zero/1.0/
*
* Converted by Brad Roy, https://github.com/prosper00
*/
/* AKWF_0001 256 samples
+-----------------------------------------------------------------------------------------------------------------+
| * * ** |
| * * * * |
| * * * ** |
| * * * ** |
| * * * * ****** |
|* * ******** * * *** *** |
|* * ** *** * * ** ** |
| * * ** * * * ** |
| * ** ** *** * * ** |
| * ** ** *** * * ** |
| * * ** *** * * ** *|
| * * *** ** * * ** ********|
| * * ***** ** * ***** |
| ** ** * * |
| **** * ** |
+-----------------------------------------------------------------------------------------------------------------+
*/
const uint16_t AKWF_0001 [] = {
33220, 36394, 40224, 44326, 48176, 50957, 55005, 58677, 61653, 63849, 64912, 65126, 64472, 63271, 61542, 59528,
57195, 54626, 51530, 47996, 43849, 39375, 34555, 29939, 25547, 22231, 19610, 16502, 14137, 12199, 10874, 10149,
9952, 10280, 11045, 12217, 13684, 15426, 17357, 19465, 21667, 23946, 26227, 28489, 30674, 32757, 34698, 36474,
38058, 39432, 40580, 41462, 42046, 42681, 43187, 43572, 43869, 44038, 44126, 44108, 44024, 43856, 43643, 43369,
43061, 42704, 42316, 41881, 41411, 40890, 40323, 39700, 39020, 38282, 37482, 36623, 35710, 34747, 33743, 32709,
31656, 30591, 29527, 28478, 27451, 26454, 25497, 24583, 23720, 22910, 22159, 21467, 20850, 20303, 19843, 19474,
19210, 19058, 19028, 19126, 19355, 19713, 20193, 20794, 21491, 22279, 23127, 24027, 24931, 25841, 26700, 27523,
28252, 28941, 29535, 30202, 30779, 31125, 31430, 31655, 31903, 32213, 32662, 33296, 34190, 35364, 36881, 38868,
41464, 43347, 45369, 48365, 51346, 54426, 57066, 59141, 60430, 60954, 60729, 59925, 58700, 57194, 55520, 53678,
51687, 49419, 46851, 43819, 40393, 36482, 32371, 28077, 24081, 20372, 17766, 15604, 13032, 11157, 9577, 8558,
8016, 7936, 8287, 9015, 10073, 11371, 12896, 14569, 16394, 18295, 20281, 22283, 24312, 26311, 28294, 30209,
32069, 33835, 35516, 37080, 38539, 39870, 41079, 42161, 43120, 43958, 44681, 45297, 45811, 46234, 46567, 46822,
46993, 47090, 47103, 47038, 46882, 46638, 46293, 45852, 45306, 44660, 43914, 43080, 42160, 41172, 40123, 39025,
37891, 36729, 35551, 34362, 33174, 31988, 30819, 29662, 28532, 27423, 26351, 25307, 24311, 23355, 22463, 21629,
20883, 20225, 19678, 19245, 18946, 18780, 18750, 18859, 19086, 19429, 19848, 20347, 20850, 21382, 21825, 22235,
22319, 22040, 21953, 21795, 21455, 21125, 20796, 20672, 20689, 21046, 21609, 22574, 23750, 25377, 27224, 29822,
};
find . -name *.wav -exec ./convert.sh {} \;
#!/bin/bash
Help()
{
# Display Help
echo "
Convert a sound file to a C/C++ header file.
Should support any filetype supported by sox
produces unsigned 16-bit integer arrays
requires sox, hexdump
usage:
$0 <filename>
"
exit
}
if [[ $1 == "" ]]
then
Help
fi
while getopts ":h" option; do
case $option in
h) # display Help
Help
exit;;
\?) # invalid case
echo "Invalid option; -h for help"
exit;;
esac
done
file="$(basename ${1%.*})" # will place output files in current dir
#file="${1%.*}" # will place output files in their original dir
echo '/* Adventure Kid Waveforms (AKWF) converted for use with Teensy Audio Library
*
* Adventure Kid Waveforms(AKWF) Open waveforms library
* https://www.adventurekid.se/akrt/waveforms/adventure-kid-waveforms/
*
* This code is in the public domain, CC0 1.0 Universal (CC0 1.0)
* https://creativecommons.org/publicdomain/zero/1.0/
*
* Converted by Brad Roy, https://github.com/prosper00
*/
' > "$file.h"
echo -e "/* $file 256 samples" >> "$file.h"
sox $1 -r 18816 $file.dat
tail -n+3 $file.dat > temp.dat
gnuplot -p -e "set term png size 1920,960; set output \"$file.png\"; plot \"temp.dat\" with lines;"
gnuplot -p -e "set terminal dumb 120 20 ; unset xtics; unset ytics; plot \"temp.dat\" notitle with lines;" >> "$file.h"
echo -e "*/\n\n" >> "$file.h"
rm "$file.dat"
rm "temp.dat"
sed -i 's/\o14//' "$file.h" # remove the ^L character that gnuplot is inserting
#ffmpeg -i in.flac -ac 1 -filter:a aresample=8000 -map 0:a -c:a pcm_s16le -f data - | \
# gnuplot -p -e "set terminal png size 640,360; set output 'out.png'; plot '<cat' binary filetype=bin format='%int16' endian=little array=1:0 with lines;"
echo "const uint16_t "$file" [] = {" >> "$file.h"
#for 8-bit unsigned, add -b 8 to the sox command
sox $1 -e unsigned -r 18816 $file.raw
# 18816 = 256/600*44100 - decreases sample count from 600 to 256
# sox -G attempts to avoid clipping. Try with/without this
#for decimal
hexdump -v -e '16/2 "%5u, " "\n" ' "$file.raw" >> "$file.h"
#for hex
#hexdump -v -e '16/2 "0x%04x, " "\n" ' $file.raw >> $file.h
# for 8-bit, use '16/1,,,'
echo '};' >> "$file.h"
rm "$file.raw"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment