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.
24 #include <sys/types.h>
26 #include "pbd/error.h"
27 #include "pbd/failed_constructor.h"
28 #include "pbd/pthread_utils.h"
30 #include "midi++/port.h"
32 #include "ardour/debug.h"
33 #include "ardour/slave.h"
34 #include "ardour/session.h"
35 #include "ardour/audioengine.h"
36 #include "ardour/cycles.h"
37 #include "ardour/tempo.h"
43 using namespace ARDOUR
;
47 #define DEBUG_MIDI_CLOCK 1
49 MIDIClock_Slave::MIDIClock_Slave (Session
& s
, MIDI::Port
& p
, int ppqn
)
51 , bandwidth (1.0 / 60.0) // 1 BpM = 1 / 60 Hz
53 session
= (ISlaveSessionProxy
*) new SlaveSessionProxy(s
);
58 MIDIClock_Slave::MIDIClock_Slave (ISlaveSessionProxy
* session_proxy
, int ppqn
)
59 : session(session_proxy
)
61 , bandwidth (1.0 / 60.0) // 1 BpM = 1 / 60 Hz
66 MIDIClock_Slave::~MIDIClock_Slave()
72 MIDIClock_Slave::rebind (MIDI::Port
& p
)
74 port_connections
.drop_connections();
78 DEBUG_TRACE (DEBUG::MidiClock
, string_compose ("MIDIClock_Slave: connecting to port %1\n", port
->name()));
80 port
->parser()->timing
.connect_same_thread (port_connections
, boost::bind (&MIDIClock_Slave::update_midi_clock
, this, _1
, _2
));
81 port
->parser()->start
.connect_same_thread (port_connections
, boost::bind (&MIDIClock_Slave::start
, this, _1
, _2
));
82 port
->parser()->contineu
.connect_same_thread (port_connections
, boost::bind (&MIDIClock_Slave::contineu
, this, _1
, _2
));
83 port
->parser()->stop
.connect_same_thread (port_connections
, boost::bind (&MIDIClock_Slave::stop
, this, _1
, _2
));
84 port
->parser()->position
.connect_same_thread (port_connections
, boost::bind (&MIDIClock_Slave::position
, this, _1
, _2
, 3));
88 MIDIClock_Slave::calculate_one_ppqn_in_frames_at(nframes64_t time
)
90 const Tempo
& current_tempo
= session
->tempo_map().tempo_at(time
);
91 const Meter
& current_meter
= session
->tempo_map().meter_at(time
);
92 double frames_per_beat
=
93 current_tempo
.frames_per_beat(session
->frame_rate(),
96 double quarter_notes_per_beat
= 4.0 / current_tempo
.note_type();
97 double frames_per_quarter_note
= frames_per_beat
/ quarter_notes_per_beat
;
99 one_ppqn_in_frames
= frames_per_quarter_note
/ double (ppqn
);
100 // DEBUG_TRACE (DEBUG::MidiClock, string_compose ("at %1, one ppqn = %2\n", time, one_ppqn_in_frames));
104 MIDIClock_Slave::calculate_song_position(uint16_t song_position_in_sixteenth_notes
)
106 nframes64_t song_position_frames
= 0;
107 for (uint16_t i
= 1; i
<= song_position_in_sixteenth_notes
; ++i
) {
108 // one quarter note contains ppqn pulses, so a sixteenth note is ppqn / 4 pulses
109 calculate_one_ppqn_in_frames_at(song_position_frames
);
110 song_position_frames
+= one_ppqn_in_frames
* (nframes64_t
)(ppqn
/ 4);
113 return song_position_frames
;
117 MIDIClock_Slave::calculate_filter_coefficients()
119 // omega = 2 * PI * Bandwidth / MIDI clock frame frequency in Hz
120 omega
= 2.0 * M_PI
* bandwidth
* one_ppqn_in_frames
/ session
->frame_rate();
121 b
= 1.4142135623730950488 * omega
;
126 MIDIClock_Slave::update_midi_clock (Parser
& /*parser*/, nframes64_t timestamp
)
128 // some pieces of hardware send MIDI Clock all the time
129 if ( (!_starting
) && (!_started
) ) {
133 calculate_one_ppqn_in_frames_at(should_be_position
);
135 nframes64_t elapsed_since_start
= timestamp
- first_timestamp
;
138 if (_starting
|| last_timestamp
== 0) {
139 midi_clock_count
= 0;
141 first_timestamp
= timestamp
;
142 elapsed_since_start
= should_be_position
;
144 // calculate filter coefficients
145 calculate_filter_coefficients();
148 e2
= double(one_ppqn_in_frames
) / double(session
->frame_rate());
149 t0
= double(elapsed_since_start
) / double(session
->frame_rate());
152 // let ardour go after first MIDI Clock Event
156 should_be_position
+= one_ppqn_in_frames
;
157 calculate_filter_coefficients();
159 // calculate loop error
160 // we use session->audible_frame() instead of t1 here
161 // because t1 is used to calculate the transport speed,
162 // so the loop will compensate for accumulating rounding errors
163 error
= (double(should_be_position
) - double(session
->audible_frame()));
164 e
= error
/ double(session
->frame_rate());
172 DEBUG_TRACE (DEBUG::MidiClock
, string_compose ("clock #%1 @ %2 arrived %3 (theoretical) audible %4 transport %5 error %6 "
173 "read delta %7 should-be delta %8 t1-t0 %9 t0 %10 t1 %11 framerate %12 appspeed %13\n",
177 session
->audible_frame(),
178 session
->transport_frame(),
180 timestamp
- last_timestamp
,
182 (t1
-t0
) * session
->frame_rate(),
183 t0
* session
->frame_rate(),
184 t1
* session
->frame_rate(),
185 session
->frame_rate(),
186 ((t1
- t0
) * session
->frame_rate()) / one_ppqn_in_frames
));
188 last_timestamp
= timestamp
;
192 MIDIClock_Slave::start (Parser
& /*parser*/, nframes64_t timestamp
)
194 DEBUG_TRACE (DEBUG::MidiClock
, string_compose ("MIDIClock_Slave got start message at time %1 engine time %2\n", timestamp
, session
->frame_time()));
202 should_be_position
= session
->transport_frame();
207 MIDIClock_Slave::reset ()
209 should_be_position
= session
->transport_frame();
215 // session->request_locate(0, false);
219 MIDIClock_Slave::contineu (Parser
& /*parser*/, nframes64_t
/*timestamp*/)
221 DEBUG_TRACE (DEBUG::MidiClock
, "MIDIClock_Slave got continue message\n");
231 MIDIClock_Slave::stop (Parser
& /*parser*/, nframes64_t
/*timestamp*/)
233 DEBUG_TRACE (DEBUG::MidiClock
, "MIDIClock_Slave got stop message\n");
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 nframes64_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 nframes64_t position_in_frames
= calculate_song_position(position_in_sixteenth_notes
);
277 DEBUG_TRACE (DEBUG::MidiClock
, string_compose ("Song Position: %1 frames: %2\n", position_in_sixteenth_notes
, position_in_frames
));
279 session
->request_locate(position_in_frames
, false);
280 should_be_position
= position_in_frames
;
286 MIDIClock_Slave::locked () const
292 MIDIClock_Slave::ok() const
298 MIDIClock_Slave::starting() const
304 MIDIClock_Slave::stop_if_no_more_clock_events(nframes64_t
& pos
, nframes64_t now
)
306 /* no timecode for 1/4 second ? conclude that its stopped */
307 if (last_timestamp
&&
308 now
> last_timestamp
&&
309 now
- last_timestamp
> session
->frame_rate() / 4) {
310 DEBUG_TRACE (DEBUG::MidiClock
, "No MIDI Clock frames received for some time, stopping!\n");
311 pos
= should_be_position
;
312 session
->request_transport_speed (0);
313 session
->request_locate (should_be_position
, false);
321 MIDIClock_Slave::speed_and_position (double& speed
, nframes64_t
& pos
)
323 if (!_started
|| _starting
) {
325 pos
= should_be_position
;
329 nframes64_t engine_now
= session
->frame_time();
331 if (stop_if_no_more_clock_events(pos
, engine_now
)) {
336 speed
= ((t1
- t0
) * session
->frame_rate()) / one_ppqn_in_frames
;
338 // provide a 3% deadzone to lock the speed
339 if (fabs(speed
- 1.0) <= 0.03)
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 nframes64_t elapsed
= engine_now
- last_timestamp
;
347 pos
= (nframes64_t
) (should_be_position
+ double(elapsed
) * speed
);
349 // A new MIDI clock message has arrived this cycle
350 pos
= should_be_position
;
353 DEBUG_TRACE (DEBUG::MidiClock
, string_compose ("speed_and_position: %1 & %2 <-> %3 (transport)\n", speed
, pos
, session
->transport_frame()));
359 MIDIClock_Slave::resolution() const
362 return (nframes_t
) one_ppqn_in_frames
* ppqn
;