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;
46 MIDIClock_Slave::MIDIClock_Slave (Session& s, MIDI::Port& p, int ppqn)
49 , bandwidth (30.0 / 60.0) // 1 BpM = 1 / 60 Hz
55 MIDIClock_Slave::~MIDIClock_Slave()
60 MIDIClock_Slave::rebind (MIDI::Port& p)
62 for (vector<sigc::connection>::iterator i = connections.begin(); i != connections.end(); ++i) {
68 #ifdef DEBUG_MIDI_CLOCK
69 std::cerr << "MIDIClock_Slave: connecting to port " << port->name() << std::endl;
72 connections.push_back (port->input()->timing.connect (mem_fun (*this, &MIDIClock_Slave::update_midi_clock)));
73 connections.push_back (port->input()->start.connect (mem_fun (*this, &MIDIClock_Slave::start)));
74 connections.push_back (port->input()->contineu.connect (mem_fun (*this, &MIDIClock_Slave::contineu)));
75 connections.push_back (port->input()->stop.connect (mem_fun (*this, &MIDIClock_Slave::stop)));
76 connections.push_back (port->input()->position.connect (mem_fun (*this, &MIDIClock_Slave::position)));
80 MIDIClock_Slave::calculate_one_ppqn_in_frames_at(nframes_t time)
82 const Tempo& current_tempo = session.tempo_map().tempo_at(time);
83 const Meter& current_meter = session.tempo_map().meter_at(time);
84 double frames_per_beat =
85 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);
95 MIDIClock_Slave::calculate_song_position(uint16_t song_position_in_sixteenth_notes)
97 nframes_t song_position_frames = 0;
98 for (uint16_t i = 1; i <= song_position_in_sixteenth_notes; ++i) {
99 // one quarter note contains ppqn pulses, so a sixteenth note is ppqn / 4 pulses
100 calculate_one_ppqn_in_frames_at(song_position_frames);
101 song_position_frames += one_ppqn_in_frames * nframes_t(ppqn / 4);
104 return song_position_frames;
108 MIDIClock_Slave::calculate_filter_coefficients()
110 // omega = 2 * PI * Bandwidth / MIDI clock frame frequency in Hz
111 omega = 2.0 * 3.14159265358979323846 * bandwidth * one_ppqn_in_frames / session.frame_rate();
112 b = 1.4142135623730950488 * omega;
117 MIDIClock_Slave::update_midi_clock (Parser& parser, nframes_t timestamp)
119 // some pieces of hardware send MIDI Clock all the time
120 if ( (!_starting) && (!_started) ) {
124 calculate_one_ppqn_in_frames_at(should_be_position);
126 nframes_t elapsed_since_start = timestamp - first_timestamp;
129 if (_starting || last_timestamp == 0) {
130 midi_clock_count = 0;
132 first_timestamp = timestamp;
133 elapsed_since_start = should_be_position;
135 // calculate filter coefficients
136 calculate_filter_coefficients();
139 e2 = double(one_ppqn_in_frames) / double(session.frame_rate());
140 t0 = double(elapsed_since_start) / double(session.frame_rate());
143 // let ardour go after first MIDI Clock Event
147 should_be_position += one_ppqn_in_frames;
148 calculate_filter_coefficients();
150 // calculate loop error
151 // we use session.transport_frame() instead of t1 here
152 // because t1 is used to calculate the transport speed,
153 // so the loop will compensate for accumulating rounding errors
154 error = (double(should_be_position) - double(session.audible_frame()));
155 e = error / double(session.frame_rate());
163 #ifdef DEBUG_MIDI_CLOCK
165 << "MIDI Clock #" << midi_clock_count
166 //<< "@" << timestamp
167 << " arrived at: " << elapsed_since_start << " (elapsed time) "
168 << " should-be transport: " << should_be_position
169 << " audible: " << session.audible_frame()
170 << " real transport: " << session.transport_frame()
171 << " error: " << error
172 //<< " engine: " << session.engine().frame_time()
173 << " real delta: " << timestamp - last_timestamp
174 << " should-be delta: " << one_ppqn_in_frames
175 << " t1-t0: " << (t1 -t0) * session.frame_rate()
176 << " t0: " << t0 * session.frame_rate()
177 << " t1: " << t1 * session.frame_rate()
178 << " frame-rate: " << session.frame_rate()
181 cerr << "frames since cycle start: " << session.engine().frames_since_cycle_start() << endl;
182 #endif // DEBUG_MIDI_CLOCK
184 last_timestamp = timestamp;
188 MIDIClock_Slave::start (Parser& parser, nframes_t timestamp)
190 #ifdef DEBUG_MIDI_CLOCK
191 cerr << "MIDIClock_Slave got start message at time " << timestamp << " engine time: " << session.engine().frame_time() << endl;
203 MIDIClock_Slave::reset ()
206 should_be_position = 0;
212 session.request_locate(0, false);
216 MIDIClock_Slave::contineu (Parser& parser, nframes_t timestamp)
218 #ifdef DEBUG_MIDI_CLOCK
219 std::cerr << "MIDIClock_Slave got continue message" << endl;
229 MIDIClock_Slave::stop (Parser& parser, nframes_t timestamp)
231 #ifdef DEBUG_MIDI_CLOCK
232 std::cerr << "MIDIClock_Slave got stop message" << endl;
235 if (_started || _starting) {
238 // locate to last MIDI clock position
239 session.request_transport_speed(0.0);
241 // we need to go back to the last MIDI beat (6 ppqn)
242 // and lets hope the tempo didnt change in the meantime :)
244 // begin at the should be position, because
245 // that is the position of the last MIDI Clock
246 // message and that is probably what the master
247 // expects where we are right now
248 nframes_t stop_position = should_be_position;
250 // find out the last MIDI beat: go back #midi_clocks mod 6
251 // and lets hope the tempo didnt change in those last 6 beats :)
252 stop_position -= (midi_clock_count % 6) * one_ppqn_in_frames;
254 session.request_locate(stop_position, false);
255 should_be_position = stop_position;
261 MIDIClock_Slave::position (Parser& parser, byte* message, size_t size)
263 // we are note supposed to get position messages while we are running
264 // so lets be robust and ignore those
265 if (_started || _starting) {
270 byte lsb = message[1];
271 byte msb = message[2];
272 assert((lsb <= 0x7f) && (msb <= 0x7f));
274 uint16_t position_in_sixteenth_notes = (uint16_t(msb) << 7) | uint16_t(lsb);
275 nframes_t position_in_frames = calculate_song_position(position_in_sixteenth_notes);
277 #ifdef DEBUG_MIDI_CLOCK
278 cerr << "Song Position: " << position_in_sixteenth_notes << " frames: " << position_in_frames << endl;
281 session.request_locate(position_in_frames, false);
282 should_be_position = position_in_frames;
288 MIDIClock_Slave::locked () const
294 MIDIClock_Slave::ok() const
300 MIDIClock_Slave::starting() const
306 MIDIClock_Slave::stop_if_no_more_clock_events(nframes_t& pos, nframes_t now)
308 /* no timecode for 1/4 second ? conclude that its stopped */
309 if (last_timestamp &&
310 now > last_timestamp &&
311 now - last_timestamp > session.frame_rate() / 4) {
312 #ifdef DEBUG_MIDI_CLOCK
313 cerr << "No MIDI Clock frames received for some time, stopping!" << endl;
315 pos = should_be_position;
316 session.request_transport_speed (0);
317 session.request_locate (should_be_position, false);
325 MIDIClock_Slave::speed_and_position (double& speed, nframes_t& pos)
327 if (!_started || _starting) {
329 pos = should_be_position;
333 nframes_t engine_now = session.engine().frame_time();
335 if (stop_if_no_more_clock_events(pos, engine_now)) {
340 speed = ((t1 - t0) * session.frame_rate()) / one_ppqn_in_frames;
342 // calculate position
343 if (engine_now > last_timestamp) {
344 // we are in between MIDI clock messages
345 // so we interpolate position according to speed
346 nframes_t elapsed = engine_now - last_timestamp;
347 pos = nframes_t (should_be_position + double(elapsed) * speed);
349 // A new MIDI clock message has arrived this cycle
350 pos = should_be_position;
353 #ifdef DEBUG_MIDI_CLOCK
354 cerr << "speed_and_position: " << speed << " & " << pos << " <-> " << session.transport_frame() << " (transport)" << endl;
361 MIDIClock_Slave::resolution() const
364 return (nframes_t) one_ppqn_in_frames * ppqn;