grame-cncm / faust

Functional programming language for signal processing and sound synthesis
http://faust.grame.fr
Other
2.58k stars 322 forks source link

-vec -vs ... produces broken code with effect chain #251

Closed giuliomoro closed 5 years ago

giuliomoro commented 5 years ago

I ran a modified faust2bela script that uses vectorization, as: faust2bela -midi -gui -tobela -nvoices 1 -effect effects.dsp ./vfl.dsp

the actual calls to the faust compiler are below:

faust -vec -vs 16 -i -a /Users/giulio/faust/architecture/bela.cpp vfl.dsp -o vfl/tmp/rendertmp.cpp
faust -vec -vs 16 -i -cn effect -a minimal-effect.cpp effects.dsp -o vfl/effect.h

the vfl.dsp file is simply physicalModeling/faust-stk/flute.dsp, and the effects.dsp file contains:

import("stdfaust.lib");
import("layout2.dsp");

process = _,_ : +
    : component("echo.dsp")
    : component("flanger.dsp")
    : component("chorus.dsp")
    : component("freeverb.dsp");

I have mixed results: sometimes the result works fine, sometimes I get a quiet squeal, sometimes I get a louder squeal when pressing a key, sometimes I just hear stuttering noise when pressing the key and then I hear the effects when releasing it,. I guess there may be some uninitialized memory somewhere? What would help you troubleshoot this?

sletz commented 5 years ago

Hum... this should obviously not happen. Are the effects from the SAM folder? Or better just send me the exact DSP code you are using.

sletz commented 5 years ago

Can you possibly test the exact same code and configuration outside of the bela? (that is on you laptop, with something like faust2caqt..etc)

giuliomoro commented 5 years ago

dsps.zip here are the files

giuliomoro commented 5 years ago
zip dsps.zip "/usr/local/share/faust/instruments.lib" "/usr/local/share/faust/envelopes.lib" "/usr/local/share/faust/basics.lib" "/usr/local/share/faust/maths.lib" "/usr/local/share/faust/signals.lib" "/usr/local/share/faust/noises.lib" "/usr/local/share/faust/oscillators.lib" "/usr/local/share/faust/delays.lib" "/usr/local/share/faust/filters.lib" "/usr/local/share/faust/reverbs.lib" "/usr/local/share/faust/routes.lib" "/Users/giulio/faust/examples/SAM/virtualAnalog/layout2.dsp" "/usr/local/share/faust/stdfaust.lib" "/Users/giulio/faust/examples/SAM/effects/echo.dsp" "/Users/giulio/faust/examples/SAM/effects/flanger.dsp" "/usr/local/share/faust/signals.lib" "/usr/local/share/faust/basics.lib" "/usr/local/share/faust/maths.lib" "/usr/local/share/faust/delays.lib" "/usr/local/share/faust/oscillators.lib" "/usr/local/share/faust/filters.lib" ~/bela.wiki/vfl.dsp ../effects/effects.dsp
sletz commented 5 years ago

I tried the code here on my MacBook with scalar (= default) then-vec -vs 16with faust2caqt -vec -vs 16 -midi -nvoices 1 -effect effects.dsp flute.dsp

No difference. The sound is not so clean when hitting a second note while a first one is playing (since -nvoices 1 , the second note "steals" the first playing one). Could it be a CPU issue ? Coud you possible make a video shown the problem?

giuliomoro commented 5 years ago

can you try to build it a few times? I have random behaviour, also when building on the mac with:

 faust2caqt -midi -httpd -nvoices 1 -effect effects.dsp ~/workspace/bela.wiki/vfl.dsp

and hacking it to have -vec -vs 16 .

sletz commented 5 years ago

Trying to reproduce here... could be a corner case of this specific DSP. Can you possibly true some others, like from physicalModeling/ folder ?

giuliomoro commented 5 years ago

