2 Copyright (C) 2008 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <sys/types.h>
25 #include "pbd/error.h"
26 #include "pbd/failed_constructor.h"
27 #include "pbd/pthread_utils.h"
28 #include "pbd/convert.h"
30 #include "midi++/port.h"
32 #include "ardour/debug.h"
33 #include "ardour/midi_buffer.h"
34 #include "ardour/midi_port.h"
35 #include "ardour/slave.h"
36 #include "ardour/tempo.h"
41 using namespace ARDOUR;
45 MIDIClock_Slave::MIDIClock_Slave (Session& s, MidiPort& p, int ppqn)
47 , bandwidth (10.0 / 60.0) // 1 BpM = 1 / 60 Hz
49 session = (ISlaveSessionProxy *) new SlaveSessionProxy(s);
54 MIDIClock_Slave::MIDIClock_Slave (ISlaveSessionProxy* session_proxy, int ppqn)
55 : session(session_proxy)
57 , bandwidth (10.0 / 60.0) // 1 BpM = 1 / 60 Hz
62 MIDIClock_Slave::~MIDIClock_Slave()
68 MIDIClock_Slave::rebind (MidiPort& port)
70 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("MIDIClock_Slave: connecting to port %1\n", port.name()));
72 port_connections.drop_connections ();
74 port.self_parser().timing.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::update_midi_clock, this, _1, _2));
75 port.self_parser().start.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::start, this, _1, _2));
76 port.self_parser().contineu.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::contineu, this, _1, _2));
77 port.self_parser().stop.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::stop, this, _1, _2));
78 port.self_parser().position.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::position, this, _1, _2, 3));
83 MIDIClock_Slave::calculate_one_ppqn_in_frames_at(framepos_t time)
85 const Tempo& current_tempo = session->tempo_map().tempo_at(time);
86 double frames_per_beat = current_tempo.frames_per_beat(session->frame_rate());
88 double quarter_notes_per_beat = 4.0 / current_tempo.note_type();
89 double frames_per_quarter_note = frames_per_beat / quarter_notes_per_beat;
91 one_ppqn_in_frames = frames_per_quarter_note / double (ppqn);
92 // DEBUG_TRACE (DEBUG::MidiClock, string_compose ("at %1, one ppqn = %2\n", time, one_ppqn_in_frames));
96 MIDIClock_Slave::calculate_song_position(uint16_t song_position_in_sixteenth_notes)
98 framepos_t song_position_frames = 0;
99 for (uint16_t i = 1; i <= song_position_in_sixteenth_notes; ++i) {
100 // one quarter note contains ppqn pulses, so a sixteenth note is ppqn / 4 pulses
101 calculate_one_ppqn_in_frames_at(song_position_frames);
102 song_position_frames += one_ppqn_in_frames * (framepos_t)(ppqn / 4);
105 return song_position_frames;
109 MIDIClock_Slave::calculate_filter_coefficients()
111 // omega = 2 * PI * Bandwidth / MIDI clock frame frequency in Hz
112 omega = 2.0 * M_PI * bandwidth * one_ppqn_in_frames / session->frame_rate();
113 b = 1.4142135623730950488 * omega;
118 MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, framepos_t timestamp)
120 // some pieces of hardware send MIDI Clock all the time
121 if ( (!_starting) && (!_started) ) {
125 calculate_one_ppqn_in_frames_at(should_be_position);
127 framepos_t elapsed_since_start = timestamp - first_timestamp;
130 if (_starting || last_timestamp == 0) {
131 midi_clock_count = 0;
133 first_timestamp = timestamp;
134 elapsed_since_start = should_be_position;
136 // calculate filter coefficients
137 calculate_filter_coefficients();
140 e2 = double(one_ppqn_in_frames) / double(session->frame_rate());
141 t0 = double(elapsed_since_start) / double(session->frame_rate());
144 // let ardour go after first MIDI Clock Event
148 should_be_position += one_ppqn_in_frames;
149 calculate_filter_coefficients();
151 // calculate loop error
152 // we use session->transport_frame() instead of t1 here
153 // because t1 is used to calculate the transport speed,
154 // so the loop will compensate for accumulating rounding errors
155 error = (double(should_be_position) - double(session->transport_frame()));
156 e = error / double(session->frame_rate());
157 current_delta = error;
165 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("clock #%1 @ %2 arrived %3 (theoretical) audible %4 transport %5 error %6 "
166 "read delta %7 should-be delta %8 t1-t0 %9 t0 %10 t1 %11 framerate %12 appspeed %13\n",
170 session->audible_frame(),
171 session->transport_frame(),
173 timestamp - last_timestamp,
175 (t1 -t0) * session->frame_rate(),
176 t0 * session->frame_rate(),
177 t1 * session->frame_rate(),
178 session->frame_rate(),
179 ((t1 - t0) * session->frame_rate()) / one_ppqn_in_frames));
181 last_timestamp = timestamp;
185 MIDIClock_Slave::start (Parser& /*parser*/, framepos_t timestamp)
187 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("MIDIClock_Slave got start message at time %1 engine time %2\n", timestamp, session->frame_time()));
195 should_be_position = session->transport_frame();
200 MIDIClock_Slave::reset ()
202 should_be_position = session->transport_frame();
208 // session->request_locate(0, false);
213 MIDIClock_Slave::contineu (Parser& /*parser*/, framepos_t /*timestamp*/)
215 DEBUG_TRACE (DEBUG::MidiClock, "MIDIClock_Slave got continue message\n");
225 MIDIClock_Slave::stop (Parser& /*parser*/, framepos_t /*timestamp*/)
227 DEBUG_TRACE (DEBUG::MidiClock, "MIDIClock_Slave got stop message\n");
229 if (_started || _starting) {
232 // locate to last MIDI clock position
233 session->request_transport_speed(0.0);
235 // we need to go back to the last MIDI beat (6 ppqn)
236 // and lets hope the tempo didnt change in the meantime :)
238 // begin at the should be position, because
239 // that is the position of the last MIDI Clock
240 // message and that is probably what the master
241 // expects where we are right now
242 framepos_t stop_position = should_be_position;
244 // find out the last MIDI beat: go back #midi_clocks mod 6
245 // and lets hope the tempo didnt change in those last 6 beats :)
246 stop_position -= (midi_clock_count % 6) * one_ppqn_in_frames;
248 session->request_locate(stop_position, false);
249 should_be_position = stop_position;
255 MIDIClock_Slave::position (Parser& /*parser*/, MIDI::byte* message, size_t size)
257 // we are note supposed to get position messages while we are running
258 // so lets be robust and ignore those
259 if (_started || _starting) {
264 MIDI::byte lsb = message[1];
265 MIDI::byte msb = message[2];
266 assert((lsb <= 0x7f) && (msb <= 0x7f));
268 uint16_t position_in_sixteenth_notes = (uint16_t(msb) << 7) | uint16_t(lsb);
269 framepos_t position_in_frames = calculate_song_position(position_in_sixteenth_notes);
271 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Song Position: %1 frames: %2\n", position_in_sixteenth_notes, position_in_frames));
273 session->request_locate(position_in_frames, false);
274 should_be_position = position_in_frames;
280 MIDIClock_Slave::locked () const
286 MIDIClock_Slave::ok() const
292 MIDIClock_Slave::starting() const
298 MIDIClock_Slave::stop_if_no_more_clock_events(framepos_t& pos, framepos_t now)
300 /* no timecode for 1/4 second ? conclude that its stopped */
301 if (last_timestamp &&
302 now > last_timestamp &&
303 now - last_timestamp > session->frame_rate() / 4) {
304 DEBUG_TRACE (DEBUG::MidiClock, "No MIDI Clock frames received for some time, stopping!\n");
305 pos = should_be_position;
306 session->request_transport_speed (0);
307 session->request_locate (should_be_position, false);
315 MIDIClock_Slave::speed_and_position (double& speed, framepos_t& pos)
317 if (!_started || _starting) {
319 pos = should_be_position;
323 framepos_t engine_now = session->frame_time();
325 if (stop_if_no_more_clock_events(pos, engine_now)) {
330 speed = ((t1 - t0) * session->frame_rate()) / one_ppqn_in_frames;
332 // provide a 0.1% deadzone to lock the speed
333 if (fabs(speed - 1.0) <= 0.001)
336 // calculate position
337 if (engine_now > last_timestamp) {
338 // we are in between MIDI clock messages
339 // so we interpolate position according to speed
340 framecnt_t elapsed = engine_now - last_timestamp;
341 pos = (framepos_t) (should_be_position + double(elapsed) * speed);
343 // A new MIDI clock message has arrived this cycle
344 pos = should_be_position;
347 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("speed_and_position: %1 & %2 <-> %3 (transport)\n", speed, pos, session->transport_frame()));
353 MIDIClock_Slave::resolution() const
356 return (framecnt_t) one_ppqn_in_frames * ppqn;
360 MIDIClock_Slave::approximate_current_delta() const
363 if (last_timestamp == 0 || _starting) {
364 snprintf(delta, sizeof(delta), "\u2012\u2012\u2012\u2012");
366 snprintf(delta, sizeof(delta), "\u0394<span foreground=\"green\" face=\"monospace\" >%s%s%" PRIi64 "</span>sm",
367 LEADINGZERO(abs(current_delta)), PLUSMINUS(-current_delta), abs(current_delta));
369 return std::string(delta);