10

我的目的是让两个性质相似的音乐曲目在不同的时间相互淡入淡出。当发生这种渐变时,一个音乐曲目应在短时间内从全音量渐变为静音,同时另一首曲目应从 0 渐变到 100 并从同一时间索引继续播放。他们必须能够随时动态地执行此操作 - 当某个动作发生时,将发生淡入淡出,并且新曲目将在另一首曲目停止的相同位置开始播放

通过使用音量操作或启动和停止音乐,这可能是合理的(但是,似乎只存在“淡出”选项,并且缺少“淡入”选项)。我怎样才能做到这一点?如果有的话,最好的方法是什么?如果无法使用 Pygame,Pygame的替代品是可以接受的

4

4 回答 4

2

这不完全是问题的答案,但对于未来的谷歌用户,我写了一个脚本,在早上从第 0 卷淡入我的音乐,这就是我使用的:

max_volume = 40 
current_volume = 0

# set the volume to the given percent using amixer
def set_volume_to(percent):
    subprocess.call(["amixer", "-D", "pulse", "sset", "Master", 
                     str(percent) + "%", "stdout=devnull"])

# play the song and fade in the song to the max_volume 
def play_song(song_file):
    global current_volume
    print("Song starting: " + song_file)
    pygame.mixer.music.load(song_file)
    pygame.mixer.music.play()

    # gradually increase volume to max
    while pygame.mixer.music.get_busy():
        if current_volume < max_volume: 
            set_volume_to(current_volume)
            current_volume += 1

        pygame.time.Clock().tick(1)

 play_song("foo.mp3")
于 2017-02-03T16:13:44.423 回答
1

伪代码:

track1 = ...
track2 = ...

track1.play_forever()
track1.volume = 100
track2.play_forever()
track2.volume = 0

playing = track1
tracks = [track1, track2]


def volume_switcher():
    while True:
        playing.volume = min(playing.volume + 1, 100)

        for track in tracks:
            if track != playing:
                track.volume = max(track.volume - 1, 100)

        time.sleep(0.1)

Thread(target=volume_switcher).start()
于 2013-09-16T13:36:20.210 回答
1

所以看起来你想要在 pygame 中做的是创建两个“声音”对象,并在它们两者之间的音量上创建一个线性插值。

我将创建两个向量,每个向量都来自 [0,100],并将它们与某个常数反向关联。因此,当声音 A 为 100 时,声音 b 为 0。然后当一个动作发生时,您修改常数。

t=0
A:[0 ... 100 ]
B:[ 0 ... 100]

t=1
动作

t=1.1
A:[0 .. 50 .. 100]
B:[0 .. 50 .. 100]

t=2
A:[ 0 ... 100]
B:[0 ... 100 ]

现在一些代码。我不熟悉 pygame,但这应该会让你走上正轨。

class Song(object):
    def __init__(self, songfilename):
        self.song = pygame.mixer.Sound(songfilename)

    def setVolume(self, somenumber):
        #number validation
        #possibly do some volume curve here if you wanted
        self.song.set_volume(somenumber)

class SongFader(object):
    def __init__(self, song1, song2):
        self.song1 = song1
        self.song2 = song2
        self.__xAxisMax = 100
        self.__xAxisMin = 0

    def fade(self, xaxis):
        assert(self.__xAxisMin <= xaxis <= self.__xAxisMax) 
          #could be any numbers you want. 
          #i chose 0-100 for convenience
        self.song1.setVolume(xaxis)
        self.song2.setVolume(self.__xAxisMax-xaxis)

song1 = Song('Song1.wav')
song2 = Song('Song2.wav')
fader = SongFader(song1, song2)

#Inside some event loop when you action is triggered
fader.fade(100) #Only song2 is playing
fader.fade(50)  #Songs are evenly split
fader.fade(0)   #Only left song is playing

编辑

线性插值可能是这里更重要的概念,所以我修改了推子类,灵感来自 Eric 的线程想法。

class SongFader(object):
    def __init__(self, song1, song2):
        self.song1 = song1
        self.song2 = song2
        self.lefttoright = False
        self.starttime = 0
        self.endtime = 0


    def fade(self, starttime, fadeleft):
        self.lefttoright = fadeleft == True #Being verbose here
        self.starttime = starttime #assuming time is in millis
        self.endtime = starttime + 1000
        Thread(target = self.fadeHelper).start()

    #this is where you define how the two songs are faded
    def fadeHelper(self):
        #if using thread, make sure you mutex the 'self.' variables
        starttime = self.starttime
        endtime = self.endtime
        lefttoright = self.lefttoright
        while starttime < endtime:
            fadevalue = (starttime - endtime) / 1000 #a val between [0,1]
            if lefttoright:
                self.song1.setVolume(fadevalue)
                self.song2.setVolume(1-fadevalue)
            else:
                self.song1.setVolume(1-fadevalue)
                self.song2.setVolume(fadefalue)
            starttime = getGameTimeFromSomewhere()
于 2013-09-16T13:50:03.207 回答
1

试试这个,很简单。。

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..
于 2013-09-18T10:11:01.450 回答