volume
This commit is contained in:
parent
ec2263596f
commit
732a00d079
102
pgfw/Audio.py
102
pgfw/Audio.py
|
@ -1,92 +1,42 @@
|
|||
from os import listdir
|
||||
from os.path import join
|
||||
|
||||
from pygame.mixer import Channel, Sound, music, find_channel
|
||||
from pygame.mixer import Channel, Sound, music, find_channel, get_num_channels
|
||||
|
||||
from GameChild import *
|
||||
from Input import *
|
||||
|
||||
class Audio(GameChild):
|
||||
|
||||
current_channel = None
|
||||
paused = False
|
||||
muted = False
|
||||
UP, DOWN = .1, -.1
|
||||
|
||||
def __init__(self, game):
|
||||
GameChild.__init__(self, game)
|
||||
self.delegate = self.get_delegate()
|
||||
self.load_fx()
|
||||
if self.check_command_line("-mute"):
|
||||
self.set_volume(mute=True)
|
||||
self.subscribe(self.respond)
|
||||
|
||||
def load_fx(self):
|
||||
fx = {}
|
||||
if self.get_configuration().has_option("audio", "sfx-path"):
|
||||
root = self.get_resource("audio", "sfx-path")
|
||||
if root:
|
||||
for name in listdir(root):
|
||||
fx[name.split(".")[0]] = Sound(join(root, name))
|
||||
self.fx = fx
|
||||
def set_volume(self, volume=None, increment=None, channels=None, mute=False):
|
||||
if not channels:
|
||||
channels = (Channel(ii) for ii in xrange(get_num_channels()))
|
||||
elif isinstance(channels, Channel):
|
||||
channels = [channels]
|
||||
for channel in channels:
|
||||
if mute:
|
||||
volume = 0
|
||||
elif increment:
|
||||
volume = channel.get_volume() + increment
|
||||
if volume > 1:
|
||||
volume = 1.0
|
||||
elif volume < 0:
|
||||
volume = 0
|
||||
channel.set_volume(volume)
|
||||
|
||||
def respond(self, event):
|
||||
if self.delegate.compare(event, "mute"):
|
||||
self.mute()
|
||||
|
||||
def mute(self):
|
||||
self.muted = True
|
||||
self.set_volume()
|
||||
|
||||
def unmute(self):
|
||||
self.muted = False
|
||||
self.set_volume()
|
||||
|
||||
def set_volume(self):
|
||||
volume = int(not self.muted)
|
||||
music.set_volume(volume)
|
||||
if self.current_channel:
|
||||
self.current_channel.set_volume(volume)
|
||||
|
||||
def play_bgm(self, path, stream=False):
|
||||
self.stop_current_channel()
|
||||
if stream:
|
||||
music.load(path)
|
||||
music.play(-1)
|
||||
else:
|
||||
self.current_channel = Sound(path).play(-1)
|
||||
self.set_volume()
|
||||
|
||||
def stop_current_channel(self):
|
||||
music.stop()
|
||||
if self.current_channel:
|
||||
self.current_channel.stop()
|
||||
self.current_channel = None
|
||||
self.paused = False
|
||||
|
||||
def play_fx(self, name, panning=.5):
|
||||
if not self.muted:
|
||||
channel = find_channel(True)
|
||||
if panning != .5:
|
||||
offset = 1 - abs(panning - .5) * 2
|
||||
if panning < .5:
|
||||
channel.set_volume(1, offset)
|
||||
else:
|
||||
channel.set_volume(offset, 1)
|
||||
channel.play(self.fx[name])
|
||||
|
||||
def pause(self):
|
||||
channel = self.current_channel
|
||||
paused = self.paused
|
||||
if paused:
|
||||
music.unpause()
|
||||
if channel:
|
||||
channel.unpause()
|
||||
else:
|
||||
music.pause()
|
||||
if channel:
|
||||
channel.pause()
|
||||
self.paused = not paused
|
||||
|
||||
def is_bgm_playing(self):
|
||||
current = self.current_channel
|
||||
if current and current.get_sound():
|
||||
return True
|
||||
return music.get_busy()
|
||||
compare = self.get_game().delegate.compare
|
||||
if compare(event, "volume-mute"):
|
||||
self.set_volume(mute=True)
|
||||
elif compare(event, "volume-up"):
|
||||
self.set_volume(increment=self.UP)
|
||||
elif compare(event, "volume-down"):
|
||||
self.set_volume(increment=self.DOWN)
|
||||
|
|
|
@ -115,7 +115,9 @@ class Configuration(RawConfigParser):
|
|||
set_option(section, "toggle-fullscreen", "K_F11", False)
|
||||
set_option(section, "reset-game", "K_F8", False)
|
||||
set_option(section, "record-video", "K_F10", False)
|
||||
set_option(section, "mute", "K_F12", False)
|
||||
set_option(section, "volume-down", "K_F1", False)
|
||||
set_option(section, "volume-up", "K_F2", False)
|
||||
set_option(section, "volume-mute", "K_F3", False)
|
||||
set_option(section, "toggle-interpolator", "K_F7", False)
|
||||
section = "joy"
|
||||
add_section(section)
|
||||
|
|
|
@ -185,5 +185,5 @@ def get_shadowed_text(text, font, offset, color, antialias=True, shadow_color=(0
|
|||
|
||||
def get_hsla_color(hue, saturation=100, lightness=50, alpha=100):
|
||||
color = Color(0, 0, 0, 0)
|
||||
color.hsla = hue, saturation, lightness, alpha
|
||||
color.hsla = hue % 360, saturation, lightness, alpha
|
||||
return color
|
||||
|
|
Loading…
Reference in New Issue