reimplemented volume controls
This commit is contained in:
parent
823149738b
commit
b9bdbc5787
|
@ -11,16 +11,16 @@ from .extension import *
|
||||||
class Audio(Animation):
|
class Audio(Animation):
|
||||||
|
|
||||||
UP, DOWN = .1, -.1
|
UP, DOWN = .1, -.1
|
||||||
BASE_VOLUME = 1.0
|
|
||||||
CONFIG_SEPARATOR = ","
|
CONFIG_SEPARATOR = ","
|
||||||
|
|
||||||
def __init__(self, game):
|
def __init__(self, game):
|
||||||
Animation.__init__(self, game)
|
Animation.__init__(self, game)
|
||||||
# self.original_volumes = {}
|
# self.original_volumes = {}
|
||||||
self.current_bgm = None
|
self.current_bgm = None
|
||||||
self.volume = self.BASE_VOLUME
|
self.volume = 1.0
|
||||||
|
self.pre_muted_volume = None
|
||||||
if self.check_command_line("-mute"):
|
if self.check_command_line("-mute"):
|
||||||
self.volume = 0
|
self.get_configuration().set("audio", "volume", 0)
|
||||||
self.register(self.play_sfx)
|
self.register(self.play_sfx)
|
||||||
self.audio_panel = AudioPanel(self)
|
self.audio_panel = AudioPanel(self)
|
||||||
self.subscribe(self.respond)
|
self.subscribe(self.respond)
|
||||||
|
@ -28,19 +28,34 @@ class Audio(Animation):
|
||||||
self.load_sfx()
|
self.load_sfx()
|
||||||
self.bgm = {}
|
self.bgm = {}
|
||||||
self.load_bgm()
|
self.load_bgm()
|
||||||
|
self.set_volume(self.get_configuration("audio", "volume"))
|
||||||
|
|
||||||
def set_volume(self, volume=None, increment=None, mute=False):
|
def set_volume(self, volume=None, increment=None, mute=False, unmute=False):
|
||||||
if mute:
|
if mute:
|
||||||
|
self.pre_muted_volume = self.volume
|
||||||
self.volume = 0
|
self.volume = 0
|
||||||
|
elif unmute and self.pre_muted_volume is not None:
|
||||||
|
self.volume = self.pre_muted_volume
|
||||||
|
self.pre_muted_volume = None
|
||||||
elif increment:
|
elif increment:
|
||||||
self.volume = clamp(self.volume + increment, 0, 1.0)
|
self.volume = clamp(self.volume + increment, 0, 1.0)
|
||||||
else:
|
else:
|
||||||
self.volume = volume
|
self.volume = volume
|
||||||
|
self.get_configuration().set("audio", "volume", self.volume)
|
||||||
|
if pygame.mixer.music.get_busy():
|
||||||
|
pygame.mixer.music.set_volume(self.current_bgm.volume * self.volume)
|
||||||
|
for ii in range(pygame.mixer.get_num_channels()):
|
||||||
|
channel = pygame.mixer.Channel(ii)
|
||||||
|
if channel.get_busy():
|
||||||
|
channel.set_volume(channel.get_sound().get_volume() * self.volume)
|
||||||
|
|
||||||
def respond(self, event):
|
def respond(self, event):
|
||||||
compare = self.get_game().delegate.compare
|
compare = self.get_game().delegate.compare
|
||||||
if compare(event, "volume-mute"):
|
if compare(event, "volume-mute"):
|
||||||
self.set_volume(mute=True)
|
if self.volume > 0:
|
||||||
|
self.set_volume(mute=True)
|
||||||
|
else:
|
||||||
|
self.set_volume(unmute=True)
|
||||||
elif compare(event, "volume-up"):
|
elif compare(event, "volume-up"):
|
||||||
self.set_volume(increment=self.UP)
|
self.set_volume(increment=self.UP)
|
||||||
elif compare(event, "volume-down"):
|
elif compare(event, "volume-down"):
|
||||||
|
@ -163,7 +178,7 @@ class Audio(Animation):
|
||||||
pygame.mixer.music.play(-1, start)
|
pygame.mixer.music.play(-1, start)
|
||||||
except pygame.error:
|
except pygame.error:
|
||||||
pygame.mixer.music.play(-1)
|
pygame.mixer.music.play(-1)
|
||||||
pygame.mixer.music.set_volume(bgm.get_volume())
|
pygame.mixer.music.set_volume(bgm.get_volume() * self.get_configuration("audio", "volume"))
|
||||||
if store_as_current:
|
if store_as_current:
|
||||||
self.current_bgm = bgm
|
self.current_bgm = bgm
|
||||||
|
|
||||||
|
@ -229,7 +244,7 @@ class SoundEffect(GameChild, pygame.mixer.Sound):
|
||||||
def play(self, loops=None, maxtime=None, fade_ms=None, position=None,
|
def play(self, loops=None, maxtime=None, fade_ms=None, position=None,
|
||||||
x=None):
|
x=None):
|
||||||
self.set_volume(
|
self.set_volume(
|
||||||
self.local_volume * self.get_configuration("audio", "sfx-volume"))
|
self.local_volume * self.get_configuration("audio", "volume"))
|
||||||
if loops is None:
|
if loops is None:
|
||||||
loops = self.loops
|
loops = self.loops
|
||||||
if maxtime is None:
|
if maxtime is None:
|
||||||
|
|
|
@ -150,6 +150,8 @@ class Configuration(RawConfigParser):
|
||||||
set_option(section, "bgm-repository-path", "~/storage/audio/bgm", False)
|
set_option(section, "bgm-repository-path", "~/storage/audio/bgm", False)
|
||||||
set_option(section, "bgm-project-path", "bgm", False)
|
set_option(section, "bgm-project-path", "bgm", False)
|
||||||
set_option(section, "sfx-volume", "1.0", False)
|
set_option(section, "sfx-volume", "1.0", False)
|
||||||
|
set_option(section, "bgm-volume", "1.0", False)
|
||||||
|
set_option(section, "volume", "1.0", False)
|
||||||
set_option(section, "panel-font", None, False)
|
set_option(section, "panel-font", None, False)
|
||||||
section = "interpolator-gui"
|
section = "interpolator-gui"
|
||||||
add_section(section)
|
add_section(section)
|
||||||
|
@ -450,7 +452,7 @@ class TypeDeclarations(dict):
|
||||||
"bgm-repository-path", "bgm-project-path"
|
"bgm-repository-path", "bgm-project-path"
|
||||||
],
|
],
|
||||||
|
|
||||||
"float": "sfx-volume"
|
"float": ["sfx-volume", "bgm-volume", "volume"]
|
||||||
},
|
},
|
||||||
|
|
||||||
"event": {"int": "command-id-offset"},
|
"event": {"int": "command-id-offset"},
|
||||||
|
|
|
@ -125,9 +125,11 @@ def constrain_dimensions_2d(vector, container):
|
||||||
dh = vector[1] - container[1]
|
dh = vector[1] - container[1]
|
||||||
if dw > 0 or dh > 0:
|
if dw > 0 or dh > 0:
|
||||||
if dw > dh:
|
if dw > dh:
|
||||||
size = container[0], int(round(container[0] / vector[0] * vector[1]))
|
size = Vector(container[0], int(round(container[0] / vector[0] * vector[1])))
|
||||||
else:
|
else:
|
||||||
size = int(round(container[1] / vector[1] * vector[0])), container[1]
|
size = Vector(int(round(container[1] / vector[1] * vector[0])), container[1])
|
||||||
|
else:
|
||||||
|
size = Vector(vector[0], vector[1])
|
||||||
return size
|
return size
|
||||||
|
|
||||||
# from http://www.realtimerendering.com/resources/GraphicsGems/gemsii/xlines.c
|
# from http://www.realtimerendering.com/resources/GraphicsGems/gemsii/xlines.c
|
||||||
|
|
Loading…
Reference in New Issue