Author Topic: Sound Delay Issue  (Read 1777 times)

Pinswe

  • Full Member
  • ***
  • Posts: 36
    • View Profile
Sound Delay Issue
« on: March 27, 2015, 05:23:42 AM »
I'm currently working on my MM Catapult mode and have stumbled upon a sound delay issue.

I'm not sure how familiar you guys are with Medieval Madness Catapult mode but here it goes:
I have a simple method that loops all the catapult elements.
Every time a new item is put on the display a tickersound is played.
Every if statement is set to 220ms delay.
My tickersound is only 130ms and is a .wav file.

here is my method:
Code: [Select]
    def fire_animation(self, step=1):
        self.rolling = True
        self.autoTrigger += 1
        if step == 1:
            self.game.sound.play(self.game.assets.sfx_CatapultTicker)
            self.winLayer = self.game.assets.dmd_cataBall
            cataBall = dmd.FrameLayer(opaque=True, frame=self.game.assets.dmd_cataBall.frames[0])
            self.layer = cataBall
            self.index = 0
            self.winItem = "BOWLING BALL"
            self.delay(name='Lotto',delay=0.220,handler=self.fire_animation,param=2)
        elif step == 2:
            self.game.sound.play(self.game.assets.sfx_CatapultTicker)
            self.winLayer = self.game.assets.dmd_cataCow
            cataCow = dmd.FrameLayer(opaque=True, frame=self.game.assets.dmd_cataCow.frames[0])
            self.layer = cataCow
            self.index = 1
            self.winItem = "COW"
            self.delay(name='Lotto',delay=0.220,handler=self.fire_animation,param=3)
        elif step == 3:
            self.game.sound.play(self.game.assets.sfx_CatapultTicker)
            self.winLayer = self.game.assets.dmd_cataChicken
            cataChicken = dmd.FrameLayer(opaque=True, frame=self.game.assets.dmd_cataChicken.frames[0])
            self.layer = cataChicken
            self.index = 2
            self.winItem = "CHICKEN"
            self.delay(name='Lotto',delay=0.220,handler=self.fire_animation,param=4)
        elif step == 4:
            self.game.sound.play(self.game.assets.sfx_CatapultTicker)
            self.winLayer = self.game.assets.dmd_cataCat
            cataCat = dmd.FrameLayer(opaque=True, frame=self.game.assets.dmd_cataCat.frames[0])
            self.layer = cataCat
            self.index = 3
            self.winItem = "CAT"
            self.delay(name='Lotto',delay=0.220,handler=self.fire_animation,param=5)
        elif step == 5:
            self.game.sound.play(self.game.assets.sfx_CatapultTicker)
            self.winLayer = self.game.assets.dmd_cataSkull
            cataSkull = dmd.FrameLayer(opaque=True, frame=self.game.assets.dmd_cataSkull.frames[0])
            self.layer = cataSkull
            self.index = 4
            self.winItem = "SKULL"
            self.delay(name='Lotto',delay=0.220,handler=self.fire_animation,param=1)
        if self.autoTrigger == self.randomEnd:
            self.play_winner()

The whole animation/method is working fine but the tickersound isn't synced with my dmdframes that are displayed.
Any idea why ?
Does it take to long time to load the tickersound ?
Do I have to somehow cache the tickersound ?

MOcean

  • P3 Developers
  • *
  • Posts: 820
  • Michael Ocean
    • View Profile
Re: Sound Delay Issue
« Reply #1 on: March 30, 2015, 11:27:38 PM »
Tickersound looks cached to me based on what you've shown. I see a few places where I might refactor the code some but none of what I'm seeing should cause an audio delay. Two simple things to check first: if this is the only place where you're seeing an audio delay, make sure that the wav/ogg is stored at the same sample rate as the pygame mixer pre-init call.
https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.pre_init

 Also print out the Pygame sound settings to confirm that they've been registered properly and that it didn't ignore your settings. If memory serves you can do that by just printing what gets returned from the call to mixer get init

