| 1 | #! /bin/bash -e |
| 2 | |
| 3 | ##################################################################### |
| 4 | # # |
| 5 | # Compiles Faust programs to iOS applications # |
| 6 | # (c) Grame, 2012-2018 # |
| 7 | # # |
| 8 | ##################################################################### |
| 9 | |
| 10 | . faustpath |
| 11 | . faustoptflags |
| 12 | . usage.sh |
| 13 | |
| 14 | ARCHFILE=$FAUSTARCH/ios-coreaudio.cpp |
| 15 | |
| 16 | NOAGC="0" |
| 17 | POLY="POLY" |
| 18 | OSCCTRL="0" |
| 19 | MIDICTRL="0" |
| 20 | ALL="1" |
| 21 | EFFECT="" |
| 22 | NVOICES=-1 |
| 23 | XCODE="0" |
| 24 | ARCHIVE="0" |
| 25 | SOUNDFILE="0" |
| 26 | |
| 27 | echoHelp() |
| 28 | { |
| 29 | usage faust2ios "[options] [Faust options] <file.dsp>" |
| 30 | platform iOS |
| 31 | echo "Compiles Faust programs to iOS applications" |
| 32 | option |
| 33 | options -midi -osc -soundfile |
| 34 | option "-nvoices <num>" |
| 35 | option "-effect <effect.dsp>" |
| 36 | option "-effect auto" |
| 37 | option -xcode "to compile and keep the intermediate Xcode project" |
| 38 | option -xcodeproj "to produce the intermediate Xcode project" |
| 39 | option -archive "to generate the archive for Apple Store" |
| 40 | option -32bits "to compile 32 bits only binary" |
| 41 | option -noagc "to deactivate audio automatic gain control" |
| 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 | #PHASE 1 : collects files and options from the command line |
| 53 | |
| 54 | while [ $1 ] |
| 55 | do |
| 56 | p=$1 |
| 57 | |
| 58 | if [ $p = "-help" ] || [ $p = "-h" ]; then |
| 59 | echoHelp |
| 60 | elif [ $p = "-osc" ]; then |
| 61 | OSCCTRL="1" |
| 62 | elif [ $p = "-soundfile" ]; then |
| 63 | SOUNDFILE="1" |
| 64 | elif [ "$p" = "-jack" ]; then |
| 65 | JACK="1" |
| 66 | elif [ "$p" = "-xcode" ]; then |
| 67 | XCODE="1" |
| 68 | elif [ "$p" = "-xcodeproj" ]; then |
| 69 | XCODE="2" |
| 70 | elif [ "$p" = "-nvoices" ]; then |
| 71 | shift |
| 72 | NVOICES=$1 |
| 73 | elif [ "$p" = "-effect" ]; then |
| 74 | POLY="POLY2" |
| 75 | shift |
| 76 | EFFECT=$1 |
| 77 | elif [ "$p" = "-midi" ]; then |
| 78 | MIDICTRL="1" |
| 79 | elif [ "$p" = "-32bits" ]; then |
| 80 | ALL="0" |
| 81 | elif [ "$p" = "-noagc" ]; then |
| 82 | NOAGC="1" |
| 83 | elif [ "$p" = "-archive" ]; then |
| 84 | ARCHIVE="1" |
| 85 | elif [ ${p:0:1} = "-" ]; then |
| 86 | OPTIONS="$OPTIONS $p" |
| 87 | elif [[ -f "$p" ]] && [ ${p: -4} == ".dsp" ]; then |
| 88 | FILES="$FILES $p" |
| 89 | else |
| 90 | OPTIONS="$OPTIONS $p" |
| 91 | fi |
| 92 | |
| 93 | shift |
| 94 | |
| 95 | done |
| 96 | |
| 97 | addOptions () |
| 98 | { |
| 99 | # OSC source always copied in the project |
| 100 | cp -r $FAUSTARCH/osclib "$T" |
| 101 | # FaustNode.h still has a dependency with public Faust headers, create a self-contained version |
| 102 | echo "process = _;" >> "$T"/tmp.dsp |
| 103 | echo "" >> "$T"/tmp.cpp |
| 104 | faust -i -inj "$T"/tmp.cpp -a $FAUSTARCH/osclib/faust/faust/osc/FaustNode.h "$T"/tmp.dsp -o "$T"/osclib/faust/faust/osc/FaustNode.h |
| 105 | rm "$T"/tmp.dsp "$T"/tmp.cpp |
| 106 | |
| 107 | if [ "$OSCCTRL" = "1" ]; then |
| 108 | echo "#define OSCCTRL 1" | cat - "$T/iOS/iOS-Prefix.pch" > temp && mv temp "$T/iOS/iOS-Prefix.pch" |
| 109 | fi |
| 110 | if [ "$MIDICTRL" = "1" ]; then |
| 111 | echo "#define MIDICTRL 1" | cat - "$T/iOS/iOS-Prefix.pch" > temp && mv temp "$T/iOS/iOS-Prefix.pch" |
| 112 | fi |
| 113 | if [ $POLY = "POLY2" ]; then |
| 114 | echo "#define POLY2 1" | cat - "$T/iOS/iOS-Prefix.pch" > temp && mv temp "$T/iOS/iOS-Prefix.pch" |
| 115 | fi |
| 116 | if [ "$NOAGC" = "1" ]; then |
| 117 | echo "#define NOAGC 1" | cat - "$T/iOS/iOS-Prefix.pch" > temp && mv temp "$T/iOS/iOS-Prefix.pch" |
| 118 | fi |
| 119 | if [ $NVOICES -ge 0 ]; then |
| 120 | echo "#define NVOICES $NVOICES" | cat - "$T/iOS/iOS-Prefix.pch" > temp && mv temp "$T/iOS/iOS-Prefix.pch" |
| 121 | fi |
| 122 | |
| 123 | # Copy libsndfile in the project |
| 124 | cp -r /usr/local/lib/ios-libsndfile.a "$T" |
| 125 | |
| 126 | if [ "$SOUNDFILE" = "1" ]; then |
| 127 | echo "#define SOUNDFILE 1" | cat - "$T/iOS/iOS-Prefix.pch" > temp && mv temp "$T/iOS/iOS-Prefix.pch" |
| 128 | fi |
| 129 | } |
| 130 | |
| 131 | warnSoundfile() |
| 132 | { |
| 133 | if [ "$SOUNDFILE" = "1" ]; then |
| 134 | echo "Warning : -soundfile is used, you'll have to manually add the needed soundfiles in the project" |
| 135 | fi |
| 136 | } |
| 137 | |
| 138 | #PHASE 2 : compile all files |
| 139 | |
| 140 | for p in $FILES; do |
| 141 | S=$(dirname "$p") |
| 142 | F=$(basename "$p") |
| 143 | P=${F%.dsp} |
| 144 | XCODEPROJ="${F%.dsp}" |
| 145 | SRCDIR=$(dirname "$p") |
| 146 | |
| 147 | T=$(mktemp -d faust.XXX) |
| 148 | cp -r $FAUSTARCH/iOS/* $T |
| 149 | |
| 150 | if [ "$ALL" = "1" ]; then |
| 151 | echo "Compile with CoreAudio support in 64/32 bits" |
| 152 | faust -i $OPTIONS -json -a $ARCHFILE "$p" -o "$T/ios-faust.h" || exit |
| 153 | addOptions |
| 154 | |
| 155 | if [ $POLY = "POLY2" ]; then |
| 156 | if [ $EFFECT = "auto" ]; then |
| 157 | cat > $T/effect.dsp << EndOfCode |
| 158 | adapt(1,1) = _; |
| 159 | adapt(2,2) = _,_; |
| 160 | adapt(1,2) = _ <: _,_; |
| 161 | adapt(2,1) = _,_ :> _; |
| 162 | adaptor(F,G) = adapt(outputs(F),inputs(G)); |
| 163 | process = adaptor(library("$SRCDIR/$F").process, library("$SRCDIR/$F").effect) : library("$SRCDIR/$F").effect; |
| 164 | EndOfCode |
| 165 | faust -i -json -a $ARCHFILE $OPTIONS "$p" -o "$T/${F%.dsp}_tmp.cpp" || exit |
| 166 | faust -i -cn effect -a minimal-effect.cpp "$T/effect.dsp" -o "$T/effect.h" || exit |
| 167 | rm "$T/effect.dsp" |
| 168 | else |
| 169 | faust -i -json -a $ARCHFILE $OPTIONS "$p" -o "$T/${F%.dsp}_tmp.cpp" || exit |
| 170 | faust -i -cn effect -a minimal-effect.cpp "$SRCDIR/$EFFECT" -o "$T/effect.h" || exit |
| 171 | fi |
| 172 | fi |
| 173 | |
| 174 | if [ "$XCODE" == "2" ]; then |
| 175 | warnSoundfile |
| 176 | mv $T $XCODEPROJ |
| 177 | # remove temporary JSON file |
| 178 | rm $p.json |
| 179 | exit |
| 180 | fi |
| 181 | ( |
| 182 | # Build the binary |
| 183 | xcodebuild -project "$T/iOS.xcodeproj" -target Template_CoreAudio PRODUCT_NAME=$P || exit |
| 184 | # Build the archive |
| 185 | if [ "$ARCHIVE" = "1" ]; then |
| 186 | cd "$T" && xcodebuild -scheme Template_CoreAudio archive -archivePath $P.xcarchive PRODUCT_NAME=$P |
| 187 | # Correct archive generated Info.plist to properly use '$P' name |
| 188 | cd $P.xcarchive && cp Info.plist Info-tmp.plist && sed -e "s/Template_CoreAudio/"$P"/g" Info-tmp.plist > Info.plist && rm Info-tmp.plist |
| 189 | fi |
| 190 | ) > /dev/null || exit |
| 191 | else |
| 192 | echo "Compile with CoreAudio support in 32 bits" |
| 193 | faust -i $OPTIONS -json -a $ARCHFILE "$p" -o "$T/ios-faust.h" || exit |
| 194 | addOptions |
| 195 | if [ "$POLY2" = "1" ]; then |
| 196 | faust -i $OPTIONS -json -cn effect -a minimal-effect.cpp $EFFECT -o "$T/effect.h" || exit |
| 197 | fi |
| 198 | |
| 199 | if [ "$XCODE" == "2" ]; then |
| 200 | warnSoundfile |
| 201 | mv $T $XCODEPROJ |
| 202 | # remove temporary JSON file |
| 203 | rm $p.json |
| 204 | exit |
| 205 | fi |
| 206 | ( |
| 207 | # Build the binary |
| 208 | xcodebuild -project "$T/iOS.xcodeproj" -target Template_CoreAudio_32bits PRODUCT_NAME=$P || exit |
| 209 | # Build the archive |
| 210 | if [ "$ARCHIVE" = "1" ]; then |
| 211 | cd "$T" && xcodebuild -scheme Template_CoreAudio_32bits archive -archivePath $P.xcarchive PRODUCT_NAME=$P |
| 212 | # Correct archive generated Info.plist to properly use '$P' name |
| 213 | cd $P.xcarchive && cp Info.plist Info-tmp.plist && sed -e "s/Template_CoreAudio/"$P"/g" Info-tmp.plist > Info.plist && rm Info-tmp.plist |
| 214 | fi |
| 215 | ) > /dev/null || exit |
| 216 | fi |
| 217 | |
| 218 | # move generated app to source directory |
| 219 | rm -rf "$S/$P.app" |
| 220 | mv "$T/build/Release-iphoneos/$P.app" "$S/" |
| 221 | if [ "$ARCHIVE" = "1" ]; then |
| 222 | rm -rf "$S/$P.xcarchive" |
| 223 | mv "$T/$P.xcarchive" "$S/" |
| 224 | fi |
| 225 | if [ "$XCODE" != "1" ]; then |
| 226 | rm -rf "$T" |
| 227 | else |
| 228 | echo "Keep Xcode project" |
| 229 | fi |
| 230 | |
| 231 | if [ "$SOUNDFILE" = "1" ]; then |
| 232 | # get all soundfiles from the JSON file |
| 233 | cat $p.json | awk ' |
| 234 | BEGIN { FS=":"; SOFI=0; } |
| 235 | /"soundfile"/ { SOFI=1; } |
| 236 | /"url"/ { |
| 237 | if (SOFI) { |
| 238 | match($2, /"[^"]*/); |
| 239 | split(substr($2, RSTART+2, RLENGTH-3), res, ";"); |
| 240 | for (x in res) print substr(res[x], 2, length(res[x])-2); |
| 241 | SOFI=0; |
| 242 | } |
| 243 | } |
| 244 | ' > $p-tmp.txt |
| 245 | # copy found soundfiles in the final binary |
| 246 | for snd in $(cat $p-tmp.txt); do |
| 247 | if [ -f $snd ]; then |
| 248 | if [ ${snd:0:1} = "/" ]; then |
| 249 | echo "Warning: soundfile with absolute path is not copied !" |
| 250 | else |
| 251 | #create destination path and possibly create directory |
| 252 | sfpath="$P.app/$(dirname $snd)/" |
| 253 | if ! [ -d $sfpath ]; then |
| 254 | echo "Create $sfpath" |
| 255 | mkdir $sfpath |
| 256 | fi |
| 257 | echo "Copy $snd in $P.app" |
| 258 | cp $snd $sfpath |
| 259 | if [ "$ARCHIVE" = "1" ]; then |
| 260 | sfarchivepath="$P.xcarchive/Products/Applications/$P.app/$(dirname $snd)/" |
| 261 | if ! [ -d $sfarchivepath ]; then |
| 262 | echo "Create $sfarchivepath" |
| 263 | mkdir $sfarchivepath |
| 264 | fi |
| 265 | echo "Copy $snd in $P.xcarchive/Products/Applications/$P" |
| 266 | cp $snd $sfarchivepath |
| 267 | fi |
| 268 | fi |
| 269 | else |
| 270 | echo "Error: file $snd not found !" |
| 271 | fi |
| 272 | done |
| 273 | rm $p-tmp.txt |
| 274 | fi |
| 275 | |
| 276 | # collect binary file name for FaustGIDE |
| 277 | if [ "$ARCHIVE" = "1" ]; then |
| 278 | BINARIES="$BINARIES$S/$P.app; $BINARIES$S/$P.xcarchive;" |
| 279 | else |
| 280 | BINARIES="$BINARIES$S/$P.app;" |
| 281 | fi |
| 282 | |
| 283 | # remove temporary JSON file |
| 284 | rm $p.json |
| 285 | done |
| 286 | |
| 287 | echo $BINARIES |