I tried with the SAM/virtualAnalog one (actually that's how I found out about it).

giuliomoro commented 5 years ago

steps to reproduce: apply this

diff --git a/tools/faust2appls/faust2caqt b/tools/faust2appls/faust2caqt
index d83f06c96..26ec131e9 100755
--- a/tools/faust2appls/faust2caqt
+++ b/tools/faust2appls/faust2caqt
@@ -161,15 +161,15 @@ for p in $FILES; do
             adaptor(F,G) = adapt(outputs(F),inputs(G));
             process = adaptor(library("$SRCDIR/$f").process, library("$SRCDIR/$f").effect) : library("$SRCDIR/$f").effect;
 EndOfCode
-            faust -i -json -a $ARCHFILE $OPTIONS "$SRCDIR/$f" -o "$TMP/${f%.dsp}_tmp.cpp" || exit
-            faust -i -cn effect -a minimal-effect.cpp "$TMP/effect.dsp" -o "$TMP/effect.h" || exit
+            faust $FAUST_ARGS -i -json -a $ARCHFILE $OPTIONS "$SRCDIR/$f" -o "$TMP/${f%.dsp}_tmp.cpp" || exit
+            faust $FAUST_ARGS -i -cn effect -a minimal-effect.cpp "$TMP/effect.dsp" -o "$TMP/effect.h" || exit
             rm "$TMP/effect.dsp"
         else
-            faust -i -json -a $ARCHFILE $OPTIONS "$SRCDIR/$f" -o "$TMP/${f%.dsp}_tmp.cpp" || exit
-            faust -i -cn effect -a minimal-effect.cpp "$SRCDIR/$EFFECT" -o "$TMP/effect.h" || exit
+            faust $FAUST_ARGS -i -json -a $ARCHFILE $OPTIONS "$SRCDIR/$f" -o "$TMP/${f%.dsp}_tmp.cpp" || exit
+            faust $FAUST_ARGS -i -cn effect -a minimal-effect.cpp "$SRCDIR/$EFFECT" -o "$TMP/effect.h" || exit
         fi
     else
-        faust -i -json -a $ARCHFILE $OPTIONS "$SRCDIR/$f" -o "$TMP/${f%.dsp}_tmp.cpp" || exit
+        faust $FAUST_ARGS -i -json -a $ARCHFILE $OPTIONS "$SRCDIR/$f" -o "$TMP/${f%.dsp}_tmp.cpp" || exit
     fi

     # add preset management

run the following script to build 20 copies of an instrument containing physicalModeling/guitarMIDI.dsp and smartKeyboard/associatedEffects/elecGuitarEffect.dsp:

 bash -exc 'for a in {1..20}; do rm -rf /tmp/tmp$a; mkdir -p /tmp/tmp$a ; cd /tmp/tmp$a; cp ~/faust/examples/physicalModeling/guitarMIDI.dsp ~/faust/examples/smartKeyboard/associatedEffects/elecGuitarEffect.dsp ./; FAUST_ARGS="-vec -vs 16" faust2caqt -midi -nvoices 1 -effect elecGuitarEffect.dsp guitarMIDI.dsp; cd ..; done'

Here are the projects that were generated for me: guitarMIDIs.zip If I listen to each one in turn. In my case, these have distortion artefacts, the others don't:

tmp11/guitarMIDI.app
tmp12/guitarMIDI.app
tmp15/guitarMIDI.app
tmp17/guitarMIDI.app
tmp3/guitarMIDI.app

(it's also possible that just running the same binary multiple times yields different effects, although I tried a few times)

Then I tried the same as above but without the effect. Here is the result: vecnoeffect.zip

If I listen to each in turn, these have a click at the beginning of each attack, that the others don't have:

tmp12/guitarMIDI.app
tmp14/guitarMIDI.app
tmp15/guitarMIDI.app
tmp18/guitarMIDI.app

Building 20 projects without vectorizing works fine, both with and without effect.

sletz commented 5 years ago

OK, I've have a look. Note that faust2caqt (and other faust2xx scripts) can take additional Faust compiler parameters (those are going in the $OPTIONS variable), so the $FAUST_ARGS stuff should not be necessary.

giuliomoro commented 5 years ago

oh ok, I checked with -help but there was nothing there. I will keep that in mind.

sletz commented 5 years ago

Right, we should add that point on all faust2xx -h part yes...

sletz commented 5 years ago

1) I cannot test any of your binaries: all of them crash with the following (strange) message:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   C4DM.guitarMIDI                 0x000000010899ceb0 JSONUIDecoder::buildUserInterface(UI*) + 2064
1   C4DM.guitarMIDI                 0x000000010899389f dsp_voice_group::init() + 79
2   C4DM.guitarMIDI                 0x0000000108993799 mydsp_poly::mydsp_poly(dsp*, int, bool, bool) + 473
3   C4DM.guitarMIDI                 0x000000010897ebf2 main + 834
4   libdyld.dylib                   0x00007fff896dd5ad start + 1

2) I tried your test here with guitarMIDI.dspand elecGuitarEffect.dsp and faust2caqt. I cannot reproduce the problem. I tested also with faust2jack (with -effect added here https://github.com/grame-cncm/faust/commit/abe234fc9381418ada226079a72bf28e67720d73). Stil don't hear the problem. Can you possibly test with faust2jack ?

3) so still investigating....

giuliomoro commented 5 years ago

So I gave it another try, this time saving the temporary .cpp and .h files (attached below). Summary:

Applied this patch to keep temporary files

