| 1 | #!/bin/bash |
| 2 | |
| 3 | #************************************************************************ |
| 4 | # Copyright (c) 2017 Analog Devices, Inc. All rights reserved. |
| 5 | #************************************************************************ |
| 6 | |
| 7 | ##################################################################### |
| 8 | # Generates inline Faust objects for the SHARC Audio Module # |
| 9 | # Gregory Pat Scandalis, moForte, ADI # |
| 10 | # based on faust2api by Romain Michon # |
| 11 | ##################################################################### |
| 12 | |
| 13 | . faustpath |
| 14 | . faustoptflags |
| 15 | . usage.sh |
| 16 | |
| 17 | # change if you want to get the log of what's happening |
| 18 | LOG="/dev/null" |
| 19 | #LOG="log" |
| 20 | |
| 21 | # exit if a command fails |
| 22 | set -e |
| 23 | |
| 24 | # global option variables |
| 25 | |
| 26 | NVOICES="0" |
| 27 | #PACKAGENAME="0" |
| 28 | POLY2="0" |
| 29 | MIDI="0" |
| 30 | NODOC="0" |
| 31 | EFFECT="" |
| 32 | |
| 33 | echoHelp() |
| 34 | { |
| 35 | usage faust2sam "[options] [Faust options] <file.dsp>" |
| 36 | platform "ADI SHARC Audio Module board" |
| 37 | echo "Generates inline Faust objects for the ADI SHARC Audio Module board" |
| 38 | option |
| 39 | option -midi |
| 40 | option "-nvoices <num>" |
| 41 | option "-effect <effect.dsp>" |
| 42 | option "Faust options" |
| 43 | exit |
| 44 | } |
| 45 | |
| 46 | if [ "$#" -eq 0 ]; then |
| 47 | echo 'Please, provide a Faust file to process !' |
| 48 | echo '' |
| 49 | echoHelp |
| 50 | fi |
| 51 | |
| 52 | createAPI () |
| 53 | { |
| 54 | cp -r $FAUSTARCH/sam/fast_pow2.h $APIFOLDER |
| 55 | cp -r $FAUSTARCH/sam/samFaustDSP.h $APIFOLDER |
| 56 | faust -i -a sam/samFaustDSP.cpp $OPTIONS "$FILE" -o "$APIFOLDER/samFaustDSP.cpp" || exit 1 |
| 57 | if [ "$POLY2" = "1" ]; then |
| 58 | faust -i -cn effect -a minimal-effect.cpp $EFFECT | cat - "$APIFOLDER/samFaustDSP.cpp" > temp && mv temp "$APIFOLDER/samFaustDSP.cpp" || exit 1 |
| 59 | echo "#define POLY2 1" | cat - "$APIFOLDER/samFaustDSP.cpp" > temp && mv temp "$APIFOLDER/samFaustDSP.cpp" |
| 60 | fi |
| 61 | if [ "$NVOICES" -gt "0" ]; then |
| 62 | echo "#define NVOICES $NVOICES" | cat - "$APIFOLDER/samFaustDSP.cpp" > temp && mv temp "$APIFOLDER/samFaustDSP.cpp" |
| 63 | fi |
| 64 | if [ "$MIDI" = "1" ]; then |
| 65 | echo "#define MIDICTRL 1" | cat - "$APIFOLDER/samFaustDSP.cpp" > temp && mv temp "$APIFOLDER/samFaustDSP.cpp" |
| 66 | fi |
| 67 | # These are things that always need to be at the top and bottom |
| 68 | echo "#if USE_FAUST_ALGORITHM" | cat - "$APIFOLDER/samFaustDSP.cpp" > temp && mv temp "$APIFOLDER/samFaustDSP.cpp" |
| 69 | echo "#include \"./samFaustDSPCore.h\"" | cat - "$APIFOLDER/samFaustDSP.cpp" > temp && mv temp "$APIFOLDER/samFaustDSP.cpp" |
| 70 | echo "#include \"common/audio_system_config.h\"" | cat - "$APIFOLDER/samFaustDSP.cpp" > temp && mv temp "$APIFOLDER/samFaustDSP.cpp" |
| 71 | echo "#endif" >> "$APIFOLDER/samFaustDSP.cpp" |
| 72 | |
| 73 | echo "#if USE_FAUST_ALGORITHM" | cat - "$APIFOLDER/samFaustDSP.h" > temp && mv temp "$APIFOLDER/samFaustDSP.h" |
| 74 | echo "#include \"./samFaustDSPCore.h\"" | cat - "$APIFOLDER/samFaustDSP.h" > temp && mv temp "$APIFOLDER/samFaustDSP.h" |
| 75 | echo "#include \"common/audio_system_config.h\"" | cat - "$APIFOLDER/samFaustDSP.h" > temp && mv temp "$APIFOLDER/samFaustDSP.h" |
| 76 | echo "#endif" >> "$APIFOLDER/samFaustDSP.h" |
| 77 | |
| 78 | echo "#if USE_FAUST_ALGORITHM" | cat - "$APIFOLDER/fast_pow2.h" > temp && mv temp "$APIFOLDER/fast_pow2.h" |
| 79 | echo "#include \"./samFaustDSPCore.h\"" | cat - "$APIFOLDER/fast_pow2.h" > temp && mv temp "$APIFOLDER/fast_pow2.h" |
| 80 | echo "#include \"common/audio_system_config.h\"" | cat - "$APIFOLDER/fast_pow2.h" > temp && mv temp "$APIFOLDER/fast_pow2.h" |
| 81 | echo "#endif" >> "$APIFOLDER/fast_pow2.h" |
| 82 | |
| 83 | # fix things that won't compile on the sharc |
| 84 | cat "$APIFOLDER/samFaustDSP.cpp" | \ |
| 85 | sed -e 's/\(std::cerr.*;\)/\/* \1 *\/ /' \ |
| 86 | -e 's/std::cout/\/\/std::cout/' \ |
| 87 | -e 's/getCPULoad(void/faustGetCPULoad(void/' \ |
| 88 | -e 's/std::stringstream/std::ostringstream/' \ |
| 89 | -e 's/dynamic_cast/static_cast/' \ |
| 90 | > temp && mv temp "$APIFOLDER/samFaustDSP.cpp" |
| 91 | } |
| 92 | |
| 93 | # dispatch command arguments |
| 94 | while [ $1 ] |
| 95 | do |
| 96 | p=$1 |
| 97 | |
| 98 | if [ $p = "-help" ] || [ $p = "-h" ]; then |
| 99 | echoHelp |
| 100 | elif [ "$PACKAGENAME" = "-1" ]; then |
| 101 | PACKAGENAME="$p" |
| 102 | elif [[ -f "$p" ]]; then |
| 103 | FILE="$p" |
| 104 | elif [ "$p" = "-effect" ]; then |
| 105 | POLY2="1" |
| 106 | shift |
| 107 | EFFECT=$1 |
| 108 | elif [ "$p" = "-midi" ]; then |
| 109 | MIDI="1" |
| 110 | elif [ $p = "-nvoices" ]; then |
| 111 | shift |
| 112 | NVOICES=$1 |
| 113 | elif [ $p = "-package" ]; then |
| 114 | PACKAGENAME="-1" |
| 115 | elif [ ${p:0:1} = "-" ]; then |
| 116 | OPTIONS="$OPTIONS $p" |
| 117 | elif [[ -f "$p" ]] && [ ${p: -4} == ".dsp" ]; then |
| 118 | FILES="$FILES $p" |
| 119 | else |
| 120 | OPTIONS="$OPTIONS $p" |
| 121 | fi |
| 122 | |
| 123 | shift |
| 124 | |
| 125 | done |
| 126 | |
| 127 | ################################### |
| 128 | # GENERATING API |
| 129 | ################################### |
| 130 | |
| 131 | APIFOLDER="${FILE%.*}-sam" |
| 132 | |
| 133 | rm -r "$APIFOLDER" 2> /dev/null || true |
| 134 | mkdir "$APIFOLDER" |
| 135 | |
| 136 | echo "Your $APIFOLDER.zip package is being created" |
| 137 | createAPI |
| 138 | |
| 139 | ################################### |
| 140 | # POST PROCESSING |
| 141 | ################################### |
| 142 | |
| 143 | ZIPOUT="$APIFOLDER.zip" |
| 144 | |
| 145 | rm $ZIPOUT 2> /dev/null || true |
| 146 | zip -r $ZIPOUT $APIFOLDER > /dev/null || exit 1 |
| 147 | rm -r $APIFOLDER || exit 1 |
| 148 | |
| 149 | # echos generated files for FaustWorks |
| 150 | echo "$ZIPOUT;" |