首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游戏中两首正在进行的音乐曲目之间的淡出

游戏中两首正在进行的音乐曲目之间的淡出
EN

Stack Overflow用户
提问于 2013-09-11 05:21:20
回答 4查看 2.9K关注 0票数 10

我的意图是让的两首音乐曲目,本质上是相似的,在不同的时间在彼此的之间褪色。当出现这种淡出时,一首音乐曲目应在短时间内从全音量逐渐减弱为静音,同时,另一首曲目应从0减弱到100,并从同一时间索引继续播放。他们必须能够在任何时候做这个动态--当某个动作发生时,会发生淡出,而新曲目将开始在另一个停止播放的位置上播放

通过使用音量操作或启动和停止音乐(然而,似乎只有"fadeout“选项存在,并且缺少"fadein”选项),这可能是合理的。我该怎么做?如果有的话,最好的方法是什么?如果不可能使用Pygame,那么替代Pygame是可以接受的

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-09-18 10:11:01

试试这个,挺直的..

代码语言:javascript
复制
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..
票数 1
EN

Stack Overflow用户

发布于 2017-02-03 16:13:44

这并不完全是对这个问题的回答,但对于未来的谷歌人来说,我写了一个要淡出的脚本--在我早上的第0卷的音乐中,这就是我所用的:

代码语言:javascript
复制
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")
票数 2
EN

Stack Overflow用户

发布于 2013-09-16 13:36:20

伪码:

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18733213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档