Is it possible to play two parts of a music track, one after another without delay?

hi, i was wondering if it’s possible to play two parts of a music track, one after another without delay? what i’m trying to do is, to play the first half of the music then get to the second half and loop it, but the transition needs to be seamless. tried lots of things; different formats of audio, onComplete or basic timer and others but no reliable solution, any ideas?

Hi @RustySly,

This kind of perfect synching will be tricky, and may not be possible.

Have you tried this?

  1. Play the entire track with no looping.

  2. On complete, call a function which uses “audio.seek()” to jump to the mid-point in the track.

  3. On complete, just do the same thing again and again, thus forcing a “loop” of sorts.

Best regards,

Brent

brent,

i tried your suggestion and had mixed results. sometimes parts connect just fine, and mostly there’s a tiny but disturbing gap. thanks for the reply, i’ll keep working on it :slight_smile:

Hi Rusty,

Can you break the wav file up into two different files, then use the oncomplete to play the second file?

What format is the audio in?  My understanding is only .wav files can be done in a seamless fashion.  The other audio formats will have some gap.

Rob

will try .wav and share the results, thanks.

a small update… tried everything; different file formats, onComplete or audio.seek, a little transition sound between parts etc… it seems that the best solution is looping single file with audio.seek. it’s a puzzle game and each game lasts about 9 - 10 mins. and the music is 10 mins. long. if player is good enough to play longer, the loop will happen. thanks again :slight_smile:

Why do you only want to loop the 2nd half?   

the music starts slowly and speeds up as the game gets harder. when player reaches the top level (like tetris) the fast part of the music will loop.

I wonder if creating two files would work.  File 1 is your complete length of the music.  File 2 is from faster mid-point to end or just isolate 16 bars of the faster part.  Then when player reaches top level, file 2 plays and just loops.

Would there still be a noticeable gap going from file 1 to 2?

Brent already said it, that type of syncing is HARD, particularly on mobile devices, particularly when isolated from the raw native API’s as with Corona.

The problem is, by the time the song ends, and the event propagates to your listener, it’s far too late to start another song (or same song at new seek point) without interruption – guaranteed lag of at least the length of the now-empty sound buffer in the BEST-case scenario.

What you really need is a low-level event like “audio system just filled the last buffer, so audio WILL BE ending in (buffer_size/play_frequency) seconds, so prepare to start feeding it new data from an alternate source to pre-fill the next buffer before this one runs out if you want uninterrupted audio.”

But no way you’re getting THAT event! :smiley:

What you could do, perhaps?, is have say 5 1-minute clips of varying tempos, that individually loop cleanly. Reserve two music channels, and open the first two streams, immediately pausing the faster one. (so the faster one is pre-buffered, ready to play on a moment’s notice) First one just keeps looping, until some later point (under your control) you fade it out simultaneously starting the second (faster) channel. You also then pre-load the NEXT faster song back into the first channel, ping-pong style, in preparation of doing it all again, until the last (fastest) clip which just loops forever.

fwiw,hth

Hi @RustySly,

This kind of perfect synching will be tricky, and may not be possible.

Have you tried this?

  1. Play the entire track with no looping.

  2. On complete, call a function which uses “audio.seek()” to jump to the mid-point in the track.

  3. On complete, just do the same thing again and again, thus forcing a “loop” of sorts.

Best regards,

Brent

brent,

i tried your suggestion and had mixed results. sometimes parts connect just fine, and mostly there’s a tiny but disturbing gap. thanks for the reply, i’ll keep working on it :slight_smile:

Hi Rusty,

Can you break the wav file up into two different files, then use the oncomplete to play the second file?

What format is the audio in?  My understanding is only .wav files can be done in a seamless fashion.  The other audio formats will have some gap.

Rob

will try .wav and share the results, thanks.

a small update… tried everything; different file formats, onComplete or audio.seek, a little transition sound between parts etc… it seems that the best solution is looping single file with audio.seek. it’s a puzzle game and each game lasts about 9 - 10 mins. and the music is 10 mins. long. if player is good enough to play longer, the loop will happen. thanks again :slight_smile:

Why do you only want to loop the 2nd half?   

the music starts slowly and speeds up as the game gets harder. when player reaches the top level (like tetris) the fast part of the music will loop.

I wonder if creating two files would work.  File 1 is your complete length of the music.  File 2 is from faster mid-point to end or just isolate 16 bars of the faster part.  Then when player reaches top level, file 2 plays and just loops.

Would there still be a noticeable gap going from file 1 to 2?