implement TC offset for slave&generator.
[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 #ifdef HAVE_LTC
99         ltc_tx_parse_offset();
100 #endif
101 }
102
103 void
104 Session::timecode_to_sample( Timecode::Time& timecode, framepos_t& sample, bool use_offset, bool use_subframes ) const
105 {
106         timecode.rate = timecode_frames_per_second();
107
108         Timecode::timecode_to_sample(
109                 timecode, sample, use_offset, use_subframes,
110                 _current_frame_rate,
111                 config.get_subframes_per_frame(),
112                 config.get_timecode_offset_negative(), config.get_timecode_offset()
113                 );
114
115 }
116
117 void
118 Session::sample_to_timecode (framepos_t sample, Timecode::Time& timecode, bool use_offset, bool use_subframes ) const
119 {
120         Timecode::sample_to_timecode (
121                 sample, timecode, use_offset, use_subframes,
122
123                 timecode_frames_per_second(),
124                 timecode_drop_frames(),
125                 double(_current_frame_rate),
126
127                 config.get_subframes_per_frame(),
128                 config.get_timecode_offset_negative(), config.get_timecode_offset()
129                 );
130 }
131
132 void
133 Session::timecode_time (framepos_t when, Timecode::Time& timecode)
134 {
135         if (last_timecode_valid && when == last_timecode_when) {
136                 timecode = last_timecode;
137                 return;
138         }
139
140         this->sample_to_timecode( when, timecode, true /* use_offset */, false /* use_subframes */ );
141
142         last_timecode_when = when;
143         last_timecode = timecode;
144         last_timecode_valid = true;
145 }
146
147 void
148 Session::timecode_time_subframes (framepos_t when, Timecode::Time& timecode)
149 {
150         if (last_timecode_valid && when == last_timecode_when) {
151                 timecode = last_timecode;
152                 return;
153         }
154
155         this->sample_to_timecode( when, timecode, true /* use_offset */, true /* use_subframes */ );
156
157         last_timecode_when = when;
158         last_timecode = timecode;
159         last_timecode_valid = true;
160 }
161
162 void
163 Session::timecode_duration (framecnt_t when, Timecode::Time& timecode) const
164 {
165         this->sample_to_timecode( when, timecode, false /* use_offset */, true /* use_subframes */ );
166 }
167
168 void
169 Session::timecode_duration_string (char* buf, framepos_t when) const
170 {
171         Timecode::Time timecode;
172
173         timecode_duration (when, timecode);
174         snprintf (buf, sizeof (buf), "%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
175 }
176
177 void
178 Session::timecode_time (Timecode::Time &t)
179
180 {
181         timecode_time (_transport_frame, t);
182 }
183
184 int
185 Session::jack_sync_callback (jack_transport_state_t state,
186                              jack_position_t* pos)
187 {
188         bool slave = synced_to_jack();
189
190         switch (state) {
191         case JackTransportStopped:
192                 if (slave && _transport_frame != pos->frame && post_transport_work() == 0) {
193                         request_locate (pos->frame, false);
194                         // cerr << "SYNC: stopped, locate to " << pos->frame << " from " << _transport_frame << endl;
195                         return false;
196                 } else {
197                         return true;
198                 }
199
200         case JackTransportStarting:
201                 // cerr << "SYNC: starting @ " << pos->frame << " a@ " << _transport_frame << " our work = " <<  post_transport_work() << " pos matches ? " << (_transport_frame == pos->frame) << endl;
202                 if (slave) {
203                         return _transport_frame == pos->frame && post_transport_work() == 0;
204                 } else {
205                         return true;
206                 }
207                 break;
208
209         case JackTransportRolling:
210                 // cerr << "SYNC: rolling slave = " << slave << endl;
211                 if (slave) {
212                         start_transport ();
213                 }
214                 break;
215
216         default:
217                 error << string_compose (_("Unknown JACK transport state %1 in sync callback"), state)
218                       << endmsg;
219         }
220
221         return true;
222 }
223
224 void
225 Session::jack_timebase_callback (jack_transport_state_t /*state*/,
226                                  pframes_t /*nframes*/,
227                                  jack_position_t* pos,
228                                  int /*new_position*/)
229 {
230         Timecode::BBT_Time bbt;
231
232         if (pos->frame != _transport_frame) {
233                 cerr << "ARDOUR says " << _transport_frame << " JACK says " << pos->frame << endl;
234         }
235
236         /* BBT info */
237
238         if (_tempo_map) {
239
240                 TempoMetric metric (_tempo_map->metric_at (_transport_frame));
241
242                 try {
243                         _tempo_map->bbt_time_rt (_transport_frame, bbt);
244
245                         pos->bar = bbt.bars;
246                         pos->beat = bbt.beats;
247                         pos->tick = bbt.ticks;
248                         
249                         // XXX still need to set bar_start_tick
250                         
251                         pos->beats_per_bar = metric.meter().divisions_per_bar();
252                         pos->beat_type = metric.meter().note_divisor();
253                         pos->ticks_per_beat = Timecode::BBT_Time::ticks_per_beat;
254                         pos->beats_per_minute = metric.tempo().beats_per_minute();
255                         
256                         pos->valid = jack_position_bits_t (pos->valid | JackPositionBBT);
257
258                 } catch (...) {
259                         /* no message */
260                 }
261         }
262
263 #ifdef HAVE_JACK_VIDEO_SUPPORT
264         //poke audio video ratio so Ardour can track Video Sync
265         pos->audio_frames_per_video_frame = frame_rate() / timecode_frames_per_second();
266         pos->valid = jack_position_bits_t (pos->valid | JackAudioVideoRatio);
267 #endif
268
269 #if 0
270         /* Timecode info */
271
272         pos->timecode_offset = config.get_timecode_offset();
273         t.timecode_frame_rate = timecode_frames_per_second();
274         pos->valid = jack_position_bits_t (pos->valid | JackPositionTimecode;
275
276         if (_transport_speed) {
277
278                 if (play_loop) {
279
280                         Location* location = _locations.auto_loop_location();
281
282                         if (location) {
283
284                                 t.transport_state = JackTransportLooping;
285                                 t.loop_start = location->start();
286                                 t.loop_end = location->end();
287                                 t.valid = jack_transport_bits_t (t.valid | JackTransportLoop);
288
289                         } else {
290
291                                 t.loop_start = 0;
292                                 t.loop_end = 0;
293                                 t.transport_state = JackTransportRolling;
294
295                         }
296
297                 } else {
298
299                         t.loop_start = 0;
300                         t.loop_end = 0;
301                         t.transport_state = JackTransportRolling;
302
303                 }
304
305         }
306 #endif
307 }
308
309 ARDOUR::framecnt_t
310 Session::convert_to_frames (AnyTime const & position)
311 {
312         double secs;
313
314         switch (position.type) {
315         case AnyTime::BBT:
316                 return _tempo_map->frame_time (position.bbt);
317                 break;
318
319         case AnyTime::Timecode:
320                 /* XXX need to handle negative values */
321                 secs = position.timecode.hours * 60 * 60;
322                 secs += position.timecode.minutes * 60;
323                 secs += position.timecode.seconds;
324                 secs += position.timecode.frames / timecode_frames_per_second();
325                 if (config.get_timecode_offset_negative()) {
326                         return (framecnt_t) floor (secs * frame_rate()) - config.get_timecode_offset();
327                 } else {
328                         return (framecnt_t) floor (secs * frame_rate()) + config.get_timecode_offset();
329                 }
330                 break;
331
332         case AnyTime::Seconds:
333                 return (framecnt_t) floor (position.seconds * frame_rate());
334                 break;
335
336         case AnyTime::Frames:
337                 return position.frames;
338                 break;
339         }
340
341         return position.frames;
342 }
343
344 ARDOUR::framecnt_t
345 Session::any_duration_to_frames (framepos_t position, AnyTime const & duration)
346 {
347         double secs;
348
349         switch (duration.type) {
350         case AnyTime::BBT:
351                 return (framecnt_t) ( _tempo_map->framepos_plus_bbt (position, duration.bbt) - position);
352                 break;
353
354         case AnyTime::Timecode:
355                 /* XXX need to handle negative values */
356                 secs = duration.timecode.hours * 60 * 60;
357                 secs += duration.timecode.minutes * 60;
358                 secs += duration.timecode.seconds;
359                 secs += duration.timecode.frames / timecode_frames_per_second();
360                 if (config.get_timecode_offset_negative()) {
361                         return (framecnt_t) floor (secs * frame_rate()) - config.get_timecode_offset();
362                 } else {
363                         return (framecnt_t) floor (secs * frame_rate()) + config.get_timecode_offset();
364                 }
365                 break;
366
367         case AnyTime::Seconds:
368                 return (framecnt_t) floor (duration.seconds * frame_rate());
369                 break;
370
371         case AnyTime::Frames:
372                 return duration.frames;
373                 break;
374         }
375
376         return duration.frames;
377 }