1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
#!/bin/bash
#-------------------------------------------------------------------
# Wrapping resources
CODE_WRAPPER=""
JS_WRAPPER=""
COMB="false"
COMB_SRC=
COMB_EXPORTED=
COMB_WRAPPED=
COMB_WRAPPED_FILES=
COMB_SEP=
EMCC="false"
POLY="false"
EXPORTED_MONO="['_"$name"_constructor','_"$name"_destructor','_"$name"_getSampleRate','_"$name"_init','_"$name"_instanceInit','_"$name"_instanceConstants','_"$name"_instanceResetUserInterface','_"$name"_instanceClear','_"$name"_compute','_"$name"_getNumInputs','_"$name"_getNumOutputs','_"$name"_setParamValue','_"$name"_getParamValue','_"$name"_getJSON']"
EXPORTED_POLY="['_"$name"_poly_constructor','_"$name"_poly_destructor','_"$name"_poly_getSampleRate','_"$name"_poly_init','_"$name"_poly_instanceInit','_"$name"_poly_instanceConstants','_"$name"_poly_instanceResetUserInterface','_"$name"_poly_instanceClear','_"$name"_poly_compute','_"$name"_poly_getNumInputs','_"$name"_poly_getNumOutputs','_"$name"_poly_setParamValue','_"$name"_poly_getParamValue','_"$name"_poly_getJSON','_"$name"_poly_keyOn','_"$name"_poly_keyOff','_"$name"_poly_allNotesOff','_"$name"_poly_ctrlChange','_"$name"_poly_pitchWheel']"
#-------------------------------------------------------------------
# Set Faust include path
if [ -f $FAUST_LIB_PATH/music.lib ]
then
FAUSTLIB=$FAUST_LIB_PATH
elif [ -f /usr/local/share/faust/music.lib ]
then
FAUSTLIB=/usr/local/share/faust/
elif [ -f /usr/share/faust/music.lib ]
then
FAUSTLIB=/usr/share/faust/
else
echo "$0: Cannot find Faust library dir (usually /usr/local/share/faust)"
fi
#-------------------------------------------------------------------
# Analyze command arguments :
# faust options -> OPTIONS
# existing *.dsp files -> FILES
#
for p in $@; do
if [ $p = "-help" ] || [ $p = "-h" ]; then
echo "faust2asmjs [-poly] [-comb] [-emcc] <file.dsp>"
echo "Use '-poly' to produce a polyphonic DSP, ready to be used with MIDI events"
echo "Use '-comb' to combine several DSP in a unique resulting 'comb.js' file, sharing the same Emscripten runtime"
echo "Use '-emcc' to compile C++ generated code to asm.js with Emscripten, otherwise the internal asm.js backend is used"
exit
elif [ $p = "-comb" ]; then
COMB="true"
elif [ $p = "-poly" ]; then
POLY="true"
elif [ $p = "-emcc" ]; then
EMCC="true"
elif [ ${p:0:1} = "-" ]; then
OPTIONS="$OPTIONS $p"
elif [[ -e "$p" ]]; then
FILES="$FILES $p"
else
OPTIONS="$OPTIONS $p"
fi
done
#-------------------------------------------------------------------
# Set the compilation wrapping files depending of the compilation options
#
if [ $POLY = true ]; then
if [ $EMCC = "true" ]; then
echo "Compiled with 'emcc' in polyphonic mode"
CODE_WRAPPER=webaudio-asm-poly.cpp
JS_WRAPPER=webaudio-asm-poly-emcc.js
else
echo "Compiled with 'asm.js' backend in polyphonic mode"
CODE_WRAPPER=webaudio-asm-poly-standalone-wrapper.js
fi
else
if [ $EMCC = "true" ]; then
echo "Compiled with 'emcc'"
CODE_WRAPPER=webaudio-asm.cpp
JS_WRAPPER=webaudio-asm-emcc.js
else
echo "Compiled with 'asm.js' backend"
CODE_WRAPPER=webaudio-asm-standalone-wrapper.js
fi
fi
#-------------------------------------------------------------------
# compile the *.dsp files
#
BINARIES=""
if [ $COMB = "false" ]; then
for f in $FILES; do
name=$(basename "$f" .dsp)
# compile the Faust DSP to C++ or asm.js code
if [ $EMCC = "true" ]; then
faust -a $FAUSTLIB/webaudio/$CODE_WRAPPER -i -uim -cn $name $OPTIONS $f -o $name.cpp || exit
else
faust -lang ajs -a $FAUSTLIB/webaudio/$CODE_WRAPPER -cn $name $OPTIONS $f -o $name.js || exit
fi
if [ $EMCC = "true" ]; then
# prepare emcc compilation files
if [ $POLY = false ]; then
EXPORTED=EXPORTED_MONO
else
EXPORTED=EXPORTED_POLY
fi
# compile the C++ code to asm.js
emcc -O3 --memory-init-file 0 $name.cpp -s TOTAL_MEMORY=100663296 --post-js $FAUSTLIB/webaudio/$JS_WRAPPER -o $name-temp.js -s EXPORTED_FUNCTIONS=$EXPORTED || exit
# compose the asm.js code
sed -e "s/DSP/"$name"/g" $name-temp.js > $name.js
rm $name-temp.js
rm $name.cpp
fi
# collect binary file name
BINARIES="$BINARIES$name.js;"
done
else
echo "Compiled with 'comb' mode"
for f in $FILES; do
name=$(basename "$f" .dsp)
# compile the Faust DSP to C++ or asm.js code
if [ $EMCC = "true" ]; then
faust -a $FAUSTLIB/webaudio/$CODE_WRAPPER -i -uim -cn $name $OPTIONS $f -o $name.cpp || exit
else
faust -lang ajs -a $FAUSTLIB/webaudio/$CODE_WRAPPER -cn $name $OPTIONS $f -o $name-temp.js || exit
fi
if [ $EMCC = "true" ]; then
# prepare emcc compilation files
if [ $POLY = false ]; then
EXPORTED=EXPORTED_MONO
else
EXPORTED=EXPORTED_POLY
fi
# compose the asm.js code
sed -e "s/DSP/"$name"/g" $FAUSTLIB/webaudio/$JS_WRAPPER > $name-wrapper.js
COMB_SRC+=$name.cpp
COMB_SRC+=" "
COMB_EXPORTED+=$COMB_SEP$EXPORTED
COMB_SEP=","
COMB_WRAPPED_FILES+=$name-wrapper.js
COMB_WRAPPED_FILES+=" "
COMB_WRAPPED+=" --post-js "
COMB_WRAPPED+=$name-wrapper.js
else
echo $name-temp.js
cat $name-temp.js >> comb.js
rm $name-temp.js
fi
done
if [ $EMCC = "true" ]; then
# compile final file
emcc -O2 --memory-init-file 0 $COMB_SRC -s TOTAL_MEMORY=100663296 $COMB_WRAPPED -o comb.js \
-s EXPORTED_FUNCTIONS="["$COMB_EXPORTED"]" || exit
rm $COMB_SRC
rm $COMB_WRAPPED_FILES
fi
# collect binary file name
BINARIES="comb.js;"
fi
echo $BINARIES
|