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)));
78 MIDIClock_Slave::calculate_one_ppqn_in_frames_at(nframes_t time)
80 const Tempo& current_tempo = session.tempo_map().tempo_at(time);
81 const Meter& current_meter = session.tempo_map().meter_at(time);
82 double frames_per_beat =
83 current_tempo.frames_per_beat(session.frame_rate(),
86 double quarter_notes_per_beat = 4.0 / current_tempo.note_type();
87 double frames_per_quarter_note = frames_per_beat / quarter_notes_per_beat;
89 one_ppqn_in_frames = frames_per_quarter_note / double (ppqn);
93 MIDIClock_Slave::calculate_filter_coefficients()
95 // omega = 2 * PI * Bandwidth / MIDI clock frame frequency in Hz
96 omega = 2.0 * 3.14159265358979323846 * bandwidth * one_ppqn_in_frames / session.frame_rate();
97 b = 1.4142135623730950488 * omega;
102 MIDIClock_Slave::update_midi_clock (Parser& parser, nframes_t timestamp)
104 // the number of midi clock messages (zero-based)
105 static long midi_clock_count;
107 calculate_one_ppqn_in_frames_at(last_position);
109 nframes_t timestamp_relative_to_transport = timestamp - first_timestamp;
112 midi_clock_count = 0;
113 assert(last_timestamp == 0);
114 assert(last_position == 0);
116 first_timestamp = timestamp;
117 timestamp_relative_to_transport = 0;
119 // calculate filter coefficients
120 calculate_filter_coefficients();
123 e2 = double(one_ppqn_in_frames) / double(session.frame_rate());
124 t0 = double(timestamp_relative_to_transport) / double(session.frame_rate());
127 // let ardour go after first MIDI Clock Event
131 last_position += one_ppqn_in_frames;
132 calculate_filter_coefficients();
134 // calculate loop error
135 // we use session.transport_frame() instead of t1 here
136 // because t1 is used to calculate the transport speed, and since this
137 // is float, the loop will compensate for accumulating rounding errors
138 e = (double(last_position) - double(session.transport_frame()))
139 / double(session.frame_rate());
148 #ifdef DEBUG_MIDI_CLOCK
150 << "MIDI Clock #" << midi_clock_count
151 //<< "@" << timestamp
152 << " (transport-relative: " << timestamp_relative_to_transport << " should be: " << last_position << ", delta: " << (double(last_position) - double(session.transport_frame())) <<" )"
153 << " transport: " << session.transport_frame()
154 //<< " engine: " << session.engine().frame_time()
155 << " real delta: " << timestamp - last_timestamp
156 << " reference: " << one_ppqn_in_frames
157 << " t1-t0: " << (t1 -t0) * session.frame_rate()
158 << " t0: " << t0 * session.frame_rate()
159 << " t1: " << t1 * session.frame_rate()
160 << " frame-rate: " << session.frame_rate()
162 #endif // DEBUG_MIDI_CLOCK
164 last_timestamp = timestamp;
168 MIDIClock_Slave::start (Parser& parser, nframes_t timestamp)
170 #ifdef DEBUG_MIDI_CLOCK
171 cerr << "MIDIClock_Slave got start message at time " << timestamp << " session time: " << session.engine().frame_time() << endl;
182 MIDIClock_Slave::contineu (Parser& parser, nframes_t timestamp)
184 #ifdef DEBUG_MIDI_CLOCK
185 std::cerr << "MIDIClock_Slave got continue message" << endl;
187 start(parser, timestamp);
192 MIDIClock_Slave::stop (Parser& parser, nframes_t timestamp)
194 #ifdef DEBUG_MIDI_CLOCK
195 std::cerr << "MIDIClock_Slave got stop message" << endl;
206 MIDIClock_Slave::locked () const
212 MIDIClock_Slave::ok() const
218 MIDIClock_Slave::starting() const
224 MIDIClock_Slave::stop_if_no_more_clock_events(nframes_t& pos, nframes_t now)
226 /* no timecode for 1/4 second ? conclude that its stopped */
227 if (last_timestamp &&
228 now > last_timestamp &&
229 now - last_timestamp > session.frame_rate() / 4) {
230 #ifdef DEBUG_MIDI_CLOCK
231 cerr << "No MIDI Clock frames received for some time, stopping!" << endl;
234 session.request_locate (pos, false);
235 session.request_transport_speed (0);
236 this->stop(*port->input(), now);
245 MIDIClock_Slave::speed_and_position (float& speed, nframes_t& pos)
247 if (!_started || _starting) {
253 nframes_t engine_now = session.engine().frame_time();
255 if (stop_if_no_more_clock_events(pos, engine_now)) {
260 double speed_double = ((t1 - t0) * session.frame_rate()) / one_ppqn_in_frames;
261 speed = float(speed_double);
263 // calculate position
264 if (engine_now > last_timestamp) {
265 // we are in between MIDI clock messages
266 // so we interpolate position according to speed
267 nframes_t elapsed = engine_now - last_timestamp;
268 pos = nframes_t (last_position + double(elapsed) * speed_double);
270 // A new MIDI clock message has arrived this cycle
278 MIDIClock_Slave::resolution() const
281 return (nframes_t) one_ppqn_in_frames * ppqn;
285 MIDIClock_Slave::reset ()
291 session.request_locate(0, false);