Remove debug print about transport sync.
[ardour.git] / libs / ardour / session_time.cc
1
2 /*
3   Copyright (C) 1999-2002 Paul Davis
4
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.
9
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.
14
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.
18
19 */
20
21 #ifdef WAF_BUILD
22 #include "libardour-config.h"
23 #endif
24
25 #include <iostream>
26 #include <cmath>
27 #include <unistd.h>
28
29 #include "ardour/timestamps.h"
30
31 #include "pbd/error.h"
32 #include "pbd/enumwriter.h"
33 #include "pbd/stacktrace.h"
34
35 #include "ardour/session.h"
36 #include "ardour/tempo.h"
37
38 #include "i18n.h"
39
40 using namespace std;
41 using namespace ARDOUR;
42 using namespace PBD;
43
44 /* BBT TIME*/
45
46 void
47 Session::bbt_time (framepos_t when, Timecode::BBT_Time& bbt)
48 {
49         _tempo_map->bbt_time (when, bbt);
50 }
51
52 /* Timecode TIME */
53
54 double
55 Session::timecode_frames_per_second() const
56 {
57         return Timecode::timecode_to_frames_per_second (config.get_timecode_format());
58 }
59
60 bool
61 Session::timecode_drop_frames() const
62 {
63         return Timecode::timecode_has_drop_frames(config.get_timecode_format());
64 }
65
66 void
67 Session::sync_time_vars ()
68 {
69         _current_frame_rate = (framecnt_t) round (_base_frame_rate * (1.0 + (config.get_video_pullup()/100.0)));
70         _frames_per_timecode_frame = (double) _current_frame_rate / (double) timecode_frames_per_second();
71         if (timecode_drop_frames()) {
72           _frames_per_hour = (int32_t)(107892 * _frames_per_timecode_frame);
73         } else {
74           _frames_per_hour = (int32_t)(3600 * rint(timecode_frames_per_second()) * _frames_per_timecode_frame);
75         }
76         _timecode_frames_per_hour = rint(timecode_frames_per_second() * 3600.0);
77
78         last_timecode_valid = false;
79         // timecode type bits are the middle two in the upper nibble
80         switch ((int) ceil (timecode_frames_per_second())) {
81         case 24:
82                 mtc_timecode_bits = 0;
83                 break;
84
85         case 25:
86                 mtc_timecode_bits = 0x20;
87                 break;
88
89         case 30:
90         default:
91                 if (timecode_drop_frames()) {
92                         mtc_timecode_bits = 0x40;
93                 } else {
94                         mtc_timecode_bits =  0x60;
95                 }
96                 break;
97         };
98         ltc_tx_parse_offset();
99 }
100
101 void
102 Session::timecode_to_sample( Timecode::Time& timecode, framepos_t& sample, bool use_offset, bool use_subframes ) const
103 {
104         timecode.rate = timecode_frames_per_second();
105
106         Timecode::timecode_to_sample(
107                 timecode, sample, use_offset, use_subframes,
108                 _current_frame_rate,
109                 config.get_subframes_per_frame(),
110                 config.get_timecode_offset_negative(), config.get_timecode_offset()
111                 );
112
113 }
114
115 void
116 Session::sample_to_timecode (framepos_t sample, Timecode::Time& timecode, bool use_offset, bool use_subframes ) const
117 {
118         Timecode::sample_to_timecode (
119                 sample, timecode, use_offset, use_subframes,
120
121                 timecode_frames_per_second(),
122                 timecode_drop_frames(),
123                 double(_current_frame_rate),
124
125                 config.get_subframes_per_frame(),
126                 config.get_timecode_offset_negative(), config.get_timecode_offset()
127                 );
128 }
129
130 void
131 Session::timecode_time (framepos_t when, Timecode::Time& timecode)
132 {
133         if (last_timecode_valid && when == last_timecode_when) {
134                 timecode = last_timecode;
135                 return;
136         }
137
138         this->sample_to_timecode( when, timecode, true /* use_offset */, false /* use_subframes */ );
139
140         last_timecode_when = when;
141         last_timecode = timecode;
142         last_timecode_valid = true;
143 }
144
145 void
146 Session::timecode_time_subframes (framepos_t when, Timecode::Time& timecode)
147 {
148         if (last_timecode_valid && when == last_timecode_when) {
149                 timecode = last_timecode;
150                 return;
151         }
152
153         this->sample_to_timecode( when, timecode, true /* use_offset */, true /* use_subframes */ );
154
155         last_timecode_when = when;
156         last_timecode = timecode;
157         last_timecode_valid = true;
158 }
159
160 void
161 Session::timecode_duration (framecnt_t when, Timecode::Time& timecode) const
162 {
163         this->sample_to_timecode( when, timecode, false /* use_offset */, true /* use_subframes */ );
164 }
165
166 void
167 Session::timecode_duration_string (char* buf, framepos_t when) const
168 {
169         Timecode::Time timecode;
170
171         timecode_duration (when, timecode);
172         snprintf (buf, sizeof (buf), "%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
173 }
174
175 void
176 Session::timecode_time (Timecode::Time &t)
177
178 {
179         timecode_time (_transport_frame, t);
180 }
181
182 int
183 Session::jack_sync_callback (jack_transport_state_t state,
184                              jack_position_t* pos)
185 {
186         bool slave = synced_to_jack();
187
188         switch (state) {
189         case JackTransportStopped:
190                 if (slave && _transport_frame != pos->frame && post_transport_work() == 0) {
191                         request_locate (pos->frame, false);
192                         // cerr << "SYNC: stopped, locate to " << pos->frame << " from " << _transport_frame << endl;
193                         return false;
194                 } else {
195                         return true;
196                 }
197
198         case JackTransportStarting:
199                 // cerr << "SYNC: starting @ " << pos->frame << " a@ " << _transport_frame << " our work = " <<  post_transport_work() << " pos matches ? " << (_transport_frame == pos->frame) << endl;
200                 if (slave) {
201                         return _transport_frame == pos->frame && post_transport_work() == 0;
202                 } else {
203                         return true;
204                 }
205                 break;
206
207         case JackTransportRolling:
208                 // cerr << "SYNC: rolling slave = " << slave << endl;
209                 if (slave) {
210                         start_transport ();
211                 }
212                 break;
213
214         default:
215                 error << string_compose (_("Unknown JACK transport state %1 in sync callback"), state)
216                       << endmsg;
217         }
218
219         return true;
220 }
221
222 void
223 Session::jack_timebase_callback (jack_transport_state_t /*state*/,
224                                  pframes_t /*nframes*/,
225                                  jack_position_t* pos,
226                                  int /*new_position*/)
227 {
228         Timecode::BBT_Time bbt;
229
230         /* BBT info */
231
232         if (_tempo_map) {
233
234                 TempoMetric metric (_tempo_map->metric_at (_transport_frame));
235
236                 try {
237                         _tempo_map->bbt_time_rt (_transport_frame, bbt);
238
239                         pos->bar = bbt.bars;
240                         pos->beat = bbt.beats;
241                         pos->tick = bbt.ticks;
242                         
243                         // XXX still need to set bar_start_tick
244                         
245                         pos->beats_per_bar = metric.meter().divisions_per_bar();
246                         pos->beat_type = metric.meter().note_divisor();
247                         pos->ticks_per_beat = Timecode::BBT_Time::ticks_per_beat;
248                         pos->beats_per_minute = metric.tempo().beats_per_minute();
249                         
250                         pos->valid = jack_position_bits_t (pos->valid | JackPositionBBT);
251
252                 } catch (...) {
253                         /* no message */
254                 }
255         }
256
257 #ifdef HAVE_JACK_VIDEO_SUPPORT
258         //poke audio video ratio so Ardour can track Video Sync
259         pos->audio_frames_per_video_frame = frame_rate() / timecode_frames_per_second();
260         pos->valid = jack_position_bits_t (pos->valid | JackAudioVideoRatio);
261 #endif
262
263 #if 0
264         /* Timecode info */
265
266         pos->timecode_offset = config.get_timecode_offset();
267         t.timecode_frame_rate = timecode_frames_per_second();
268         pos->valid = jack_position_bits_t (pos->valid | JackPositionTimecode;
269
270         if (_transport_speed) {
271
272                 if (play_loop) {
273
274                         Location* location = _locations.auto_loop_location();
275
276                         if (location) {
277
278                                 t.transport_state = JackTransportLooping;
279                                 t.loop_start = location->start();
280                                 t.loop_end = location->end();
281                                 t.valid = jack_transport_bits_t (t.valid | JackTransportLoop);
282
283                         } else {
284
285                                 t.loop_start = 0;
286                                 t.loop_end = 0;
287                                 t.transport_state = JackTransportRolling;
288
289                         }
290
291                 } else {
292
293                         t.loop_start = 0;
294                         t.loop_end = 0;
295                         t.transport_state = JackTransportRolling;
296
297                 }
298
299         }
300 #endif
301 }
302
303 ARDOUR::framecnt_t
304 Session::convert_to_frames (AnyTime const & position)
305 {
306         double secs;
307
308         switch (position.type) {
309         case AnyTime::BBT:
310                 return _tempo_map->frame_time (position.bbt);
311                 break;
312
313         case AnyTime::Timecode:
314                 /* XXX need to handle negative values */
315                 secs = position.timecode.hours * 60 * 60;
316                 secs += position.timecode.minutes * 60;
317                 secs += position.timecode.seconds;
318                 secs += position.timecode.frames / timecode_frames_per_second();
319                 if (config.get_timecode_offset_negative()) {
320                         return (framecnt_t) floor (secs * frame_rate()) - config.get_timecode_offset();
321                 } else {
322                         return (framecnt_t) floor (secs * frame_rate()) + config.get_timecode_offset();
323                 }
324                 break;
325
326         case AnyTime::Seconds:
327                 return (framecnt_t) floor (position.seconds * frame_rate());
328                 break;
329
330         case AnyTime::Frames:
331                 return position.frames;
332                 break;
333         }
334
335         return position.frames;
336 }
337
338 ARDOUR::framecnt_t
339 Session::any_duration_to_frames (framepos_t position, AnyTime const & duration)
340 {
341         double secs;
342
343         switch (duration.type) {
344         case AnyTime::BBT:
345                 return (framecnt_t) ( _tempo_map->framepos_plus_bbt (position, duration.bbt) - position);
346                 break;
347
348         case AnyTime::Timecode:
349                 /* XXX need to handle negative values */
350                 secs = duration.timecode.hours * 60 * 60;
351                 secs += duration.timecode.minutes * 60;
352                 secs += duration.timecode.seconds;
353                 secs += duration.timecode.frames / timecode_frames_per_second();
354                 if (config.get_timecode_offset_negative()) {
355                         return (framecnt_t) floor (secs * frame_rate()) - config.get_timecode_offset();
356                 } else {
357                         return (framecnt_t) floor (secs * frame_rate()) + config.get_timecode_offset();
358                 }
359                 break;
360
361         case AnyTime::Seconds:
362                 return (framecnt_t) floor (duration.seconds * frame_rate());
363                 break;
364
365         case AnyTime::Frames:
366                 return duration.frames;
367                 break;
368         }
369
370         return duration.frames;
371 }