gpt4 book ai didi

python - 使用pygame在左声道和右声道上播放两个不同的音频文件

转载 作者:太空宇宙 更新时间:2023-11-04 04:52:20 27 4
gpt4 key购买 nike

我有一个代码,我在两个不同的 channel 中指定了两个不同的音频文件并同时播放,但我需要一种方法让每个文件只在一个 channel 上播放,另一个在另一个 channel 上播放。例如,两个音频文件同时在两个单独的 channel (右 channel 和左 channel )上播放。这样一个音频在右扬声器上播放,另一个音频在左扬声器上播放。

我尝试使用下面的代码,但音频未映射到任何特定 channel ,而是在两个扬声器上播放。

pygame.mixer.init(frequency=44000, size=-16,channels=2, buffer=4096)
#pygame.mixer.set_num_channels(2)
m = pygame.mixer.Sound('tone.wav')
n = pygame.mixer.Sound('sound.wav')
pygame.mixer.Channel(1).play(m,-1)
pygame.mixer.Channel(2).play(n,-1)

非常感谢任何帮助。

最佳答案

文档说你必须将左右扬声器的音量传递给 Channel.set_volume .

# Create Sound and Channel instances.
sound0 = pg.mixer.Sound('my_sound.wav')
channel0 = pg.mixer.Channel(0)

# Play the sound (that will reset the volume to the default).
channel0.play(sound0)
# Now change the volume of the specific speakers.
# The first argument is the volume of the left speaker and
# the second argument is the volume of the right speaker.
channel0.set_volume(1.0, 0.0)

此外,如果您不想自己管理 channel ,您可以只使用 Sound.play() 返回的 channel 。

channel = sound0.play()
channel.set_volume(1.0, 0.0)

关于python - 使用pygame在左声道和右声道上播放两个不同的音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47921638/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com