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>
40 using namespace ARDOUR;
45 MIDIClock_Slave::MIDIClock_Slave (Session& s, MIDI::Port& p, int ppqn)
48 , bandwidth (30.0 / 60.0) // 1 BpM = 1 / 60 Hz
54 MIDIClock_Slave::~MIDIClock_Slave()
59 MIDIClock_Slave::rebind (MIDI::Port& p)
61 for (vector<sigc::connection>::iterator i = connections.begin(); i != connections.end(); ++i) {
67 #ifdef DEBUG_MIDI_CLOCK
68 std::cerr << "MIDIClock_Slave: connecting to port " << port->name() << std::endl;
71 connections.push_back (port->input()->timing.connect (mem_fun (*this, &MIDIClock_Slave::update_midi_clock)));
72 connections.push_back (port->input()->start.connect (mem_fun (*this, &MIDIClock_Slave::start)));
73 connections.push_back (port->input()->contineu.connect (mem_fun (*this, &MIDIClock_Slave::contineu)));
74 connections.push_back (port->input()->stop.connect (mem_fun (*this, &MIDIClock_Slave::stop)));
75 connections.push_back (port->input()->position.connect (mem_fun (*this, &MIDIClock_Slave::position)));
79 MIDIClock_Slave::calculate_one_ppqn_in_frames_at(nframes_t time)
81 const Tempo& current_tempo = session.tempo_map().tempo_at(time);
82 const Meter& current_meter = session.tempo_map().meter_at(time);
83 double frames_per_beat =
84 current_tempo.frames_per_beat(session.frame_rate(),
87 double quarter_notes_per_beat = 4.0 / current_tempo.note_type();
88 double frames_per_quarter_note = frames_per_beat / quarter_notes_per_beat;
90 one_ppqn_in_frames = frames_per_quarter_note / double (ppqn);
94 MIDIClock_Slave::calculate_song_position(uint16_t song_position_in_sixteenth_notes)
96 nframes_t song_position_frames = 0;
97 for (uint16_t i = 1; i <= song_position_in_sixteenth_notes; ++i) {
98 // one quarter note contains ppqn pulses, so a sixteenth note is ppqn / 4 pulses
99 calculate_one_ppqn_in_frames_at(song_position_frames);
100 song_position_frames += one_ppqn_in_frames * nframes_t(ppqn / 4);
103 return song_position_frames;
107 MIDIClock_Slave::calculate_filter_coefficients()
109 // omega = 2 * PI * Bandwidth / MIDI clock frame frequency in Hz
110 omega = 2.0 * 3.14159265358979323846 * bandwidth * one_ppqn_in_frames / session.frame_rate();
111 b = 1.4142135623730950488 * omega;
116 MIDIClock_Slave::update_midi_clock (Parser& parser, nframes_t timestamp)
118 // some pieces of hardware send MIDI Clock all the time
119 if ( (!_starting) && (!_started) ) {
123 calculate_one_ppqn_in_frames_at(should_be_position);
125 nframes_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 // calculate filter coefficients
135 calculate_filter_coefficients();
138 e2 = double(one_ppqn_in_frames) / double(session.frame_rate());
139 t0 = double(elapsed_since_start) / double(session.frame_rate());
142 // let ardour go after first MIDI Clock Event
146 should_be_position += one_ppqn_in_frames;
147 calculate_filter_coefficients();
149 // calculate loop error
150 // we use session.transport_frame() instead of t1 here
151 // because t1 is used to calculate the transport speed,
152 // so the loop will compensate for accumulating rounding errors
153 error = (double(should_be_position) - double(session.audible_frame()));
154 e = error / double(session.frame_rate());
162 #ifdef DEBUG_MIDI_CLOCK
164 << "MIDI Clock #" << midi_clock_count
165 //<< "@" << timestamp
166 << " arrived at: " << elapsed_since_start << " (elapsed time) "
167 << " should-be transport: " << should_be_position
168 << " audible: " << session.audible_frame()
169 << " real transport: " << session.transport_frame()
170 << " error: " << error
171 //<< " engine: " << session.engine().frame_time()
172 << " real delta: " << timestamp - last_timestamp
173 << " should-be delta: " << one_ppqn_in_frames
174 << " t1-t0: " << (t1 -t0) * session.frame_rate()
175 << " t0: " << t0 * session.frame_rate()
176 << " t1: " << t1 * session.frame_rate()
177 << " frame-rate: " << session.frame_rate()
180 cerr << "frames since cycle start: " << session.engine().frames_since_cycle_start() << endl;
181 #endif // DEBUG_MIDI_CLOCK
183 last_timestamp = timestamp;
187 MIDIClock_Slave::start (Parser& parser, nframes_t timestamp)
189 #ifdef DEBUG_MIDI_CLOCK
190 cerr << "MIDIClock_Slave got start message at time " << timestamp << " engine time: " << session.engine().frame_time() << endl;
202 MIDIClock_Slave::reset ()
205 should_be_position = 0;
211 session.request_locate(0, false);
215 MIDIClock_Slave::contineu (Parser& parser, nframes_t timestamp)
217 #ifdef DEBUG_MIDI_CLOCK
218 std::cerr << "MIDIClock_Slave got continue message" << endl;
228 MIDIClock_Slave::stop (Parser& parser, nframes_t timestamp)
230 #ifdef DEBUG_MIDI_CLOCK
231 std::cerr << "MIDIClock_Slave got stop message" << endl;
234 if (_started || _starting) {
237 // locate to last MIDI clock position
238 session.request_transport_speed(0.0);
240 // we need to go back to the last MIDI beat (6 ppqn)
241 // and lets hope the tempo didnt change in the meantime :)
243 // begin at the should be position, because
244 // that is the position of the last MIDI Clock
245 // message and that is probably what the master
246 // expects where we are right now
247 nframes_t stop_position = should_be_position;
249 // find out the last MIDI beat: go back #midi_clocks mod 6
250 // and lets hope the tempo didnt change in those last 6 beats :)
251 stop_position -= (midi_clock_count % 6) * one_ppqn_in_frames;
253 session.request_locate(stop_position, false);
254 should_be_position = stop_position;
260 MIDIClock_Slave::position (Parser& parser, byte* message, size_t size)
262 // we are note supposed to get position messages while we are running
263 // so lets be robust and ignore those
264 if (_started || _starting) {
269 byte lsb = message[1];
270 byte msb = message[2];
271 assert((lsb <= 0x7f) && (msb <= 0x7f));
273 uint16_t position_in_sixteenth_notes = (uint16_t(msb) << 7) | uint16_t(lsb);
274 nframes_t position_in_frames = calculate_song_position(position_in_sixteenth_notes);
276 #ifdef DEBUG_MIDI_CLOCK
277 cerr << "Song Position: " << position_in_sixteenth_notes << " frames: " << position_in_frames << endl;
280 session.request_locate(position_in_frames, false);
281 should_be_position = position_in_frames;
287 MIDIClock_Slave::locked () const
293 MIDIClock_Slave::ok() const
299 MIDIClock_Slave::starting() const
305 MIDIClock_Slave::stop_if_no_more_clock_events(nframes_t& pos, nframes_t now)
307 /* no timecode for 1/4 second ? conclude that its stopped */
308 if (last_timestamp &&
309 now > last_timestamp &&
310 now - last_timestamp > session.frame_rate() / 4) {
311 #ifdef DEBUG_MIDI_CLOCK
312 cerr << "No MIDI Clock frames received for some time, stopping!" << endl;
314 pos = should_be_position;
315 session.request_transport_speed (0);
316 session.request_locate (should_be_position, false);
324 MIDIClock_Slave::speed_and_position (double& speed, nframes_t& pos)
326 if (!_started || _starting) {
328 pos = should_be_position;
332 nframes_t engine_now = session.engine().frame_time();
334 if (stop_if_no_more_clock_events(pos, engine_now)) {
339 speed = ((t1 - t0) * session.frame_rate()) / one_ppqn_in_frames;
341 // calculate position
342 if (engine_now > last_timestamp) {
343 // we are in between MIDI clock messages
344 // so we interpolate position according to speed
345 nframes_t elapsed = engine_now - last_timestamp;
346 pos = nframes_t (should_be_position + double(elapsed) * speed);
348 // A new MIDI clock message has arrived this cycle
349 pos = should_be_position;
352 #ifdef DEBUG_MIDI_CLOCK
353 cerr << "speed_and_position: " << speed << " & " << pos << " <-> " << session.transport_frame() << " (transport)" << endl;
360 MIDIClock_Slave::resolution() const
363 return (nframes_t) one_ppqn_in_frames * ppqn;