File: faust2unity

package info (click to toggle)
faust 2.81.10%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 431,496 kB
  • sloc: cpp: 283,941; ansic: 116,215; javascript: 18,529; sh: 14,356; vhdl: 14,052; java: 5,900; python: 5,091; objc: 3,852; makefile: 2,725; cs: 1,672; lisp: 1,146; ruby: 954; yacc: 586; xml: 471; lex: 247; awk: 111; tcl: 26
file content (244 lines) | stat: -rwxr-xr-x 7,563 bytes parent folder | download | duplicates (2)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#! /bin/bash

#############################################################################################
#                                                                                           #
#   Compiles Faust programs to Android, OSX, iOS, Microsoft 32bits and 64 bits libraries    #
#   suitable for the Unity environment                                                      #
#                                                                                           #
#   (c) Grame, 2017                                                                         #
#                                                                                           #
#############################################################################################

#   The libraries are first wrapped by the unityplugin.cpp architecture file.
#   They have to be used in the Unity environment.
#   The libraries do not work by themselves.
#   They need the FaustPlugin_<dspname>.cs and FaustUtilities_<dspname>.cs files to be functional.
#   These files are automatically generated in this script.
#   By default, all generated files are packed in a unitypackage archive to be read as a package by Unity.
#   Several options are available, see architecture/unity/README.md for more info.

. faustpath
. usage.sh

echoHelp()
{
    usage faust2unity "[options] [Faust options] <file.dsp>"
    platform Android, Linux, MacOSX, iOS, Windows
    require Unity
    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)"
    option
    option -w32 "generates a Windows 32 bits library"
    option -w64 "generates a Windows 64 bits library"
    option -osx "generates a macOS library"
    option -ios "generates an iOS library"
    option -android "generates Android libraries (armeabi-v7a and x86)."
    option -linux "generates a Linux library"
    option "-nvoices <num>"
    option -source "generates the source files (uncompressed folder)"
    option -universal "generates a universal (arm/intel) external on OSX"
    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."
    option "Faust options"
     $ARCH
    echo "See architecture/unity/README.md for more info."
    exit
}

if [ "$#" -eq 0 ]; then
    echo 'Please, provide a Faust file to process !'
    echo ''
    echoHelp
fi

#-----------------------------------------------------------------
# Dispatch command arguments

ALL="1"
W32="0"
W64="0"
OSX="0"
IOS="0"
ANDROID="0"
LINUX="0"
SRC="0"
UNPCK="0"
NVOICES=-1
FAUST_UTILITY=FaustUtilities_template.cs
ARCH=""

while [ $1 ]
do
    p=$1

    if [ $p = "-help" ] || [ $p = "-h" ]; then
        echoHelp
    elif [ $p = "-w32" ]; then
        W32="1"
        ALL="0"
    elif [ $p = "-w64" ]; then
        W64="1"
        ALL="0"
    elif [ $p = "-osx" ]; then
        OSX="1"
        ALL="0"
    elif [ $p = "-ios" ]; then
        IOS="1"
        ALL="0"
    elif [ $p = "-android" ]; then
        ANDROID="1"
        ALL="0"
    elif [ $p = "-linux" ]; then
        LINUX="1"
        ALL="0"
    elif [ $p = "-universal" ]; then
        ARCH="-universal"
    elif [ $p = "-nvoices" ]; then
        shift
        NVOICES=$1
        FAUST_UTILITY=FaustPolyUtilities_template.cs
    elif [ $p = "-source" ]; then
        SRC="1"
        ALL="0"
    elif [ $p = "-unpacked" ]; then
        UNPCK="1"
    elif [ ${p:0:1} = "-" ]; then
        OPTIONS="$OPTIONS $p"
    elif [[ -f "$p" ]] && [ ${p: -4} == ".dsp" ]; then
        FILES="$FILES $p"
    else
        OPTIONS="$OPTIONS $p"
    fi

shift
done

#-------------------------------------
# Compiles all *.dsp files

ARCHIVES=""

