Skip to content
Permalink
Browse files

add track to all sound commands in lisy v0.9

  • Loading branch information...
jabdoa2 committed Aug 30, 2019
1 parent c06deff commit 3058fc6c599ca2db8cd088520327493160480752
@@ -29,17 +29,17 @@ def play(self, settings, context, calling_context, priority=0, **kwargs):
if s['action'] == "stop":
sound_system.stop_all_sounds()
elif s['action'] == "play":
sound_system.play(item)
sound_system.play(item, s["track"])
elif s['action'] == "play_file":
sound_system.play_file(item, s.get("platform_options", {}))
sound_system.play_file(item, s.get("platform_options", {}), s["track"])
elif s['action'] == "text_to_speech":
sound_system.text_to_speech(item, s.get("platform_options", {}))
sound_system.text_to_speech(item, s.get("platform_options", {}), s["track"])
elif s['action'] == "set_volume":
sound_system.set_volume(float(item))
sound_system.set_volume(float(item), s["track"])
elif s['action'] == "increase_volume":
sound_system.increase_volume(float(item))
sound_system.increase_volume(float(item), s["track"])
elif s['action'] == "decrease_volume":
sound_system.decrease_volume(float(item))
sound_system.decrease_volume(float(item), s["track"])
else:
raise AssertionError("Invalid action {}".format(s['action']))

@@ -579,6 +579,7 @@ hardware_sound_systems:
hardware_sound_player:
__valid_in__: machine, mode, show
action: single|enum(play,play_file,text_to_speech,set_volume,increase_volume,decrease_volume,stop)|play
track: single|int|1
value: single|str|None
platform_options: single|dict|None
sound_system: single|machine(hardware_sound_systems)|default
@@ -1,5 +1,6 @@
"""Hardware sound system."""
import asyncio
from collections import defaultdict

from mpf.core.system_wide_device import SystemWideDevice
from mpf.platforms.interfaces.hardware_sound_platform_interface import HardwareSoundPlatformInterface
@@ -20,7 +21,7 @@ def __init__(self, machine, name):
super().__init__(machine, name)
self.hw_device = None # type: HardwareSoundPlatformInterface
self.platform = None
self._volume = 1.0
self._volume = defaultdict(lambda: 1.0)

@asyncio.coroutine
def _initialize(self):
@@ -30,33 +31,33 @@ def _initialize(self):
'hardware_sound_system', self.config['platform'])
self.hw_device = self.platform.configure_hardware_sound_system()

def play(self, sound_number: int):
def play(self, sound_number: int, track: int = 1):
"""Play a sound."""
self.hw_device.play_sound(sound_number)
self.hw_device.play_sound(sound_number, track)

def play_file(self, file: str, platform_options):
def play_file(self, file: str, platform_options, track: int = 1):
"""Play a sound file."""
self.hw_device.play_sound_file(file, platform_options)
self.hw_device.play_sound_file(file, platform_options, track)

def text_to_speech(self, text: str, platform_options):
def text_to_speech(self, text: str, platform_options, track: int = 1):
"""Text to speech output."""
self.hw_device.text_to_speech(text, platform_options)
self.hw_device.text_to_speech(text, platform_options, track)

def set_volume(self, volume: float):
def set_volume(self, volume: float, track: int = 1):
"""Set volume."""
self._volume = float(volume)
self.hw_device.set_volume(self._volume)
self._volume[track] = float(volume)
self.hw_device.set_volume(self._volume[track], track)

def increase_volume(self, volume: float):
def increase_volume(self, volume: float, track: int = 1):
"""Increase volume."""
self._volume += float(volume)
self.hw_device.set_volume(self._volume)
self._volume[track] += float(volume)
self.hw_device.set_volume(self._volume[track], track)

def decrease_volume(self, volume: float):
def decrease_volume(self, volume: float, track: int = 1):
"""Increase volume."""
self._volume -= float(volume)
self.hw_device.set_volume(self._volume)
self._volume[track] -= float(volume)
self.hw_device.set_volume(self._volume[track], track)

