【问题标题】:Pygame 1.9.2 for Python 3.4: Simulating 3D soundPygame 1.9.2 for Python 3.4:模拟 3D 声音
【发布时间】:2014-07-09 23:11:44
【问题描述】:

长话短说: 我在这里遵循了本教程和 pygame 1.9.2 的文档。根据两者的说法,应该可以在立体声模式下调整左右耳机的音量,但是,它根本不适合我。无论我尝试什么,在任一耳机上都能听到声音。

这是我的尝试:

简短的前言:我用

pygame.mixer.pre_init(44100, -16, 2, 3072)

这实际上应该为我的系统完成在pygame中启用立体声的工作,但也许我在这里犯了一个我已经忽略的错误,所以我提到它。

尝试1:

class MainProgram(object):

    def __init__(self, width = 640, height = 640):
        # Much Code

        # Sounds
        self.testsound = pygame.mixer.Sound("data\\sounds\\ping.ogg")
        self.chan1 = pygame.mixer.Channel(0)
        self.chan2 = pygame.mixer.Channel(1)

        self.mainloop()


    def mainloop(self):
        while 1:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    terminate()
                if event.type == pygame.KEYDOWN:
                    self.testsound.play().set_volume(1.0, 0.0)
                elif event.type == pygame.KEYUP:
                    self.testsound.play().set_volume(0.0, 1.0)
        # More code here

当我不调节音量时,我可以听到两边全音量的声音。如果我如上所示进行调整,那么我仍然可以听到两侧的声音,只有一半的音量(我粗略估计,但显然更安静),就像,而不是将一侧设置为 0,另一侧设置为 1,pygame只取两边的平均值 (0.5)。

尝试2:

class MainProgram(object):

    def __init__(self, width = 640, height = 640):
        # Much Code

        # Sounds
        self.testsound = pygame.mixer.Sound("data\\sounds\\ping.ogg")
        self.chan1 = pygame.mixer.Channel(0).set_volume(1.0, 0.0)
        self.chan2 = pygame.mixer.Channel(1).set_volume(0.0, 1.0)

        self.mainloop()


    def mainloop(self):
        while 1:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    terminate()
                if event.type == pygame.KEYDOWN:
                    self.chan1.play(self.testsound)
                elif event.type == pygame.KEYUP:
                    self.chan2.play(self.testsound)
        # More code here

这没有任何效果。所有声音都以最大音量播放。

尝试 3:

class MainProgram(object):

    def __init__(self, width = 640, height = 640):
        # Much Code

        # Sounds
        self.testsound = pygame.mixer.Sound("data\\sounds\\ping.ogg")
        self.chan1 = pygame.mixer.Channel(0)
        self.chan2 = pygame.mixer.Channel(1)

        self.mainloop()


    def mainloop(self):
        while 1:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    terminate()
                if event.type == pygame.KEYDOWN:
                    self.testsound.play()
                    self.testsound.set_volume(1.0, 0.0)
                elif event.type == pygame.KEYUP:
                    self.testsound.play()
                    self.testsound.set_volume(0.0, 1.0)
        # More code here

如您所见,我只是在尝试 3 中将尝试 2 的方法分成两行。有趣的是,有一个新问题:我得到了一个

TypeError: function takes exactly 1 argument (2 given)

这让一切变得更加有趣,因为在一行中使用相同的内容不会触发任何错误,尽管它并没有按照预期的方式进行。

正如我所看到的,我的程序处于立体声模式,并且根据教程(我已经检查了旁边的其他人)和文档,这两种第一种方法中的任何一种都应该产生我预期的结果 - 但两者都没有......

非常感谢本期提供的任何提示和技巧。我可以添加某种 3D 声音真的很重要。如果 pygame 本身确实存在问题,而不是我的代码,你知道我可以用来实现这种效果的任何其他模块吗 - 最好是与 Pygame 兼容,那么我不必重写所有内容(但我会这样做如有必要...)。

提前谢谢你!

拍拍

//我的短评: 我刚刚看到 Try 2 注定要失败,文档说,当再次使用频道时,所有音量都会重置,所以我不能一劳永逸地设置一个固定音量...

【问题讨论】:

    标签: pygame stereo-3d


    【解决方案1】:

    正如documentation 所说:set_volume 采用一个参数,即音量为float 之间的0.01.0

    这意味着:

    ad "Try 1":这在语法上是错误的。 play() 不返回任何内容,因此使用 set_volume() 进行链式调用是错误的。


    ad "Try 2":如果您像这样设置频道,这实际上可能会起作用:

    self.chan1 = pygame.mixer.Channel(0)
    self.chan1.set_volume(1.0, 0.0)
    self.chan2 = pygame.mixer.Channel(1)
    self.chan2.set_volume(0.0, 1.0)
    

    ad "Try 3: 在Sounds set_volume 上只有一个value is allowed

    【讨论】:

    • 感谢您的回答,我真的成功了!我用新代码更新了我自己的答案,因为在我看来,以这种方式处理它有点 *****,尤其是通道音量每次重置的事实在我的眼睛。 //编辑:是的,尝试2个作品,但是每次播放声音时我都必须重置音量,我将其发布在我自己的答案中。
    • 也许你添加了每个通道每次播放声音后必须重置每个通道的音量的部分。然后我可以让你接受接受的答案,我自己做不到。
    【解决方案2】:

    这是对我有用的最终版本。我使用了我的问题中的 Try 2 并进行了一些调整。毕竟,pygame 在声音方面似乎没有那么有用......无论如何,我的代码是一个尝试一些东西的实验,所以现在就这样工作 - 对于最终项目,我无论如何都会这样做编写我自己的声音模块,因为我有一些要求还没有涵盖;)

    这是最终代码,是我的 Try 2 的修改版本:

    class MainProgram(object):
    
        def __init__(self, width = 640, height = 640):
            # Much Code
    
            # Sounds
            self.sound = pygame.mixer.Sound("data\\sounds\\ping.ogg")
            self.chan1 = pygame.mixer.Channel(0)
            self.chan1.set_volume(1.0, 0.0)
            self.chan2 = pygame.mixer.Channel(1)
            self.chan2.set_volume(0.0, 1.0)
    
    
            self.mainloop()
    
    
        def mainloop(self):
            while 1:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        terminate()
                    if event.type == pygame.KEYDOWN:
                        self.chan1.play(self.sound)
                        # IMPORTANT NOTE! We have to reset the volume after each time
                        # a sound was played by the respective channel!
                        self.chan1.set_volume(1.0, 0.0)
                    elif event.type == pygame.KEYUP:
                        self.chan2.play(self.sound)
                        self.chan2.set_volume(0.0, 1.0)
            # More code here
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多