2 Copyright (C) 2008 Hans Baier
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <sys/types.h>
24 #include <pbd/error.h>
25 #include <pbd/failed_constructor.h>
26 #include <pbd/pthread_utils.h>
28 #include <midi++/port.h>
29 #include <midi++/jack.h>
30 #include <ardour/slave.h>
31 #include <ardour/session.h>
32 #include <ardour/audioengine.h>
33 #include <ardour/cycles.h>
34 #include <ardour/tempo.h>
39 using namespace ARDOUR;
44 MIDIClock_Slave::MIDIClock_Slave (Session& s, MIDI::Port& p, int ppqn)
47 , bandwidth (30.0 / 60.0) // 1 BpM = 1 / 60 Hz
53 MIDIClock_Slave::~MIDIClock_Slave()
58 MIDIClock_Slave::rebind (MIDI::Port& p)
60 for (vector<sigc::connection>::iterator i = connections.begin(); i != connections.end(); ++i) {
66 #ifdef DEBUG_MIDI_CLOCK
67 std::cerr << "MIDIClock_Slave: connecting to port " << port->name() << std::endl;
70 connections.push_back (port->input()->timing.connect (mem_fun (*this, &MIDIClock_Slave::update_midi_clock)));
71 connections.push_back (port->input()->start.connect (mem_fun (*this, &MIDIClock_Slave::start)));
72 connections.push_back (port->input()->contineu.connect (mem_fun (*this, &MIDIClock_Slave::contineu)));
73 connections.push_back (port->input()->stop.connect (mem_fun (*this, &MIDIClock_Slave::stop)));
77 MIDIClock_Slave::calculate_one_ppqn_in_frames_at(nframes_t time)
79 const Tempo& current_tempo = session.tempo_map().tempo_at(time);
80 const Meter& current_meter = session.tempo_map().meter_at(time);
81 double frames_per_beat =
82 current_tempo.frames_per_beat(session.frame_rate(),
85 double quarter_notes_per_beat = 4.0 / current_tempo.note_type();
86 double frames_per_quarter_note = frames_per_beat / quarter_notes_per_beat;
88 one_ppqn_in_frames = frames_per_quarter_note / double (ppqn);
92 MIDIClock_Slave::calculate_filter_coefficients()
94 // omega = 2 * PI * Bandwidth / MIDI clock frame frequency in Hz
95 omega = 2.0 * 3.14159265358979323846 * bandwidth * one_ppqn_in_frames / session.frame_rate();
96 b = 1.4142135623730950488 * omega;
101 MIDIClock_Slave::update_midi_clock (Parser& parser, nframes_t timestamp)
103 // the number of midi clock messages (zero-based)
104 static long midi_clock_count;
106 calculate_one_ppqn_in_frames_at(last_position);
108 nframes_t timestamp_relative_to_transport = timestamp - first_timestamp;
111 midi_clock_count = 0;
112 assert(last_timestamp == 0);
113 assert(last_position == 0);
115 first_timestamp = timestamp;
116 timestamp_relative_to_transport = 0;
118 // calculate filter coefficients
119 calculate_filter_coefficients();
122 e2 = double(one_ppqn_in_frames) / double(session.frame_rate());
123 t0 = double(timestamp_relative_to_transport) / double(session.frame_rate());
126 // let ardour go after first MIDI Clock Event
130 last_position += one_ppqn_in_frames;
131 calculate_filter_coefficients();
133 // calculate loop error
134 // we use session.transport_frame() instead of t1 here
135 // because t1 is used to calculate the transport speed, and since this
136 // is float, the loop will compensate for accumulating rounding errors
137 e = (double(last_position) - double(session.transport_frame()))
138 / double(session.frame_rate());
147 #ifdef DEBUG_MIDI_CLOCK
149 << "MIDI Clock #" << midi_clock_count
150 //<< "@" << timestamp
151 << " (transport-relative: " << timestamp_relative_to_transport << " should be: " << last_position << ", delta: " << (double(last_position) - double(session.transport_frame())) <<" )"
152 << " transport: " << session.transport_frame()
153 //<< " engine: " << session.engine().frame_time()
154 << " real delta: " << timestamp - last_timestamp
155 << " reference: " << one_ppqn_in_frames
156 << " t1-t0: " << (t1 -t0) * session.frame_rate()
157 << " t0: " << t0 * session.frame_rate()
158 << " t1: " << t1 * session.frame_rate()
159 << " frame-rate: " << session.frame_rate()
161 #endif // DEBUG_MIDI_CLOCK
163 last_timestamp = timestamp;
167 MIDIClock_Slave::start (Parser& parser, nframes_t timestamp)
169 #ifdef DEBUG_MIDI_CLOCK
170 cerr << "MIDIClock_Slave got start message at time " << timestamp << " session time: " << session.engine().frame_time() << endl;
181 MIDIClock_Slave::contineu (Parser& parser, nframes_t timestamp)
183 #ifdef DEBUG_MIDI_CLOCK
184 std::cerr << "MIDIClock_Slave got continue message" << endl;
186 start(parser, timestamp);
191 MIDIClock_Slave::stop (Parser& parser, nframes_t timestamp)
193 #ifdef DEBUG_MIDI_CLOCK
194 std::cerr << "MIDIClock_Slave got stop message" << endl;
205 MIDIClock_Slave::locked () const
211 MIDIClock_Slave::ok() const
217 MIDIClock_Slave::starting() const
223 MIDIClock_Slave::stop_if_no_more_clock_events(nframes_t& pos, nframes_t now)
225 /* no timecode for 1/4 second ? conclude that its stopped */
226 if (last_timestamp &&
227 now > last_timestamp &&
228 now - last_timestamp > session.frame_rate() / 4) {
229 #ifdef DEBUG_MIDI_CLOCK
230 cerr << "No MIDI Clock frames received for some time, stopping!" << endl;
233 session.request_locate (pos, false);
234 session.request_transport_speed (0);
235 this->stop(*port->input(), now);
244 MIDIClock_Slave::speed_and_position (float& speed, nframes_t& pos)
246 if (!_started || _starting) {
252 nframes_t engine_now = session.engine().frame_time();
254 if (stop_if_no_more_clock_events(pos, engine_now)) {
259 double speed_double = ((t1 - t0) * session.frame_rate()) / one_ppqn_in_frames;
260 speed = float(speed_double);
262 // calculate position
263 if (engine_now > last_timestamp) {
264 // we are in between MIDI clock messages
265 // so we interpolate position according to speed
266 nframes_t elapsed = engine_now - last_timestamp;
267 pos = nframes_t (last_position + double(elapsed) * speed_double);
269 // A new MIDI clock message has arrived this cycle
277 MIDIClock_Slave::resolution() const
280 return (nframes_t) one_ppqn_in_frames * ppqn;
284 MIDIClock_Slave::reset ()
290 session.request_locate(0, false);