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)
48 , bandwidth (30.0 / 60.0) // 1 BpM = 1 / 60 Hz
50 session = (ISlaveSessionProxy *) new SlaveSessionProxy(s);
55 MIDIClock_Slave::MIDIClock_Slave (ISlaveSessionProxy* session_proxy, int ppqn)
56 : session(session_proxy)
58 , bandwidth (30.0 / 60.0) // 1 BpM = 1 / 60 Hz
60 session = session_proxy;
64 MIDIClock_Slave::~MIDIClock_Slave()
70 MIDIClock_Slave::rebind (MIDI::Port& p)
72 for (vector<sigc::connection>::iterator i = connections.begin(); i != connections.end(); ++i) {
78 #ifdef DEBUG_MIDI_CLOCK
79 std::cerr << "MIDIClock_Slave: connecting to port " << port->name() << std::endl;
82 connections.push_back (port->input()->timing.connect (mem_fun (*this, &MIDIClock_Slave::update_midi_clock)));
83 connections.push_back (port->input()->start.connect (mem_fun (*this, &MIDIClock_Slave::start)));
84 connections.push_back (port->input()->contineu.connect (mem_fun (*this, &MIDIClock_Slave::contineu)));
85 connections.push_back (port->input()->stop.connect (mem_fun (*this, &MIDIClock_Slave::stop)));
86 connections.push_back (port->input()->position.connect (mem_fun (*this, &MIDIClock_Slave::position)));
90 MIDIClock_Slave::calculate_one_ppqn_in_frames_at(nframes_t time)
92 const Tempo& current_tempo = session->tempo_map().tempo_at(time);
93 const Meter& current_meter = session->tempo_map().meter_at(time);
94 double frames_per_beat =
95 current_tempo.frames_per_beat(session->frame_rate(),
98 double quarter_notes_per_beat = 4.0 / current_tempo.note_type();
99 double frames_per_quarter_note = frames_per_beat / quarter_notes_per_beat;
101 one_ppqn_in_frames = frames_per_quarter_note / double (ppqn);
105 MIDIClock_Slave::calculate_song_position(uint16_t song_position_in_sixteenth_notes)
107 nframes_t song_position_frames = 0;
108 for (uint16_t i = 1; i <= song_position_in_sixteenth_notes; ++i) {
109 // one quarter note contains ppqn pulses, so a sixteenth note is ppqn / 4 pulses
110 calculate_one_ppqn_in_frames_at(song_position_frames);
111 song_position_frames += one_ppqn_in_frames * nframes_t(ppqn / 4);
114 return song_position_frames;
118 MIDIClock_Slave::calculate_filter_coefficients()
120 // omega = 2 * PI * Bandwidth / MIDI clock frame frequency in Hz
121 omega = 2.0 * 3.14159265358979323846 * bandwidth * one_ppqn_in_frames / session->frame_rate();
122 b = 1.4142135623730950488 * omega;
127 MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, nframes_t timestamp)
129 // some pieces of hardware send MIDI Clock all the time
130 if ( (!_starting) && (!_started) ) {
134 calculate_one_ppqn_in_frames_at(should_be_position);
136 nframes_t elapsed_since_start = timestamp - first_timestamp;
139 if (_starting || last_timestamp == 0) {
140 midi_clock_count = 0;
142 first_timestamp = timestamp;
143 elapsed_since_start = should_be_position;
145 // calculate filter coefficients
146 calculate_filter_coefficients();
149 e2 = double(one_ppqn_in_frames) / double(session->frame_rate());
150 t0 = double(elapsed_since_start) / double(session->frame_rate());
153 // let ardour go after first MIDI Clock Event
157 should_be_position += one_ppqn_in_frames;
158 calculate_filter_coefficients();
160 // calculate loop error
161 // we use session->transport_frame() instead of t1 here
162 // because t1 is used to calculate the transport speed,
163 // so the loop will compensate for accumulating rounding errors
164 error = (double(should_be_position) - double(session->audible_frame()));
165 e = error / double(session->frame_rate());
173 #ifdef DEBUG_MIDI_CLOCK
175 << "MIDI Clock #" << midi_clock_count
176 //<< "@" << timestamp
177 << " arrived at: " << elapsed_since_start << " (elapsed time) "
178 << " should-be transport: " << should_be_position
179 << " audible: " << session->audible_frame()
180 << " real transport: " << session->transport_frame()
181 << " error: " << error
182 //<< " engine: " << session->frame_time()
183 << " real delta: " << timestamp - last_timestamp
184 << " should-be delta: " << one_ppqn_in_frames
185 << " t1-t0: " << (t1 -t0) * session->frame_rate()
186 << " t0: " << t0 * session->frame_rate()
187 << " t1: " << t1 * session->frame_rate()
188 << " frame-rate: " << session->frame_rate()
191 cerr << "frames since cycle start: " << session->frames_since_cycle_start() << endl;
192 #endif // DEBUG_MIDI_CLOCK
194 last_timestamp = timestamp;
198 MIDIClock_Slave::start (Parser& /*parser*/, nframes_t /*timestamp*/)
200 #ifdef DEBUG_MIDI_CLOCK
201 cerr << "MIDIClock_Slave got start message at time " << timestamp << " engine time: " << session->frame_time() << endl;
213 MIDIClock_Slave::reset ()
216 should_be_position = 0;
222 session->request_locate(0, false);
226 MIDIClock_Slave::contineu (Parser& /*parser*/, nframes_t /*timestamp*/)
228 #ifdef DEBUG_MIDI_CLOCK
229 std::cerr << "MIDIClock_Slave got continue message" << endl;
239 MIDIClock_Slave::stop (Parser& /*parser*/, nframes_t /*timestamp*/)
241 #ifdef DEBUG_MIDI_CLOCK
242 std::cerr << "MIDIClock_Slave got stop message" << endl;
245 if (_started || _starting) {
248 // locate to last MIDI clock position
249 session->request_transport_speed(0.0);
251 // we need to go back to the last MIDI beat (6 ppqn)
252 // and lets hope the tempo didnt change in the meantime :)
254 // begin at the should be position, because
255 // that is the position of the last MIDI Clock
256 // message and that is probably what the master
257 // expects where we are right now
258 nframes_t stop_position = should_be_position;
260 // find out the last MIDI beat: go back #midi_clocks mod 6
261 // and lets hope the tempo didnt change in those last 6 beats :)
262 stop_position -= (midi_clock_count % 6) * one_ppqn_in_frames;
264 session->request_locate(stop_position, false);
265 should_be_position = stop_position;
271 MIDIClock_Slave::position (Parser& /*parser*/, byte* message, size_t size)
273 // we are note supposed to get position messages while we are running
274 // so lets be robust and ignore those
275 if (_started || _starting) {
280 byte lsb = message[1];
281 byte msb = message[2];
282 assert((lsb <= 0x7f) && (msb <= 0x7f));
284 uint16_t position_in_sixteenth_notes = (uint16_t(msb) << 7) | uint16_t(lsb);
285 nframes_t position_in_frames = calculate_song_position(position_in_sixteenth_notes);
287 #ifdef DEBUG_MIDI_CLOCK
288 cerr << "Song Position: " << position_in_sixteenth_notes << " frames: " << position_in_frames << endl;
291 session->request_locate(position_in_frames, false);
292 should_be_position = position_in_frames;
298 MIDIClock_Slave::locked () const
304 MIDIClock_Slave::ok() const
310 MIDIClock_Slave::starting() const
316 MIDIClock_Slave::stop_if_no_more_clock_events(nframes_t& pos, nframes_t now)
318 /* no timecode for 1/4 second ? conclude that its stopped */
319 if (last_timestamp &&
320 now > last_timestamp &&
321 now - last_timestamp > session->frame_rate() / 4) {
322 #ifdef DEBUG_MIDI_CLOCK
323 cerr << "No MIDI Clock frames received for some time, stopping!" << endl;
325 pos = should_be_position;
326 session->request_transport_speed (0);
327 session->request_locate (should_be_position, false);
335 MIDIClock_Slave::speed_and_position (double& speed, nframes_t& pos)
337 if (!_started || _starting) {
339 pos = should_be_position;
343 nframes_t engine_now = session->frame_time();
345 if (stop_if_no_more_clock_events(pos, engine_now)) {
350 speed = ((t1 - t0) * session->frame_rate()) / one_ppqn_in_frames;
352 // calculate position
353 if (engine_now > last_timestamp) {
354 // we are in between MIDI clock messages
355 // so we interpolate position according to speed
356 nframes_t elapsed = engine_now - last_timestamp;
357 pos = nframes_t (should_be_position + double(elapsed) * speed);
359 // A new MIDI clock message has arrived this cycle
360 pos = should_be_position;
363 #ifdef DEBUG_MIDI_CLOCK
364 cerr << "speed_and_position: " << speed << " & " << pos << " <-> " << session->transport_frame() << " (transport)" << endl;
371 MIDIClock_Slave::resolution() const
374 return (nframes_t) one_ppqn_in_frames * ppqn;