Example #1
0
    def _init_input(self):
        self.input_queue = EventQueue()
        self.input = KeyboardInput(self.input_queue)
        self.input.start()

        self.input_thread = threading.Thread(target=self._process_input_queue)
        self.input_thread.start()
Example #2
0
    def test_onrelease_SpecialKeyShouldNotPutKeyInQueue(self):
        queue = EventQueueMock()
        key_released = Key.esc

        kb = KeyboardInput(queue)
        kb.onrelease(key_released)

        self.assertTrue(queue.empty(), "Expected the queue to be empty")
Example #3
0
    def test_onrelease_OneKeyPressShouldCreateOneMidiOnMessage(self):
        queue = EventQueueMock()
        key_released = KeyCode.from_char('q')

        kb = KeyboardInput(queue)
        kb.onrelease(key_released)

        self._assert_queue_size(queue)

        msg = queue.get()

        self._assert_message(msg, EVT_KEY_RELEASED, 'q')
Example #4
0
    def test_onpress_SpecialKeyShouldPutKeyInQueue(self):
        queue = EventQueueMock()
        key_pressed = Key.esc

        kb = KeyboardInput(queue)
        kb.onpress(key_pressed)

        self._assert_queue_size(queue)

        msg = queue.get()

        self._assert_message(msg, EVT_KEY_PRESSED, '<esc>')
Example #5
0
    def test_onpress_KeepPressingAKeyShouldPutOnlyOneNoteOnMessageOnTheQueue(
            self):
        queue = EventQueueMock()
        key_pressed = KeyCode.from_char('y')

        kb = KeyboardInput(queue)

        # Holding a key calls onpress multiple times
        kb.onpress(key_pressed)
        kb.onpress(key_pressed)
        kb.onpress(key_pressed)
        kb.onpress(key_pressed)
        kb.onpress(key_pressed)

        self._assert_queue_size(queue)

        msg = queue.get()

        self._assert_message(msg, EVT_KEY_PRESSED, 'y')
Example #6
0
    def test_pressReleasePress_shouldNotBlockTheLastPress(self):
        queue = EventQueueMock()
        target_key = KeyCode.from_char('y')

        kb = KeyboardInput(queue)

        # Holding a key calls onpress multiple times
        kb.onpress(target_key)
        kb.onrelease(target_key)
        kb.onpress(target_key)

        self._assert_queue_size(queue, 3)

        msg1 = queue.get()
        msg2 = queue.get()
        msg3 = queue.get()

        self._assert_message(msg1, EVT_KEY_PRESSED, 'y')
        self._assert_message(msg2, EVT_KEY_RELEASED, 'y')
        self._assert_message(msg3, EVT_KEY_PRESSED, 'y')
Example #7
0
class App(object):
    def __init__(self, Iface, *args, **kwargs):
        super(App).__init__(*args, **kwargs)
        self.stop = False
        self.log = logging.getLogger(LOGGER_NAME)

        self.log.debug('Initializing Input...')
        self._init_input()

        self.log.debug('Initializing synth...')
        self._init_synth()

        self.log.debug('Initializing Interface...')
        self.IfaceType = Iface
        self._init_interface()

    def _init_input(self):
        self.input_queue = EventQueue()
        self.input = KeyboardInput(self.input_queue)
        self.input.start()

        self.input_thread = threading.Thread(target=self._process_input_queue)
        self.input_thread.start()

    def _process_input_queue(self):
        while not self.stop:
            event = self.input_queue.get()  # Blocking

            self.log.debug(f'Got event {event}')

            item = event.item

            if event.type.startswith(EVT_SYS):
                if item == EXIT_ITEM:
                    self._terminate()

            if event.type.startswith(EVT_KEY):
                if item in keyboard_note_table:
                    note, octave = keyboard_note_table[item]

                    new_event = Event(event_type_midi_action[event.type](
                        note, octave, 127),
                                      midi.EVT_MIDI,
                                      ancestor=event,
                                      timestamp=time.time())

                    self.synth_queue.put(new_event)
                else:
                    if item == EXIT_KEY:
                        self.input_queue.put(
                            Event(EXIT_ITEM,
                                  EVT_SYS,
                                  ancestor=event,
                                  timestamp=time.time()))

        self.log.debug('Exiting input Thread loop...')

    def _init_interface(self):
        self.interface = self.IfaceType(self.synth, log=self.log)
        self.interface.start(exit_action=self.exit)

    def _init_synth(self):
        self.synth = Synth(log=self.log)
        self.synth_queue = self.synth.input_queue

    def _terminate(self):
        self.log.debug("Terminating app...")
        self.stop = True

        self.log.debug("Terminating Input...")
        self.input.stop()

        self.log.debug("Terminating Synth...")
        self.synth.terminate()

        if threading.get_ident() != self.input_thread.ident:
            self.log.debug('Stopping input Thread...')
            self.input_thread.join()

        self.log.debug("Terminating Interface...")
        self.interface.stop()

        self.log.debug("Finished terminating app!")

    def exit(self):
        self.input_queue.put(EXIT_EVENT)