Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pygame 1.9.2 for Python 3.4:模拟3D声音_Pygame_Stereo 3d - Fatal编程技术网

Pygame 1.9.2 for Python 3.4:模拟3D声音

Pygame 1.9.2 for Python 3.4:模拟3D声音,pygame,stereo-3d,Pygame,Stereo 3d,长话短说: 我在这里学习了本教程和pygame 1.9.2的文档。根据这两种方法,应该可以在立体声模式下调整左右耳机的音量,但是,这对我根本不起作用。无论我如何尝试,在任何一个耳机上都可以听到声音 以下是我的尝试: 简短前言:我用 pygame.mixer.pre_init(44100, -16, 2, 3072) 这实际上应该可以让我的系统在pygame中启用立体声,但也许我在这里已经犯了一个错误,我忽略了这个错误,所以我提到了它 尝试1: class MainProgram(object)

长话短说: 我在这里学习了本教程和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
当我不调整音量时,我可以听到两边满音量的声音。如果我像上面所看到的那样调整,那么我仍然可以听到两边的声音,只是我粗略估计的音量的一半,但明显更安静,就像,pygame没有将一边设置为0,另一边设置为1,而是将两边的平均值设为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
正如您所看到的,我刚刚在try 3中将try 2的方法分成了两行。有趣的是,有一个新问题:我得到一个

TypeError: function takes exactly 1 argument (2 given)
这使得所有的事情都变得更加有趣,因为在一行中使用相同的代码不会触发错误,尽管它并没有做它应该做的事情

正如我所看到的,我的程序处于立体声模式,根据我在旁边查阅的教程和文档,前两种方法中的任何一种都会产生我所期望的结果,但两者都不会

我真的很感谢在这个问题上的任何提示和提示。我能添加一些3D声音真的很重要。如果pygame本身真的有问题,而不是我的代码,你知道我可以使用任何其他模块来实现这个效果吗?最好是与pygame兼容,那么我不必重写所有内容,但如果必要的话,我会这样做

提前谢谢你

//我的简短评论:
我刚刚看到Try 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

这是对我有效的最终版本。我使用了问题中的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
状态为:set_volume接受一个参数,即0.0和1.0之间的浮点值

这意味着:

广告尝试1:这在语法上是错误的。play不会返回任何内容,因此使用set_volume的链式调用是错误的

广告尝试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:在声音上只设置一个音量。

因为状态:设置音量采用一个参数,音量为0.0到1.0之间的浮点值

这意味着:

广告尝试1:这在语法上是错误的。play不会返回任何内容,因此使用set_volume的链式调用是错误的

广告尝试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)

广告试听3:声音设置只有一个音量。

谢谢你的回答,我真的让它工作了!我用新的代码更新了我自己的答案,因为在我看来,用这种方式处理它有点困难,尤其是频道音量每次都被重置的事实在我看来没有多大意义//编辑:是的,试2次可以,但每次播放声音时我都要重置音量,我会在我自己的答案中贴在上面。也许你可以添加一个部分,即每次播放声音后,每个频道的音量都必须重置。然后我可以让你的答案被接受,但我不能用我自己的答案。谢谢你的回答,我真的成功了!我用新的代码更新了我自己的答案,因为在我看来,用这种方式处理它有点困难,尤其是频道音量每次都被重置的事实在我看来没有多大意义//编辑:是的,试2次可以,但每次播放声音时我都要重置音量,我会在我自己的答案中贴在上面。也许你可以添加一个部分,即每次播放声音后,每个频道的音量都必须重置。那我就可以让你的回答成为公认的答案,但我自己不能这么做。