2 Copyright (C) 2008 Hans Baier
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include "pbd/compose.h"
20 #include "pbd/stacktrace.h"
22 #include "evoral/midi_events.h"
24 #include "ardour/async_midi_port.h"
25 #include "ardour/audioengine.h"
26 #include "ardour/midi_buffer.h"
27 #include "ardour/midi_port.h"
28 #include "ardour/ticker.h"
29 #include "ardour/session.h"
30 #include "ardour/tempo.h"
31 #include "ardour/debug.h"
33 using namespace ARDOUR;
36 /** MIDI Clock Position tracking */
37 class MidiClockTicker::Position : public Timecode::BBT_Time
41 Position() : speed(0.0f), frame(0), midi_beats(0) { }
44 /** Sync timing information taken from the given Session
45 * @return True if timings differed
48 bool sync (Session* s) {
52 double sp = s->transport_speed();
53 framecnt_t fr = s->transport_frame();
65 /* Midi beats and clocks always gets updated for now */
67 s->bbt_time (this->frame, *this);
69 const TempoMap& tempo = s->tempo_map();
71 const double divisions = tempo.meter_at(frame).divisions_per_bar();
72 const double divisor = tempo.meter_at(frame).note_divisor();
73 const double qnote_scale = divisor * 0.25f;
76 /** Midi Beats in terms of Song Position Pointer is equivalent to total
77 * sixteenth notes at 'time'
80 mb = (((bars - 1) * divisions) + beats - 1);
81 mb += (double)ticks / (double)Position::ticks_per_beat * qnote_scale;
82 mb *= 16.0f / divisor;
84 if (mb != midi_beats) {
86 midi_clocks = midi_beats * 6.0f;
98 void print (std::ostream& s) {
99 s << "frames: " << frame << " midi beats: " << midi_beats << " speed: " << speed;
104 MidiClockTicker::MidiClockTicker ()
108 , _send_state (false)
110 _pos.reset (new Position());
113 MidiClockTicker::~MidiClockTicker()
119 MidiClockTicker::set_session (Session* s)
121 SessionHandlePtr::set_session (s);
124 _session->TransportStateChange.connect_same_thread (_session_connections, boost::bind (&MidiClockTicker::transport_state_changed, this));
125 _session->TransportLooped.connect_same_thread (_session_connections, boost::bind (&MidiClockTicker::transport_looped, this));
126 _session->Located.connect_same_thread (_session_connections, boost::bind (&MidiClockTicker::session_located, this));
128 update_midi_clock_port();
129 _pos->sync (_session);
134 MidiClockTicker::session_located()
136 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Session Located: %1, speed: %2\n", _session->transport_frame(), _session->transport_speed()));
138 if (!_session || !_pos->sync (_session)) {
142 _last_tick = _pos->frame;
144 if (!Config->get_send_midi_clock()) {
152 MidiClockTicker::session_going_away ()
154 SessionHandlePtr::session_going_away();
159 MidiClockTicker::update_midi_clock_port()
161 _midi_port = _session->midi_clock_output_port();
165 MidiClockTicker::transport_state_changed()
167 if (_session->exporting()) {
168 /* no midi clock during export, for now */
172 if (!_session->engine().running()) {
173 /* Engine stopped, we can't do anything */
177 if (! _pos->sync (_session)) {
181 DEBUG_TRACE (DEBUG::MidiClock,
182 string_compose ("Transport state change @ %4, speed: %1 position: %2 play loop: %3\n",
183 _pos->speed, _pos->frame, _session->get_play_loop(), _pos->frame)
186 _last_tick = _pos->frame;
188 if (! Config->get_send_midi_clock()) {
194 // tick (_pos->frame);
198 MidiClockTicker::transport_looped()
200 Location* loop_location = _session->locations()->auto_loop_location();
201 assert(loop_location);
203 DEBUG_TRACE (DEBUG::MidiClock,
204 string_compose ("Transport looped, position: %1, loop start: %2, loop end: %3, play loop: %4\n",
205 _session->transport_frame(), loop_location->start(), loop_location->end(), _session->get_play_loop())
208 // adjust _last_tick, so that the next MIDI clock message is sent
209 // in due time (and the tick interval is still constant)
211 framecnt_t elapsed_since_last_tick = loop_location->end() - _last_tick;
213 if (loop_location->start() > elapsed_since_last_tick) {
214 _last_tick = loop_location->start() - elapsed_since_last_tick;
221 MidiClockTicker::tick (const framepos_t& /* transport_frame */, pframes_t nframes)
223 if (!Config->get_send_midi_clock() || _session == 0 || _midi_port == 0) {
228 if (_pos->speed == 0.0f) {
229 send_position_event (llrint (_pos->midi_beats), 0, nframes);
230 } else if (_pos->speed == 1.0f) {
231 send_stop_event (0, nframes);
233 if (_pos->frame == 0) {
234 send_start_event (0, nframes);
236 send_position_event (llrint (_pos->midi_beats), 0, nframes);
237 send_continue_event (0, nframes);
240 /* Varispeed not supported */
248 if (_pos->speed == 1.0f) {
249 if (_session->get_play_loop()) {
250 assert(_session->locations()->auto_loop_location());
252 if (_pos->frame == _session->locations()->auto_loop_location()->start()) {
253 send_start_event (0, nframes);
255 send_continue_event (0, nframes);
258 } else if (_pos->frame == 0) {
259 send_start_event (0, nframes);
261 send_continue_event (0, nframes);
264 // send_midi_clock_event (0);
266 } else if (_pos->speed == 0.0f) {
267 send_stop_event (0, nframes);
268 send_position_event (llrint (_pos->midi_beats), 0, nframes);
274 if (_session->transport_speed() != 1.0f) {
275 /* no varispeed support and nothing to do after this if stopped */
279 const framepos_t end = _pos->frame + nframes;
280 double iter = _last_tick;
283 double clock_delta = one_ppqn_in_frames (llrint (iter));
284 double next_tick = iter + clock_delta;
285 frameoffset_t next_tick_offset = llrint (next_tick) - end;
287 DEBUG_TRACE (DEBUG::MidiClock,
288 string_compose ("Tick: iter: %1, last tick time: %2, next tick time: %3, offset: %4, cycle length: %5\n",
289 iter, _last_tick, next_tick, next_tick_offset, nframes));
291 if (next_tick_offset >= nframes) {
295 if (next_tick_offset >= 0) {
296 send_midi_clock_event (next_tick_offset, nframes);
307 MidiClockTicker::one_ppqn_in_frames (framepos_t transport_position)
309 const Tempo& current_tempo = _session->tempo_map().tempo_at (transport_position);
310 double frames_per_beat = current_tempo.frames_per_beat (_session->nominal_frame_rate());
312 double quarter_notes_per_beat = 4.0 / current_tempo.note_type();
313 double frames_per_quarter_note = frames_per_beat / quarter_notes_per_beat;
315 return frames_per_quarter_note / double (_ppqn);
319 MidiClockTicker::send_midi_clock_event (pframes_t offset, pframes_t nframes)
325 static uint8_t msg = MIDI_CMD_COMMON_CLOCK;
327 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
328 mb.push_back (offset, 1, &msg);
330 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Tick with offset %1\n", offset));
334 MidiClockTicker::send_start_event (pframes_t offset, pframes_t nframes)
340 static uint8_t msg = { MIDI_CMD_COMMON_START };
341 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
342 mb.push_back (offset, 1, &msg);
344 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Start %1\n", _last_tick));
348 MidiClockTicker::send_continue_event (pframes_t offset, pframes_t nframes)
354 static uint8_t msg = { MIDI_CMD_COMMON_CONTINUE };
355 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
356 mb.push_back (offset, 1, &msg);
358 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Continue %1\n", _last_tick));
362 MidiClockTicker::send_stop_event (pframes_t offset, pframes_t nframes)
368 static uint8_t msg = MIDI_CMD_COMMON_STOP;
369 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
370 mb.push_back (offset, 1, &msg);
372 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Stop %1\n", _last_tick));
376 MidiClockTicker::send_position_event (uint32_t midi_beats, pframes_t offset, pframes_t nframes)
382 /* can only use 14bits worth */
383 if (midi_beats > 0x3fff) {
387 /* split midi beats into a 14bit value */
389 msg[0] = MIDI_CMD_COMMON_SONG_POS;
390 msg[1] = midi_beats & 0x007f;
391 msg[2] = midi_beats >> 7;
393 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
394 mb.push_back (offset, 3, &msg[0]);
396 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Song Position Sent: %1 to %2 (events now %3, buf = %4)\n", midi_beats, _midi_port->name(),