2 Copyright (C) 2002-4 Paul Davis
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 <ardour/slave.h>
30 #include <ardour/session.h>
31 #include <ardour/audioengine.h>
32 #include <ardour/cycles.h>
33 #include <ardour/tempo.h>
37 using namespace ARDOUR;
42 MIDIClock_Slave::MIDIClock_Slave (Session& s, MIDI::Port& p, int ppqn)
50 MIDIClock_Slave::~MIDIClock_Slave()
55 MIDIClock_Slave::rebind (MIDI::Port& p)
57 for (vector<sigc::connection>::iterator i = connections.begin(); i != connections.end(); ++i) {
63 std::cerr << "MIDIClock_Slave: connecting to port " << port->name() << std::endl;
65 connections.push_back (port->input()->timing.connect (mem_fun (*this, &MIDIClock_Slave::update_midi_clock)));
66 connections.push_back (port->input()->start.connect (mem_fun (*this, &MIDIClock_Slave::start)));
67 connections.push_back (port->input()->stop.connect (mem_fun (*this, &MIDIClock_Slave::stop)));
71 MIDIClock_Slave::update_midi_clock (Parser& parser)
73 // ignore clock events if no start event received
77 nframes_t now = session.engine().frame_time();
82 const Tempo& current_tempo = session.tempo_map().tempo_at(now);
83 const Meter& current_meter = session.tempo_map().meter_at(now);
84 double frames_per_beat =
85 current_tempo.frames_per_beat(session.nominal_frame_rate(),
88 double quarter_notes_per_beat = 4.0 / current_tempo.note_type();
89 double frames_per_quarter_note = frames_per_beat / quarter_notes_per_beat;
91 one_ppqn_in_frames = frames_per_quarter_note / ppqn;
94 Also compensate for audio latency.
97 midi_clock_frame += (long) (one_ppqn_in_frames)
98 + session.worst_output_latency();
101 std::cerr << "got MIDI Clock message at time " << now
102 << " result: " << midi_clock_frame
103 << " open_ppqn_in_frames: " << one_ppqn_in_frames << std::endl;
106 if (first_midi_clock_frame == 0) {
107 first_midi_clock_frame = midi_clock_frame;
108 first_midi_clock_time = now;
112 current.position = midi_clock_frame;
113 current.timestamp = now;
116 last_inbound_frame = now;
120 MIDIClock_Slave::start (Parser& parser)
122 std::cerr << "MIDIClock_Slave got start message" << endl;
124 midi_clock_speed = 1.0f;
129 MIDIClock_Slave::stop (Parser& parser)
131 std::cerr << "MIDIClock_Slave got stop message" << endl;
133 midi_clock_speed = 0.0f;
134 midi_clock_frame = 0;
138 current.position = midi_clock_frame;
139 current.timestamp = 0;
144 MIDIClock_Slave::read_current (SafeTime *st) const
149 error << _("MIDI Clock Slave: atomic read of current time failed, sleeping!") << endmsg;
157 } while (st->guard1 != st->guard2);
161 MIDIClock_Slave::locked () const
167 MIDIClock_Slave::ok() const
173 MIDIClock_Slave::speed_and_position (float& speed, nframes_t& pos)
175 //std::cerr << "MIDIClock_Slave speed and position() called" << endl;
176 nframes_t now = session.engine().frame_time();
177 nframes_t frame_rate = session.frame_rate();
182 read_current (&last);
184 if (first_midi_clock_time == 0) {
190 /* no timecode for 1/4 second ? conclude that its stopped */
192 if (last_inbound_frame && now > last_inbound_frame && now - last_inbound_frame > frame_rate / 4) {
193 midi_clock_speed = 0;
195 session.request_locate (pos, false);
196 session.request_transport_speed (0);
197 this->stop(*port->input());
202 speed_now = (float) ((last.position - first_midi_clock_frame) / (double) (now - first_midi_clock_time));
204 cerr << "speed_and_position: speed_now: " << speed_now ;
206 midi_clock_speed = speed_now;
208 if (midi_clock_speed == 0.0f) {
214 /* scale elapsed time by the current MIDI Clock speed */
216 if (last.timestamp && (now > last.timestamp)) {
217 elapsed = (nframes_t) floor (midi_clock_speed * (now - last.timestamp));
219 elapsed = 0; /* XXX is this right? */
223 /* now add the most recent timecode value plus the estimated elapsed interval */
225 pos = elapsed + last.position;
227 speed = midi_clock_speed;
229 cerr << " final speed: " << speed << " position: " << pos << endl;
234 MIDIClock_Slave::resolution() const
236 return (nframes_t) one_ppqn_in_frames;
240 MIDIClock_Slave::reset ()
242 /* XXX massive thread safety issue here. MTC could
243 be being updated as we call this. but this
244 supposed to be a realtime-safe call.
247 last_inbound_frame = 0;
249 current.position = 0;
250 current.timestamp = 0;
252 first_midi_clock_frame = 0;
253 first_midi_clock_time = 0;
255 midi_clock_speed = 0;