gogins / csound-extended

Extensions for Csound including algorithmic composition, Android app, and WebAssembly.
GNU Lesser General Public License v2.1
40 stars 1 forks source link

The Csound for Android app appears to load, but does not play, SoundFonts. #100

Closed gogins closed 5 years ago

gogins commented 5 years ago

I have created a .csd file to play Astor Piazzola's "Oblivion" arranged for the default sf_GMbank.sf2 SoundFont. This runs without any problems on Linux.

All necessary files are in my public data directory /storage/emulated/0/Music. The app appears to load the SoundFont as reflect in the logcat trace, but then it immediately crashes without providing additional information.

I will have to debug the native code in Android Studio to find out what is actually happening.

gogins commented 5 years ago

Logcat shows:

2019-08-11 15:44:02.770 511-511/com.csounds.Csound6 D/CsoundOboe:: Created fluidEngine 0x0x71f5176b80 with sampling rate = 48000.000000, chorus off, reverb off, channels 256, voices 4096.
2019-08-11 15:44:02.771 511-511/com.csounds.Csound6 I/Csound:: Created fluidEngine 0x0x71f5176b80 with sampling rate = 48000.000000, chorus off, reverb off, channels 256, voices 4096.
2019-08-11 15:44:02.771 511-511/com.csounds.Csound6 D/CsoundOboe:: Loading SoundFont : /storage/emulated/0/Music/sf_GMbank.sf2.
2019-08-11 15:44:02.772 511-511/com.csounds.Csound6 I/Csound:: Loading SoundFont : /storage/emulated/0/Music/sf_GMbank.sf2.
2019-08-11 15:44:02.772 511-511/com.csounds.Csound6 W/csounds.Csound: 0xebadde09 skipped times: 0
2019-08-11 15:44:02.785 511-935/com.csounds.Csound6 W/csounds.Csound: 0xebadde09 skipped times: 0
2019-08-11 15:44:02.825 3242-3432/? W/InputDispatcher: channel '9274ca6 com.csounds.Csound6/com.csounds.Csound6.CsoundAppActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9, fd=429
2019-08-11 15:44:02.825 3242-3432/? E/InputDispatcher: channel '9274ca6 com.csounds.Csound6/com.csounds.Csound6.CsoundAppActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-08-11 15:44:02.828 3242-5853/? I/WindowManager: WIN DEATH: Window{9274ca6 u0 com.csounds.Csound6/com.csounds.Csound6.CsoundAppActivity}
gogins commented 5 years ago

0xebadde09 appears to be related to this issue: 0xebadde09, "leak references with garbage". Some were able to work around this by disabling hardware acceleration in the WebView by configuring the Android manifest: https://github.com/react-native-community/react-native-webview/issues/575.

I suppose that the cryptic error message means that when the garbage is collected, some references are treated as dead but are not. The referenced object is freed, and then dereferenced, which causes a crash. I will review my code for the FluidSynth opcodes.

gogins commented 5 years ago

The crash occurs in this code:


    filepath = csound->FindInputFile(csound, filename, "SFDIR;SSDIR");
    if (filepath && fluid_is_soundfont(filepath)) {
      log(csound, "Loading SoundFont: %s.\n", filepath);
      soundFontId = fluid_synth_sfload(fluidSynth, filepath, 0);
      log(csound, "fluidSynth: 0x%p  soundFontId: %d.\n", fluidSynth,
          soundFontId);
    }
``
on the `fluid_synth_sfload` call. The pathname appears to be valid and is in the public data directory.
gogins commented 5 years ago

There is a question whether casting a MYFLT value to a pointer works in this context. I will change the casting system.

gogins commented 5 years ago

I have fixed the bug by storing thefluid_synth_t pointer in a table instead of type casting it.

Please note, the problem was caused because the original code assumed that MYFLT would be size 32 bits on 32 bit CPU architecture, and 64 bits on 64 bit CPU architecture. But in the current build of Csound for Android, MYFLT is size 32 but the CPU architecture is 64 bits. Therefore, the type cast truncated the pointer from 64 bits to 32 bits, which of course would never work.