https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.get_init

I once had a similar issue and found that my sample rate was different from what I was requesting because my pre-init call was "too late" in my code and with the sample rate not matching it introduced a delay for my sounds which I assume was because the sample rate needed to be changed by pygame itself before it could be played.


Rosh

  • P3 Developers
  • *
  • Posts: 667
  • Josh Kugler
    • View Profile
Re: Sound Delay Issue
« Reply #2 on: March 31, 2015, 10:07:31 AM »
also check your buffer size on the mixer init, the default buffer was increased in a more recent release of pygame and is now too big and can create audio delays. 

Here is what I have . . .
Code: [Select]
            mixer.pre_init(frequency=44100, size=-16, channels=8, buffer=256)
            mixer.init()
« Last Edit: March 31, 2015, 10:09:26 AM by Rosh »

Pinswe

  • Full Member
  • ***
  • Posts: 36
    • View Profile
Re: Sound Delay Issue
« Reply #3 on: April 20, 2015, 12:37:31 AM »
Sorry for not responding sooner but I have been away abroad on a funeraltrip for a while with no access to my Proc setup.

Thanks guys I will check things suggested here and post any findings here.

Koen

  • Wizard
  • *****
  • Posts: 387
  • Koen Heltzel
    • View Profile
    • Dutch Pinball
Re: Sound Delay Issue
« Reply #4 on: April 20, 2015, 10:59:34 AM »
Here is what I have . . .
Code: [Select]
mixer.pre_init(frequency=44100, size=-16, channels=8, buffer=256)
mixer.init()

Hey Rosh,

The "channels" argument is kinda funny & confusing I must admit... but the pygame docs say:
The channels argument is used to specify whether to use mono or stereo. 1 for mono and 2 for stereo. No other values are supported (negative values are treated as 1, values greater than 2 as 2).

So I think what you need is:

Code: [Select]
mixer.pre_init(frequency=44100, size=-16, channels=2, buffer=256)
mixer.init()
mixer.set_num_channels(8) # Or increase this number if needed.

Once again the pygame docs and naming conventions are not perfect... I had to do a double take too. Also I don't really see the benefit the use of pre_init() if you're going to call init() right after that, but the docs say "The best way to set custom mixer playback values is to call pygame.mixer.pre_init() before calling the top level pygame.init()." so I guess there's something to it.

Anyway, my mixer settings / initialization looks like this:
Code: [Select]
mixer.init(frequency=44100, size=16, channels=2, buffer=2048)
mixer.set_num_channels(32)

Rosh

  • P3 Developers
  • *
  • Posts: 667
  • Josh Kugler
    • View Profile
Re: Sound Delay Issue
« Reply #5 on: April 20, 2015, 11:39:48 AM »
I agree the channels thing is confusing.

Doubt you would ever need 32 channels (hard to see you have 32 sounds playing simultaneously), and not sure of they implications of going that big. 

Koen

  • Wizard
  • *****
  • Posts: 387
  • Koen Heltzel
    • View Profile
    • Dutch Pinball
Re: Sound Delay Issue
« Reply #6 on: April 20, 2015, 06:04:03 PM »
I think I cranked the channels up because I had a stacking spinner sound.. but right now I changed it to cut itself off so 32 is overkill indeed...

Rosh

  • P3 Developers
  • *
  • Posts: 667
  • Josh Kugler
    • View Profile
Re: Sound Delay Issue
« Reply #7 on: April 21, 2015, 11:37:29 AM »
interesting with the spinner, I don't have one, yet, so have not had to deal with that, my first thought would be to throttle by insuring enough time has past since last sound effect was requested before making another, since I would think you would not want it playing multiple effects.  E.g. if you sound effect was 250 ms, then you would only send the sound effect if that much time had passed.  Which is sort of what I do with pulsing the magnet on my roulette wheel to keep it from going too fast.

Nice to see you posting, I guess Barry and the guys have let you out of the coding dungeon  :-)