Example #1
0
    def setUp(self):
        self._num_velocity_bins = 32
        self._max_shift_steps = 100
        self.enc = ModuloPerformanceEventSequenceEncoderDecoder(
            num_velocity_bins=self._num_velocity_bins,
            max_shift_steps=self._max_shift_steps)

        self._expected_input_size = (
            2 * performance_encoder_decoder.MODULO_PITCH_ENCODER_WIDTH +
            performance_encoder_decoder.MODULO_VELOCITY_ENCODER_WIDTH +
            performance_encoder_decoder.MODULO_TIME_SHIFT_ENCODER_WIDTH)

        self._expected_num_classes = (self._num_velocity_bins +
                                      self._max_shift_steps + 2 *
                                      (performance_lib.MAX_MIDI_PITCH -
                                       performance_lib.MIN_MIDI_PITCH + 1))
  def setUp(self):
    self._num_velocity_bins = 32
    self._max_shift_steps = 100
    self.enc = ModuloPerformanceEventSequenceEncoderDecoder(
        num_velocity_bins=self._num_velocity_bins,
        max_shift_steps=self._max_shift_steps)

    self._expected_input_size = (
        2 * performance_encoder_decoder.MODULO_PITCH_ENCODER_WIDTH +
        performance_encoder_decoder.MODULO_VELOCITY_ENCODER_WIDTH +
        performance_encoder_decoder.MODULO_TIME_SHIFT_ENCODER_WIDTH)

    self._expected_num_classes = (self._num_velocity_bins +
                                  self._max_shift_steps +
                                  2 * (performance_lib.MAX_MIDI_PITCH -
                                       performance_lib.MIN_MIDI_PITCH + 1))
