Reduce frequency of collision sounds

Hello.

I was hoping someone might be able to suggest a neat and clever way of preventing my collision sounds from ‘stuttering’ to a halt. I have a physics object (a ball) that collides with other objects and these objects trigger an event sound when the ball collides with them.

local function objectCollision( self, event )  
 if ( event.phase == "began" ) then  
 if (event.other.name=="ball") then  
 media.playEventSound( sound )  
 end  
 end  
end  
  
object.collision = objectCollision  
object:addEventListener( "collision", object )  

But the thing is; I want to be able to set a time limit on how often a collision can trigger a sound to stop glitchy tight audible repeats. I don’t want to weigh my code down with creating a timer to toggle a boolean on each collision.

Any suggestions? [import]uid: 62617 topic_id: 20242 reply_id: 320242[/import]

Hey, @juliusbangert, you may want to take a look at
this, which says "The audio functions in this section have been replaced by a new OpenAL audio library that supports multiple channels and a wide range of features. It is recommended that all audio playback use this new library. http://developer.anscamobile.com/reference/audio

Maybe switching over to the new OpenAL audio library would fix your issue? (If not, at least, it would be a step forward to the right direction, I hope.)

Naomi

[import]uid: 67217 topic_id: 20242 reply_id: 79080[/import]

As said use the new openAL sound apis. Eventsounds are depreciated [import]uid: 84637 topic_id: 20242 reply_id: 79132[/import]

There are several options for that i guess.

  1. Use timer.performWithDelay to play the sound only every x number of seconds.
  2. Get the velocity of the ball and only play the sound if the velocity is equal or greater than you require (to play sound)
  3. add silence to the end of your sound effect [import]uid: 84637 topic_id: 20242 reply_id: 79167[/import]

Thanks for your replies and the advice on the deprecated event sounds, I will move to the audio method…
But how will this aid me in preventing the collision listener from triggering the sounds more than once every given time interval?

To illustrate my point, if you imagine a bouncing ball, it’s initial bounces are longer than it’s final bounces so the collision events get closer together and the sound plays repeatedly and becomes an irritatingly sawtooth-like sound. I need to set a cutoff point where the collisions don’t trigger the callback when they occur within x ms of one another. [import]uid: 62617 topic_id: 20242 reply_id: 79156[/import]

In the end I went away from using the “collision” event and went for the “postCollision” event. I used a combination of a time limit and force threshold to control the number of times the collision sound was played as the physics object settles down out of a bounce.

As always, I spent hours coming up with this and then I read this post:
http://developer.anscamobile.com/forum/2011/09/17/playing-sounds-collision
I should have probably searched the forums better first.

[code]
local collisionSound = {}
collisionSound.audio = audio.loadSound(‘sound.mp3’)
collisionSound.channel = 1
collisionSound.enabled = true

local timeLimit = 200
local forceThreshold = 0.006

local function onPostCollision(event)
if ( event.force > forceThreshold and event.other.name == “ball”) then
if (collisionSound.enabled) then
collisionSound.enabled = false
audio.play(collisionSound.audio, {channel= collisionSound.channel})
collisionSound.timer = timer.performWithDelay(
timeLimit,
function() collisionSound.enabled = true end
)
end
end
end

– Create the event listener for post collision
object.postCollision = onPostCollision
object:addEventListener( “postCollision”, object ) [import]uid: 62617 topic_id: 20242 reply_id: 80170[/import]