3 Copyright (C) 1999-2002 Paul Davis
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.
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.
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.
22 #include "libardour-config.h"
29 #include "ardour/timestamps.h"
31 #include "pbd/error.h"
32 #include "pbd/enumwriter.h"
33 #include "pbd/stacktrace.h"
35 #include "ardour/session.h"
36 #include "ardour/tempo.h"
41 using namespace ARDOUR;
47 Session::bbt_time (framepos_t when, Timecode::BBT_Time& bbt)
49 _tempo_map->bbt_time (when, bbt);
55 Session::timecode_frames_per_second() const
57 return Timecode::timecode_to_frames_per_second (config.get_timecode_format());
61 Session::timecode_drop_frames() const
63 return Timecode::timecode_has_drop_frames(config.get_timecode_format());
67 Session::sync_time_vars ()
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);
74 _frames_per_hour = (int32_t)(3600 * rint(timecode_frames_per_second()) * _frames_per_timecode_frame);
76 _timecode_frames_per_hour = rint(timecode_frames_per_second() * 3600.0);
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())) {
82 mtc_timecode_bits = 0;
86 mtc_timecode_bits = 0x20;
91 if (timecode_drop_frames()) {
92 mtc_timecode_bits = 0x40;
94 mtc_timecode_bits = 0x60;
98 ltc_tx_parse_offset();
102 Session::timecode_to_sample( Timecode::Time& timecode, framepos_t& sample, bool use_offset, bool use_subframes ) const
104 timecode.rate = timecode_frames_per_second();
106 Timecode::timecode_to_sample(
107 timecode, sample, use_offset, use_subframes,
109 config.get_subframes_per_frame(),
110 config.get_timecode_offset_negative(), config.get_timecode_offset()
116 Session::sample_to_timecode (framepos_t sample, Timecode::Time& timecode, bool use_offset, bool use_subframes ) const
118 Timecode::sample_to_timecode (
119 sample, timecode, use_offset, use_subframes,
121 timecode_frames_per_second(),
122 timecode_drop_frames(),
123 double(_current_frame_rate),
125 config.get_subframes_per_frame(),
126 config.get_timecode_offset_negative(), config.get_timecode_offset()
131 Session::timecode_time (framepos_t when, Timecode::Time& timecode)
133 if (last_timecode_valid && when == last_timecode_when) {
134 timecode = last_timecode;
138 this->sample_to_timecode( when, timecode, true /* use_offset */, false /* use_subframes */ );
140 last_timecode_when = when;
141 last_timecode = timecode;
142 last_timecode_valid = true;
146 Session::timecode_time_subframes (framepos_t when, Timecode::Time& timecode)
148 if (last_timecode_valid && when == last_timecode_when) {
149 timecode = last_timecode;
153 this->sample_to_timecode( when, timecode, true /* use_offset */, true /* use_subframes */ );
155 last_timecode_when = when;
156 last_timecode = timecode;
157 last_timecode_valid = true;
161 Session::timecode_duration (framecnt_t when, Timecode::Time& timecode) const
163 this->sample_to_timecode( when, timecode, false /* use_offset */, true /* use_subframes */ );
167 Session::timecode_duration_string (char* buf, framepos_t when) const
169 Timecode::Time timecode;
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);
176 Session::timecode_time (Timecode::Time &t)
179 timecode_time (_transport_frame, t);
183 Session::jack_sync_callback (jack_transport_state_t state,
184 jack_position_t* pos)
186 bool slave = synced_to_jack();
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;
198 case JackTransportStarting:
199 // cerr << "SYNC: starting @ " << pos->frame << " a@ " << _transport_frame << " our work = " << post_transport_work() << " pos matches ? " << (_transport_frame == pos->frame) << endl;
201 return _transport_frame == pos->frame && post_transport_work() == 0;
207 case JackTransportRolling:
208 // cerr << "SYNC: rolling slave = " << slave << endl;
215 error << string_compose (_("Unknown JACK transport state %1 in sync callback"), state)
223 Session::jack_timebase_callback (jack_transport_state_t /*state*/,
224 pframes_t /*nframes*/,
225 jack_position_t* pos,
226 int /*new_position*/)
228 Timecode::BBT_Time bbt;
234 TempoMetric metric (_tempo_map->metric_at (_transport_frame));
237 _tempo_map->bbt_time_rt (_transport_frame, bbt);
240 pos->beat = bbt.beats;
241 pos->tick = bbt.ticks;
243 // XXX still need to set bar_start_tick
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();
250 pos->valid = jack_position_bits_t (pos->valid | JackPositionBBT);
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);
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;
270 if (_transport_speed) {
274 Location* location = _locations.auto_loop_location();
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);
287 t.transport_state = JackTransportRolling;
295 t.transport_state = JackTransportRolling;
304 Session::convert_to_frames (AnyTime const & position)
308 switch (position.type) {
310 return _tempo_map->frame_time (position.bbt);
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();
322 return (framecnt_t) floor (secs * frame_rate()) + config.get_timecode_offset();
326 case AnyTime::Seconds:
327 return (framecnt_t) floor (position.seconds * frame_rate());
330 case AnyTime::Frames:
331 return position.frames;
335 return position.frames;
339 Session::any_duration_to_frames (framepos_t position, AnyTime const & duration)
343 switch (duration.type) {
345 return (framecnt_t) ( _tempo_map->framepos_plus_bbt (position, duration.bbt) - position);
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();
357 return (framecnt_t) floor (secs * frame_rate()) + config.get_timecode_offset();
361 case AnyTime::Seconds:
362 return (framecnt_t) floor (duration.seconds * frame_rate());
365 case AnyTime::Frames:
366 return duration.frames;
370 return duration.frames;