+ DEBUG_TRACE (DEBUG::MidiClock, "MIDIClock_Slave got stop message\n");
+
+ if (_started || _starting) {
+ _starting = false;
+ _started = false;
+ // locate to last MIDI clock position
+ session->request_transport_speed(0.0);
+
+ // we need to go back to the last MIDI beat (6 ppqn)
+ // and lets hope the tempo didnt change in the meantime :)
+
+ // begin at the should be position, because
+ // that is the position of the last MIDI Clock
+ // message and that is probably what the master
+ // expects where we are right now
+ framepos_t stop_position = should_be_position;
+
+ // find out the last MIDI beat: go back #midi_clocks mod 6
+ // and lets hope the tempo didnt change in those last 6 beats :)
+ stop_position -= (midi_clock_count % 6) * one_ppqn_in_frames;
+
+ session->request_locate(stop_position, false);
+ should_be_position = stop_position;
+ last_timestamp = 0;
+ }
+}
+
+void
+MIDIClock_Slave::position (Parser& /*parser*/, byte* message, size_t size)
+{
+ // we are note supposed to get position messages while we are running
+ // so lets be robust and ignore those
+ if (_started || _starting) {
+ return;
+ }
+
+ assert(size == 3);
+ byte lsb = message[1];
+ byte msb = message[2];
+ assert((lsb <= 0x7f) && (msb <= 0x7f));
+
+ uint16_t position_in_sixteenth_notes = (uint16_t(msb) << 7) | uint16_t(lsb);
+ framepos_t position_in_frames = calculate_song_position(position_in_sixteenth_notes);
+
+ DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Song Position: %1 frames: %2\n", position_in_sixteenth_notes, position_in_frames));
+
+ session->request_locate(position_in_frames, false);
+ should_be_position = position_in_frames;