How to prevent 17dTesting Error with clearing buffer from source: Invalid Operation

I get the message: 17dTesting Error with clearing buffer from source: Invalid Operation

What can I do to fix this?

show us all the code where you call audio.loadSound and audio.loadStream

Better yet, can you make a small demo the produces this error/message?  

Also you failed to tell us any details about your build environment, version of corona, where you’re running, etc.  Give us all the details you would normally give a in a bug report.

If you’re having a problem with just one or a few sounds, put them through audacity and re-export them.

The error appears with Corona build 3395 in the simulator.

The sounds are loaded in a module at game start like this:

sfx.click = audio.loadSound( "sfx/sound\_11\_normalClick.mp3" )

and later are called with

      freechannel=audio.findFreeChannel(4)       playingalso=audio.isChannelPlaying(freechannel)       if freechannel and freechannel\>=4 and playingalso==false then           audio.play( handle, {channel=freechannel} )           if volume then               audio.setVolume( volume, { channel = freechannel } )           end           return freechannel       end

with handle being the sound.

The strange thing is the sounds which are called are working during a level and then the error appears when a sound (which was working before fine) is called again right out of the blue. So it doesn’t look like it has to do with the sound file itself.

Hi @d.mach,

How often is called function with sound code you provide?

See audio.play() - 17dTesting Error with clearing buffer from source: Invalid Operation

freechannel variable always evaluate to true value since audio.findFreeChannel always returns number.

Note: Lua consider false and nil as false and anything else as true.

Read more:

ldurniat

The function is called very often when the action gets more and more in the game.

Thanks for the info!

I suggest using WAV files for sound effects, not MP3.

(MP3 files need to be decoded each time you play them and you have limited hardware for that on some devices. So, playing a lot of sounds that need to be decoded can be a choke point.)
 
( Sound experts.  If I have this wrong, please post back.  I understand the want to use MP3 files for their wonderful compactness, so If I do have this wrong I’d love to know. )
 
Sound Flooding
Also, you should really implement code to prevent sound flooding. Otherwise,

  1. The game will sound crazy or bad when the action gets too furious
  2. You will run out of free channels.

I prevent sound flooding, by maintaining a record with a minimum between-time assigned for some sounds. Then, if my code tries to play a sound twice and the second time is too soon, the sound is not played.

Ex:
name: boom
file: boom.wav
minTime: 100

Play boom @ 0 - OK
Play boom @ 150 - OK
Play boom @ 200 - Not played
Play boom @ 260 - OK

 
If you are acquiring free channels as shown for all sounds, then that may act as a gate and global sound flooding preventer, but you may find your most used sounds prevent more important sounds from playing.  If you do, then you can implement a more robust/complex sound flooding prevention scheme.
 
@ldurdiat is right, this is what you want:

 if( ( freechannel == 0 ) and (freechannel \>= 4 and playingalso == false) ) then

(I still do this sometimes too… I come from a background of many other languages including C and C++ which mostly treat 0 as ‘false’. Lua is goofy this way.)

Coding tip: You need to use more parentheses in your code. Don’t default to relying on order of operations.
 
You should logically group operations and key comparisons.  It will make your code better, safer, and more legible.
 
PS - I stand by the idea of recoding the sound files that are causing trouble.  Even if you re-code them as MP3, I find that sometimes sound files have an internal encoding problem and re-exporting them with Audacity solves the problem.
 
https://www.audacityteam.org/

Thanks for the detailed info! Much appreciated!
One question: Can the error and problem described cause some Runtime errors on a device like for example make the game stop or anything? In the simulator there only is this text output but the game still continues to run.

I don’t know for certain, but I think your game will continue running,

However, I also have to wonder if this will eventually cause some or all sounds to stop working.

show us all the code where you call audio.loadSound and audio.loadStream

Better yet, can you make a small demo the produces this error/message?  

Also you failed to tell us any details about your build environment, version of corona, where you’re running, etc.  Give us all the details you would normally give a in a bug report.

If you’re having a problem with just one or a few sounds, put them through audacity and re-export them.

The error appears with Corona build 3395 in the simulator.

The sounds are loaded in a module at game start like this:

sfx.click = audio.loadSound( "sfx/sound\_11\_normalClick.mp3" )

and later are called with

      freechannel=audio.findFreeChannel(4)       playingalso=audio.isChannelPlaying(freechannel)       if freechannel and freechannel\>=4 and playingalso==false then           audio.play( handle, {channel=freechannel} )           if volume then               audio.setVolume( volume, { channel = freechannel } )           end           return freechannel       end

with handle being the sound.

The strange thing is the sounds which are called are working during a level and then the error appears when a sound (which was working before fine) is called again right out of the blue. So it doesn’t look like it has to do with the sound file itself.

Hi @d.mach,

How often is called function with sound code you provide?

See audio.play() - 17dTesting Error with clearing buffer from source: Invalid Operation

freechannel variable always evaluate to true value since audio.findFreeChannel always returns number.

Note: Lua consider false and nil as false and anything else as true.

Read more:

ldurniat

The function is called very often when the action gets more and more in the game.

Thanks for the info!

I suggest using WAV files for sound effects, not MP3.

(MP3 files need to be decoded each time you play them and you have limited hardware for that on some devices. So, playing a lot of sounds that need to be decoded can be a choke point.)
 
( Sound experts.  If I have this wrong, please post back.  I understand the want to use MP3 files for their wonderful compactness, so If I do have this wrong I’d love to know. )
 
Sound Flooding
Also, you should really implement code to prevent sound flooding. Otherwise,

  1. The game will sound crazy or bad when the action gets too furious
  2. You will run out of free channels.

I prevent sound flooding, by maintaining a record with a minimum between-time assigned for some sounds. Then, if my code tries to play a sound twice and the second time is too soon, the sound is not played.

Ex:
name: boom
file: boom.wav
minTime: 100

Play boom @ 0 - OK
Play boom @ 150 - OK
Play boom @ 200 - Not played
Play boom @ 260 - OK

 
If you are acquiring free channels as shown for all sounds, then that may act as a gate and global sound flooding preventer, but you may find your most used sounds prevent more important sounds from playing.  If you do, then you can implement a more robust/complex sound flooding prevention scheme.
 
@ldurdiat is right, this is what you want:

 if( ( freechannel == 0 ) and (freechannel \>= 4 and playingalso == false) ) then

(I still do this sometimes too… I come from a background of many other languages including C and C++ which mostly treat 0 as ‘false’. Lua is goofy this way.)

Coding tip: You need to use more parentheses in your code. Don’t default to relying on order of operations.
 
You should logically group operations and key comparisons.  It will make your code better, safer, and more legible.
 
PS - I stand by the idea of recoding the sound files that are causing trouble.  Even if you re-code them as MP3, I find that sometimes sound files have an internal encoding problem and re-exporting them with Audacity solves the problem.
 
https://www.audacityteam.org/

Thanks for the detailed info! Much appreciated!
One question: Can the error and problem described cause some Runtime errors on a device like for example make the game stop or anything? In the simulator there only is this text output but the game still continues to run.

I don’t know for certain, but I think your game will continue running,

However, I also have to wonder if this will eventually cause some or all sounds to stop working.