def stop_all_sounds(self):
"""Stop all sounds."""
self.hw_device.stop_all_sounds()
def stop_all_sounds(self, track: int = 1):
"""Stop all sounds on track."""
self.hw_device.stop_all_sounds(track)
@@ -9,26 +9,26 @@ class HardwareSoundPlatformInterface(metaclass=abc.ABCMeta):
__slots__ = []

@abc.abstractmethod
def play_sound(self, number: int):
def play_sound(self, number: int, track: int = 1):
"""Play a sound."""
raise NotImplementedError

@abc.abstractmethod
def play_sound_file(self, file: str, platform_options: dict):
def play_sound_file(self, file: str, platform_options: dict, track: int = 1):
"""Play a sound file."""
raise NotImplementedError

@abc.abstractmethod
def text_to_speech(self, text: str, platform_options: dict):
def text_to_speech(self, text: str, platform_options: dict, track: int = 1):
"""Text to speech output."""
raise NotImplementedError

@abc.abstractmethod
def set_volume(self, volume: float):
def set_volume(self, volume: float, track: int = 1):
"""Set volume."""
raise NotImplementedError

@abc.abstractmethod
def stop_all_sounds(self):
def stop_all_sounds(self, track: int = 1):
"""Play a sound."""
raise NotImplementedError
@@ -204,32 +204,49 @@ def __init__(self, platform):
"""Initialise hardware sound."""
self.platform = platform # type: LisyHardwarePlatform

def play_sound(self, number: int):
def play_sound(self, number: int, track: int = 1):
"""Play sound with number."""
self.platform.send_byte(LisyDefines.SoundPlaySound, bytes([number]))
if self.platform.api_version >= StrictVersion("0.9"):
self.platform.send_byte(LisyDefines.SoundPlaySound, bytes([track, number]))
else:
assert track == 1
self.platform.send_byte(LisyDefines.SoundPlaySound, bytes([number]))

def play_sound_file(self, file: str, platform_options: dict):
def play_sound_file(self, file: str, platform_options: dict, track: int = 1):
"""Play sound file."""
flags = 1 if platform_options.get("loop", False) else 0
flags += 2 if platform_options.get("no_cache", False) else 0
self.platform.send_string(LisyDefines.SoundPlaySoundFile, chr(flags) + file)
if self.platform.api_version >= StrictVersion("0.9"):
self.platform.send_string(LisyDefines.SoundPlaySoundFile, chr(track) + chr(flags) + file)
else:
assert track == 1
self.platform.send_string(LisyDefines.SoundPlaySoundFile, chr(flags) + file)

def text_to_speech(self, text: str, platform_options: dict):
def text_to_speech(self, text: str, platform_options: dict, track: int = 1):
"""Text to speech."""
flags = 1 if platform_options.get("loop", False) else 0
flags += 2 if platform_options.get("no_cache", False) else 0
self.platform.send_string(LisyDefines.SoundTextToSpeech, chr(flags) + text)
if self.platform.api_version >= StrictVersion("0.9"):
self.platform.send_string(LisyDefines.SoundTextToSpeech, chr(track) + chr(flags) + text)
else:
assert track == 1
self.platform.send_string(LisyDefines.SoundTextToSpeech, chr(flags) + text)

def set_volume(self, volume: float):
def set_volume(self, volume: float, track: int = 1):
"""Set volume."""
if self.platform.api_version >= StrictVersion("0.9"):
self.platform.send_byte(LisyDefines.SoundSetVolume, bytes([1, int(volume * 100)]))
self.platform.send_byte(LisyDefines.SoundSetVolume, bytes([track, int(volume * 100)]))
else:
assert track == 1
self.platform.send_byte(LisyDefines.SoundSetVolume, bytes([int(volume * 100)]))

