Пример #1
0
    def __init__(self, chunk=1024):
        """コンストラクタ

        Keyword Arguments:
            chunk {int} -- バッファサイズ (default: {1024})
        """
        self.chunk = chunk
        self.ac = AudioController()
        self.buffer = np.zeros((self.chunk), dtype=np.float32)
        self.window_func = np.hamming(self.chunk)
        self.osc = OscClient("localhost", 5000)
        self.separate_results = None
        self.timbers = None
        self.separate_flag = True
        self.sep_thread = None
        self.osc_thread = None
Пример #2
0
    def __init__(self):
        channels = 10
        self.audio_controller = AudioController(8)
        self.board = Board(self.audio_controller)
        self.board_size = self.board.board_size
        self.board_size_px = BOARD_SIZE_PX
        self.tile_size = BOARD_SIZE_PX[0] // BOARD_SIZE[0], BOARD_SIZE_PX[1] // BOARD_SIZE[1]
        # Define Handler
        self.pygame_handler = PygameHandler(self.tile_size)

        # Animation
        self.lcm = self.pygame_handler.lcm
        self.animation_cnt = 0
        # Highlighter
        self.old_loc = None
        self.hloc = [0, 0]
        self.curr_type = 0
        self.flower_idx = 0
        # Menu
        base_loc = BORDER_WIDTH + self.board_size[0] * self.tile_size[0], BORDER_WIDTH + self.tile_size[1]
        self.type_locations = [(base_loc[0], base_loc[1] + self.tile_size[0] * i) for i in range(len(FLOWERS))]
        self.cost_locations = [(loc[0] + self.tile_size[0] + BORDER_WIDTH, loc[1] +
                                self.tile_size[0] / 4.0) for loc in self.type_locations]
        self.bottom_loc = BORDER_WIDTH, BORDER_WIDTH + self.board_size[1] * self.tile_size[1]
        self.seed_loc = BORDER_WIDTH + self.board_size[0] * self.tile_size[0], BORDER_WIDTH
        # Game Stuff
        self.round_started = False
        self.round = 0
        self.steps_until_lava = 15
        self.steps = 0
        self.lose = False
        self.dir_dict = get_dir_dict()
        # Set tiles for initial board
        self.board.set_tile(3, (self.board_size[0]//2, self.board_size[1]//2))
        self.seeds = 20
        self.costs = [5, 5, 10, 10, 10, 10, 20]
        self.seed_prod = 5
Пример #3
0
class Server:
    audio_controller = AudioController()
    communicator = Communicator()
    mouse_controller = MouseController()
    keyboard_controller = KeyboardController()

    def __init__(self):
        self.connection = self.init_connection()
        self.handle_connection()

    @staticmethod
    def init_connection():
        host = socket.gethostname()
        connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        connection.bind((host, Constants.PORT_NR))
        return connection

    def handle_connection(self):
        self.start_listen()
        try:
            while True:
                message_str = self.communicator.receive_message()
                message = Message.from_string(message_str)
                self.read_message(message)
        except (ConnectionResetError, ConnectionAbortedError,
                TimeoutError) as e:
            self.handle_communication_error(e)

    def start_listen(self):
        self.connection.listen(1)
        client_socket, address = self.connection.accept()
        self.communicator.set_client_socket(client_socket)
        print("waiting for data...")

    def handle_communication_error(self, error):
        pass
        print(str(error))
        print('Connection broken, waiting for host reconnection...')
        self.handle_connection()

    def read_message(self, message):
        if not message.command:
            self.handle_empty_command()
        elif message.command == Constants.COMMAND_PING:
            self.handle_ping_command()
        elif message.command == Constants.COMMAND_SHUTDOWN_NOW:
            self.handle_shutdown_now_command()
        elif message.command == Constants.COMMAND_SCHEDULED_SHUTDOWN:
            self.handle_scheduled_shutdown_command(message.params)
        elif message.command == Constants.COMMAND_ABORT_SHUTDOWN:
            self.handle_abort_shutdown_command()
        elif message.command == Constants.COMMAND_RESTART:
            self.handle_restart_command()
        elif message.command == Constants.COMMAND_SET_VOLUME:
            self.handle_set_volume_command(message.params)
        elif message.command == Constants.COMMAND_GET_VOLUME:
            self.handle_get_volume_command()
        elif message.command == Constants.COMMAND_MUTE:
            self.handle_mute_command()
        elif message.command == Constants.COMMAND_UNMUTE:
            self.handle_unmute_command()
        elif message.command == Constants.COMMAND_MOUSE_MOVE:
            self.handle_mouse_move_command(message.params)
        elif message.command == Constants.COMMAND_MOUSE_LEFT_CLICK:
            self.handle_mouse_left_click_command()
        elif message.command == Constants.COMMAND_MOUSE_RIGHT_CLICK:
            self.handle_mouse_right_click_command()
        elif message.command == Constants.COMMAND_KEYBOARD_FETCH_KEY_STATE:
            self.handle_keyboard_fetch_state()
        elif message.command == Constants.COMMAND_KEYBOARD_SPECIAL_KEY:
            self.handle_keyboard_special_key_command(message.params)
        elif message.command == Constants.COMMAND_KEYBOARD_REGULAR_KEY:
            self.handle_keyboard_regular_key_command(message.params)
        else:
            self.handle_unknown_command()

    @staticmethod
    def handle_empty_command():
        print('no data, restarting...')
        raise ConnectionAbortedError

    def handle_unknown_command(self):
        print('unknown command')
        self.communicator.send_unsuccessful_response()

    def handle_ping_command(self):
        print("ping")
        self.communicator.send_successful_response([Constants.FEEDBACK_PONG])

    def handle_shutdown_now_command(self):
        print("shut system down")
        self.communicator.send_successful_response()
        os.system(WindowsCommands.SHUTDOWN_NOW)

    def handle_scheduled_shutdown_command(self, params):
        print("shut system down with timeout")
        if len(params) == 1:
            os.system(WindowsCommands.SHUTDOWN_ABORT)
            os.system(WindowsCommands.SCHEDULE_SHUTDOWN + params[0])
            self.communicator.send_successful_response()
        else:
            self.communicator.send_unsuccessful_response()

    def handle_abort_shutdown_command(self):
        print("system shutdown aborted")
        self.communicator.send_successful_response()
        os.system(WindowsCommands.SHUTDOWN_ABORT)

    def handle_restart_command(self):
        print("system restart")
        self.communicator.send_successful_response()
        os.system(WindowsCommands.RESTART)

    def handle_set_volume_command(self, params):
        print("set volume")
        if len(params) == 1:
            level = int(params[0])
            self.audio_controller.set_volume(level)
            self.communicator.send_successful_response()
        else:
            self.communicator.send_unsuccessful_response()

    def handle_get_volume_command(self):
        print("get volume")
        current_volume = self.audio_controller.get_volume()
        is_muted = self.audio_controller.is_muted()
        self.communicator.send_successful_response([current_volume, is_muted])

    def handle_mute_command(self):
        print("mute")
        self.audio_controller.mute()
        self.communicator.send_successful_response()

    def handle_unmute_command(self):
        print("unmute")
        self.audio_controller.unmute()
        self.communicator.send_successful_response()

    def handle_mouse_move_command(self, params):
        print("mouse move")
        if len(params) == 2:
            x_offset = float(params[0])
            y_offset = float(params[1])
            self.mouse_controller.move(x_offset, y_offset)
        self.communicator.send_successful_response()

    def handle_mouse_left_click_command(self):
        print("mouse left click")
        self.mouse_controller.left_click()
        self.communicator.send_successful_response()

    def handle_mouse_right_click_command(self):
        print("mouse right click")
        self.mouse_controller.right_click()
        self.communicator.send_successful_response()

    def handle_keyboard_special_key_command(self, params):
        print("keyboard key")
        if len(params) == 1:
            key = params[0]
            self.keyboard_controller.press_special_key(key)
        self.communicator.send_successful_response()

    def handle_keyboard_regular_key_command(self, params):
        print("keyboard key regular")
        if len(params) == 1:
            key = params[0]
            self.keyboard_controller.writeChar(key)
        self.communicator.send_successful_response()

    def handle_keyboard_fetch_state(self):
        print("keyboard fetch state")
        state = self.keyboard_controller.get_keys_state()
        self.communicator.send_successful_response([state])
Пример #4
0
class MainMenu(ImcTabview):

    _keyHandledMextId = False

    def _keyHandler(self, cmdList):
        #cmdList is a list of dictonaries containing the command being executed
        #cmdList can be specified as None in control tree which means nothing to do
        if cmdList is None:
            return

        for cmd in cmdList:
            #Check if cmd is also a list, if so recursively we will execute
            if isinstance(cmd, list):
                self._keyHandler(cmd)
                continue

            #last entry, this will stop execution
            if 'nextid' in cmd and not self._keyHandledMextId:
                self._keyHandledMextId = True
                self.curId = cmd['nextid']
                return

            if not all(k in cmd for k in ("id", "func")):
                cmd = "_keyHandler: You did not specify id/func for tree elemnt "
                cmd = cmd + "with id = {} ".format(self.curId)
                logging.error(cmd)
                return

            tmpId = cmd['id']
            func = cmd['func']

            #args attribute is optional, can be used when we want to pass something to callback
            args = {}
            if 'args' in cmd:
                args = cmd['args']

            #Execute build in fucntions/object functions
            ret = getattr(self.selectableWidgets[tmpId], func)(args)

            if ret and 'true' in cmd:  #execute ret functions if specified
                self._keyHandler(cmd['true'])

            if not ret and 'false' in cmd:  #execute ret functions if specified
                self._keyHandler(cmd['false'])

    def _powerOffShowMenu(self):
        includes.screenSaver.disable()
        self.root.current = "shutdown"
        self.selectableWidgets[selectId['powermenu']].lastId = self.curId
        self.curId = selectId['powermenu']

    def _globalKeyHandler(self, keycode):
        """Key handler for global keys like volume up / volume down /mute etc."""

        if keycode[1] == "pgdown":
            if includes.playerCore.isPlaying():
                vol = self.audioController.getVolume()
                self.audioController.audioFadeout(10, 12, 15, 60, 40, 0.1)
                self.playerControl.stop(None)
                self.audioController.setVolume(vol)

            return

        if keycode[1] == "volumeup":
            self.audioController.volumeUp()
            return

        if keycode[1] == "volumedown":
            self.audioController.volumeDown()
            return

        if keycode[1] == "mute":
            self.audioController.mute()
            return

        if keycode[1] == "power":
            if not includes.playerCore.isPlaying():
                if self.root.current != "shutdown":
                    self._powerOffShowMenu()
                return

    # Callback function triggered by the key handler. This is triggered when
    #keyboard key is pressed.
    def _keyDown(self, keycode):
        '''Callback function for keyboard events. All key handling is done here.'''
        ret = self.keyDownSemaphore.acquire(False)

        msg = "Menu: Key Pressed [{}] ".format(keycode)
        msg = msg + "on element with curId = {}".format(self.curId)
        logging.debug(msg)

        if self.curId != selectId['osd']:
            if self.screenSaver.active and self.screenSaver.ena:
                self.screenSaver.resetTime()
                self.keyDownSemaphore.release()
                return 0
            self.screenSaver.resetTime()

        self._globalKeyHandler(keycode)

        if keycode[1] in self.controlTree[self.curId]:
            self._keyHandledMextId = False  #prepare keyHandler function
            self._keyHandler(self.controlTree[self.curId][keycode[1]])
            self.keyDownSemaphore.release()
            return 0

        self.keyDownSemaphore.release()
        return -1

    # Osd related things
    #
    def osdDisable(self, args):
        '''Switch control back to last element after OSD was displayed'''
        if self.lastId > 0:
            self.curId = self.lastId

    def osdEnable(self, mode):
        '''Before switching to the OSD remember the current selected element'''
        if self.lastId < 0:
            self.lastId = self.curId
        self.curId = selectId[mode]

    def osdControl(self, mode):
        if mode == 0:
            self.lastId = self.curId
        elif mode == 1:
            self.curId = selectId['osd']
        elif mode == 2:
            self.curId = selectId['playerCore']
        elif mode == 3:
            self.curId = self.lastId

    @_addJsonSystemcall("_cmdMuteToggle")
    def _cmdMuteToggle(self, args):
        self.selectableWidgets[selectId['osd']].enable(None)
        self.ipc.sendCmd({'cmd': {
            'func': 'muteToggle'
        }}, includes.config['ipcOsdPort'])

    def _server(self):
        ip = includes.config['httpServerIp']['ip']
        port = includes.config['httpServerIp']['port']
        self.httpd = http.server.HTTPServer((ip, int(port)), server.WebServer)

        try:
            self.httpd.serve_forever()
        except KeyboardInterrupt:
            pass
        finally:
            self.httpd.server_close()

    def clearValues(self, args):
        self.menuSystem.clearValues()

    def __init__(self, **kwargs):
        self.selectableWidgets = {}
        self.keyDownSemaphore = threading.Semaphore()
        self.curId = 0
        self.lastId = 0
        self.controlTree = control_tree.CONTROL_TREE

        super(MainMenu, self).__init__(**kwargs)

        #setup the screen saver and also make it available as global object
        self.screenSaver = ScreenSaver(self.root, "blackscreen", "main_menu")
        includes.screenSaver = self.screenSaver

        #Setup the player core as global object
        includes.playerCore = PlayerCore(
            runtimeInterval=includes.config['settings']['runtimeInterval'],
            writeDb=includes.writeDb,
            db=includes.db)
        includes.playerCore.setPlayer(includes.config['settings']['player'])
        includes.playerCore.activateColorBar = includes.changeFooterColor
        includes.playerCore.screenSaverRun = self.screenSaver.swtich
        # includes.playerCore.osdEnable = self.osdEnable
        # includes.playerCore.osdDisable = self.osdDisable
        includes.playerCore.osdControl = self.osdControl
        self.selectableWidgets[selectId['playerCore']] = includes.playerCore

        #Shutdown screen setup
        self.menuShutdown = MenuShutdown(screenManager=self.root)
        self.shutdownScreen = Screen(name="shutdown")
        self.shutdownScreen.add_widget(self.menuShutdown)
        self.root.add_widget(self.shutdownScreen)
        self.selectableWidgets[selectId['powermenu']] = self.menuShutdown

        #setup key handler
        self.keyHandler = KeyHandler()
        self.keyHandler.onPress = self._keyDown

        #System Menu setup
        self.menuSystem = MenuSystem(mainMenu=self)
        self.menuSystem.callbackPlaySingle = includes.playerCore.startSingle
        self.setContent(selectId['system'], self.menuSystem)

        self.selectableWidgets[selectId['systemMsg']] = self.menuSystem.handler

        #Video FIle menu
        self.menuVideo = MenuVideoAudio(
            #id="0",
            rootdir=includes.config['video']['rootdir'],
            enaColor=includes.styles['enaColor0'],
            bar_width=0,
            supportedTypes=includes.config['video']['types'],
            type="video",
            playerStartHandler=includes.playerCore.startPlaylist,
            line_color=includes.colors['imcBlue'],
            text="Video Files")
        self.setContent(selectId['videos'], self.menuVideo)

        #music File menu
        self.menuMusic = MenuVideoAudio(
            #id="0",
            rootdir=includes.config['music']['rootdir'],
            enaColor=includes.styles['enaColor0'],
            bar_width=0,
            supportedTypes=includes.config['music']['types'],
            type="music",
            playerStartHandler=includes.playerCore.startPlaylist,
            line_color=includes.colors['imcBlue'],
            text="Music Files")
        self.setContent(selectId['music'], self.menuMusic)

        #playlist menu
        self.menuPlaylist = PlaylistMenu(
            line_color_playlist=includes.colors['imcBlue'],
            line_color_media=includes.colors['imcLigthGray'],
            highlight_color=includes.styles['enaColor0'],
            line_height=6,
            bar_width=35,
            font_size=includes.styles['fontSize'],
            rootdir=includes.config['playlist']['rootdir'],
            supported_types=includes.config['playlist']['types'],
            player_start_handler=includes.playerCore.startPlaylist,
        )
        self.setContent(selectId['playlist'], self.menuPlaylist)

        #Setup the settings screen
        self.menuSettings = MenuSettings()
        self.setContent(selectId['settings'], self.menuSettings)

        self.update(None)

        #audio controller
        self.audioController = AudioController(
            includes.config['settings']['volIncVal'], self.volumeIndicator,
            False)

        vol = self.audioController.getVolume()

        _systemCallbacks['getVolume'] = self.audioController._getVolume
        _systemCallbacks['setVolume'] = self.audioController.setVolume

        self.volumeIndicator.value = vol

        #Setup the selectable widget object for key handling
        self.selectableWidgets[selectId['settings']] = self.getMenuBtn(
            selectId['settings'])
        self.selectableWidgets[selectId['videos']] = self.getMenuBtn(
            selectId['videos'])
        self.selectableWidgets[selectId['music']] = self.getMenuBtn(
            selectId['music'])
        self.selectableWidgets[selectId['playlist']] = self.getMenuBtn(
            selectId['playlist'])
        self.selectableWidgets[selectId['system']] = self.getMenuBtn(
            selectId['system'])
        self.selectableWidgets[selectId['root']] = self
        self.selectableWidgets[selectId['settingsMenu']] = self.menuSettings
        self.selectableWidgets[selectId['vFiles']] = self.menuVideo
        self.selectableWidgets[selectId['mFiles']] = self.menuMusic
        self.selectableWidgets[selectId['pFiles']] = self.menuPlaylist
        self.selectableWidgets[selectId['osd']] = OsdController()
        self.selectableWidgets[selectId['audioCtrl']] = self.audioController

        includes.playerCore.addEndHandler(
            self.selectableWidgets[selectId['osd']].videoEnd)

        #Setup the server
        self.serverThread = threading.Thread(target=self._server)
        self.serverThread.setDaemon(True)
        self.serverThread.start()

        #prepare system for execution....
        self.screenSaver.enable()
        self.lastId = -1

        #Try to enable the start widget
        try:
            self.selectableWidgets[self.curId].enable(None)
        except Exception as allExceptions:
            logging.error("Menu: cannot find default widget...")
Пример #5
0
    def __init__(self, **kwargs):
        self.selectableWidgets = {}
        self.keyDownSemaphore = threading.Semaphore()
        self.curId = 0
        self.lastId = 0
        self.controlTree = control_tree.CONTROL_TREE

        super(MainMenu, self).__init__(**kwargs)

        #setup the screen saver and also make it available as global object
        self.screenSaver = ScreenSaver(self.root, "blackscreen", "main_menu")
        includes.screenSaver = self.screenSaver

        #Setup the player core as global object
        includes.playerCore = PlayerCore(
            runtimeInterval=includes.config['settings']['runtimeInterval'],
            writeDb=includes.writeDb,
            db=includes.db)
        includes.playerCore.setPlayer(includes.config['settings']['player'])
        includes.playerCore.activateColorBar = includes.changeFooterColor
        includes.playerCore.screenSaverRun = self.screenSaver.swtich
        # includes.playerCore.osdEnable = self.osdEnable
        # includes.playerCore.osdDisable = self.osdDisable
        includes.playerCore.osdControl = self.osdControl
        self.selectableWidgets[selectId['playerCore']] = includes.playerCore

        #Shutdown screen setup
        self.menuShutdown = MenuShutdown(screenManager=self.root)
        self.shutdownScreen = Screen(name="shutdown")
        self.shutdownScreen.add_widget(self.menuShutdown)
        self.root.add_widget(self.shutdownScreen)
        self.selectableWidgets[selectId['powermenu']] = self.menuShutdown

        #setup key handler
        self.keyHandler = KeyHandler()
        self.keyHandler.onPress = self._keyDown

        #System Menu setup
        self.menuSystem = MenuSystem(mainMenu=self)
        self.menuSystem.callbackPlaySingle = includes.playerCore.startSingle
        self.setContent(selectId['system'], self.menuSystem)

        self.selectableWidgets[selectId['systemMsg']] = self.menuSystem.handler

        #Video FIle menu
        self.menuVideo = MenuVideoAudio(
            #id="0",
            rootdir=includes.config['video']['rootdir'],
            enaColor=includes.styles['enaColor0'],
            bar_width=0,
            supportedTypes=includes.config['video']['types'],
            type="video",
            playerStartHandler=includes.playerCore.startPlaylist,
            line_color=includes.colors['imcBlue'],
            text="Video Files")
        self.setContent(selectId['videos'], self.menuVideo)

        #music File menu
        self.menuMusic = MenuVideoAudio(
            #id="0",
            rootdir=includes.config['music']['rootdir'],
            enaColor=includes.styles['enaColor0'],
            bar_width=0,
            supportedTypes=includes.config['music']['types'],
            type="music",
            playerStartHandler=includes.playerCore.startPlaylist,
            line_color=includes.colors['imcBlue'],
            text="Music Files")
        self.setContent(selectId['music'], self.menuMusic)

        #playlist menu
        self.menuPlaylist = PlaylistMenu(
            line_color_playlist=includes.colors['imcBlue'],
            line_color_media=includes.colors['imcLigthGray'],
            highlight_color=includes.styles['enaColor0'],
            line_height=6,
            bar_width=35,
            font_size=includes.styles['fontSize'],
            rootdir=includes.config['playlist']['rootdir'],
            supported_types=includes.config['playlist']['types'],
            player_start_handler=includes.playerCore.startPlaylist,
        )
        self.setContent(selectId['playlist'], self.menuPlaylist)

        #Setup the settings screen
        self.menuSettings = MenuSettings()
        self.setContent(selectId['settings'], self.menuSettings)

        self.update(None)

        #audio controller
        self.audioController = AudioController(
            includes.config['settings']['volIncVal'], self.volumeIndicator,
            False)

        vol = self.audioController.getVolume()

        _systemCallbacks['getVolume'] = self.audioController._getVolume
        _systemCallbacks['setVolume'] = self.audioController.setVolume

        self.volumeIndicator.value = vol

        #Setup the selectable widget object for key handling
        self.selectableWidgets[selectId['settings']] = self.getMenuBtn(
            selectId['settings'])
        self.selectableWidgets[selectId['videos']] = self.getMenuBtn(
            selectId['videos'])
        self.selectableWidgets[selectId['music']] = self.getMenuBtn(
            selectId['music'])
        self.selectableWidgets[selectId['playlist']] = self.getMenuBtn(
            selectId['playlist'])
        self.selectableWidgets[selectId['system']] = self.getMenuBtn(
            selectId['system'])
        self.selectableWidgets[selectId['root']] = self
        self.selectableWidgets[selectId['settingsMenu']] = self.menuSettings
        self.selectableWidgets[selectId['vFiles']] = self.menuVideo
        self.selectableWidgets[selectId['mFiles']] = self.menuMusic
        self.selectableWidgets[selectId['pFiles']] = self.menuPlaylist
        self.selectableWidgets[selectId['osd']] = OsdController()
        self.selectableWidgets[selectId['audioCtrl']] = self.audioController

        includes.playerCore.addEndHandler(
            self.selectableWidgets[selectId['osd']].videoEnd)

        #Setup the server
        self.serverThread = threading.Thread(target=self._server)
        self.serverThread.setDaemon(True)
        self.serverThread.start()

        #prepare system for execution....
        self.screenSaver.enable()
        self.lastId = -1

        #Try to enable the start widget
        try:
            self.selectableWidgets[self.curId].enable(None)
        except Exception as allExceptions:
            logging.error("Menu: cannot find default widget...")
Пример #6
0
class Controller:

    def __init__(self):
        channels = 10
        self.audio_controller = AudioController(8)
        self.board = Board(self.audio_controller)
        self.board_size = self.board.board_size
        self.board_size_px = BOARD_SIZE_PX
        self.tile_size = BOARD_SIZE_PX[0] // BOARD_SIZE[0], BOARD_SIZE_PX[1] // BOARD_SIZE[1]
        # Define Handler
        self.pygame_handler = PygameHandler(self.tile_size)

        # Animation
        self.lcm = self.pygame_handler.lcm
        self.animation_cnt = 0
        # Highlighter
        self.old_loc = None
        self.hloc = [0, 0]
        self.curr_type = 0
        self.flower_idx = 0
        # Menu
        base_loc = BORDER_WIDTH + self.board_size[0] * self.tile_size[0], BORDER_WIDTH + self.tile_size[1]
        self.type_locations = [(base_loc[0], base_loc[1] + self.tile_size[0] * i) for i in range(len(FLOWERS))]
        self.cost_locations = [(loc[0] + self.tile_size[0] + BORDER_WIDTH, loc[1] +
                                self.tile_size[0] / 4.0) for loc in self.type_locations]
        self.bottom_loc = BORDER_WIDTH, BORDER_WIDTH + self.board_size[1] * self.tile_size[1]
        self.seed_loc = BORDER_WIDTH + self.board_size[0] * self.tile_size[0], BORDER_WIDTH
        # Game Stuff
        self.round_started = False
        self.round = 0
        self.steps_until_lava = 15
        self.steps = 0
        self.lose = False
        self.dir_dict = get_dir_dict()
        # Set tiles for initial board
        self.board.set_tile(3, (self.board_size[0]//2, self.board_size[1]//2))
        self.seeds = 20
        self.costs = [5, 5, 10, 10, 10, 10, 20]
        self.seed_prod = 5

    def start_round(self):
        if self.round_started:
            return 0
        self.round_started = True
        self.audio_controller.start_threat()
        self.board.threaten_lava_outbreak(ROUNDS[self.round][1])
        return 1

    def check_in_screen(self, pos):
        return (pos[0] < 0 or pos[0] > TOTAL_SIZE[0]) or (pos[1] < 0 or pos[1] > TOTAL_SIZE[1])

    def check_in_board(self, pos):
        return (pos[0] < self.board_size_px[0]) and (pos[1] < self.board_size_px[1])

    def check_in_sidebar(self, pos):
        tl = self.type_locations[0]
        in_x = (pos[0] >= tl[0] and pos[0] < (tl[0] + self.tile_size[0]))
        in_y = (pos[1] >= tl[1] and pos[1] < (tl[0] + len(FLOWERS) * self.tile_size[0]))
        return in_x and in_y

    def handle_mouse(self, pos):
        # Check bounds
        if self.check_in_screen(pos):
            return
        elif self.check_in_board(pos):
            self.hloc = [pos[0] // self.tile_size[0], pos[1] // self.tile_size[1]]

    def click(self, pos):
        if self.check_in_screen(pos):
            return
        elif self.check_in_board(pos):
            self.hloc = [pos[0] // self.tile_size[0], pos[1] // self.tile_size[1]]
            self.enter()
        elif self.check_in_sidebar(pos):
            tl = self.type_locations[0]
            self.flower_idx = (pos[1] - tl[1]) // self.tile_size[1]

    def step(self):
        self.board.step()
        shake = 0
        if self.round_started:
            if self.steps == self.steps_until_lava:
                self.audio_controller.start_erupt()
                shake = 1
                self.board.lava_outbreak(ROUNDS[self.round][0])
            self.steps += 1
            if not np.max(self.board.board == 1) and not np.max(self.board.threaten_tiles):
                self.round_started = False
                self.steps = 0
                self.seeds += self.seed_prod * np.sum(self.board.board == 3)
                self.board.delete_ice()
                self.round += 1
        # Check lose
        if not np.max(self.board.board == 3):
            self.lose = True
        return shake

    def set_tile(self, tile):
        self.board.set_tile(tile, self.hloc)

    def move_highlight(self, direction):
        self.hloc[0] = (self.hloc[0] + direction[0]) % self.board_size[0]
        self.hloc[1] = (self.hloc[1] + direction[1]) % self.board_size[1]

    def toggle_inv(self):
        self.flower_idx = (self.flower_idx + 1) % len(FLOWERS)

    def enter(self):
        if self.seeds < self.costs[self.flower_idx]:
            return
        c_type = self.board.board[self.hloc[0], self.hloc[1]]
        flower = FLOWERS[self.flower_idx]
        idx = TYPES.index(flower)
        if c_type == 1 or c_type == idx:
            return
        self.audio_controller.play_start(idx)
        self.seeds -= self.costs[self.flower_idx]
        self.set_tile(idx)

    def step_animate(self):
        self.animation_cnt = (self.animation_cnt + 1) % self.lcm

    def render(self, screen):
        # Render board
        for i in range(self.board_size[0]):
            for j in range(self.board_size[1]):
                lay = ["Soil", None, None]
                type = TYPES[self.board.board[i, j]]
                if type in BACKGROUNDS:
                    lay[0] = type
                if self.board.threaten_tiles[i, j]:
                    lay[1] = "Threat"
                if type in TOP:
                    lay[2] = type

                all_dir = 0
                back = lay[0]
                for i2, cdir in enumerate([(0, -1), (1, 0), (0, 1), (-1, 0)][::-1]):
                    ni, nj = i + cdir[0], j + cdir[1]
                    if ni < 0 or ni >= self.board_size[0] or nj < 0 or nj >= self.board_size[1]:
                        continue
                    nt = TYPES[self.board.board[ni, nj]]
                    if nt not in BACKGROUNDS:
                        nt = "Soil"
                    if nt == back:
                        all_dir += 2**i2

                image_info = self.dir_dict[all_dir]
                tl_location = i * self.tile_size[0], j * self.tile_size[1]
                self.pygame_handler.render_tile(
                    screen, lay, image_info, tl_location, self.animation_cnt)

        # Render Sidebar
        for i, l in enumerate(self.type_locations):
            lay = ["Soil", None, None]
            lay[2] = FLOWERS[i]
            self.pygame_handler.render_tile(screen, lay, (0, 0), l, self.animation_cnt)

        tl_location = self.tile_size[0] * self.hloc[0], self.tile_size[1] * self.hloc[1]
        self.pygame_handler.render_highlight(screen, tl_location)

        # Render Seeds
        self.pygame_handler.render_text(screen, f"Seeds: {self.seeds}", self.seed_loc)
        self.pygame_handler.render_text(screen, f"Round: {self.round}",
                                        (self.seed_loc[0], self.seed_loc[1] + BORDER_WIDTH))
        for i, c in enumerate(self.costs):
            self.pygame_handler.render_text(screen, f"{c}", self.cost_locations[i])
        self.pygame_handler.render_text(
            screen, "Don't lose all your prized flowers!", self.bottom_loc)
        t = "r = restart" if self.lose else "s = Start Round    a = Speed Up"
        self.pygame_handler.render_text(
            screen, t, (self.bottom_loc[0], self.bottom_loc[1] + self.tile_size[1]//2))

        if self.lose:
            self.pygame_handler.render_title(screen, "You didn't keep it alive...", self.board_size_px)

        self.pygame_handler.render_select(screen, self.type_locations[self.flower_idx])
Пример #7
0
class RealTimeAudioSeparator(object):
    def __init__(self, chunk=1024):
        """コンストラクタ

        Keyword Arguments:
            chunk {int} -- バッファサイズ (default: {1024})
        """
        self.chunk = chunk
        self.ac = AudioController()
        self.buffer = np.zeros((self.chunk), dtype=np.float32)
        self.window_func = np.hamming(self.chunk)
        self.osc = OscClient("localhost", 5000)
        self.separate_results = None
        self.timbers = None
        self.separate_flag = True
        self.sep_thread = None
        self.osc_thread = None

    def calc_spectrum(self, data):
        """スペクトルを計算する

        Arguments:
            data {numpy.array} -- 入力データ

        Returns:
            numpy.array -- スペクトル
        """
        self.buffer[:self.chunk // 2] = self.buffer[self.chunk // 2:]
        self.buffer[self.chunk // 2:] = data

        F = np.fft.fft(self.buffer * self.window_func)
        amp = np.abs(F)[:self.chunk // 2]
        return amp / np.sum(amp)

    def setup(self):
        """初期設定を行う"""
        files = glob.glob("./audio/*")
        if len(files) == 0:
            print("audioディレクトリにwavファイルを置いてください")
            exit(1)
        self.separator = Separator(self.chunk // 2, 1, len(files))
        for i, f in enumerate(files):
            name = f.split("/")[-1].split('.')[0]
            data, ch, fs = self.ac.read_wav(f)
            spectrum = np.zeros((self.chunk // 2), dtype=np.float32)

            for j in range(0, len(data), self.chunk // 2):
                amp = self.calc_spectrum(data[j:j + self.chunk // 2])
                spectrum += amp
            self.separator.set_dictionary(spectrum / j, i, name=name)

    def run(self):
        """処理を開始する"""
        print("======================= RUN =======================")
        self.ac.setup_stream(chunck=self.chunk // 2)

        with conf.ThreadPoolExecutor(max_workers=2) as executor:
            self.start_separate(executor)
            while True:
                print("> ", end="")
                line = input()
                if line == 'a':
                    self.stop_separate(executor)
                    self.add_timber()
                    self.start_separate(executor)
                elif line == 'c':
                    self.stop_separate(executor)
                    self.change_timber()
                    self.start_separate(executor)
                elif line == 'q':
                    self.stop_separate(executor)
                    break
                elif line == 's':
                    self.print_timber_list()
                elif line == 'h':
                    print('\'a\' is add timber.')
                    print('\'c\' is change timber.')
                    print('\'s\' is show timber list.')
                    print('\'q\' is shutdown application.')
            self.ac.close_stream()

    def print_countdown(self):
        """録音時のカウントダウンを表示する"""
        print('please input audio {}[sec]!!!'.format(TIME))
        print('============3============')
        time.sleep(1)
        print('============2============')
        time.sleep(1)
        print('============1============')
        time.sleep(1)
        print('RECORD!!')

    def print_timber_list(self):
        """現在の音色のリストを表示する"""
        timbers = self.separator.get_timber()
        for k, v in timbers.items():
            print("Timber:{} : {}".format(k, v))

    def add_timber(self):
        """音色を追加する"""
        self.print_countdown()
        spectrum = self.record()
        self.separator.add_dictionary(spectrum)
        print("finish add")

    def change_timber(self):
        """登録してある音色を変更する"""
        timber_index = -1
        timber_name = None
        while True:
            print(
                'Please input [timber_index,timber_name] to change. (cancel is [q] key)'
            )
            self.print_timber_list()
            print("> ", end="")
            line = input()
            if len(line.split(",")) != 2 and line != 'q':
                continue
            if line == 'q':
                return
            if not line.split(",")[0].isdecimal():
                continue
            timber_index, timber_name = int(
                line.split(",")[0]), line.split(",")[1]
            if (timber_index > len(self.separator.get_timber()) - 1) or (
                    timber_index < 0):
                print('[error] index out of range.')
                continue
            break

        self.print_countdown()
        spectrum = self.record()
        self.separator.set_dictionary(spectrum, timber_index, name=timber_name)
        print('finish change')

    def record(self):
        """録音する

        Returns:
            numpy.array -- スペクトル
        """
        counter = 0
        it = 0
        spectrum = np.zeros((self.chunk // 2), dtype=np.float32)
        self.ac.clear_buffer()
        self.ac.start_stream()
        while counter < TIME:
            if self.ac.q.__len__() > 0:
                data = self.ac.q.popleft()
                spectrum += self.calc_spectrum(data)
                counter += self.ac.chunk / self.ac.rate
                it += 1
        self.ac.stop_stream()
        return spectrum / it

    def start_separate(self, executor):
        """スレッド処理を開始する

        Arguments:
            executor {conf.ThreadPoolExecutor} -- confインスタンス
        """
        self.timbers = self.separator.get_timber()
        if "noise" not in list(self.timbers.values()):
            self.timbers[len(self.timbers)] = "noise"
        msg = [len(self.timbers)]
        msg.extend([v for v in self.timbers.values()])
        self.osc.send(OSC_TIMBER_ADDR, msg)
        self.ac.clear_buffer()
        self.ac.start_stream()
        self.separate_flag = True
        self.sep_thread = executor.submit(self.separate_sound)
        self.osc_thread = executor.submit(self.send_result)

    def stop_separate(self, executor):
        """スレッド処理を停止する

        Arguments:
            executor {conf.ThreadPoolExecutor} -- confインスタンス
        """
        self.separate_flag = False
        self.sep_thread.result()
        self.sep_thread = None
        self.osc_thread.result()
        self.osc_thread = None
        self.ac.clear_buffer()
        self.ac.stop_stream()

    def separate_sound(self):
        """音源分離を行う"""
        while self.separate_flag:
            if self.ac.q.__len__() > 0:
                data = self.ac.q.popleft()
                spectrum = self.calc_spectrum(data)
                self.separate_results = self.separator.separate(spectrum)

    def send_result(self):
        """音源分離の結果を送信する"""
        while self.separate_flag:
            response = []
            if self.separate_results is None:
                continue
            if self.separate_results.shape[0] != len(self.timbers):
                continue
            for i, _ in enumerate(self.timbers.values()):
                response.append(self.separate_results[i][0])
            self.osc.send(OSC_RESULT_ADDR, response)
            time.sleep(OSC_FREQ)