alnitak / flutter_soloud

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

fix: audio speed discrepancy #111

Closed Neimerai closed 3 weeks ago

Neimerai commented 4 months ago

Description The audio files for some reason play faster than their actual speed. This speed discrepancy causes a very small noticeable drift between audio and visual elements in my app. The issue persists across multiple audio files, all of which play correctly in other media players. For example, when starting the audio in my app at the same time as the same file in my editor, it slowly but surely outpaces it.

I use the songs position to track the tempo of the song that I am currently playing so that I can track every beat of the song. This beat works completely fine, lining up with song audio that does not have any speed discrepancy. For example, when I play the song audio in the app side by side with the original audio file in the editor, the beat tracker matches the original song file and not the song when playing in my app. Don't really understand what is going on behind this, why the song position remains the same but the actual song audio plays faster.

Steps To Reproduce Set up a page that plays a given song and allows you to pause and unpause(ideally a song with noticeable beats and that is over 2 minutes so that you can start to hear the difference in speed). After playing the song, use the pause buttons on both the app page as well as the editor to sync up the music with each other.(tap the pause button twice quickly in succession for whichever audio is further ahead to delay it by just a little until it is matched up). After two or so minutes since the audios have been synced up, you will be able to hear the audio playing in the app be noticeably ahead than the one playing in the editor.

Expected Behavior I expect the audio to play at the exact same speed as the original audio especially when considering the fact that getting the current position of the audio will reflect the position of the original audio and not the one playing.

alnitak commented 4 months ago

Hi @Neimerai, I tried with a 102 BPM music of about 4 minutes doing the play/pause trick to synchronize the player and the Flutter app somehow. I tried on Linux and I didn't notice what you are experiencing. My best hypothesis is something related to the sample rate of the audio file. The plugin uses by default 44100KHz sample rate with 2 channels (not yet on pub.dev, but in the main branch with the init() method you can choose those parameters) so if you have an audio with another sample rate, internally SoLoud resample it to 44100. So I tried to resample my audio and I tried with that, but still no gaps.

Please can you tell me the sample rate, channels, and format of your audio file? Also on which platform did you notice this?

alnitak commented 3 weeks ago

Feel free to reopen if the problem still exists.