def stop_all_sounds(self):
def stop_all_sounds(self, track: int = 1):
"""Stop all sounds."""
self.platform.send_byte(LisyDefines.SoundStopAllSounds)
if self.platform.api_version >= StrictVersion("0.9"):
self.platform.send_byte(LisyDefines.SoundStopAllSounds, bytes([track]))
else:
assert track == 1
self.platform.send_byte(LisyDefines.SoundStopAllSounds)


# pylint: disable-msg=too-many-instance-attributes
@@ -322,19 +322,19 @@ def __init__(self) -> None:
self.playing = None
self.volume = None

def play_sound(self, number: int):
def play_sound(self, number: int, track: int = 1):
"""Play virtual sound."""
self.playing = number

def play_sound_file(self, file: str, platform_options: dict):
def play_sound_file(self, file: str, platform_options: dict, track: int = 1):
"""Play a sound file."""
self.playing = file

def text_to_speech(self, text: str, platform_options: dict):
def text_to_speech(self, text: str, platform_options: dict, track: int = 1):
"""Text to speech output."""
self.playing = text

def set_volume(self, volume: float):
def set_volume(self, volume: float, track: int = 1):
"""Set volume."""
self.volume = volume

@@ -86,6 +86,10 @@ hardware_sound_player:
test2:
2:
action: play
test4:
5:
track: 2
action: play
play_file:
"some_file": play_file
play_file_loop:
@@ -503,6 +503,86 @@ def test_platform(self):
}
self._wait_for_processing()

# test sound
self.serialMock.expected_commands = {
b'\x32\x01\x02': None
}
self.post_event("test2")
self._wait_for_processing()
self.assertFalse(self.serialMock.expected_commands)

# test sound on track 2
self.serialMock.expected_commands = {
b'\x32\x02\x05': None
}
self.post_event("test4")
self._wait_for_processing()
self.assertFalse(self.serialMock.expected_commands)

# test sound file
self.serialMock.expected_commands = {
b'\x34\x01\x00some_file\x00': None
}
self.post_event("play_file")
self._wait_for_processing()
self.assertFalse(self.serialMock.expected_commands)

# test sound file looping
self.serialMock.expected_commands = {
b'\x34\x01\x01some_file\x00': None
}
self.post_event("play_file_loop")
self._wait_for_processing()
self.assertFalse(self.serialMock.expected_commands)

# text to speech
self.serialMock.expected_commands = {
b'\x35\x01\x02Hello MPF\x00': None
}
self.post_event("play_text")
self._wait_for_processing()
self.assertFalse(self.serialMock.expected_commands)

# set volume to 50 (32 hex)
self.serialMock.expected_commands = {
b'\x36\x01\x32': None
}
self.post_event("volume_05")
self._wait_for_processing()
self.assertFalse(self.serialMock.expected_commands)

# increase volume by 0.1 -> 60 -> hex 3C
self.serialMock.expected_commands = {
b'\x36\x01\x3C': None
}
self.post_event("increase_volume")
self._wait_for_processing()
self.assertFalse(self.serialMock.expected_commands)

# decrease volume by 0.01 -> 59 -> hex 3B
self.serialMock.expected_commands = {
b'\x36\x01\x3B': None
}
self.post_event("decrease_volume")
self._wait_for_processing()
self.assertFalse(self.serialMock.expected_commands)

# test another sound
self.serialMock.expected_commands = {
b'\x32\x01\x03': None
}
self.post_event("test3")
self._wait_for_processing()
self.assertFalse(self.serialMock.expected_commands)

# stop sound
self.serialMock.expected_commands = {
b'\x33\01': None
}
self.post_event("test_stop")
self._wait_for_processing()
self.assertFalse(self.serialMock.expected_commands)

def test_rules(self):
"""Test HW Rules."""
self.serialMock.expected_commands = {

0 comments on commit 3058fc6

Please sign in to comment.
You can’t perform that action at this time.