dimitris-c / AudioStreaming

An AudioPlayer/Streaming library for iOS written in Swift using AVAudioEngine.
MIT License
266 stars 56 forks source link

[Help] Gapless Playback #38

Closed BLeeEZ closed 2 years ago

BLeeEZ commented 2 years ago

Hi, I am currently evaluating different audio libraries to add gapless playback to my Ampache/Subsonic App: Amperfy I integrated AudioStream and I am able to play songs with it. I am still noticing a small gap between songs. The engine is used with default configuration. The first song is played via player.play(url: url). 10 seconds before the currently playing song is finished I queue the next song via player.queue(url: url). Do I need to adjust/configure something get a true gapless playback?

dimitris-c commented 2 years ago

Hey, thanks for raising this, is there a demo server for Amperfy that I can use to try and investigate further?

BLeeEZ commented 2 years ago

Thank you for your offered support. I have contacted you via your webpage so I can provide you login credentials for my demo server.

After you signed in with the Amperfy App you can navigate to playlist. There I prepared the Playlist call "Gapless". There are two songs in it "Phantom Part 1" and "Phantom Part 2". They were originally one song which I cut in two pieces. When the player view is open, you can tab on the icon in the upper left corner to show the currently queued songs for easier navigation.

BLeeEZ commented 2 years ago

I recorded the output and as you can see in the screenshot there is a small but notable gap (18 ms).

Bildschirmfoto 2022-05-05 um 09 26 55

dimitris-c commented 2 years ago

After some investigation I'm not sure this can be improved... Although I added the mp3 files to the sample project and queued them and noticed a slightly less gap than your app.

I will have it as a TODO if I can come up with anything

And I do believe that mp3 encoding is somewhat non gapless, which I have converted the tracks to wav files and the was no gap audible.

Though there's another nasty bug that I found when seeking a wav file...

BLeeEZ commented 2 years ago

Thank you for your time and afford.

bbsixzz commented 2 years ago

The default Music app in iOS will play mp3 files truly gapless, at least until this happened:

iOS 15.4 breaks 'gapless' playback in Apple Music

Maybe related?

Seems to be fixed in 15.5 betas, maybe the underlying framework is causing the gap in AudioStreaming too?

We need someone to test the beta.

dimitris-c commented 2 years ago

I don't think this is related to that bug After researching a bit there maybe a way to remove junk from mp3 files but that would require actually knowing when and if it's needed to remove that data... more info here https://en.wikipedia.org/wiki/Gapless_playback and technical info here https://lame.sourceforge.io/tech-FAQ.txt

bbsixzz commented 2 years ago

Alright, other than that all I can add is that iSub has working true gapless playback but it's a homegrown solution. So there must be a way to do it.

dimitris-c commented 2 years ago

iSub uses BASS player by unseen https://www.un4seen.com under the hood - so it's quite different than AudioStreaming approach, which uses AudioFileStream from Core Audio to play audio content.

I'm sure that BASS player uses their own decoding/encoding for supporting various audio types - since it's not open source is hard to tell their approach

dimitris-c commented 2 years ago

Closing this, mp3 are not gapless by default and should have extra handling to support this.