File: faust2android

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 (273 lines) | stat: -rwxr-xr-x 8,737 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#! /bin/bash -e

#####################################################################
#                                                                   #
#               Compile a Faust program to an Android app           #
#               (c) Romain Michon CCRMA and Grame, 2014             #
#               (c) Yann Orlarey Grame, 2015                        #
#                                                                   #
#####################################################################

. faustpath
. faustoptflags
. usage.sh

# change if you want to get the log of what's happening
LOG="/dev/null"

# exit if a command fails
set -e

# Global variables for file and options
FILE=
INSTALL=0
SOURCE=0
SWIG=0
FAUST=0
KEYBOARD=0
REUSE=0
SOUNDFILE=0
OSCCTRL=0

echoHelp() 
{
    usage faust2android "[options] [Faust options] <file.dsp>"
    platform Android
    require Android SDK
    echo "Compile a Faust program to an Android app (see https://github.com/grame-cncm/faust/tree/master-dev/architecture/android)"
    option
    option -osc
    option -source "creates an eclipse project of the app in the current directory."
    option -swig "regenerates the C++ and the JAVA interface for the native portion of the app."
    option -faust "only carries out the Faust compilation and install the generated C++ file in the JNI folder."
    option -reuse "preserves build directory and reuse it to speedup compilation."
    option -soundfile
    option -install "once compilation is over, installs the generated apk on the Android device connected to the computer"
    option -debug "activates verbose output"
    option "Faust options"
    exit
}

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

# PHASE 2 : dispatch command arguments
for p in $@; do
	if [ $p = "-swig" ]; then
		SWIG=1
	elif [[ -f "$p" ]] && [ ${p: -4} == ".dsp" ]; then
		FILE="$p"
	elif [ $p = "-install" ]; then
		INSTALL=1
	elif [ $p = "-soundfile" ]; then
		SOUNDFILE="1"
	elif [ $p = "-osc" ]; then
		OSCCTRL="1"
	elif [ $p = "-source" ]; then
		SOURCE=1
	elif [ $p = "-faust" ]; then
		FAUST=1
	elif [ $p = "-reuse" ]; then
		REUSE=1
	elif [ $p = "-keyboard" ]; then
		KEYBOARD=1
	elif [ $p = "-debug" ]; then
		LOG="/dev/stdout"
	elif [ "$p" = "-noagc" ]; then
		NOAGC="1"
	elif [ $p = "-help" ] || [ $p = "-h" ]; then
		echoHelp
	elif [ ${p:0:1} = "-" ]; then
		OPTIONS="$OPTIONS $p"
	else
		OPTIONS="$OPTIONS $p"
	fi
done

# only carry out the faust compilation
if [ $FAUST -eq 1 ]; then
	faust $OPTIONS -i -a api/DspFaust.cpp "$FILE" -o "app/src/main/cpp/DspFaust.cpp"
	exit 1
fi

# Create the temporary directory where compilation will take place

APPNAME=$(basename "$FILE")
APPNAME="${APPNAME%.dsp}"
APPNAME=`filename2ident "$APPNAME"`
BUILDDIR="faustandro.$APPNAME"
APPFOLDER="$BUILDDIR/app/src/main"
JNIFOLDER="$APPFOLDER/cpp"

if [ $REUSE -eq 0 ]; then
	if [ -d "$BUILDDIR" ]; then
		echo "Delete existing Android project $BUILDDIR" > $LOG
		rm -rf "$BUILDDIR"
	fi
fi

