| 1 | #! /bin/bash |
| 2 | |
| 3 | ############################################################################################# |
| 4 | # # |
| 5 | # Compiles Faust programs to Android, OSX, iOS, Microsoft 32bits and 64 bits libraries # |
| 6 | # suitable for the Unity environment # |
| 7 | # # |
| 8 | # (c) Grame, 2017 # |
| 9 | # # |
| 10 | ############################################################################################# |
| 11 | |
| 12 | # The libraries are first wrapped by the unityplugin.cpp architecture file. |
| 13 | # They have to be used in the Unity environment. |
| 14 | # The libraries do not work by themselves. |
| 15 | # They need the FaustPlugin_<dspname>.cs and FaustUtilities_<dspname>.cs files to be functional. |
| 16 | # These files are automatically generated in this script. |
| 17 | # By default, all generated files are packed in a unitypackage archive to be read as a package by Unity. |
| 18 | # Several options are available, see architecture/unity/README.md for more info. |
| 19 | |
| 20 | . faustpath |
| 21 | . usage.sh |
| 22 | |
| 23 | echoHelp() |
| 24 | { |
| 25 | usage faust2unity "[options] [Faust options] <file.dsp>" |
| 26 | platform Android, Linux, MacOSX, iOS, Windows |
| 27 | require Unity |
| 28 | echo "Generates a unity package (compressed .unitypackage folder) with all available architecture libraries for faust unity plugin and the C# files required. Use arguments to generate specific architectures (see https://github.com/grame-cncm/faust/tree/master-dev/architecture/unity)" |
| 29 | option |
| 30 | option -w32 "generates a Windows 32 bits library" |
| 31 | option -w64 "generates a Windows 64 bits library" |
| 32 | option -osx "generates a macOS library" |
| 33 | option -ios "generates an iOS library" |
| 34 | option -android "generates Android libraries (armeabi-v7a and x86)." |
| 35 | option -linux "generates a Linux library" |
| 36 | option "-nvoices <num>" |
| 37 | option -source "generates the source files (uncompressed folder)" |
| 38 | option -universal "generates a universal (arm/intel) external on OSX" |
| 39 | option -unpacked "generates an unpacked folder with files organized like the Unity Asset hierarchy. Use this options to add specific files in the unity package (in the Assets folder, then use 'encoderunitypackage <folder>' to compress and pack it." |
| 40 | option "Faust options" |
| 41 | $ARCH |
| 42 | echo "See architecture/unity/README.md for more info." |
| 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 | #----------------------------------------------------------------- |
| 53 | # Dispatch command arguments |
| 54 | |
| 55 | ALL="1" |
| 56 | W32="0" |
| 57 | W64="0" |
| 58 | OSX="0" |
| 59 | IOS="0" |
| 60 | ANDROID="0" |
| 61 | LINUX="0" |
| 62 | SRC="0" |
| 63 | UNPCK="0" |
| 64 | NVOICES=-1 |
| 65 | FAUST_UTILITY=FaustUtilities_template.cs |
| 66 | ARCH="" |
| 67 | |
| 68 | while [ $1 ] |
| 69 | do |
| 70 | p=$1 |
| 71 | |
| 72 | if [ $p = "-help" ] || [ $p = "-h" ]; then |
| 73 | echoHelp |
| 74 | elif [ $p = "-w32" ]; then |
| 75 | W32="1" |
| 76 | ALL="0" |
| 77 | elif [ $p = "-w64" ]; then |
| 78 | W64="1" |
| 79 | ALL="0" |
| 80 | elif [ $p = "-osx" ]; then |
| 81 | OSX="1" |
| 82 | ALL="0" |
| 83 | elif [ $p = "-ios" ]; then |
| 84 | IOS="1" |
| 85 | ALL="0" |
| 86 | elif [ $p = "-android" ]; then |
| 87 | ANDROID="1" |
| 88 | ALL="0" |
| 89 | elif [ $p = "-linux" ]; then |
| 90 | LINUX="1" |
| 91 | ALL="0" |
| 92 | elif [ $p = "-universal" ]; then |
| 93 | ARCH="-universal" |
| 94 | elif [ $p = "-nvoices" ]; then |
| 95 | shift |
| 96 | NVOICES=$1 |
| 97 | FAUST_UTILITY=FaustPolyUtilities_template.cs |
| 98 | elif [ $p = "-source" ]; then |
| 99 | SRC="1" |
| 100 | ALL="0" |
| 101 | elif [ $p = "-unpacked" ]; then |
| 102 | UNPCK="1" |
| 103 | elif [ ${p:0:1} = "-" ]; then |
| 104 | OPTIONS="$OPTIONS $p" |
| 105 | elif [[ -f "$p" ]] && [ ${p: -4} == ".dsp" ]; then |
| 106 | FILES="$FILES $p" |
| 107 | else |
| 108 | OPTIONS="$OPTIONS $p" |
| 109 | fi |
| 110 | |
| 111 | shift |
| 112 | done |
| 113 | |
| 114 | #------------------------------------- |
| 115 | # Compiles all *.dsp files |
| 116 | |
| 117 | ARCHIVES="" |
| 118 | |
| 119 | for p in $FILES; do |
| 120 | |
| 121 | CUR=$(pwd) |
| 122 | f=$(basename "$p") |
| 123 | SRCDIR=$(dirname "$p") |
| 124 | NAME=${f%.dsp} |
| 125 | FNAME=FaustPlugin_$NAME |
| 126 | FIN=$SRCDIR/$FNAME |
| 127 | |
| 128 | # creates final dir / overwrites it if already created |
| 129 | if [ -d "$FIN" ]; then |
| 130 | rm -rf "$FIN" |
| 131 | fi |
| 132 | mkdir "$FIN" |
| 133 | |
| 134 | # adds README to final dir while setting dspname |
| 135 | sed "s/<dspname>/$NAME/g" $FAUSTARCH/unity/README.md > $FIN/README.md |
| 136 | |
| 137 | # finds number of parameters, required for the c# scripts |
| 138 | faust -uim $f -o $NAME.cpp |
| 139 | grep FAUST_ACTIVES $NAME.cpp > params.txt |
| 140 | var1=$(cut -d" " -f3- params.txt) |
| 141 | grep FAUST_PASSIVES $NAME.cpp > params.txt |
| 142 | var2=$(cut -d" " -f3- params.txt) |
| 143 | let "var=var1+var2" |
| 144 | rm $NAME.cpp |
| 145 | rm params.txt |
| 146 | |
| 147 | # compiles faust to c++ and json (adds JSON to C# string) |
| 148 | faust -uim -json "$SRCDIR/$f" -o /dev/null; |
| 149 | tr -d '\n' < $f.json > $NAME.json |
| 150 | sed 's/\"/\\\\"/g;s/\//\\\//g' $NAME.json > $f.json |
| 151 | JSON=$(<$f.json) |
| 152 | |
| 153 | # generates c# files |
| 154 | sed -e "s/MODEL/$NAME/g;s/PLUGNAME/lib$FNAME/g;s/PLUGINNAME/$FNAME/g;s/RELEVANTJSONFILE/$JSON/g;" $FAUSTARCH/unity/$FAUST_UTILITY > $FIN/FaustUtilities_$NAME.cs |
| 155 | sed -e "s/MODEL/$NAME/g;s/VARIABLES/$var/g;" $FAUSTARCH/unity/FaustPlugin_template.cs > $FIN/$FNAME.cs |
| 156 | |
| 157 | rm -rf $NAME.json |
| 158 | rm -rf $f.json |
| 159 | |
| 160 | # compiles c++ to binaries |
| 161 | |
| 162 | # w64 |
| 163 | if [ $W64 = "1" ] || [ $ALL = "1" ]; then |
| 164 | WINBITS="$WINBITS -64" |
| 165 | fi |
| 166 | |
| 167 | # w32 |
| 168 | if [ $W32 = "1" ] || [ $ALL = "1" ]; then |
| 169 | WINBITS="$WINBITS -32" |
| 170 | fi |
| 171 | |
| 172 | # win |
| 173 | if [ -n "$WINBITS" ]; then |
| 174 | faust2winunity -nvoices $NVOICES $WINBITS $f || exit |
| 175 | fi |
| 176 | |
| 177 | # macos |
| 178 | if [ $OSX = "1" ] || [ $ALL = "1" ]; then |
| 179 | faust2osxiosunity $ARCH -nvoices $NVOICES $f || exit |
| 180 | fi |
| 181 | |
| 182 | # ios |
| 183 | if [ $IOS = "1" ] || [ $ALL = "1" ]; then |
| 184 | faust2osxiosunity -nvoices $NVOICES -ios $f || exit |
| 185 | fi |
| 186 | |
| 187 | # android |
| 188 | if [ $ANDROID = "1" ] || [ $ALL = "1" ]; then |
| 189 | faust2androidunity -nvoices $NVOICES $f || exit |
| 190 | fi |
| 191 | |
| 192 | # Linux |
| 193 | if [ $LINUX = "1" ] || [ $ALL = "1" ]; then |
| 194 | faust2linuxunity -nvoices $NVOICES $f || exit |
| 195 | fi |
| 196 | |
| 197 | # Dispatch files in Unity hierarchy |
| 198 | if [ "$SRC" = "0" ]; then |
| 199 | if [ "$UNPCK" = "1" ]; then # Specific folder name if unpacked |
| 200 | PLGN=$FNAME\_Assets/Plugins |
| 201 | SCRPT=$FNAME\_Assets/Scripts |
| 202 | else |
| 203 | PLGN=Assets/Plugins |
| 204 | SCRPT=Assets/Scripts |
| 205 | fi |
| 206 | |
| 207 | if [ ! -d "$PLGN" ]; then |
| 208 | mkdir -p $PLGN |
| 209 | else |
| 210 | rm -rf $PLGN |
| 211 | mkdir -p $PLGN |
| 212 | fi |
| 213 | |
| 214 | if [ ! -d "$SCRPT" ]; then |
| 215 | mkdir -p $SCRPT |
| 216 | else |
| 217 | rm -rf $SCRPT |
| 218 | mkdir -p $SCRPT |
| 219 | fi |
| 220 | |
| 221 | echo $NAME > .VAR |
| 222 | mv $FNAME/*.cs $SCRPT |
| 223 | mv $FNAME $PLGN |
| 224 | |
| 225 | if [ "$UNPCK" = "0" ]; then |
| 226 | encoderunitypackage Assets # encode unity package |
| 227 | ARCHIVES="$ARCHIVES$FNAME.unitypackage;" # add package name ARCHIVES variable to display compile package name |
| 228 | elif [ "$UNPCK" = "1" ]; then |
| 229 | ARCHIVES=$ARCHIVES$FNAME\_Assets\; # add package name ARCHIVES variable to display compile folder |
| 230 | fi |
| 231 | |
| 232 | elif [ "$SRC" = "1" ]; then |
| 233 | # add .dsp and .cpp files to sources folder |
| 234 | faust -uim -i -a $FAUSTARCH/unity/unityplugin.cpp $f -o $NAME.cpp |
| 235 | mv "$NAME.cpp" "$FNAME/$NAME.cpp" |
| 236 | cp "$f" "$FNAME/$f" |
| 237 | SNAME=$FNAME\_sources |
| 238 | mv "$FNAME" "$SNAME" |
| 239 | ARCHIVES="$ARCHIVES$SNAME;" |
| 240 | fi |
| 241 | |
| 242 | done |
| 243 | |
| 244 | echo "$ARCHIVES" |