| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. -e |
| 2 | |
| 3 | ##################################################################### |
| 4 | # # |
| 5 | # faust2vcvrack generator # |
| 6 | # (c) Grame, 2020-2022 # |
| 7 | # # |
| 8 | ##################################################################### |
| 9 | |
| 10 | . faustpathscip-shell source . . faustpath. |
| 11 | . faustoptflagsscip-shell source . . faustoptflags. |
| 12 | . usage.shscip-shell source . . `usage.sh`. |
| 13 | |
| 14 | CXXFLAGS+=" $MYGCCFLAGS" # So that additional CXXFLAGS can be used |
| 15 | |
| 16 | ARCHFILE=$FAUSTARCH/vcvrack/template/src/FaustModule.cpp |
| 17 | |
| 18 | # exit if a command fails |
| 19 | set -e |
| 20 | |
| 21 | # global option variables |
| 22 | NVOICES=1 |
| 23 | POLY="" |
| 24 | SOURCE=false |
| 25 | SOUNDFILE=false |
| 26 | VERSION="2.0.0" |
| 27 | |
| 28 | echoHelpscip-shell shell shell-project 0.0.0 echoHelp().() |
| 29 | { |
| 30 | echoscip-shell system . . echo. "faust2vcvrack [-soundfile] [-source] [-nvoices <num>] [Faust options] <file.dsp>" |
| 31 | echoscip-shell system . . echo. "Compiles Faust programs to VCV Rack modules (see https://github.com/grame-cncm/faust/tree/master-dev/architecture/vcvrack)" |
| 32 | options -soundfile |
| 33 | option "-source" |
| 34 | option "-nvoices <num>" |
| 35 | option "-version <1|2>" "to set the plugin version, 2 by default" |
| 36 | option "Faust options" |
| 37 | echoscip-shell system . . echo. "" |
| 38 | exit |
| 39 | } |
| 40 | |
| 41 | if [scip-shell system . . `[`. "$#" -eq 0 ]; then |
| 42 | echoscip-shell system . . echo. 'Please, provide a Faust file to process !' |
| 43 | echoscip-shell system . . echo. '' |
| 44 | echoHelpscip-shell shell shell-project 0.0.0 echoHelp(). |
| 45 | fi |
| 46 | |
| 47 | ########################### |
| 48 | # Processing Arguments |
| 49 | ########################### |
| 50 | |
| 51 | while [scip-shell system . . `[`. $1 ] |
| 52 | do |
| 53 | p=$1 |
| 54 | # help |
| 55 | if [scip-shell system . . `[`. $p = "-help" ] || [scip-shell system . . `[`. $p = "-h" ]; then |
| 56 | echoHelpscip-shell shell shell-project 0.0.0 echoHelp(). |
| 57 | # -nvoices: |
| 58 | elif [scip-shell system . . `[`. $p = "-nvoices" ]; then |
| 59 | shift |
| 60 | NVOICES=$1 |
| 61 | elif [scip-shell system . . `[`. $p = "-soundfile" ]; then |
| 62 | SOUNDFILE=true |
| 63 | elif [scip-shell system . . `[`. $p = "-source" ]; then |
| 64 | SOURCE=true |
| 65 | elif [scip-shell system . . `[`. $p = "-version" ]; then |
| 66 | shift |
| 67 | VERSION=$1.0.0 |
| 68 | elif [[ -f "$p" ]] && [scip-shell system . . `[`. ${p: -4} == ".dsp" ]; then |
| 69 | FILE="$p" |
| 70 | # other compile options |
| 71 | else |
| 72 | OPTIONS="$OPTIONS $p" |
| 73 | fi |
| 74 | |
| 75 | shift |
| 76 | |
| 77 | done |
| 78 | |
| 79 | createPluginJSONscip-shell shell shell-project 0.0.0 createPluginJSON().() |
| 80 | { |
| 81 | ( |
| 82 | echoscip-shell system . . echo. '{' |
| 83 | echoscip-shell system . . echo. -e "\t\"slug\": \"$1\"," |
| 84 | echoscip-shell system . . echo. -e "\t\"name\": \"$1\"," |
| 85 | echoscip-shell system . . echo. -e "\t\"version\": \"$VERSION\"," |
| 86 | echoscip-shell system . . echo. -e '\t"license": "MIT",' |
| 87 | echoscip-shell system . . echo. -e '\t"brand": "Faust",' |
| 88 | echoscip-shell system . . echo. -e '\t"author": "GRAME",' |
| 89 | echoscip-shell system . . echo. -e '\t"authorEmail": "[email protected]",' |
| 90 | echoscip-shell system . . echo. -e '\t"authorUrl": "https://faust.grame.fr",' |
| 91 | echoscip-shell system . . echo. -e '\t"pluginUrl": "",' |
| 92 | echoscip-shell system . . echo. -e '\t"manualUrl": "",' |
| 93 | echoscip-shell system . . echo. -e '\t"sourceUrl": "https://github.com/grame-cncm/faust",' |
| 94 | echoscip-shell system . . echo. -e '\t"donateUrl": "",' |
| 95 | echoscip-shell system . . echo. -e '\t"changelogUrl": "",' |
| 96 | echoscip-shell system . . echo. -e '\t"modules": [' |
| 97 | echoscip-shell system . . echo. -e '\t\t{' |
| 98 | echoscip-shell system . . echo. -e "\t\t\t\"slug\": \"$1\"," |
| 99 | echoscip-shell system . . echo. -e "\t\t\t\"name\": \"$1\"," |
| 100 | echoscip-shell system . . echo. -e '\t\t\t"description": "",' |
| 101 | echoscip-shell system . . echo. -e '\t\t\t"tags": [' |
| 102 | if [scip-shell system . . `[`. $NVOICES -gt 1 ]; then |
| 103 | echoscip-shell system . . echo. -e '\t\t\t\t"Polyphonic"' |
| 104 | fi |
| 105 | echoscip-shell system . . echo. -e '\t\t\t]' |
| 106 | echoscip-shell system . . echo. -e '\t\t}' |
| 107 | echoscip-shell system . . echo. -e '\t]' |
| 108 | echoscip-shell system . . echo. '}' |
| 109 | ) > "$2" |
| 110 | } |
| 111 | |
| 112 | ########################### |
| 113 | # Compile the *.dsp files |
| 114 | ########################### |
| 115 | |
| 116 | for p in $FILE; do |
| 117 | CUR=$(pwdscip-shell system . . pwd.) |
| 118 | f=$(basenamescip-shell system . . basename. "$p") |
| 119 | SRCDIR=$(dirnamescip-shell system . . dirname. "$p") |
| 120 | |
| 121 | # creates the dir |
| 122 | dspName="${f%.dsp}" |
| 123 | rmscip-shell system . . rm. -rf "$SRCDIR/$dspName" |
| 124 | mkdirscip-shell system . . mkdir. "$SRCDIR/$dspName" |
| 125 | |
| 126 | # compile faust to c++ |
| 127 | cpscip-shell system . . cp. -r $FAUSTARCH/vcvrack/template/* "$SRCDIR/$dspName/" |
| 128 | faust -json -os -cn $dspName -scn rack_dsp -i -a $ARCHFILE $OPTIONS $f -o "$SRCDIR/$dspName/src/FaustModule.cpp" || exit |
| 129 | |
| 130 | # add NVOICES flag |
| 131 | echoscip-shell system . . echo. "#define NVOICES $NVOICES" | catscip-shell system . . cat. - "$SRCDIR/$dspName/src/FaustModule.cpp" > temp && mvscip-shell system . . mv. temp "$SRCDIR/$dspName/src/FaustModule.cpp" |
| 132 | |
| 133 | # set DSP name and polyphonic mode in plugin.json |
| 134 | createPluginJSONscip-shell shell shell-project 0.0.0 createPluginJSON(). "$dspName" "$SRCDIR/$dspName/plugin.json" |
| 135 | |
| 136 | # soundfiles handling |
| 137 | if [scip-shell system . . `[`. $SOUNDFILE = true ]; then |
| 138 | # needed for SoundUI.h to compile |
| 139 | echoscip-shell system . . echo. "#define __VCVRACK__" | catscip-shell system . . cat. - "$SRCDIR/$dspName/src/FaustModule.cpp" > temp && mvscip-shell system . . mv. temp "$SRCDIR/$dspName/src/FaustModule.cpp" |
| 140 | echoscip-shell system . . echo. "#define SOUNDFILE" | catscip-shell system . . cat. - "$SRCDIR/$dspName/src/FaustModule.cpp" > temp && mvscip-shell system . . mv. temp "$SRCDIR/$dspName/src/FaustModule.cpp" |
| 141 | |
| 142 | echoscip-shell system . . echo. $p.json |
| 143 | # get all soundfiles from the JSON file |
| 144 | catscip-shell system . . cat. $p.json | awkscip-shell system . . awk. ' |
| 145 | BEGIN { FS=":"; SOFI=0; } |
| 146 | /"soundfile"/ { SOFI=1; } |
| 147 | /"url"/ { |
| 148 | if (SOFI) { |
| 149 | match($2, /"[^"]*/); |
| 150 | split(substr($2, RSTART+2, RLENGTH-3), res, ";"); |
| 151 | for (x in res) print substr(res[x], 2, length(res[x])-2); |
| 152 | SOFI=0; |
| 153 | } |
| 154 | } |
| 155 | ' > $p-tmp.txt |
| 156 | # copy found soundfiles in the final binary |
| 157 | for snd in $(catscip-shell system . . cat. $p-tmp.txt); do |
| 158 | if [scip-shell system . . `[`. -f $snd ]; then |
| 159 | if [scip-shell system . . `[`. ${snd:0:1} = "/scip-shell filesystem . . `/`." ]; then |
| 160 | echoscip-shell system . . echo. "Warning: soundfile with absolute path is not copied !" |
| 161 | else |
| 162 | # create destination path and possibly create directory |
| 163 | sfpath="$SRCDIR/$dspName/res/$(dirnamescip-shell system . . dirname. $snd)/" |
| 164 | if ! [scip-shell system . . `[`. -d $sfpath ]; then |
| 165 | echoscip-shell system . . echo. "Create $sfpath" |
| 166 | mkdirscip-shell system . . mkdir. -p $sfpath |
| 167 | fi |
| 168 | echoscip-shell system . . echo. "Copy $snd in ${f%.dsp}$EXT" |
| 169 | cpscip-shell system . . cp. $snd $sfpath |
| 170 | fi |
| 171 | else |
| 172 | echoscip-shell system . . echo. "Error: file $snd not found !" |
| 173 | fi |
| 174 | done |
| 175 | rmscip-shell system . . rm. $p-tmp.txt |
| 176 | fi |
| 177 | |
| 178 | # compile and install plugin or keep the source folder |
| 179 | if [scip-shell system . . `[`. $SOURCE == false ]; then |
| 180 | ( cd "$SRCDIR/$dspName" && makescip-shell system . . make. && makescip-shell system . . make. install ) > /dev/nullscip-shell filesystem . . `/dev/null`. || exit |
| 181 | rmscip-shell system . . rm. -rf "$SRCDIR/$dspName" |
| 182 | else |
| 183 | echoscip-shell system . . echo. "Create the $SRCDIR/$dspName folder" |
| 184 | fi |
| 185 | done |