if [ ! -d "$BUILDDIR" ]; then
	echo "Creating new Android project $BUILDDIR"  > $LOG

	mkdir -p "$BUILDDIR"
	cp -r $FAUSTARCH/android/*  "$BUILDDIR"
	install -d $JNIFOLDER
	# Copy include files *.h if any (ignore any error here)
	(cp *.h $JNIFOLDER 2> $LOG) || true

	# change 'faust' with real *APPNAME
	PLATFORM=$(uname)

	if [ $PLATFORM = "Darwin" ]; then
		sed -i '' 's,com.faust,com.'$APPNAME',g' $BUILDDIR/app/build.gradle
	if [ "$NOAGC" = "1" ]; then
		sed -i '' 's,-DDISABLE_AGC,'-DDISABLE_AGC',g' $BUILDDIR/app/build.gradle
	else
		sed -i '' 's,-DDISABLE_AGC,'',g' $BUILDDIR/app/build.gradle
	fi
		sed -i '' 's,com.faust,com.'$APPNAME',g' $APPFOLDER/java/com/faust/*
		sed -i '' 's,com.faust,com.'$APPNAME',g' $APPFOLDER/java/com/triggertrap/seekarc/*
		sed -i '' 's,com.faust,com.'$APPNAME',g' $APPFOLDER/AndroidManifest.xml
		sed -i '' 's,com.faust,com.'$APPNAME',g' $APPFOLDER/res/layout/*
		sed -i '' 's,1,'$APPNAME',g' $APPFOLDER/res/values/strings.xml
	else
		sed -i 's,com.faust,com.'$APPNAME',g' $BUILDDIR/app/build.gradle
	if [ "$NOAGC" = "1" ]; then
		sed -i 's,-DDISABLE_AGC,'-DDISABLE_AGC',g' $BUILDDIR/app/build.gradle
	else
		sed -i 's,-DDISABLE_AGC,'',g' $BUILDDIR/app/build.gradle
	fi
		sed -i 's,com.faust,com.'$APPNAME',g' $APPFOLDER/java/com/faust/*
		sed -i 's,com.faust,com.'$APPNAME',g' $APPFOLDER/java/com/triggertrap/seekarc/*
		sed -i 's,com.faust,com.'$APPNAME',g' $APPFOLDER/AndroidManifest.xml
		sed -i 's,com.faust,com.'$APPNAME',g' $APPFOLDER/res/layout/*
		sed -i 's,1,'$APPNAME',g' $APPFOLDER/res/values/strings.xml
	fi

	mv $APPFOLDER/java/com/faust $APPFOLDER/java/com/$APPNAME

	# TODO wrong: should be checked
	if [ $SWIG -eq 1 ]; then
		rm -rf $APPFOLDER/java/com/dsp_faust || true
		mkdir -p $APPFOLDER/java/com/dsp_faust
		swig -java -package com.dsp_faust -includeall -verbose -outdir $APPFOLDER/java/com/dsp_faust -c++ -I$FAUSTINC -I/System/Library/Frameworks/JavaVM.framework/Headers -I$JNIFOLDER -o $JNIFOLDER/java_interface_wrap.cpp $BUILDDIR/dsp_faust_interface.i
	fi
else
	echo "Reusing existing Android project $BUILDDIR" > $LOG
fi

# Copying the Faust API files in the project
cp $FAUSTARCH/api/DspFaust.h $JNIFOLDER
cp $FAUSTARCH/api/android/jni/java_interface_wrap.cpp $JNIFOLDER
mkdir $APPFOLDER/java/com/DspFaust
cp $FAUSTARCH/api/android/jni/*.java $APPFOLDER/java/com/DspFaust

# Compile the Faust code for the NDK
faust $OPTIONS -i -json -a api/DspFaust.cpp "$FILE" -o "$JNIFOLDER/DspFaust.cpp" || exit

# Add soundfile support if needed
if [ $SOUNDFILE -eq 1 ]; then
	cat $FILE.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;
                                }
                        }
                        ' > $APPNAME-tmp.txt
	for snd in $(cat $APPNAME-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="$APPFOLDER/assets/$(dirname $snd)/"
							if ! [ -d $sfpath ]; then
									echo "Create $sfpath"
									mkdir -p $sfpath
							fi
							echo "Copy $snd in apk"
							cp $snd $sfpath
					fi
			else
					echo "Error: file $snd not found !"
			fi
	done
	if [ $PLATFORM = "Darwin" ]; then
		sed -i '' 's,__CURRENT_ANDROID_PACKAGE__,com.'$APPNAME',g' $APPFOLDER/cpp/DspFaust.cpp
	else
		sed -i 's,__CURRENT_ANDROID_PACKAGE__,com.'$APPNAME',g' $APPFOLDER/cpp/DspFaust.cpp
	fi
	mv $BUILDDIR/app/CMakeLists_sndfile.txt $BUILDDIR/app/CMakeLists.txt
	if [ $OSCCTRL -eq 1 ]; then
	if [ $PLATFORM = "Darwin" ]; then
		sed -i '' 's,set(OSCCTRL\ false),set(OSCCTRL\ true),g' $BUILDDIR/app/CMakeLists.txt
	else
		sed -i 's,set(OSCCTRL\ false),set(OSCCTRL\ true),g' $BUILDDIR/app/CMakeLists.txt
	fi
  fi
	rm $APPNAME-tmp.txt
else
	rm $BUILDDIR/app/CMakeLists_sndfile.txt
	if [ $OSCCTRL -eq 1 ]; then
	if [ $PLATFORM = "Darwin" ]; then
		sed -i '' 's,set(OSCCTRL\ false),set(OSCCTRL\ true),g' $BUILDDIR/app/CMakeLists.txt
	else
		sed -i 's,set(OSCCTRL\ false),set(OSCCTRL\ true),g' $BUILDDIR/app/CMakeLists.txt
	fi
  fi
	rm -r $BUILDDIR/app/lib # that's soooo dirty but that will do for now...
fi
rm $FILE.json

# Run Gradle
cd $BUILDDIR

# Try to use installed gradle instead of local ./gradlew in order
# to avoid potential problems when compiling in a shared folder
# where execution rights may not be granted (i.e. on a google VM)
# In order to install gradle-4.6:
#	wget https://services.gradle.org/distributions/gradle-4.6-bin.zip
#   unzip -d /opt/gradle gradle-4.6-bin.zip

if [ -f /opt/gradle/gradle-4.10.1/bin/gradle ]; then
	FAUSTGRADLE=${FAUSTGRADLE:=/opt/gradle/gradle-4.10.1/bin/gradle}
else
	FAUSTGRADLE=${FAUSTGRADLE:=./gradlew}
	chmod a+x ./gradlew
fi

echo "USED GRADLE=$FAUSTGRADLE" > $LOG

$FAUSTGRADLE assembleRelease > $LOG
cd ..

cp -r $BUILDDIR/app/build/outputs/apk/release/app-release.apk $APPNAME.apk

# ****************
# TREAT OPTIONS
# ****************

if [ $INSTALL -eq 1 ]; then
	adb install -r $APPNAME.apk
fi

if [ $SOURCE -eq 1 ]; then
	rm -rf faustApp
	mv $BUILDDIR faustApp
	echo "An Android studio project named faustApp was created." > $LOG
else
	if [ $REUSE -eq 0 ]; then
		echo "Delete Android project $BUILDDIR" > $LOG
		rm -rf $BUILDDIR
	fi
fi

echo "$APPNAME.apk;"