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"
29 #include "midi++/port.h"
30 #include "midi++/jack.h"
31 #include "ardour/slave.h"
32 #include "ardour/session.h"
33 #include "ardour/audioengine.h"
34 #include "ardour/cycles.h"
35 #include "ardour/tempo.h"
41 using namespace ARDOUR;
45 MIDIClock_Slave::MIDIClock_Slave (Session& s, MIDI::Port& p, int ppqn)
47 , bandwidth (30.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 (30.0 / 60.0) // 1 BpM = 1 / 60 Hz
59 session = session_proxy;
63 MIDIClock_Slave::~MIDIClock_Slave()
69 MIDIClock_Slave::rebind (MIDI::Port& p)
71 port_connections.drop_connections();
75 #ifdef DEBUG_MIDI_CLOCK
76 std::cerr << "MIDIClock_Slave: connecting to port " << port->name() << std::endl;
79 port->input()->timing.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::update_midi_clock, this, _1, _2));
80 port->input()->start.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::start, this, _1, _2));
81 port->input()->contineu.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::contineu, this, _1, _2));
82 port->input()->stop.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::stop, this, _1, _2));
83 port->input()->position.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::position, this, _1, _2, 3));
87 MIDIClock_Slave::calculate_one_ppqn_in_frames_at(nframes64_t time)
89 const Tempo& current_tempo = session->tempo_map().tempo_at(time);
90 const Meter& current_meter = session->tempo_map().meter_at(time);
91 double frames_per_beat =
92 current_tempo.frames_per_beat(session->frame_rate(),
95 double quarter_notes_per_beat = 4.0 / current_tempo.note_type();
96 double frames_per_quarter_note = frames_per_beat / quarter_notes_per_beat;
98 one_ppqn_in_frames = frames_per_quarter_note / double (ppqn);
102 MIDIClock_Slave::calculate_song_position(uint16_t song_position_in_sixteenth_notes)
104 nframes64_t song_position_frames = 0;
105 for (uint16_t i = 1; i <= song_position_in_sixteenth_notes; ++i) {
106 // one quarter note contains ppqn pulses, so a sixteenth note is ppqn / 4 pulses
107 calculate_one_ppqn_in_frames_at(song_position_frames);
108 song_position_frames += one_ppqn_in_frames * (nframes64_t)(ppqn / 4);
111 return song_position_frames;
115 MIDIClock_Slave::calculate_filter_coefficients()
117 // omega = 2 * PI * Bandwidth / MIDI clock frame frequency in Hz
118 omega = 2.0 * 3.14159265358979323846 * bandwidth * one_ppqn_in_frames / session->frame_rate();
119 b = 1.4142135623730950488 * omega;
124 MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, nframes64_t timestamp)
126 // some pieces of hardware send MIDI Clock all the time
127 if ( (!_starting) && (!_started) ) {
131 calculate_one_ppqn_in_frames_at(should_be_position);
133 nframes64_t elapsed_since_start = timestamp - first_timestamp;
136 if (_starting || last_timestamp == 0) {
137 midi_clock_count = 0;
139 first_timestamp = timestamp;
140 elapsed_since_start = should_be_position;
142 // calculate filter coefficients
143 calculate_filter_coefficients();
146 e2 = double(one_ppqn_in_frames) / double(session->frame_rate());
147 t0 = double(elapsed_since_start) / double(session->frame_rate());
150 // let ardour go after first MIDI Clock Event
154 should_be_position += one_ppqn_in_frames;
155 calculate_filter_coefficients();
157 // calculate loop error
158 // we use session->transport_frame() instead of t1 here
159 // because t1 is used to calculate the transport speed,
160 // so the loop will compensate for accumulating rounding errors
161 error = (double(should_be_position) - double(session->audible_frame()));
162 e = error / double(session->frame_rate());
170 #ifdef DEBUG_MIDI_CLOCK
172 << "MIDI Clock #" << midi_clock_count
173 //<< "@" << timestamp
174 << " arrived at: " << elapsed_since_start << " (elapsed time) "
175 << " should-be transport: " << should_be_position
176 << " audible: " << session->audible_frame()
177 << " real transport: " << session->transport_frame()
178 << " error: " << error
179 //<< " engine: " << session->frame_time()
180 << " real delta: " << timestamp - last_timestamp
181 << " should-be delta: " << one_ppqn_in_frames
182 << " t1-t0: " << (t1 -t0) * session->frame_rate()
183 << " t0: " << t0 * session->frame_rate()
184 << " t1: " << t1 * session->frame_rate()
185 << " frame-rate: " << session->frame_rate()
188 cerr << "frames since cycle start: " << session->frames_since_cycle_start() << endl;
189 #endif // DEBUG_MIDI_CLOCK
191 last_timestamp = timestamp;
195 MIDIClock_Slave::start (Parser& /*parser*/, nframes64_t /*timestamp*/)
197 #ifdef DEBUG_MIDI_CLOCK
198 cerr << "MIDIClock_Slave got start message at time " << timestamp << " engine time: " << session->frame_time() << endl;
210 MIDIClock_Slave::reset ()
213 should_be_position = 0;
219 session->request_locate(0, false);
223 MIDIClock_Slave::contineu (Parser& /*parser*/, nframes64_t /*timestamp*/)
225 #ifdef DEBUG_MIDI_CLOCK
226 std::cerr << "MIDIClock_Slave got continue message" << endl;
236 MIDIClock_Slave::stop (Parser& /*parser*/, nframes64_t /*timestamp*/)
238 #ifdef DEBUG_MIDI_CLOCK
239 std::cerr << "MIDIClock_Slave got stop message" << endl;
242 if (_started || _starting) {
245 // locate to last MIDI clock position
246 session->request_transport_speed(0.0);
248 // we need to go back to the last MIDI beat (6 ppqn)
249 // and lets hope the tempo didnt change in the meantime :)
251 // begin at the should be position, because
252 // that is the position of the last MIDI Clock
253 // message and that is probably what the master
254 // expects where we are right now
255 nframes64_t stop_position = should_be_position;
257 // find out the last MIDI beat: go back #midi_clocks mod 6
258 // and lets hope the tempo didnt change in those last 6 beats :)
259 stop_position -= (midi_clock_count % 6) * one_ppqn_in_frames;
261 session->request_locate(stop_position, false);
262 should_be_position = stop_position;
268 MIDIClock_Slave::position (Parser& /*parser*/, byte* message, size_t size)
270 // we are note supposed to get position messages while we are running
271 // so lets be robust and ignore those
272 if (_started || _starting) {
277 byte lsb = message[1];
278 byte msb = message[2];
279 assert((lsb <= 0x7f) && (msb <= 0x7f));
281 uint16_t position_in_sixteenth_notes = (uint16_t(msb) << 7) | uint16_t(lsb);
282 nframes64_t position_in_frames = calculate_song_position(position_in_sixteenth_notes);
284 #ifdef DEBUG_MIDI_CLOCK
285 cerr << "Song Position: " << position_in_sixteenth_notes << " frames: " << position_in_frames << endl;
288 session->request_locate(position_in_frames, false);
289 should_be_position = position_in_frames;
295 MIDIClock_Slave::locked () const
301 MIDIClock_Slave::ok() const
307 MIDIClock_Slave::starting() const
313 MIDIClock_Slave::stop_if_no_more_clock_events(nframes64_t& pos, nframes64_t now)
315 /* no timecode for 1/4 second ? conclude that its stopped */
316 if (last_timestamp &&
317 now > last_timestamp &&
318 now - last_timestamp > session->frame_rate() / 4) {
319 #ifdef DEBUG_MIDI_CLOCK
320 cerr << "No MIDI Clock frames received for some time, stopping!" << endl;
322 pos = should_be_position;
323 session->request_transport_speed (0);
324 session->request_locate (should_be_position, false);
332 MIDIClock_Slave::speed_and_position (double& speed, nframes64_t& pos)
334 if (!_started || _starting) {
336 pos = should_be_position;
340 nframes64_t engine_now = session->frame_time();
342 if (stop_if_no_more_clock_events(pos, engine_now)) {
347 speed = ((t1 - t0) * session->frame_rate()) / one_ppqn_in_frames;
349 // calculate position
350 if (engine_now > last_timestamp) {
351 // we are in between MIDI clock messages
352 // so we interpolate position according to speed
353 nframes64_t elapsed = engine_now - last_timestamp;
354 pos = (nframes64_t) (should_be_position + double(elapsed) * speed);
356 // A new MIDI clock message has arrived this cycle
357 pos = should_be_position;
360 #ifdef DEBUG_MIDI_CLOCK
361 cerr << "speed_and_position: " << speed << " & " << pos << " <-> " << session->transport_frame() << " (transport)" << endl;
368 MIDIClock_Slave::resolution() const
371 return (nframes_t) one_ppqn_in_frames * ppqn;