rorywalsh / CsoundUnity

GNU Lesser General Public License v2.1
70 stars 19 forks source link

Crashing when exiting play mode #22

Closed Despair-Bear closed 3 years ago

Despair-Bear commented 3 years ago

Greetings. So I'm running into an occasional issue when exiting play mode where the audio being outputted by csounds will "stutter" (repeat the last probably 0.5 a second, similar to CD skipping essentially) when exiting play mode. I'm assuming this is due to something not being cleaned up properly. My initial thought was to edit CsoundUnity and to add a SetChannel call to trigger an instrument that looks like so:

instr DISABLE_ALL_INSTRUMENTS
    prints "DISABLE ALL INSTRUMENTS TRIGGER ENABLED"
    kDisableAll chnget "TURNOFFALL"
    if changed(kDisableAll)==1 then
        kindx = 1
        while kindx < 7 do
            turnoff2    kindx, 0, 0
            kindx += 1
        od
    endif
endin

and this is the call being made from the Unity side:

private void OnApplicationQuit()
  {
        if (LoggingCoroutine != null)
            StopCoroutine(LoggingCoroutine);

        if (csound != null)
        {
            csound.SetChannel("TURNOFFALL", 1.0);
            csound.OnApplicationQuit();
        }
  }

There are 6 instruments total (named 1-6) so it calls turnoff2 on each of them. This didn't seem to resolve the issue but I'm guessing I may need a way to verify that each instrument has actually been disabled on the Unity side before executing the csound.OnApplicationQuit() line? I'm dynamically creating these instruments and passing them their desired output channels (which route to run another) at init/runtime so I'm guessing that something that's expected to exist is being cleared out mid processing (as I think this would explain the audio stutter/skipping on exit).

Any thoughts/insights you have would be greatly appreciated. Thanks so much.

giovannibedetti commented 3 years ago

We already had a crash on exit issue, but it was fixed recently (have a look here), it was caused by some missing fields in a wrapped struct. If you're facing this issue maybe there are other structs around with missing/wrong fields. I'll try to have a look in the weekend. Btw the issue you're facing could be indeed caused by the Csound code itself. Could you please post the relevant portions of your csd?

Despair-Bear commented 3 years ago

I just emailed you the .csd file directly along with some initialization instructions for the Unity side. The relevant portions are instruments 2, 3 and 5. None of the others are being called/instantiated in my current testing. I also tested just initializing the filter (i5) connection to the output (i3) and also receive crashes from that despite the output level being 0.

EDIT: I emailed you again, same thread, with an even more simple setup that just uses instrument 1 which is just a generic oscillator outputting the same way as in the example scenes provided. For testing I also removed the call to initialize the DISABLE_ALL_INSTRUMENTS instrument initialization just to ensure that it wasn't anything directly pertaining to my code. Essentially in that test case the only thing being created/run is instrument 1 which is the basic oscillator outputting at a static frequency. It seems to happen less often but I am averaging a minimum of 1 crash every 10 play/exit cycles.

SECOND EDIT: I'm not sure if I'm crazy but it seems like the issue is more prone at certain points in an oscillators cycle. When modulating a filter for instance, I noticed that if I'm modulating cutoff with a square wave, it seems far more likely to crash if I click to exit right below the square wave shifts back down from its on position. Again this is just speculation, I don't know enough about the inner workings of csounds or how it's interfacing with Unity to really say, but I'm wondering if even on the straight oscillator output that it happens to crash when I get "lucky" and the wave is at a similar point in its cycle as previous crashes.

giovannibedetti commented 3 years ago

I'd try 3 things:

  1. uncheck the Log Csound Messages toggle
  2. test your Csd on a Csound Editor, like Cabbage or CsoundQT, and see if the crash happens there too.
  3. do some tests with the simpler Csd (like the basic example in the Samples of the Package)

Let me know how it goes. Today I'm very busy, but I promise I'll have a look at your code tomorrow!

Despair-Bear commented 3 years ago

Okay so far I've tested by stripping out basically everything, in a clean scene I was using this code:

<Cabbage>
form caption("Test Crash Bug"), size(300, 200)
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -m0d
</CsOptions>
<CsInstruments>
sr  =   48000 
ksmps   =   32
nchnls  =   2
0dbfs   =   1 

instr 1
    irnd1 random 220, 400 
    a1 oscili 1.0, irnd1
    outs a1, a1
endin

</CsInstruments>
<CsScore>

i 1 0 [3600*12]

</CsScore>
</CsoundSynthesizer>

I still received the same crash in a clean scene. I wanted to verify that it wasn't something directly related to my csd code. I get the crash less frequently running this(less cpu usage in the scene, last one wasn't a lot but it was more than this clean test scene), but if I exit play mode at differing points enough times I still get a crash. I disabled logging for testing, everything else in the CsoundUnity inspector was left in its default state.

For reference I am running Unity 2019.4.9f1

I will test tomorrow on Cabbage or CsoundQT. I believe that this must have something to do with the communication between csound and Unity as I can't imagine a bug like this wouldn't have been caught out in the wild for csounds itself at this point (considering the test script is literally just a call to oscili) considering how much use that probably gets by comparison.

giovannibedetti commented 3 years ago

probably this was caused by OnAudioFilterRead being called during the OnApplicationQuit method (since they run on different threads). this commit should fix it

Despair-Bear commented 3 years ago

Thanks so much @giovannibedetti That seemed to do the trick, no crashes so far :D

giovannibedetti commented 3 years ago

Great! Thanks for your help with the investigation, you showed me the path ;)