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 (2.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 (2.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_samples_at(samplepos_t time)
85 const double samples_per_quarter_note = session->tempo_map().samples_per_quarter_note_at (time, session->sample_rate());
87 one_ppqn_in_samples = samples_per_quarter_note / double (ppqn);
88 // DEBUG_TRACE (DEBUG::MidiClock, string_compose ("at %1, one ppqn = %2\n", time, one_ppqn_in_samples));
92 MIDIClock_Slave::calculate_song_position(uint16_t song_position_in_sixteenth_notes)
94 samplepos_t song_position_samples = 0;
95 for (uint16_t i = 1; i <= song_position_in_sixteenth_notes; ++i) {
96 // one quarter note contains ppqn pulses, so a sixteenth note is ppqn / 4 pulses
97 calculate_one_ppqn_in_samples_at(song_position_samples);
98 song_position_samples += one_ppqn_in_samples * (samplepos_t)(ppqn / 4);
101 return song_position_samples;
105 MIDIClock_Slave::calculate_filter_coefficients()
107 // omega = 2 * PI * Bandwidth / MIDI clock sample frequency in Hz
108 omega = 2.0 * M_PI * bandwidth * one_ppqn_in_samples / session->sample_rate();
109 b = 1.4142135623730950488 * omega;
114 MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, samplepos_t timestamp)
116 // some pieces of hardware send MIDI Clock all the time
117 if ( (!_starting) && (!_started) ) {
121 pframes_t cycle_offset = timestamp - session->sample_time_at_cycle_start();
123 calculate_one_ppqn_in_samples_at(should_be_position);
125 samplepos_t elapsed_since_start = timestamp - first_timestamp;
128 if (_starting || last_timestamp == 0) {
129 midi_clock_count = 0;
131 first_timestamp = timestamp;
132 elapsed_since_start = should_be_position;
134 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("first clock message after start received @ %1\n", timestamp));
136 // calculate filter coefficients
137 calculate_filter_coefficients();
140 e2 = double(one_ppqn_in_samples) / double(session->sample_rate());
141 t0 = double(elapsed_since_start) / double(session->sample_rate());
144 // let ardour go after first MIDI Clock Event
148 should_be_position += one_ppqn_in_samples;
149 calculate_filter_coefficients();
151 // calculate loop error
152 // we use session->transport_sample() 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_sample()) + double(cycle_offset)));
156 e = error / double(session->sample_rate());
157 current_delta = error;
165 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("clock #%1 @ %2 should-be %3 transport %4 error %5 appspeed %6 "
166 "read-delta %7 should-be delta %8 t1-t0 %9 t0 %10 t1 %11 framerate %12 engine %13\n",
167 midi_clock_count, // #
168 elapsed_since_start, // @
169 should_be_position, // should-be
170 session->transport_sample(), // transport
172 ((t1 - t0) * session->sample_rate()) / one_ppqn_in_samples, // appspeed
173 timestamp - last_timestamp, // read delta
174 one_ppqn_in_samples, // should-be delta
175 (t1 - t0) * session->sample_rate(), // t1-t0
176 t0 * session->sample_rate(), // t0
177 t1 * session->sample_rate(), // t1
178 session->sample_rate(), // framerate
179 session->sample_time()
183 last_timestamp = timestamp;
187 MIDIClock_Slave::start (Parser& /*parser*/, samplepos_t timestamp)
189 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("MIDIClock_Slave got start message at time %1 engine time %2 transport_sample %3\n", timestamp, session->sample_time(), session->transport_sample()));
197 should_be_position = session->transport_sample();
202 MIDIClock_Slave::reset ()
204 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("MidiClock_Slave reset(): calculated filter bandwidth is %1 for period size %2\n", bandwidth, session->samples_per_cycle()));
206 should_be_position = session->transport_sample();
212 // session->request_locate(0, false);
217 MIDIClock_Slave::contineu (Parser& /*parser*/, samplepos_t /*timestamp*/)
219 DEBUG_TRACE (DEBUG::MidiClock, "MIDIClock_Slave got continue message\n");
229 MIDIClock_Slave::stop (Parser& /*parser*/, samplepos_t /*timestamp*/)
231 DEBUG_TRACE (DEBUG::MidiClock, "MIDIClock_Slave got stop message\n");
233 if (_started || _starting) {
236 // locate to last MIDI clock position
237 session->request_transport_speed(0.0);
239 // we need to go back to the last MIDI beat (6 ppqn)
240 // and lets hope the tempo didnt change in the meantime :)
242 // begin at the should be position, because
243 // that is the position of the last MIDI Clock
244 // message and that is probably what the master
245 // expects where we are right now
246 samplepos_t stop_position = should_be_position;
248 // find out the last MIDI beat: go back #midi_clocks mod 6
249 // and lets hope the tempo didnt change in those last 6 beats :)
250 stop_position -= (midi_clock_count % 6) * one_ppqn_in_samples;
252 session->request_locate(stop_position, false);
253 should_be_position = stop_position;
259 MIDIClock_Slave::position (Parser& /*parser*/, MIDI::byte* message, size_t size)
261 // we are note supposed to get position messages while we are running
262 // so lets be robust and ignore those
263 if (_started || _starting) {
268 MIDI::byte lsb = message[1];
269 MIDI::byte msb = message[2];
270 assert((lsb <= 0x7f) && (msb <= 0x7f));
272 uint16_t position_in_sixteenth_notes = (uint16_t(msb) << 7) | uint16_t(lsb);
273 samplepos_t position_in_samples = calculate_song_position(position_in_sixteenth_notes);
275 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Song Position: %1 samples: %2\n", position_in_sixteenth_notes, position_in_samples));
277 session->request_locate(position_in_samples, false);
278 should_be_position = position_in_samples;
284 MIDIClock_Slave::locked () const
290 MIDIClock_Slave::ok() const
296 MIDIClock_Slave::starting() const
302 MIDIClock_Slave::stop_if_no_more_clock_events(samplepos_t& pos, samplepos_t now)
304 /* no timecode for 1/4 second ? conclude that its stopped */
305 if (last_timestamp &&
306 now > last_timestamp &&
307 now - last_timestamp > session->sample_rate() / 4) {
308 DEBUG_TRACE (DEBUG::MidiClock, "No MIDI Clock samples received for some time, stopping!\n");
309 pos = should_be_position;
310 session->request_transport_speed (0);
311 session->request_locate (should_be_position, false);
319 MIDIClock_Slave::speed_and_position (double& speed, samplepos_t& pos)
321 if (!_started || _starting) {
323 pos = should_be_position;
327 samplepos_t engine_now = session->sample_time();
329 if (stop_if_no_more_clock_events(pos, engine_now)) {
334 speed = ((t1 - t0) * session->sample_rate()) / one_ppqn_in_samples;
336 // provide a 0.1% deadzone to lock the speed
337 if (fabs(speed - 1.0) <= 0.001)
340 // calculate position
341 if (engine_now > last_timestamp) {
342 // we are in between MIDI clock messages
343 // so we interpolate position according to speed
344 samplecnt_t elapsed = engine_now - last_timestamp;
345 pos = (samplepos_t) (should_be_position + double(elapsed) * speed);
347 // A new MIDI clock message has arrived this cycle
348 pos = should_be_position;
351 DEBUG_TRACE (DEBUG::MidiClock, string_compose ("speed_and_position: speed %1 should-be %2 transport %3 \n", speed, pos, session->transport_sample()));
357 MIDIClock_Slave::resolution() const
360 return (samplecnt_t) one_ppqn_in_samples * ppqn;
364 MIDIClock_Slave::approximate_current_delta() const
367 if (last_timestamp == 0 || _starting) {
368 snprintf(delta, sizeof(delta), "\u2012\u2012\u2012\u2012");
370 snprintf(delta, sizeof(delta), "\u0394<span foreground=\"green\" face=\"monospace\" >%s%s%" PRIi64 "</span>sm",
371 LEADINGZERO(abs(current_delta)), PLUSMINUS(-current_delta), abs(current_delta));
373 return std::string(delta);