How to clear audio buffers

I am working on a game that has game play music and also background music that plays on game over.   i.e., when the level ends, the game play music pauses and the background music resumes.   Conversely, when they start play again, game play music resumes and background music pauses.

The code toggles back and forth using audio.pause and resume on the respective channels.  However, I want to restart the game music from the beginning every time, so I preface audio.resume() with audio.seek(0, {channel=gameMusicChannel}) when resuming the game music.

That works, kind of, but I still hear a fraction of a second of the game music from where it left off at the last pause, before it seeks back to 0 and starts again from the beginning.   

Is that buffered music from before audio.pause()?   If so, how to avoid/clear this?

Are you using looped MP3’s?  I know they are a little “sloppy” and can have sound artifacts especially at the beginning or end of a loop - maybe pausing a resuming artifacts could exist as well but I’ve never had that problem.

Maybe show us some code in case it’s an order of operations thing.

Thanks for the quick reply.  Yes, looped mp3’s, but they’re clean.   They loop perfectly.   Below are some code snippets. 

I load them like this:
 

gameSong = audio.loadStream("game.mp3") backSong = audio.loadStream("back.mp3")

Init game:

audio.play(backSong, {loops=-1, channel=BACKGROUND\_MUSIC\_CHANNEL}) audio.play(playSong, {loops=-1, channel=GAMEPLAY\_MUSIC\_CHANNEL})

Just after calling play for both, I continue only with background music:

audio.pause(GAMEPLAY\_MUSIC\_CHANNEL) audio.resume(BACKGROUND\_MUSIC\_CHANNEL)

To play game music:

audio.pause(BACKGROUND\_MUSIC\_CHANNEL) audio.seek(0, {channel=GAMEPLAY\_MUSIC\_CHANNEL}) audio.resume(GAMEPLAY\_MUSIC\_CHANNEL)

And vice versa to play background.   Doesn’t matter when I call seek, when audio.resume is called for the game play channel, there’s a 10th of a second of buffered sound before it starts over.

Can you do an experiment and see if you change to audio.loadSound instead of audio.loadStream if that fixes it?

Also, can you try using audio.rewind instead of audio.seek just in case that makes a difference?

[lua]

audio.rewind( {channel=GAMEPLAY_MUSIC_CHANNEL} )

[/lua]

Already tried rewind, to no avail.   But…

loadSound worked!   

Thanks a lot, friend!

You’re welcome, just watch out for potential loading delays on older devices.  

Are you using looped MP3’s?  I know they are a little “sloppy” and can have sound artifacts especially at the beginning or end of a loop - maybe pausing a resuming artifacts could exist as well but I’ve never had that problem.

Maybe show us some code in case it’s an order of operations thing.

Thanks for the quick reply.  Yes, looped mp3’s, but they’re clean.   They loop perfectly.   Below are some code snippets. 

I load them like this:
 

gameSong = audio.loadStream("game.mp3") backSong = audio.loadStream("back.mp3")

Init game:

audio.play(backSong, {loops=-1, channel=BACKGROUND\_MUSIC\_CHANNEL}) audio.play(playSong, {loops=-1, channel=GAMEPLAY\_MUSIC\_CHANNEL})

Just after calling play for both, I continue only with background music:

audio.pause(GAMEPLAY\_MUSIC\_CHANNEL) audio.resume(BACKGROUND\_MUSIC\_CHANNEL)

To play game music:

audio.pause(BACKGROUND\_MUSIC\_CHANNEL) audio.seek(0, {channel=GAMEPLAY\_MUSIC\_CHANNEL}) audio.resume(GAMEPLAY\_MUSIC\_CHANNEL)

And vice versa to play background.   Doesn’t matter when I call seek, when audio.resume is called for the game play channel, there’s a 10th of a second of buffered sound before it starts over.

Can you do an experiment and see if you change to audio.loadSound instead of audio.loadStream if that fixes it?

Also, can you try using audio.rewind instead of audio.seek just in case that makes a difference?

[lua]

audio.rewind( {channel=GAMEPLAY_MUSIC_CHANNEL} )

[/lua]

Already tried rewind, to no avail.   But…

loadSound worked!   

Thanks a lot, friend!

You’re welcome, just watch out for potential loading delays on older devices.