File: faust2bela

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 (266 lines) | stat: -rwxr-xr-x 8,461 bytes parent folder | download | duplicates (3)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/bash -e

. faustpath
. usage.sh

#FAUST DATA:
[ -z "$ARCHFILE" ] && ARCHFILE=$FAUSTARCH/bela.cpp
#BELA DATA:
[ -z "$BBB_ADDRESS" ] && BBB_ADDRESS="root@192.168.7.2"
[ -z "$BBB_BELA_HOME" ] && BBB_BELA_HOME="/root/Bela"
[ -z "$BBB_PROJECT_HOME" ] && BBB_PROJECT_HOME="${BBB_BELA_HOME}/projects/"
[ -z "$BELA_CPPFLAGS" ] && BELA_CPPFLAGS=""
[ -z "$BELA_LDLIBS" ] && BELA_LDLIBS=""

BELA_CPPFLAGS="$BELA_CPPFLAGS"

#variables:
NVOICES=0
POLY=""
EFFECT=""
MIDIDEFS=""
OSCDEFS=""
SEND2BELA=""
OPTIONS=""

echoHelp() 
{
    usage faust2bela "[options] [Faust options] <file.dsp>"
    platform Bela
    echo "Compiles Faust programs to the BELA board"
    option
    options -osc -midi -soundfile
    option "-nvoices <num>"
    echo "Polyphonic mode means MIDI instrument with at least 1 voice. Use no arguments for a simple effect."
    option -gui "activates a self-hosted GUI interface. Requires to have libmicrohttpd and libHTTPDFaust installed."
    option "-effect <effect.dsp>" "generates a polyphonic DSP connected to a global output effect, ready to be used with MIDI"
    option "-effect auto" "generates a polyphonic DSP connected to a global output effect defined as 'effect' in <file.dsp>, ready to be used with MIDI"
    option -tobela "to send C++ file into bela, and to run it"
    option "Faust options"
    exit
}

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

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

    # HELP:
    if [ $p = "-help" ] || [ $p = "-h" ]; then
        echoHelp
    # -NVOICES:
    elif [ $p = "-nvoices" ]; then
        shift
        NVOICES=$1
    # -EFFECT:
    elif [ $p = "-effect" ]; then
        POLY="POLY2"
        shift
        EFFECT=$1
    # -MIDI
    elif [ $p = "-midi" ]; then
        MIDIDEFS="MIDI"
    elif [ $p = "-osc" ]; then
        OSCDEFS="OSC"
    elif [ $p = "-gui" ]; then
        GUIDEFS="GUI"
    elif [ $p = "-soundfile" ]; then
        SOUNDFILEDEFS="SOUNDFILE"
    elif [ $p = "-tobela" ]; then
        SEND2BELA="OK"
    else
        OPTIONS="$OPTIONS $p"
    fi

shift

done
#-------------------------------------------------------------------
#PHASE 2 : compile the *.dsp files
#-------------------------------------------------------------------

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

# creates a dir
TMP="${f%.dsp}"
mkdir -p "$TMP/tmp"

PROJECTDIR="$CUR/$TMP"
FAUST_MAIN_OUTPUT=$TMP/tmp/rendertmp.cpp
# compile faust to c++
faust $FAUST_ARGS -i -a $ARCHFILE $OPTIONS "$INPUT_FILE" -o "$FAUST_MAIN_OUTPUT"

EFFECT_OUTPUT=$TMP/effect.h
if [[ "$POLY" == *POLY2* ]]; then
    if [ $EFFECT = "auto" ]; then
        # Creer un fichier .dsp simple
        cat > $TMP/effect.dsp << EndOfCode
        adapt(1,1) = _;
        adapt(2,2) = _,_;
        adapt(1,2) = _ <: _,_;
        adapt(2,1) = _,_ :> _;

        adaptor(F,G) = adapt(outputs(F),inputs(G));

        process = adaptor(library("$INPUT_FILE").process, library("$INPUT_FILE").effect) : library("$INPUT_FILE").effect;
EndOfCode
        faust $FAUST_ARGS -i -cn effect -a minimal-effect.cpp "$TMP/effect.dsp" -o "$EFFECT_OUTPUT"

    else
        faust $FAUST_ARGS -i -cn effect -a minimal-effect.cpp "$EFFECT" -o "$EFFECT_OUTPUT"
    fi
fi

#-------------------------------------------------------------------
#PHASE 3 : Data for BELA
#-------------------------------------------------------------------

echo '// Options :' > "$PROJECTDIR/tmp.txt"
if [ $NVOICES -gt 0 ]; then
    echo '#define NVOICES '$NVOICES >> "$PROJECTDIR/tmp.txt"
fi

if [[ "$MIDIDEFS" == *MIDI* ]]; then
    echo '#define MIDICTRL' >> "$PROJECTDIR/tmp.txt"
fi

if [[ "$POLY" == *POLY2* ]]; then
    echo '#define POLY2' >> "$PROJECTDIR/tmp.txt"
fi

if [[ "$OSCDEFS" == *OSC* ]]; then
    echo '#define OSCCTRL' >> "$PROJECTDIR/tmp.txt"
fi

if [[ "$GUIDEFS" == *GUI* ]]; then
    echo '#define HTTPDGUI' >> "$PROJECTDIR/tmp.txt"
    BELA_LDLIBS="$BELA_LDLIBS -lHTTPDFaust -lmicrohttpd"
