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/lmath.h"
29 #include "ardour/ticker.h"
30 #include "ardour/session.h"
31 #include "ardour/tempo.h"
32 #include "ardour/debug.h"
34 using namespace ARDOUR;
37 /** MIDI Clock Position tracking */
38 class MidiClockTicker::Position : public Timecode::BBT_Time
42 Position() : speed(0.0f), frame(0), midi_beats(0) { }
45 /** Sync timing information taken from the given Session
46 * @return True if timings differed
49 bool sync (Session* s) {
53 double sp = s->transport_speed();
54 framecnt_t fr = s->transport_frame();
66 /* Midi beats and clocks always gets updated for now */
68 s->bbt_time (this->frame, *this);
70 const TempoMap& tempo = s->tempo_map();
71 const Meter& meter = tempo.meter_at_frame (frame);
73 const double divisions = meter.divisions_per_bar();
74 const double divisor = meter.note_divisor();
75 const double qnote_scale = divisor * 0.25f;
78 /** Midi Beats in terms of Song Position Pointer is equivalent to total
79 * sixteenth notes at 'time'
82 mb = (((bars - 1) * divisions) + beats - 1);
83 mb += (double)ticks / (double)Position::ticks_per_beat * qnote_scale;
84 mb *= 16.0f / divisor;
86 if (mb != midi_beats) {
88 midi_clocks = midi_beats * 6.0f;
100 void print (std::ostream& s) {
101 s << "frames: " << frame << " midi beats: " << midi_beats << " speed: " << speed;
106 MidiClockTicker::MidiClockTicker ()
110 , _send_state (false)
112 _pos.reset (new Position());
115 MidiClockTicker::~MidiClockTicker()
121 MidiClockTicker::set_session (Session* s)
123 SessionHandlePtr::set_session (s);
126 _session->TransportStateChange.connect_same_thread (_session_connections, boost::bind (&MidiClockTicker::transport_state_changed, this));
127 _session->TransportLooped.connect_same_thread (_session_connections, boost::bind (&MidiClockTicker::transport_looped, this));
128 _session->Located.connect_same_thread (_session_connections, boost::bind (&MidiClockTicker::session_located, this));
130 update_midi_clock_port();
131 _pos->sync (_session);
136 MidiClockTicker::session_located()
138 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Session Located: %1, speed: %2\n", _session->transport_frame(), _session->transport_speed()));
140 if (!_session || !_pos->sync (_session)) {
144 _last_tick = _pos->frame;
146 if (!Config->get_send_midi_clock()) {
154 MidiClockTicker::session_going_away ()
156 SessionHandlePtr::session_going_away();
161 MidiClockTicker::update_midi_clock_port()
163 _midi_port = _session->midi_clock_output_port();
167 MidiClockTicker::transport_state_changed()
169 if (_session->exporting()) {
170 /* no midi clock during export, for now */
174 if (!_session->engine().running()) {
175 /* Engine stopped, we can't do anything */
179 if (! _pos->sync (_session)) {
183 DEBUG_TRACE (DEBUG::MidiClock,
184 string_compose ("Transport state change @ %4, speed: %1 position: %2 play loop: %3\n",
185 _pos->speed, _pos->frame, _session->get_play_loop(), _pos->frame)
188 _last_tick = _pos->frame;
190 if (! Config->get_send_midi_clock()) {
196 // tick (_pos->frame);
200 MidiClockTicker::transport_looped()
202 Location* loop_location = _session->locations()->auto_loop_location();
203 assert(loop_location);
205 DEBUG_TRACE (DEBUG::MidiClock,
206 string_compose ("Transport looped, position: %1, loop start: %2, loop end: %3, play loop: %4\n",
207 _session->transport_frame(), loop_location->start(), loop_location->end(), _session->get_play_loop())
210 // adjust _last_tick, so that the next MIDI clock message is sent
211 // in due time (and the tick interval is still constant)
213 framecnt_t elapsed_since_last_tick = loop_location->end() - _last_tick;
215 if (loop_location->start() > elapsed_since_last_tick) {
216 _last_tick = loop_location->start() - elapsed_since_last_tick;
223 MidiClockTicker::tick (const framepos_t& /* transport_frame */, pframes_t nframes)
225 if (!Config->get_send_midi_clock() || _session == 0 || _midi_port == 0) {
230 if (_pos->speed == 0.0f) {
231 send_position_event (llrint (_pos->midi_beats), 0, nframes);
232 } else if (_pos->speed == 1.0f) {
233 send_stop_event (0, nframes);
235 if (_pos->frame == 0) {
236 send_start_event (0, nframes);
238 send_position_event (llrint (_pos->midi_beats), 0, nframes);
239 send_continue_event (0, nframes);
242 /* Varispeed not supported */
250 if (_pos->speed == 1.0f) {
251 if (_session->get_play_loop()) {
252 assert(_session->locations()->auto_loop_location());
254 if (_pos->frame == _session->locations()->auto_loop_location()->start()) {
255 send_start_event (0, nframes);
257 send_continue_event (0, nframes);
260 } else if (_pos->frame == 0) {
261 send_start_event (0, nframes);
263 send_continue_event (0, nframes);
266 // send_midi_clock_event (0);
268 } else if (_pos->speed == 0.0f) {
269 send_stop_event (0, nframes);
270 send_position_event (llrint (_pos->midi_beats), 0, nframes);
276 if (_session->transport_speed() != 1.0f) {
277 /* no varispeed support and nothing to do after this if stopped */
281 const framepos_t end = _pos->frame + nframes;
282 double iter = _last_tick;
285 double clock_delta = one_ppqn_in_frames (llrint (iter));
286 double next_tick = iter + clock_delta;
287 frameoffset_t next_tick_offset = llrint (next_tick) - end;
289 DEBUG_TRACE (DEBUG::MidiClock,
290 string_compose ("Tick: iter: %1, last tick time: %2, next tick time: %3, offset: %4, cycle length: %5\n",
291 iter, _last_tick, next_tick, next_tick_offset, nframes));
293 if (next_tick_offset >= nframes) {
297 if (next_tick_offset >= 0) {
298 send_midi_clock_event (next_tick_offset, nframes);
309 MidiClockTicker::one_ppqn_in_frames (framepos_t transport_position)
311 const Tempo& current_tempo = _session->tempo_map().tempo_at_frame (transport_position);
312 double frames_per_beat = _session->tempo_map().frames_per_beat_at (transport_position, _session->nominal_frame_rate());
314 double quarter_notes_per_beat = 4.0 / current_tempo.note_type();
315 double frames_per_quarter_note = frames_per_beat / quarter_notes_per_beat;
317 return frames_per_quarter_note / double (_ppqn);
321 MidiClockTicker::send_midi_clock_event (pframes_t offset, pframes_t nframes)
327 static uint8_t msg = MIDI_CMD_COMMON_CLOCK;
329 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
330 mb.push_back (offset, 1, &msg);
332 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Tick with offset %1\n", offset));
336 MidiClockTicker::send_start_event (pframes_t offset, pframes_t nframes)
342 static uint8_t msg = { MIDI_CMD_COMMON_START };
343 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
344 mb.push_back (offset, 1, &msg);
346 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Start %1\n", _last_tick));
350 MidiClockTicker::send_continue_event (pframes_t offset, pframes_t nframes)
356 static uint8_t msg = { MIDI_CMD_COMMON_CONTINUE };
357 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
358 mb.push_back (offset, 1, &msg);
360 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Continue %1\n", _last_tick));
364 MidiClockTicker::send_stop_event (pframes_t offset, pframes_t nframes)
370 static uint8_t msg = MIDI_CMD_COMMON_STOP;
371 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
372 mb.push_back (offset, 1, &msg);
374 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Stop %1\n", _last_tick));
378 MidiClockTicker::send_position_event (uint32_t midi_beats, pframes_t offset, pframes_t nframes)
384 /* can only use 14bits worth */
385 if (midi_beats > 0x3fff) {
389 /* split midi beats into a 14bit value */
391 msg[0] = MIDI_CMD_COMMON_SONG_POS;
392 msg[1] = midi_beats & 0x007f;
393 msg[2] = midi_beats >> 7;
395 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
396 mb.push_back (offset, 3, &msg[0]);
398 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Song Position Sent: %1 to %2 (events now %3, buf = %4)\n", midi_beats, _midi_port->name(),