Pygame:使用多个通道设置绝对音量

2024-09-29 21:39:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在循环的每一次迭代中为右耳和左耳设置不同的音量。我用两个声道来表示左右耳的声音,每个声道一个。在

但是,根据文档,每次调用set_volume时,它会将volume设置为先前值的百分比。在

 channel1.set_volume(0.0, 0.5) # sets at 0.5
 channel1.set_volume(0.0, 0.5) # sets at 0.5 * 0.5 = 0.25

我现在通过打电话来避免这个问题频道播放在循环的每次迭代中,都会将卷重置为1。不过,它也只是在每次迭代时重新启动声音文件。有更好的方法吗?在

^{pr2}$

还有一个类似的问题(Setting volume globally in pygame.Sound module)。我试着控制两个不同声音变量的音量,但是左右耳的音量没有变化

如果这在pygame中是不可能的,那么您知道在其他python声音库中这是可能的吗?在

谢谢你


Tags: 文档声音sets频道pygameat重置音量
1条回答
网友
1楼 · 发布于 2024-09-29 21:39:48

我通过为每个通道分配一个声音对象并设置该声音对象的音量来控制音量

# initialise variables
sound = pygame.mixer.Sound(sound_path)
sound_2 = pygame.mixer.Sound(sound_path)
chan1 = pygame.mixer.Channel(0)
chan2 = pygame.mixer.Channel(1)

# set channel to max
chan1.set_volume(1, 0)
chan2.set_volume(0, 1)

# set sound to initial value
sound.set_volume(0)
sound2.set_volume(0) 

chan1.play(sound, 50, 0, 0)
chan2.play(sound2, 50, 0, 0)

while True:

   volLeft = getLeftVol()
   volRight = getRightVol()

   # change volume of sound every iteration
   sound.set_volume(volLeft)
   sound2.set_volume(volRight)

   if isDone:
      break

相关问题 更多 >

    热门问题