fi

if [[ "$SOUNDFILEDEFS" == *SOUNDFILE* ]]; then
    echo '#define SOUNDFILE' >> "$PROJECTDIR/tmp.txt"
    BELA_LDLIBS="$BELA_LDLIBS -lsndfile"
fi

cat "$FAUST_MAIN_OUTPUT" >> "$PROJECTDIR/tmp.txt"
mv "$PROJECTDIR/tmp.txt" "$PROJECTDIR/render.cpp"
rm -rf "$PROJECTDIR/tmp"

#-------------------------------------------------------------------
#PHASE 4 : SEND data to BELA and RUN it.
#-------------------------------------------------------------------

if [[ "$SEND2BELA" == *OK* ]]; then
    echo "Send to bela"
    echo $PROJECTDIR

#-------------------------------------------------------------------
#From build_project.sh :
#-------------------------------------------------------------------
echo "Start communication with bela"

BELA_EXPERT_MODE=0
BELA_DONT_RUN_FIRST=0

# FUNCTIONS:+++++++++++++++++++++++++++++++
check_rsync(){
    if [ -z "`which rsync 2> /dev/null`" ]
    then
        return 1
    else
        return 0
    fi
}

check_board_alive_and_set_date(){
    printf "Checking the board is up and running at $BBB_ADDRESS..."
    # Clear the host if it is cached in ~/.ssh/known_hosts.
    ssh-keygen -R $BBB_HOSTNAME &> /dev/null || true
    ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 $BBB_ADDRESS "date -s \"`date '+%Y%m%d %T %z'`\" > /dev/null" && printf "done\n" || {
        printf "\nERROR: the board does not respond at $BBB_ADDRESS, check that the address is correct and the board is connected.\n";
        exit 1;
    }
}

#+++++++++++++++++++++++++++++++++++++++++++

# Test if project folder contain correct file(s):
FIND_STRING="find $PROJECTDIR -maxdepth 1 -type f "
EXTENSIONS_TO_FIND='\.cpp\|\.c\|\.S\|\.pd\|\.scd'
FOUND_FILES=$($FIND_STRING 2>/dev/null | grep "$EXTENSIONS_TO_FIND")
if [ -z "$FOUND_FILES" ]
then
    printf "ERROR: Please provide a directory containing .c, .cpp, .S, .pd or .scd files.\n\n"
    exit 1
fi

[ -z $BBB_PROJECT_NAME ] && BBB_PROJECT_NAME="$(basename $(cd "$PROJECTDIR" && pwd))"

echo $PROJECTDIR
echo $BBB_PROJECT_NAME

BBB_PROJECT_FOLDER=$BBB_PROJECT_HOME"/"$BBB_PROJECT_NAME #make sure there is no trailing slash here
BBB_NETWORK_TARGET_FOLDER=$BBB_ADDRESS:$BBB_PROJECT_FOLDER

# The expert will have to remember to run set_date after powering up the board if needed
[ "$BELA_EXPERT_MODE" -eq 0 ] && check_board_alive_and_set_date

# Stop running process
echo "Stop running process..."
ssh $BBB_ADDRESS make QUIET=true --no-print-directory -C $BBB_BELA_HOME stop

# Check if rsync is available
check_rsync && RSYNC_AVAILABLE=1 || RSYNC_AVAILABLE=0

uploadBuildRun(){
    # Copy new source files to the board
    echo "uploadBuildRun START"
    printf "Copying new source files to BeagleBone..."
    if [ "$RSYNC_AVAILABLE" -eq 0 ];
    then
        echo "Using scp..."

        echo "Cleaning the destination folder..."
        #if rsync is not available, brutally clean the destination folder
        ssh $BBB_ADDRESS "rm -rf \"$BBB_PROJECT_FOLDER\"; mkdir -p \"$BBB_PROJECT_FOLDER\""
        echo "Copying the project files"
        scp -r $PROJECTDIR/* "$BBB_NETWORK_TARGET_FOLDER"
    else
        #rsync
        # --delete makes sure it removes files that are not in the origin folder
        # -c evaluates changes using md5 checksum instead of file date, so we don't care about time skews
        # --no-t makes sure file timestamps are not preserved, so that the Makefile will not think that targets are up to date when replacing files on the BBB
        #  with older files from the host. This will solve 99% of the issues with Makefile thinking a target is up to date when it is not.
        echo "Using rsync..."

        rsync -ac --out-format="   %n" --no-t --delete-after --exclude=$BBB_PROJECT_NAME --exclude=build --exclude=settings.json $PROJECTDIR"/" "$BBB_NETWORK_TARGET_FOLDER/" #trailing slashes used here make sure rsync does not create another folder inside the target folder
    fi

    if [ $? -ne 0 ]
    then
        printf "\nError while copying files\n"
        exit
    fi

    # Make new Bela executable and run
    MAKE_COMMAND="make --no-print-directory QUIET=true -C $BBB_BELA_HOME PROJECT='$BBB_PROJECT_NAME' CL='$COMMAND_ARGS' LDLIBS='$BELA_LDLIBS' CPPFLAGS='$BELA_CPPFLAGS' AT="

    ssh -t $BBB_ADDRESS "$MAKE_COMMAND run"
}

echo "Run bela now"
uploadBuildRun

fi
echo "$PROJECTDIR/render.cpp;"