Pygame - 声音延迟 [英] Pygame - Sound delay

查看:62
本文介绍了Pygame - 声音延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个按钮类,用于检查按钮是否被选中(当鼠标悬停在按钮上时).当按钮被选中、取消选中或单击时,它会播放一个 wav 文件.问题是声音播放和按钮状态改变之间存在巨大的延迟.程序应该检查每一帧,看看是否满足播放声音的条件,但 fps 似乎不是问题(60 和 600 fps 给出相同的延迟).我已经尝试减少 pygame.mixer.init() 中的缓冲区值,但这也没有区别.

I've made a button class that checks if a button is selected (when the mouse is hovering over the button). When the button is selected, unselected or clicked it plays a wav file. The problem is that there is a huge delay between the sound playing and the button's state changing. The program should check every frame to see if the conditions for the sound to play have been met but the fps doesn't seem to be the problem (60 and 600 fps give the same delay). I've tried decreasing the buffer value in pygame.mixer.init() but that also shows no difference.

声音文件:

buttonSoundSelect = pygame.mixer.Sound(os.path.join(soundPath, "button1.wav"))
buttonSoundUnselect = pygame.mixer.Sound(os.path.join(soundPath, "button2.wav"))
buttonSoundClick = pygame.mixer.Sound(os.path.join(soundPath, "button3.wav"))
buttonSounds = [buttonSoundSelect, buttonSoundUnselect, buttonSoundClick]

创建对象:

playButton = button(textInactive = "Play", font = mainFont, sounds = buttonSounds,  command = playAction)

来自按钮类的代码,用于检查按钮是否被选中(这是在方法 .display 中,每帧都会调用):

Code from the button class that checks if the button is selected (this is inside the method .display which is called every frame):

    if pygame.mouse.get_pos()[0] >= self.x and pygame.mouse.get_pos()[0] <= self.x + self.width and \
       pygame.mouse.get_pos()[1] >= self.y and pygame.mouse.get_pos()[1] <= self.y + self.height:

        self.surfaceActive.blit(self.textSurfaceActive, (self.width / 2 - self.font.size(self.textActive)[0] / 2,
                                                   self.height / 2 - self.font.size(self.textActive)[1] / 2))

        self.surface.blit(self.surfaceActive, (self.x, self.y))

        if self.selected == False:
            if self.sounds != None:
                self.sounds[0].stop()
                self.sounds[1].stop()
                self.sounds[2].stop()
                self.sounds[0].play()
            self.selected = True

    else:

        self.surfaceInactive.blit(self.textSurfaceInactive, (self.width / 2 - self.font.size(self.textInactive)[0] / 2,
                                                     self.height / 2 - self.font.size(self.textInactive)[1] / 2))

        self.surface.blit(self.surfaceInactive, (self.x, self.y))

        if self.selected == True:
            if self.sounds != None:
                self.sounds[0].stop()
                self.sounds[1].stop()
                self.sounds[2].stop()
                self.sounds[1].play()
            self.selected = False

来自按钮类的代码,用于检查按钮是否被点击(这是在点击鼠标左键时调用的方法 .clickEvent 内部):

Code from the button class that checks if the button is clicked (this is inside the method .clickEvent which is called when the left mouse button is clicked):

    if self.command != None:

        if pygame.mouse.get_pos()[0] >= self.x and pygame.mouse.get_pos()[0] <= self.x + self.width and \
           pygame.mouse.get_pos()[1] >= self.y and pygame.mouse.get_pos()[1] <= self.y + self.height:    

            if self.sounds != None:
                self.sounds[2].play()
            self.command()

所以我的问题是:为什么会有很长的延迟,我可以缩短它吗?

So my question is: Why is there a long delay and can I make it shorter?

推荐答案

减小缓冲区的大小将减少延迟.缓冲区必须是 2 的幂.默认缓冲区是 4096,但您可以在初始化混频器时更改它,如下所示:

Decreasing the size of the buffer will reduce the latency. The buffer must be a power of 2. The default buffer is 4096, but you can change it when you initialize mixer as shown below:

pygame.mixer.init(44100, -16, 2, 64)

更多信息可以在 pygame 文档

这篇关于Pygame - 声音延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