for p in $FILES; do

    CUR=$(pwd)
    f=$(basename "$p")
    SRCDIR=$(dirname "$p")
    NAME=${f%.dsp}
    FNAME=FaustPlugin_$NAME
    FIN=$SRCDIR/$FNAME

    # creates final dir / overwrites it if already created
    if [ -d "$FIN" ]; then
        rm -rf "$FIN"
    fi
    mkdir "$FIN"

    # adds README to final dir while setting dspname
    sed "s/<dspname>/$NAME/g" $FAUSTARCH/unity/README.md > $FIN/README.md

    # finds number of parameters, required for the c# scripts
    faust -uim $f -o $NAME.cpp
    grep FAUST_ACTIVES $NAME.cpp > params.txt
    var1=$(cut -d" " -f3- params.txt)
    grep FAUST_PASSIVES $NAME.cpp > params.txt
    var2=$(cut -d" " -f3- params.txt)
    let "var=var1+var2"
    rm $NAME.cpp
    rm params.txt

    # compiles faust to c++ and json (adds JSON to C# string)
    faust -uim -json "$SRCDIR/$f" -o /dev/null;
    tr -d '\n' < $f.json > $NAME.json
    sed 's/\"/\\\\"/g;s/\//\\\//g' $NAME.json > $f.json
    JSON=$(<$f.json)

    # generates c# files
    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
    sed -e "s/MODEL/$NAME/g;s/VARIABLES/$var/g;" $FAUSTARCH/unity/FaustPlugin_template.cs > $FIN/$FNAME.cs

    rm -rf $NAME.json
    rm -rf $f.json

    # compiles c++ to binaries

    # w64
    if [ $W64 = "1" ] || [ $ALL = "1" ]; then
        WINBITS="$WINBITS -64"
    fi

    # w32
    if [ $W32 = "1" ] || [ $ALL = "1" ]; then
        WINBITS="$WINBITS -32"
    fi

    # win
    if [ -n "$WINBITS" ]; then
        faust2winunity -nvoices $NVOICES $WINBITS $f || exit
    fi

    # macos
    if [ $OSX = "1" ] || [ $ALL = "1" ]; then
        faust2osxiosunity $ARCH -nvoices $NVOICES $f || exit
    fi

    # ios
    if [ $IOS = "1" ] || [ $ALL = "1" ]; then
        faust2osxiosunity -nvoices $NVOICES -ios $f || exit
    fi

    # android
    if [ $ANDROID = "1" ] || [ $ALL = "1" ]; then
        faust2androidunity -nvoices $NVOICES $f || exit
    fi

    # Linux
    if [ $LINUX = "1" ] || [ $ALL = "1" ]; then
        faust2linuxunity -nvoices $NVOICES $f || exit
    fi

    # Dispatch files in Unity hierarchy
    if [ "$SRC" = "0" ]; then
        if [ "$UNPCK" = "1" ]; then # Specific folder name if unpacked
            PLGN=$FNAME\_Assets/Plugins
            SCRPT=$FNAME\_Assets/Scripts
        else
            PLGN=Assets/Plugins
            SCRPT=Assets/Scripts
        fi

        if [ ! -d "$PLGN" ]; then
            mkdir -p $PLGN
        else
            rm -rf $PLGN
            mkdir -p $PLGN
        fi

        if [ ! -d "$SCRPT" ]; then
            mkdir -p $SCRPT
        else
            rm -rf $SCRPT
            mkdir -p $SCRPT
        fi

        echo $NAME > .VAR
        mv $FNAME/*.cs $SCRPT
        mv $FNAME $PLGN

        if [ "$UNPCK" = "0" ]; then
            encoderunitypackage Assets # encode unity package
            ARCHIVES="$ARCHIVES$FNAME.unitypackage;" # add package name ARCHIVES variable to display compile package name
        elif [ "$UNPCK" = "1" ]; then
            ARCHIVES=$ARCHIVES$FNAME\_Assets\; # add package name ARCHIVES variable to display compile folder
        fi

    elif [ "$SRC" = "1" ]; then
        # add .dsp and .cpp files to sources folder
        faust -uim -i -a $FAUSTARCH/unity/unityplugin.cpp $f -o $NAME.cpp
        mv "$NAME.cpp" "$FNAME/$NAME.cpp"
        cp "$f" "$FNAME/$f"
        SNAME=$FNAME\_sources
        mv "$FNAME" "$SNAME"
        ARCHIVES="$ARCHIVES$SNAME;"
    fi

done

echo "$ARCHIVES"