Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using with Android via Oboe - threading question #49

Open
chriscoomber opened this issue Jun 29, 2020 · 5 comments
Open

Using with Android via Oboe - threading question #49

chriscoomber opened this issue Jun 29, 2020 · 5 comments

Comments

@chriscoomber
Copy link

Hi there,

I'm trying to use TinySoundFont to render sound to Android using https://github.com/google/oboe/.

I'm having trouble marrying these two bits of documentation:

Any ideas of what I can do? For what it's worth, running without mutexes seems to work fine, and adding mutexes adds only very slight audio glitches here and there, so maybe I can just not include mutexes, but that feels dangerous.

@schellingb
Copy link
Owner

Hi there!

If you have two threads, one calling tsf_render, and another one calling tsf_note_on without any concurrency control, you are in dangerous territory. Except!

While writing this response, I started writing a suggestion to modify the library so that a maximum number of possible voices could be pre-allocated to avoid the need to re-allocate during tsf_note_on. This lead me to commit a new function in bf57451 which I'd love if you could test out some more.

After tsf_load you can now call tsf_set_max_voices(tsf_instance, 64); to set a maximum number of voices and TSF will never try to allocate more and will not play more than that at the same time. If the number is too low (and depending on the complexity of the used SF2 file a single note could need many voices) TSF will skip playing voices.

If you want to try yourself to see the problem without concurrency control, you can remove the mutex and SDL_Delay from from example2.c. Then add a while (true) around the loop that does note_on. It will very immediately crash. Now if you add the new tsf_set_max_voices to it, all will be fine!

Keep in mind, that I think that there is a theoretical chance that ending notes would negatively influence a voice that is rendering at the same time. I tried to avoid the worst case whereas a voice that was supposed to end with note_off would continue to play because the rendering just happened to overwrite the segment change. I did this by just setting the segment twice inside tsf_voice_end when a maximum number of voices has been set. No idea if that is a good idea but that was all that I could come up with for now :-)

Hope this helps! And thanks for using the library and reporting this!
Cheers!

@chriscoomber
Copy link
Author

Awesome, thanks for that. That does appear to fix the issues I was seeing, and in fact I was indeed crashing occasionally when running without mutexes before, and it doesn't appear to be crashing any more.

I've set the max_voices in my app to 256, which seems like way overkill for the sorts of MIDI it will play, but it doesn't seem to use up much memory, and it plays perfectly.

I'll let you know if I get any stuck hanging notes (i.e. note_offs dropped), but so far it's been fine.

@romanbsd
Copy link

romanbsd commented Aug 2, 2020

Tried it, and looks like it works flawlessly. I also wrote a trivial stream implementation which can load the sound font from assets using AAssetManager.

@renetik
Copy link

renetik commented Jul 19, 2021

Can you share your oboe implementation, and load using AAssetManager please ?

@romanbsd
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants