igorski / MWEngine

Audio engine and DSP library for Android, written in C++ providing low latency performance within a musical context, while providing a Java/Kotlin API. Supports both OpenSL and AAudio.
MIT License
259 stars 45 forks source link

Sequencer accidentally stopped after engine stop and start again #95

Closed hypeastrum closed 5 years ago

hypeastrum commented 5 years ago

https://github.com/igorski/MWEngine/blob/342f8f4a529e24b03b1466d5a6256897c1fd7567/src/main/cpp/sequencercontroller.cpp#L46

If stop the engine and dispose it in java (for example on activity stop), destructor of SequencerController will be called later with GC. So if we stop the activity and the open it again, next scenario became possible:

  1. Run audio engine
  2. Stop activity, stop and dispose audio engine
  3. Resume activity, init and start audio engine again
  4. Play sound
  5. SequencerController destructor called (for old object), but it affects global audio engine namespace and sound will be stopped
igorski commented 5 years ago

Hi Andrey,

thanks for pointing this out. I have updated the destructor to not invoke any actions in the global namespace. Especially as disposal of the audio engine will have triggered this exact action indirectly by halting the rendering thread.

It highlights an interesting issue though: the controller being a class instance that sets flag in the static audio engine namespace. Whereas it would be interesting to run multiple sequencers at the same time, the purpose of the sequencer controller is to act as a single transport control (e.g. singleton only). The only real reason that this is a class rather than a namespace with static functions, is to allow easier exposure of its methods through JNI to Java (this is sadly not possible unless making these pure C...). The MWEngine.java class maintains a unique SequencerController through the MWEngine singleton instance not making this a problem in Java (after the fix mentioned above), leaves a bit of a curious design decision for those using C++ solely though...