Sou*_*are 10 python pygame playback
我的目的是让两首音乐曲目在性质上相似,在不同的时间彼此淡化.当发生这样的淡入淡出时,一个音乐曲目应该在短时间内从全音量淡入淡出,并且同时,另一个音轨应该从0渐变到100并且从同一时间索引继续播放.他们必须能够在任何时候动态地执行此操作 - 当某个动作发生时,淡入淡出将发生并且新音轨将开始播放在另一个音轨停止的相同位置.
无论是使用音量操作还是通过启动和停止音乐,这都可能是合理的(但是,似乎只存在"淡出"选项,而且缺少"fadein"选项).我怎样才能做到这一点?存在的最佳方法是什么?如果不可能使用Pygame,Pygame的替代品是可以接受的.
试试这个,这很简单。
import pygame
pygame.mixer.init()
pygame.init()
# Maybe you can subclass the pygame.mixer.Sound and
# add the methods below to it..
class Fader(object):
instances = []
def __init__(self, fname):
super(Fader, self).__init__()
assert isinstance(fname, basestring)
self.sound = pygame.mixer.Sound(fname)
self.increment = 0.01 # tweak for speed of effect!!
self.next_vol = 1 # fade to 100 on start
Fader.instances.append(self)
def fade_to(self, new_vol):
# you could change the increment here based on something..
self.next_vol = new_vol
@classmethod
def update(cls):
for inst in cls.instances:
curr_volume = inst.sound.get_volume()
# print inst, curr_volume, inst.next_vol
if inst.next_vol > curr_volume:
inst.sound.set_volume(curr_volume + inst.increment)
elif inst.next_vol < curr_volume:
inst.sound.set_volume(curr_volume - inst.increment)
sound1 = Fader("1.wav")
sound2 = Fader("2.wav")
sound1.sound.play()
sound2.sound.play()
sound2.sound.set_volume(0)
# fading..
sound1.fade_to(0)
sound2.fade_to(1)
while True:
Fader.update() # a call that will update all the faders..
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1752 次 |
| 最近记录: |