class ModuloPerformanceEventSequenceEncoderTest(tf.test.TestCase):
  """Test class for ModuloPerformanceEventSequenceEncoder.

  ModuloPerformanceEventSequenceEncoderDecoder is tightly coupled with the
  PerformanceModuloEncoding, and PerformanceOneHotEncoding classes. As a result,
  in the test set up, the test object is initialized with one of each objects
  and tested accordingly. Since this class only modifies the input encoding
  of performance events, and otherwise its treatment of labels is the same as
  OneHotEventSequenceEncoderDecoder, the events_to_labels(), and
  class_index_to_event() methods of the class are not tested.
  """

  def setUp(self):
    self._num_velocity_bins = 32
    self._max_shift_steps = 100
    self.enc = ModuloPerformanceEventSequenceEncoderDecoder(
        num_velocity_bins=self._num_velocity_bins,
        max_shift_steps=self._max_shift_steps)

    self._expected_input_size = (
        2 * performance_encoder_decoder.MODULO_PITCH_ENCODER_WIDTH +
        performance_encoder_decoder.MODULO_VELOCITY_ENCODER_WIDTH +
        performance_encoder_decoder.MODULO_TIME_SHIFT_ENCODER_WIDTH)

    self._expected_num_classes = (self._num_velocity_bins +
                                  self._max_shift_steps +
                                  2 * (performance_lib.MAX_MIDI_PITCH -
                                       performance_lib.MIN_MIDI_PITCH + 1))

  def testInputSize(self):
    self.assertEqual(self._expected_input_size, self.enc.input_size)

  def testNumClasses(self):
    self.assertEqual(self._expected_num_classes, self.enc.num_classes)

  def testDefaultEventLabel(self):
    label = self._expected_num_classes - self._num_velocity_bins - 1
    self.assertEqual(label, self.enc.default_event_label)

  def testEventsToInput(self):
    num_shift_bins = self._max_shift_steps
    num_velocity_bins = self._num_velocity_bins
    slow_base = 2.0 * pi / 144.0
    fast_base = 2.0 * pi / 12.0
    shift_base = 2.0 * pi / num_shift_bins
    velocity_base = 2.0 * pi / num_velocity_bins

    expected_pairs = [
        (PerformanceEvent(event_type=PerformanceEvent.NOTE_ON, event_value=60),
         [1.0, cos(60.0 * slow_base), sin(60.0 * slow_base),
          cos(60.0 * fast_base), sin(60.0 * fast_base),
          0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
        (PerformanceEvent(event_type=PerformanceEvent.NOTE_ON, event_value=0),
         [1.0, cos(0.0 * slow_base), sin(0.0 * slow_base),
          cos(0.0 * fast_base), sin(0.0 * fast_base),
          0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
        (PerformanceEvent(event_type=PerformanceEvent.NOTE_ON, event_value=127),
         [1.0, cos(127.0 * slow_base), sin(127.0 * slow_base),
          cos(127.0 * fast_base), sin(127.0 * fast_base),
          0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
        (PerformanceEvent(event_type=PerformanceEvent.NOTE_OFF, event_value=72),
         [0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
          cos(72.0 * slow_base), sin(72.0 * slow_base),
          cos(72.0 * fast_base), sin(72.0 * fast_base),
          0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
        (PerformanceEvent(event_type=PerformanceEvent.NOTE_OFF, event_value=0),
         [0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
          cos(0.0 * slow_base), sin(0.0 * slow_base),
          cos(0.0 * fast_base), sin(0.0 * fast_base),
          0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
        (PerformanceEvent(
            event_type=PerformanceEvent.NOTE_OFF, event_value=127),
         [0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
          cos(127.0 * slow_base), sin(127.0 * slow_base),
          cos(127.0 * fast_base), sin(127.0 * fast_base),
          0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
        (PerformanceEvent(
            event_type=PerformanceEvent.TIME_SHIFT, event_value=10),
         [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
          1.0, cos(9.0 * shift_base), sin(9.0 * shift_base),
          0.0, 0.0, 0.0]),
        (PerformanceEvent(
            event_type=PerformanceEvent.TIME_SHIFT, event_value=1),
         [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
          1.0, cos(0.0 * shift_base), sin(0.0 * shift_base),
          0.0, 0.0, 0.0]),
        (PerformanceEvent(
            event_type=PerformanceEvent.TIME_SHIFT, event_value=100),
         [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
          1.0, cos(99.0 * shift_base), sin(99.0 * shift_base),
          0.0, 0.0, 0.0]),
        (PerformanceEvent(event_type=PerformanceEvent.VELOCITY, event_value=5),
         [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
          0.0, 0.0, 0.0,
          1.0, cos(4.0 * velocity_base), sin(4.0 * velocity_base)]),
        (PerformanceEvent(event_type=PerformanceEvent.VELOCITY, event_value=1),
         [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
          0.0, 0.0, 0.0,
          1.0, cos(0.0 * velocity_base), sin(0.0 * velocity_base)]),
        (PerformanceEvent(event_type=PerformanceEvent.VELOCITY, event_value=16),
         [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
          0.0, 0.0, 0.0,
          1.0, cos(15.0 * velocity_base), sin(15.0 * velocity_base)]),
    ]

    events = []
    position = 0
    for event, expected_encoded_modulo_event in expected_pairs:
      events += [event]
      actual_encoded_modulo_event = self.enc.events_to_input(events, position)
      position += 1
      for i in range(self._expected_input_size):
        self.assertAlmostEqual(expected_encoded_modulo_event[i],
                               actual_encoded_modulo_event[i])
Example #4
0
class ModuloPerformanceEventSequenceEncoderTest(tf.test.TestCase):
    """Test class for ModuloPerformanceEventSequenceEncoder.

  ModuloPerformanceEventSequenceEncoderDecoder is tightly coupled with the
  PerformanceModuloEncoding, and PerformanceOneHotEncoding classes. As a result,
  in the test set up, the test object is initialized with one of each objects
  and tested accordingly. Since this class only modifies the input encoding
  of performance events, and otherwise its treatment of labels is the same as
  OneHotEventSequenceEncoderDecoder, the events_to_labels(), and
  class_index_to_event() methods of the class are not tested.
  """
    def setUp(self):
        self._num_velocity_bins = 32
        self._max_shift_steps = 100
        self.enc = ModuloPerformanceEventSequenceEncoderDecoder(
            num_velocity_bins=self._num_velocity_bins,
            max_shift_steps=self._max_shift_steps)

        self._expected_input_size = (
            2 * performance_encoder_decoder.MODULO_PITCH_ENCODER_WIDTH +
            performance_encoder_decoder.MODULO_VELOCITY_ENCODER_WIDTH +
            performance_encoder_decoder.MODULO_TIME_SHIFT_ENCODER_WIDTH)

        self._expected_num_classes = (self._num_velocity_bins +
                                      self._max_shift_steps + 2 *
                                      (performance_lib.MAX_MIDI_PITCH -
                                       performance_lib.MIN_MIDI_PITCH + 1))

    def testInputSize(self):
        self.assertEquals(self._expected_input_size, self.enc.input_size)

    def testNumClasses(self):
        self.assertEqual(self._expected_num_classes, self.enc.num_classes)

    def testDefaultEventLabel(self):
        label = self._expected_num_classes - self._num_velocity_bins - 1
        self.assertEquals(label, self.enc.default_event_label)

    def testEventsToInput(self):
        num_shift_bins = self._max_shift_steps
        num_velocity_bins = self._num_velocity_bins
        slow_base = 2.0 * pi / 144.0
        fast_base = 2.0 * pi / 12.0
        shift_base = 2.0 * pi / num_shift_bins
        velocity_base = 2.0 * pi / num_velocity_bins

        expected_pairs = [
            (PerformanceEvent(event_type=PerformanceEvent.NOTE_ON,
                              event_value=60), [
                                  1.0,
                                  cos(60.0 * slow_base),
                                  sin(60.0 * slow_base),
                                  cos(60.0 * fast_base),
                                  sin(60.0 * fast_base), 0.0, 0.0, 0.0, 0.0,
                                  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
                              ]),
            (PerformanceEvent(event_type=PerformanceEvent.NOTE_ON,
                              event_value=0), [
                                  1.0,
                                  cos(0.0 * slow_base),
                                  sin(0.0 * slow_base),
                                  cos(0.0 * fast_base),
                                  sin(0.0 * fast_base), 0.0, 0.0, 0.0, 0.0,
                                  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
                              ]),
            (PerformanceEvent(event_type=PerformanceEvent.NOTE_ON,
                              event_value=127), [
                                  1.0,
                                  cos(127.0 * slow_base),
                                  sin(127.0 * slow_base),
                                  cos(127.0 * fast_base),
                                  sin(127.0 * fast_base), 0.0, 0.0, 0.0, 0.0,
                                  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
                              ]),
            (PerformanceEvent(event_type=PerformanceEvent.NOTE_OFF,
                              event_value=72), [
                                  0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
                                  cos(72.0 * slow_base),
                                  sin(72.0 * slow_base),
                                  cos(72.0 * fast_base),
                                  sin(72.0 * fast_base), 0.0, 0.0, 0.0, 0.0,
                                  0.0, 0.0
                              ]),
            (PerformanceEvent(event_type=PerformanceEvent.NOTE_OFF,
                              event_value=0), [
                                  0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
                                  cos(0.0 * slow_base),
                                  sin(0.0 * slow_base),
                                  cos(0.0 * fast_base),
                                  sin(0.0 * fast_base), 0.0, 0.0, 0.0, 0.0,
                                  0.0, 0.0
                              ]),
            (PerformanceEvent(event_type=PerformanceEvent.NOTE_OFF,
                              event_value=127), [
                                  0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
                                  cos(127.0 * slow_base),
                                  sin(127.0 * slow_base),
                                  cos(127.0 * fast_base),
                                  sin(127.0 * fast_base), 0.0, 0.0, 0.0, 0.0,
                                  0.0, 0.0
                              ]),
            (PerformanceEvent(event_type=PerformanceEvent.TIME_SHIFT,
                              event_value=10), [
                                  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                                  0.0, 1.0,
                                  cos(9.0 * shift_base),
                                  sin(9.0 * shift_base), 0.0, 0.0, 0.0
                              ]),
            (PerformanceEvent(event_type=PerformanceEvent.TIME_SHIFT,
                              event_value=1), [
                                  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                                  0.0, 1.0,
                                  cos(0.0 * shift_base),
                                  sin(0.0 * shift_base), 0.0, 0.0, 0.0
                              ]),
            (PerformanceEvent(event_type=PerformanceEvent.TIME_SHIFT,
                              event_value=100), [
                                  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                                  0.0, 1.0,
                                  cos(99.0 * shift_base),
                                  sin(99.0 * shift_base), 0.0, 0.0, 0.0
                              ]),
            (PerformanceEvent(event_type=PerformanceEvent.VELOCITY,
                              event_value=5), [
                                  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                                  0.0, 0.0, 0.0, 0.0, 1.0,
                                  cos(4.0 * velocity_base),
                                  sin(4.0 * velocity_base)
                              ]),
            (PerformanceEvent(event_type=PerformanceEvent.VELOCITY,
                              event_value=1), [
                                  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                                  0.0, 0.0, 0.0, 0.0, 1.0,
                                  cos(0.0 * velocity_base),
                                  sin(0.0 * velocity_base)
                              ]),
            (PerformanceEvent(event_type=PerformanceEvent.VELOCITY,
                              event_value=16), [
                                  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                                  0.0, 0.0, 0.0, 0.0, 1.0,
                                  cos(15.0 * velocity_base),
                                  sin(15.0 * velocity_base)
                              ]),
        ]

        events = []
        position = 0
        for event, expected_encoded_modulo_event in expected_pairs:
            events += [event]
            actual_encoded_modulo_event = self.enc.events_to_input(
                events, position)
            position += 1
            for i in range(self._expected_input_size):
                self.assertAlmostEqual(expected_encoded_modulo_event[i],
                                       actual_encoded_modulo_event[i])