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) { }
44 /** Sync timing information taken from the given Session
45 @return True if timings differed */
46 bool sync (Session* s) {
50 double sp = s->transport_speed();
51 framecnt_t fr = s->transport_frame();
63 /* Midi beats and clocks always gets updated for now */
65 s->bbt_time (this->frame, *this);
67 const TempoMap& tempo = s->tempo_map();
69 const double divisions = tempo.meter_at(frame).divisions_per_bar();
70 const double divisor = tempo.meter_at(frame).note_divisor();
71 const double qnote_scale = divisor * 0.25f;
73 /** Midi Beats in terms of Song Position Pointer is equivalent to total
74 sixteenth notes at 'time' */
76 midi_beats = (((bars - 1) * divisions) + beats - 1);
77 midi_beats += (double)ticks / (double)Position::ticks_per_beat * qnote_scale;
78 midi_beats *= 16.0f / divisor;
80 midi_clocks = midi_beats * 6.0f;
90 void print (std::ostream& s) {
91 s << "frames: " << frame << " midi beats: " << midi_beats << " speed: " << speed;
96 MidiClockTicker::MidiClockTicker ()
100 , _send_state (false)
102 _pos.reset (new Position());
105 MidiClockTicker::~MidiClockTicker()
111 MidiClockTicker::set_session (Session* s)
113 SessionHandlePtr::set_session (s);
116 _session->TransportStateChange.connect_same_thread (_session_connections, boost::bind (&MidiClockTicker::transport_state_changed, this));
117 _session->TransportLooped.connect_same_thread (_session_connections, boost::bind (&MidiClockTicker::transport_looped, this));
118 _session->Located.connect_same_thread (_session_connections, boost::bind (&MidiClockTicker::session_located, this));
120 update_midi_clock_port();
121 _pos->sync (_session);
126 MidiClockTicker::session_located()
128 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Session Located: %1, speed: %2\n", _session->transport_frame(), _session->transport_speed()));
130 if (0 == _session || ! _pos->sync (_session)) {
134 _last_tick = _pos->frame;
136 if (!Config->get_send_midi_clock()) {
144 MidiClockTicker::session_going_away ()
146 SessionHandlePtr::session_going_away();
151 MidiClockTicker::update_midi_clock_port()
153 _midi_port = AudioEngine::instance()->midi_clock_output_port();
157 MidiClockTicker::transport_state_changed()
159 if (_session->exporting()) {
160 /* no midi clock during export, for now */
164 if (!_session->engine().running()) {
165 /* Engine stopped, we can't do anything */
169 if (! _pos->sync (_session)) {
173 DEBUG_TRACE (DEBUG::MidiClock,
174 string_compose ("Transport state change @ %4, speed: %1 position: %2 play loop: %3\n",
175 _pos->speed, _pos->frame, _session->get_play_loop(), _pos->frame)
178 _last_tick = _pos->frame;
180 if (! Config->get_send_midi_clock()) {
186 // tick (_pos->frame);
190 MidiClockTicker::transport_looped()
192 Location* loop_location = _session->locations()->auto_loop_location();
193 assert(loop_location);
195 DEBUG_TRACE (DEBUG::MidiClock,
196 string_compose ("Transport looped, position: %1, loop start: %2, loop end: %3, play loop: %4\n",
197 _session->transport_frame(), loop_location->start(), loop_location->end(), _session->get_play_loop())
200 // adjust _last_tick, so that the next MIDI clock message is sent
201 // in due time (and the tick interval is still constant)
203 framecnt_t elapsed_since_last_tick = loop_location->end() - _last_tick;
205 if (loop_location->start() > elapsed_since_last_tick) {
206 _last_tick = loop_location->start() - elapsed_since_last_tick;
213 MidiClockTicker::tick (const framepos_t& /* transport_frame */, pframes_t nframes)
215 if (!Config->get_send_midi_clock() || _session == 0 || _session->transport_speed() != 1.0f || _midi_port == 0) {
220 if (_pos->speed == 0.0f) {
221 uint32_t where = llrint (_pos->midi_beats);
222 send_position_event (where, 0, nframes);
223 } else if (_pos->speed == 1.0f) {
225 /* Experimental. To really do this and have accuracy, the
226 stop/locate/continue sequence would need queued to send immediately
227 before the next midi clock. */
229 send_stop_event (0, nframes);
231 if (_pos->frame == 0) {
232 send_start_event (0, nframes);
234 uint32_t where = llrint (_pos->midi_beats);
235 send_position_event (where, 0, nframes);
236 send_continue_event (0, nframes);
240 /* Varispeed not supported */
247 if (_pos->speed == 1.0f) {
248 if (_session->get_play_loop()) {
249 assert(_session->locations()->auto_loop_location());
251 if (_pos->frame == _session->locations()->auto_loop_location()->start()) {
252 send_start_event (0, nframes);
254 send_continue_event (0, nframes);
257 } else if (_pos->frame == 0) {
258 send_start_event (0, nframes);
260 send_continue_event (0, nframes);
263 // send_midi_clock_event (0);
265 } else if (_pos->speed == 0.0f) {
266 send_stop_event (0, nframes);
267 send_position_event (llrint (_pos->midi_beats), 0, nframes);
274 const framepos_t end = _pos->frame + nframes;
275 double iter = _last_tick;
278 double clock_delta = one_ppqn_in_frames (llrint (iter));
279 double next_tick = iter + clock_delta;
280 frameoffset_t next_tick_offset = llrint (next_tick) - end;
282 DEBUG_TRACE (DEBUG::MidiClock,
283 string_compose ("Tick: iter: %1, last tick time: %2, next tick time: %3, offset: %4, cycle length: %5\n",
284 iter, _last_tick, next_tick, next_tick_offset, nframes));
286 if (next_tick_offset >= nframes) {
290 if (next_tick_offset >= 0) {
291 send_midi_clock_event (next_tick_offset, nframes);
302 MidiClockTicker::one_ppqn_in_frames (framepos_t transport_position)
304 const Tempo& current_tempo = _session->tempo_map().tempo_at (transport_position);
305 double frames_per_beat = current_tempo.frames_per_beat (_session->nominal_frame_rate());
307 double quarter_notes_per_beat = 4.0 / current_tempo.note_type();
308 double frames_per_quarter_note = frames_per_beat / quarter_notes_per_beat;
310 return frames_per_quarter_note / double (_ppqn);
314 MidiClockTicker::send_midi_clock_event (pframes_t offset, pframes_t nframes)
320 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Tick with offset %1\n", offset));
322 static uint8_t tick_byte = { MIDI_CMD_COMMON_CLOCK };
323 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
324 mb.push_back (offset, 1, &tick_byte);
328 MidiClockTicker::send_start_event (pframes_t offset, pframes_t nframes)
334 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Start %1\n", _last_tick));
336 static uint8_t tick_byte = { MIDI_CMD_COMMON_START };
337 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
338 mb.push_back (offset, 1, &tick_byte);
342 MidiClockTicker::send_continue_event (pframes_t offset, pframes_t nframes)
348 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Continue %1\n", _last_tick));
350 static uint8_t tick_byte = { MIDI_CMD_COMMON_CONTINUE };
351 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
352 mb.push_back (offset, 1, &tick_byte);
356 MidiClockTicker::send_stop_event (pframes_t offset, pframes_t nframes)
362 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Stop %1\n", _last_tick));
364 static uint8_t tick_byte = { MIDI_CMD_COMMON_STOP };
365 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
366 mb.push_back (offset, 1, &tick_byte);
370 MidiClockTicker::send_position_event (uint32_t midi_beats, pframes_t offset, pframes_t nframes)
376 /* can only use 14bits worth */
377 if (midi_beats > 0x3fff) {
381 /* split midi beats into a 14bit value */
383 msg[0] = MIDI_CMD_COMMON_SONG_POS;
384 msg[1] = midi_beats & 0x007f;
385 msg[2] = midi_beats >> 7;
387 MidiBuffer& mb (_midi_port->get_midi_buffer (nframes));
388 mb.push_back (offset, 3, &msg[0]);
390 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Song Position Sent: %1\n", midi_beats));