File: faust2msp

package info (click to toggle)
faust 2.79.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 397,496 kB
  • sloc: cpp: 278,433; ansic: 116,164; javascript: 18,529; vhdl: 14,052; sh: 13,884; java: 5,900; objc: 3,852; python: 3,222; makefile: 2,655; cs: 1,672; lisp: 1,146; ruby: 954; yacc: 586; xml: 471; lex: 247; awk: 110; tcl: 26
file content (237 lines) | stat: -rwxr-xr-x 7,410 bytes parent folder | download
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
#! /bin/bash -e

#####################################################################
#                                                                   #
#               Compiles Faust programs to Max externals            #
#               using simple precision samples                      #
#               (c) Grame, 2012-2019                                #
#                                                                   #
#####################################################################

. faustpath
. faustoptflags
. usage.sh

CXXFLAGS+=" $MYGCCFLAGSGENERIC"  # So that additional CXXFLAGS can be used

OSCDEFS=""
OSCLIB=""
NVOICES=-1
EFFECT=""
SOUNDFILE="0"
SOUNDFILEDEFS=""
SOUNDFILELIBS=""
US="0"
DS="0"
FILTER="0"

# path to max SDK
# MAXSDK is declared in faustpath
MAXINC=$MAXSDK/max-includes
MSPINC=$MAXSDK/msp-includes

createInfoPList() {
(
    echo '<?xml version="1.0" encoding="UTF-8"?>'
    echo '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">'
    echo '<plist version="1.0">'
    echo '<dict>'
    echo '  <key>CFBundleDevelopmentRegion</key>'
    echo '  <string>English</string>'
    echo '  <key>CFBundleExecutable</key>'
    echo "    <string>$1</string>"
    echo '  <key>CFBundleIconFile</key>'
    echo '  <string></string>'
    echo '  <key>CFBundleIdentifier</key>'
    echo "  <string>com.grame.$1</string>"
    echo '  <key>CFBundleInfoDictionaryVersion</key>'
    echo '  <string>1.0.0</string>'
    echo '  <key>CFBundlePackageType</key>'
    echo '  <string>iLaX</string>'
    echo '  <key>CFBundleSignature</key>'
    echo '  <string>max2</string>'
    echo '  <key>CFBundleVersion</key>'
    echo '  <string>1.0.0</string>'
    echo '  <key>CFBundleShortVersionString</key>'
    echo '  <string>1.0.0</string>'
    echo '  <key>CFBundleLongVersionString</key>'
    echo "  <string>$1 1.0.0, Copyright 2012-2024 Grame</string>"
    echo '  <key>CSResourcesFileMapped</key>'
    echo '  <true/>'
    echo '</dict>'
    echo '</plist>'
    ) > "$2"
}

#-------------------------------------------------------------------
# Analyze command arguments :
# faust options                 -> OPTIONS
# if -omp : -openmp or -fopenmp -> OPENMP
# existing *.dsp files          -> FILES
#

# dispatch command arguments
while [ $1 ]
do
    p=$1

    if [ $p = "-help" ] || [ $p = "-h" ]; then
        usage faust2msp "[options] [Faust options] <file.dsp>"
        require Max/MSP SDK
        echo "Compiles Faust programs to Max6 externals using simple precision samples"
        option
        options -osc -midi -soundfile
        option "-nvoices <num>"
        option "-us <factor>"
        option "-ds <factor>"
        option "-filter <filter>"
        option "-effect <effect.dsp>"
        option -native "to compile for the native CPU (otherwise the 'generic' mode is used by default)"
        option "Faust options"
        exit
    fi

    if [ $p = "-nvoices" ]; then
        shift
        NVOICES=$1
        if [ $NVOICES -ge 0 ]; then
            CXXFLAGS="$CXXFLAGS -DNVOICES=$NVOICES"
        fi
    elif [ $p = "-effect" ]; then
        POLYDEFS="-DPOLY2"
        shift
    EFFECT=$1
    elif [ $p = "-midi" ]; then
        MIDIDEFS="-DMIDICTRL"
    elif [ $p = "-soundfile" ]; then
        SOUNDFILE="1"
        SOUNDFILEDEFS="-DSOUNDFILE"
        SOUNDFILELIBS=`pkg-config --cflags --static --libs sndfile`
    elif [ $p = "-osc" ]; then
        OSCDEFS="-DOSCCTRL"
        OSCLIBS="-lOSCFaust"
    elif [ $p = "-native" ]; then
        CXXFLAGS=$MYGCCFLAGS
    elif [ $p = "-us" ]; then
        shift
        US=$1
    elif [ $p = "-ds" ]; then
        shift
        DS=$1
    elif [ $p = "-filter" ]; then
        shift
        FILTER=$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

