Skip to content
Snippets Groups Projects
Commit 2171c9b1 authored by David Trattnig's avatar David Trattnig
Browse files

refact(oo): encapsulate channel state handling #65

parent 3e0db1f3
Branches
Tags
No related merge requests found
......@@ -244,6 +244,7 @@ class GenericChannel:
self.logger.info(SU.pink(f"Fade in channel {self}"))
faded = self.mixer.select_channel(self, True)
selected = self.mixer.fade_in(self, volume)
self.mixer.set_active_channel(self)
return faded and selected
def fade_out(self, instant=False):
......@@ -264,6 +265,7 @@ class GenericChannel:
self.logger.info(SU.pink(f"Fade out channel {self}"))
faded = self.mixer.fade_out(self)
selected = self.mixer.select_channel(self, False)
self.mixer.set_active_channel(None)
return faded and selected
def roll(self, seconds_to_roll):
......
......@@ -96,22 +96,6 @@ class Mixer:
outputs = LU.json_to_dict(outputs)
return outputs
def set_active_channel_name(self, channel_name: ChannelName):
"""
Set the currently active channel.
TODO active channel state should be handled internally.
"""
self.active_channel_name = channel_name
def get_active_channel_name(self) -> ChannelName:
"""
Retrieve the currently active channel.
TODO active channel state should be handled internally.
"""
return self.active_channel_name
def get_channel(self, channel_name: ChannelName) -> GenericChannel:
"""
Retrieve a channel identified by name.
......@@ -122,12 +106,22 @@ class Mixer:
def get_active_channel(self) -> GenericChannel:
"""
Retrieve a channel identified by name.
Retrieve the currently active channel.
"""
if self.active_channel_name:
return self.channels.get(self.active_channel_name)
return self.get_channel(self.active_channel_name)
return None
def set_active_channel(self, channel: GenericChannel):
"""
Set the currently active channel.
"""
if channel:
self.active_channel_name = channel.name
else:
self.active_channel_name = None
self.logger.info(SU.pink("Reset active channel"))
def get_free_channel(self, channel_type: ChannelType) -> GenericChannel:
"""
Return any _free_ channel of the given type.
......@@ -330,7 +324,7 @@ class Mixer:
except ValueError:
idx = create_channel(name)
status = self.get_channel_status(idx)
msg = f"Channel #{idx} status: {status}"
msg = f"Channel {self.get_channel(name)} status: {status}"
self.logger.info(SU.pink(msg))
@private
......
......@@ -389,9 +389,6 @@ class Player:
instant = not (transition == Player.TransitionType.FADE)
entry.channel.fade_in(entry.volume, instant=instant)
# Update active channel
self.mixer.set_active_channel_name(entry.channel.name)
# Dispatch event
self.event_dispatcher.on_play(entry)
......@@ -403,8 +400,7 @@ class Player:
transition (Player.TransitionType): The type of transition to use e.g. fade-out
"""
with suppress(CoreConnectionError):
channel_name = self.mixer.get_active_channel_name()
channel = self.mixer.get_channel(channel_name)
channel = self.mixer.get_active_channel()
if not channel:
self.logger.info(SU.pink("Nothing to stop, no active channel"))
return
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment