ardour
session_time.cc
Go to the documentation of this file.
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, size_t len, framepos_t when) const
168 {
169  Timecode::Time timecode;
170 
171  timecode_duration (when, timecode);
172  snprintf (buf, len, "%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::backend_sync_callback (TransportState state, framepos_t pos)
184 {
185  bool slave = synced_to_engine();
186 
187  switch (state) {
188  case TransportStopped:
189  if (slave && _transport_frame != pos && post_transport_work() == 0) {
190  request_locate (pos, 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 TransportStarting:
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 && post_transport_work() == 0;
201  } else {
202  return true;
203  }
204  break;
205 
206  case TransportRolling:
207  // cerr << "SYNC: rolling slave = " << slave << endl;
208  if (slave) {
209  start_transport ();
210  }
211  break;
212 
213  default:
214  error << string_compose (_("Unknown transport state %1 in sync callback"), state)
215  << endmsg;
216  }
217 
218  return true;
219 }
220 
221 
223 Session::convert_to_frames (AnyTime const & position)
224 {
225  double secs;
226 
227  switch (position.type) {
228  case AnyTime::BBT:
229  return _tempo_map->frame_time (position.bbt);
230  break;
231 
232  case AnyTime::Timecode:
233  /* XXX need to handle negative values */
234  secs = position.timecode.hours * 60 * 60;
235  secs += position.timecode.minutes * 60;
236  secs += position.timecode.seconds;
237  secs += position.timecode.frames / timecode_frames_per_second();
238  if (config.get_timecode_offset_negative()) {
239  return (framecnt_t) floor (secs * frame_rate()) - config.get_timecode_offset();
240  } else {
241  return (framecnt_t) floor (secs * frame_rate()) + config.get_timecode_offset();
242  }
243  break;
244 
245  case AnyTime::Seconds:
246  return (framecnt_t) floor (position.seconds * frame_rate());
247  break;
248 
249  case AnyTime::Frames:
250  return position.frames;
251  break;
252  }
253 
254  return position.frames;
255 }
256 
258 Session::any_duration_to_frames (framepos_t position, AnyTime const & duration)
259 {
260  double secs;
261 
262  switch (duration.type) {
263  case AnyTime::BBT:
264  return (framecnt_t) ( _tempo_map->framepos_plus_bbt (position, duration.bbt) - position);
265  break;
266 
267  case AnyTime::Timecode:
268  /* XXX need to handle negative values */
269  secs = duration.timecode.hours * 60 * 60;
270  secs += duration.timecode.minutes * 60;
271  secs += duration.timecode.seconds;
272  secs += duration.timecode.frames / timecode_frames_per_second();
273  if (config.get_timecode_offset_negative()) {
274  return (framecnt_t) floor (secs * frame_rate()) - config.get_timecode_offset();
275  } else {
276  return (framecnt_t) floor (secs * frame_rate()) + config.get_timecode_offset();
277  }
278  break;
279 
280  case AnyTime::Seconds:
281  return (framecnt_t) floor (duration.seconds * frame_rate());
282  break;
283 
284  case AnyTime::Frames:
285  return duration.frames;
286  break;
287  }
288 
289  return duration.frames;
290 }
Definition: Beats.hpp:239
LIBPBD_API Transmitter error
std::ostream & endmsg(std::ostream &ostr)
Definition: transmitter.h:71
framecnt_t frames
Definition: types.h:244
Timecode::Time timecode
Definition: types.h:240
Timecode::BBT_Time bbt
Definition: types.h:241
#define _(Text)
Definition: i18n.h:11
int64_t framecnt_t
Definition: types.h:76
Definition: amp.h:29
int64_t framepos_t
Definition: types.h:66
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > position
Definition: region.cc:65
Definition: debug.h:30
TransportState
Definition: types.h:600
double seconds
Definition: types.h:245
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:208