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