diff --git a/Users/giulio/faust/tools/faust2appls/faust2caqt b/usr/local/bin/faust2caqt
index bbe05a9..55412ab 100755
--- a/Users/giulio/faust/tools/faust2appls/faust2caqt
+++ b/usr/local/bin/faust2caqt
@@ -67,7 +67,6 @@ do
     elif [ "$p" = -icc ]; then
         ignore=" "
     elif [ $p = "-nvoices" ]; then
-        POLYDEFS="DEFINES += POLY2"
         shift
         NVOICES=$1
         if [ $NVOICES -ge 0 ]; then
@@ -240,7 +239,7 @@ EndOfCode
     rm $p.json

     cp -r "$TMP/${f%.dsp}$EXT" "$SRCDIR/${f%.dsp}$EXT"
-    RMTMP=true
+    RMTMP=false
     if $DEBUG;
     then
            echo "====> delete temporary folder $TDR ? [Y/n]"

Using this build script (with parallel builds!):

#!/bin/bash -ex
export PATH=/opt/local/libexec/qt5/bin:$PATH

faust_path=~/faust
num_builds=20
srcs="$faust_path/examples/physicalModeling/guitarMIDI.dsp $faust_path/examples/smartKeyboard/associatedEffects/elecGuitarEffect.dsp"
base_dsp=guitarMIDI.dsp
base_opts="-midi -nvoices 1 "
faust_command=faust2caqt
jobs_max=3

function build ()
{
    rm -rf $base_folder
    for a in $(seq 1 $num_builds); do
        set -x
        dir=$base_folder/build$a
        mkdir -p $dir
        cd $dir
        cp $srcs $dir/
        $faust_command $base_opts $opts $base_dsp &
        while [ `jobs | wc -l` -gt $(($jobs_max - 1)) ]; do
            set +x
            sleep 0.5
        done
        cd ..
    done
    set -x
    printf "Wait for jobs to complete..."
    while [ `jobs | wc -l` -ne 0 ]; do
        jobs
        sleep 0.5;
    done
    echo done
    zip -r $base_folder.zip $base_folder
}

opts="-vec -vs 16 -effect elecGuitarEffect.dsp"
base_folder=/tmp/veceffect
build

opts="-vec -vs 16"
base_folder=/tmp/vecnoeffect
build

opts="-effect elecGuitarEffect.dsp"
base_folder=/tmp/noveceffect
build

Outcomes: -vec -vs 16 -effect: veceffect.zip The below have a much noisier attack than the others: veceffect/build13 THIS veceffect/build18 THIS veceffect/build9 THIS

-vec -vs 16: (no effect) vecnoeffect.zip The below have a much noisier attack than the others:

vecnoeffect/build10 THIS
vecnoeffect/build13 THIS
vecnoeffect/build14 THIS
vecnoeffect/build16 THIS
vecnoeffect/build17 THIS
vecnoeffect/build8 THIS

Vectorize and no effect: noveceffect.zip

sletz commented 5 years ago

Thanks; Still cannot test the binary (SIGILL execution , so non compatible CPU I guess).

Nothing has changed in the generated C++ code since yesterday.

I've now try to integrate those tests in our "impulse-test" that test DSP produced samples with a given reference. This will possibly help understand if the problem is related to C++ generated code, or control parameter handling (like incorrect restoration...)

giuliomoro commented 5 years ago

Can you try to rebuild the .cpp files from the archive?

New finding: the noisy attack I get does not happen when using the GUI's "gate", but only when using a MIDI controller to trigger it.

sletz commented 5 years ago

Aha... so you get noisy attack with MIDI controller mode only and -vec compilation mode yes ?

giuliomoro commented 5 years ago

yes that seems to be the case now.

sletz commented 5 years ago

Several things:

1) I could not reproduce the initial problem with our automatic tests ))-:

2) But I did some reworking and simplifications in the polyphonic handling code in https://github.com/grame-cncm/faust/commit/9692a58add0f4236753822345e1e8233a2eb482a that may help

3) Now I am reworking the automatic tests themselves to test more things. Up to now I could not detect any problem with the new polyphonic model.

4) I would be interested if you can test on your side.

giuliomoro commented 5 years ago

yeah it seems that I cannot detect it any more now ... This is very weird, because when I opened the issue (c2478b2) I was getting horrible squeals and thunderous sounds with both the flute and the virtualAnalog examples and some weird distortion with the guitarMIDI one. Both on Bela and on caqt. Then the next day ( 375ff40 ) I could only get that noisy attack on guitarMIDI and now (9692a58 ) nothing ... I also tried to roll back to c2478b2, but I get none of the obvious issues I had there ... but I still have some of the generated C++ code from when I started having issues, if that helps?

sletz commented 5 years ago

Nothing has changed at the code generation level between c2478b2 and 9692a58 . The thing that I've changed is part of the architecture files (so outside the generated code itself...)