alnitak / flutter_soloud

Flutter low-level audio plugin using SoLoud C++ library and FFI
MIT License
210 stars 21 forks source link

docs: Some questions about gapless looping audio. #102

Closed FluffyBunniesTasteTheBest closed 2 months ago

FluffyBunniesTasteTheBest commented 3 months ago

Description

What's the recommended approach for gapless looping ambient audio? What file format should be used? And is there anything else that needs to be considered?

Reason I'm asking is, right now, my app uses just_audio with m4a files. The m4a files have been processed to ensure their number of samples is a multiple of 1024, which makes them seamlessly loopable. But m4a file format is not supported by flutter_soloud, mp3 causes gaps when looping, and as far as I'm aware of, iOS doesn't support ogg, making me wonder how to accomplish seamlessly looping sounds.

Any advise is appreciated, Thank you.

Requirements

alnitak commented 3 months ago

When you load an audio file, SoLoud decompresses it to PCM format into memory AFAIK. So I have no clue about the mp3 you used. You can try with WAV or FLAC but I am not sure. Please let me know!

FluffyBunniesTasteTheBest commented 3 months ago

@alnitak thank you very much for the quick response! I just did a quick test and converted one of the perfectly looping m4a to mp3 using ffmpeg/lame and played it in a loop. The gap was clearly audible - which, TBH, was expected, as mp3 is notorious for it. WAV/FLAC are unfortunately not an option because of their significantly bigger file sizes.

About ogg: Does SoLoud come with its own .ogg decoder, or does it rely on the operating system to decompress them?

Again, thank you very much for your help!

alnitak commented 3 months ago

Does SoLoud come with its own .ogg decoder, or does it rely on the operating system to decompress them?

It has its own decoder, but, I don't remember well, it cannot read some non-standard ogg header. If I remember well, Xiph introduced something that is not fully compatible with what SoLoud uses. So no all ogg files are supported.

FluffyBunniesTasteTheBest commented 3 months ago

It has its own decoder, but, I don't remember well, it cannot read some non-standard ogg header. If I remember well, Xiph introduced something that is not fully compatible with what SoLoud uses. So no all ogg files are supported.

Good to know. Thanks for sharing this! I'll do some tests later this week/early next week and will get back with the results.

sbauly commented 3 months ago

For what it’s worth @FluffyBunniesTasteTheBest, I’ve been using .ogg files and haven’t experienced any issues with playback on iOS, they loop seamlessly on both of the Android and iPhone physical devices I’ve been testing on.

FluffyBunniesTasteTheBest commented 3 months ago

@sbauly Thank you very much for sharing this! This provides great hope...

A little background info:

So far, the audio issues arose only on Android: The only way to get background audio to loop seamlessly was by ensuring that the number of samples were a multiple of the audio files data chunk size (1024 for m4a). Otherwise both Audioplayers and just_audio had a clearly audible gap during playback on some devices. In general, just_audio, which is currently in use, performed better. Problem is, that on some devices, playback starts up to a few hundred milliseconds past the beginning, which is especially noticeable with very short audio clips. Clips with less than a second duration are, in some rare circumstances, even skipped completely. There were a lot of subtle issues in my app, with both, Audioplayers and just_audio and I hope flutter_soloud is finally fixing them for good...

alnitak commented 3 months ago

Problem is, that on some devices, playback starts up to a few hundred milliseconds past the beginning, which is especially noticeable with very short audio clips.

This sounds like the metronome issue #100 and #78. You should not have big problems if the sound starts after 40ms, but soon there will be an option to start the player with a custom buffer size which could be the problem for the latency delays.