【问题标题】:Pygame: set absolute sound volume using multiple channelsPygame:使用多个通道设置绝对音量
【发布时间】:2017-03-22 17:45:12
【问题描述】:

我需要在循环的每次迭代中为左右耳设置不同的音量。我通过使用两个通道来代表左右耳的声音,每只耳朵。

但是,根据文档,每次调用 set_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

我目前通过在循环的每次迭代中调用 channel.play 来避免这个问题,这会将音量重置为 1。但是它也只是在每次迭代时重新启动声音文件。有没有更好的方法来做到这一点?

# Current implementation
while True:
   chan1.play(sound)
   chan1.set_volume(volLeft, 0.0)

   chan2.play(sound)
   chan2.set_volume(0.0, volRight)

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

如果这在 pygame 中是不可能的,你知道任何其他的 python 声音库可以做到这一点吗?

谢谢

【问题讨论】:

    标签: python audio pygame volume


    【解决方案1】:

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

    # 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
    

    【讨论】:

      猜你喜欢
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      相关资源
      最近更新 更多