#customize CXXFLAGS

CXXFLAGS+=" -fbracket-depth=512"

#-------------------------------------------------------------------
# Check darwin specifics
#
if [[ $(uname) == Darwin ]]; then
    EXT="~.mxo"
fi

#-------------------------------------------------------------------
# compile the *.dsp files
#
PATH=$PATH:/usr/local/bin

for p in $FILES; do

    CC=g++

    CUR=$(pwd)
    f=$(basename "$p")
    SRCDIR=$(dirname "$p")

    # creates a temporary dir
    TDR=$(mktemp -d faust.XXXXXX)
    TMP="$TDR/${f%.dsp}"
    mkdir "$TMP"

    # compile faust to c++
    if [ "$EFFECT" = "" ]; then
        faust -i -cn ${f%.dsp} -uim -json -a $FAUSTARCH/max-msp/max-msp.cpp $OPTIONS "$SRCDIR/$f" -o "$TMP/${f%.dsp}.cpp" || exit
    else
        faust -i -cn ${f%.dsp} -uim -json -a $FAUSTARCH/max-msp/max-msp.cpp $OPTIONS "$SRCDIR/$f" -o "$TMP/${f%.dsp}.cpp" || exit
        faust -i -cn effect -a $FAUSTARCH/minimal-effect.cpp "$SRCDIR/$EFFECT" -o "$TMP/effect.h" || exit
    fi

    # compile c++ to binary
    (
        cd "$TMP"
        install -d "${f%.dsp}$EXT/Contents/MacOS"
        $CXX $CXXFLAGS -mmacosx-version-min=10.9 -Wfatal-errors -framework Carbon $POLYDEFS $MIDIDEFS $SOUNDFILEDEFS $OSCDEFS -DDOWN_SAMPLING=$DS -DUP_SAMPLING=$US -DFILTER_TYPE=$FILTER -I ../../ -I"$MAXINC" -I"$MSPINC" -F"$MAXINC" -F"$MSPINC" -framework MaxAPI -framework MaxAudioAPI $SOUNDFILELIBS $OSCLIBS -arch i386 -Wl,-Y,1455 -bundle "${f%.dsp}.cpp" -o "${f%.dsp}$EXT/Contents/MacOS/${f%.dsp}~"
    ) >/dev/null  || exit

    rm -rf "$SRCDIR/${f%.dsp}$EXT"

    # Keep .dsp and .cpp files in the plug-in
    cp "$TMP/${f%.dsp}.cpp" "$TMP/${f%.dsp}$EXT"
    cp "$SRCDIR/$f" "$TMP/${f%.dsp}$EXT"
    # Create Info.plist
    createInfoPList "${f%.dsp}~" "$TMP/${f%.dsp}$EXT/Contents/Info.plist"

    if [ "$SOUNDFILE" = "1" ]; then
        # get all soundfiles from the JSON file
        cat $p.json | awk '
                        BEGIN { FS=":"; SOFI=0; }
                            /"soundfile"/ { SOFI=1; }
                            /"url"/ {
                            if (SOFI) {
                                match($2, /"[^"]*/);
                                split(substr($2, RSTART+2, RLENGTH-3), res, ";");
                                for (x in res) print substr(res[x], 2, length(res[x])-2);
                                SOFI=0;
                            }
                        }
        ' > $p-tmp.txt
        # copy found soundfiles in the final binary
        for snd in $(cat $p-tmp.txt); do
            if [ -f $snd ]; then
                if [ ${snd:0:1} = "/" ]; then
                    echo "Warning: soundfile with absolute path is not copied !"
                else
                    #create destination path and possibly create directory
                    sfpath="$TMP/${f%.dsp}$EXT/$(dirname $snd)/"
                    if ! [ -d $sfpath ]; then
                        echo "Create $sfpath"
                        mkdir $sfpath
                    fi
                    echo "Copy $snd in ${f%.dsp}$EXT"
                    cp $snd $sfpath
                fi
            else
                echo "Error: file $snd not found !"
            fi
        done
        rm $p-tmp.txt
    fi

    cp -r "$TMP/${f%.dsp}$EXT" "$SRCDIR/${f%.dsp}$EXT"
    rm -rf "$TDR"
    rm $p.json

    # collect binary file name for FaustWorks
    BINARIES="$BINARIES$SRCDIR/${f%.dsp}$EXT;"

done

BINARIES="$BINARIES ui.js"

echo $BINARIES