2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
/*
|
2009-10-14 16:10:01 +00:00
|
|
|
Copyright (C) 1999-2002 Paul Davis
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2009-07-13 00:26:28 +00:00
|
|
|
#ifdef WAF_BUILD
|
|
|
|
|
#include "libardour-config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2008-06-02 21:41:35 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2009-02-25 18:26:51 +00:00
|
|
|
#include "ardour/timestamps.h"
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-25 18:26:51 +00:00
|
|
|
#include "pbd/error.h"
|
|
|
|
|
#include "pbd/enumwriter.h"
|
|
|
|
|
#include "pbd/stacktrace.h"
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-25 18:26:51 +00:00
|
|
|
#include "ardour/session.h"
|
|
|
|
|
#include "ardour/tempo.h"
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
2009-05-12 17:03:42 +00:00
|
|
|
using namespace std;
|
2008-06-02 21:41:35 +00:00
|
|
|
using namespace ARDOUR;
|
|
|
|
|
using namespace PBD;
|
|
|
|
|
|
|
|
|
|
/* BBT TIME*/
|
|
|
|
|
|
|
|
|
|
void
|
2010-12-14 18:13:37 +00:00
|
|
|
Session::bbt_time (framepos_t when, Timecode::BBT_Time& bbt)
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
|
|
|
|
_tempo_map->bbt_time (when, bbt);
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-26 14:38:58 +00:00
|
|
|
/* Timecode TIME */
|
2012-10-12 01:08:29 +00:00
|
|
|
|
2012-11-13 02:00:45 +00:00
|
|
|
double
|
2009-10-26 14:38:58 +00:00
|
|
|
Session::timecode_frames_per_second() const
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2012-10-12 01:08:29 +00:00
|
|
|
return Timecode::timecode_to_frames_per_second (config.get_timecode_format());
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
2012-10-12 01:08:29 +00:00
|
|
|
|
2008-06-02 21:41:35 +00:00
|
|
|
bool
|
2009-10-26 14:38:58 +00:00
|
|
|
Session::timecode_drop_frames() const
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2012-10-12 01:08:29 +00:00
|
|
|
return Timecode::timecode_has_drop_frames(config.get_timecode_format());
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
2012-10-12 01:08:29 +00:00
|
|
|
|
2008-06-02 21:41:35 +00:00
|
|
|
void
|
|
|
|
|
Session::sync_time_vars ()
|
|
|
|
|
{
|
2010-12-03 22:26:29 +00:00
|
|
|
_current_frame_rate = (framecnt_t) round (_base_frame_rate * (1.0 + (config.get_video_pullup()/100.0)));
|
2009-10-26 14:38:58 +00:00
|
|
|
_frames_per_timecode_frame = (double) _current_frame_rate / (double) timecode_frames_per_second();
|
|
|
|
|
if (timecode_drop_frames()) {
|
2010-07-22 16:08:11 +00:00
|
|
|
_frames_per_hour = (int32_t)(107892 * _frames_per_timecode_frame);
|
2008-06-02 21:41:35 +00:00
|
|
|
} else {
|
2010-07-22 16:08:11 +00:00
|
|
|
_frames_per_hour = (int32_t)(3600 * rint(timecode_frames_per_second()) * _frames_per_timecode_frame);
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
2010-12-03 22:26:29 +00:00
|
|
|
_timecode_frames_per_hour = rint(timecode_frames_per_second() * 3600.0);
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-10-26 14:38:58 +00:00
|
|
|
last_timecode_valid = false;
|
|
|
|
|
// timecode type bits are the middle two in the upper nibble
|
|
|
|
|
switch ((int) ceil (timecode_frames_per_second())) {
|
2008-06-02 21:41:35 +00:00
|
|
|
case 24:
|
2009-10-26 14:38:58 +00:00
|
|
|
mtc_timecode_bits = 0;
|
2008-06-02 21:41:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 25:
|
2009-10-26 14:38:58 +00:00
|
|
|
mtc_timecode_bits = 0x20;
|
2008-06-02 21:41:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 30:
|
|
|
|
|
default:
|
2009-10-26 14:38:58 +00:00
|
|
|
if (timecode_drop_frames()) {
|
|
|
|
|
mtc_timecode_bits = 0x40;
|
2008-06-02 21:41:35 +00:00
|
|
|
} else {
|
2009-10-26 14:38:58 +00:00
|
|
|
mtc_timecode_bits = 0x60;
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 17:51:17 +00:00
|
|
|
void
|
|
|
|
|
Session::timecode_to_sample( Timecode::Time& timecode, framepos_t& sample, bool use_offset, bool use_subframes ) const
|
|
|
|
|
{
|
2012-10-13 23:27:40 +00:00
|
|
|
timecode.rate = timecode_frames_per_second();
|
2010-12-01 17:51:17 +00:00
|
|
|
|
2012-10-13 23:27:40 +00:00
|
|
|
Timecode::timecode_to_sample(
|
|
|
|
|
timecode, sample, use_offset, use_subframes,
|
|
|
|
|
_current_frame_rate,
|
|
|
|
|
config.get_subframes_per_frame(),
|
|
|
|
|
config.get_timecode_offset_negative(), config.get_timecode_offset()
|
|
|
|
|
);
|
2010-12-01 17:51:17 +00:00
|
|
|
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-12-01 17:51:17 +00:00
|
|
|
Session::sample_to_timecode (framepos_t sample, Timecode::Time& timecode, bool use_offset, bool use_subframes ) const
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2012-10-13 23:27:40 +00:00
|
|
|
Timecode::sample_to_timecode (
|
|
|
|
|
sample, timecode, use_offset, use_subframes,
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2012-10-13 23:27:40 +00:00
|
|
|
timecode_frames_per_second(),
|
|
|
|
|
timecode_drop_frames(),
|
|
|
|
|
double(_current_frame_rate),
|
2009-10-14 16:10:01 +00:00
|
|
|
|
2012-10-13 23:27:40 +00:00
|
|
|
config.get_subframes_per_frame(),
|
|
|
|
|
config.get_timecode_offset_negative(), config.get_timecode_offset()
|
|
|
|
|
);
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-12-03 22:26:29 +00:00
|
|
|
Session::timecode_time (framepos_t when, Timecode::Time& timecode)
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2009-10-26 14:38:58 +00:00
|
|
|
if (last_timecode_valid && when == last_timecode_when) {
|
|
|
|
|
timecode = last_timecode;
|
2008-06-02 21:41:35 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-13 23:27:40 +00:00
|
|
|
this->sample_to_timecode( when, timecode, true /* use_offset */, false /* use_subframes */ );
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-10-26 14:38:58 +00:00
|
|
|
last_timecode_when = when;
|
|
|
|
|
last_timecode = timecode;
|
|
|
|
|
last_timecode_valid = true;
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-12-03 22:26:29 +00:00
|
|
|
Session::timecode_time_subframes (framepos_t when, Timecode::Time& timecode)
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2009-10-26 14:38:58 +00:00
|
|
|
if (last_timecode_valid && when == last_timecode_when) {
|
|
|
|
|
timecode = last_timecode;
|
2008-06-02 21:41:35 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2009-10-14 16:10:01 +00:00
|
|
|
|
2012-10-13 23:27:40 +00:00
|
|
|
this->sample_to_timecode( when, timecode, true /* use_offset */, true /* use_subframes */ );
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-10-26 14:38:58 +00:00
|
|
|
last_timecode_when = when;
|
|
|
|
|
last_timecode = timecode;
|
|
|
|
|
last_timecode_valid = true;
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-11-04 20:37:46 +00:00
|
|
|
Session::timecode_duration (framecnt_t when, Timecode::Time& timecode) const
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2012-10-13 23:27:40 +00:00
|
|
|
this->sample_to_timecode( when, timecode, false /* use_offset */, true /* use_subframes */ );
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-09-17 16:24:22 +00:00
|
|
|
Session::timecode_duration_string (char* buf, framepos_t when) const
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2009-10-26 14:38:58 +00:00
|
|
|
Timecode::Time timecode;
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-10-26 14:38:58 +00:00
|
|
|
timecode_duration (when, timecode);
|
|
|
|
|
snprintf (buf, sizeof (buf), "%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2009-10-26 14:38:58 +00:00
|
|
|
Session::timecode_time (Timecode::Time &t)
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
{
|
2009-10-26 14:38:58 +00:00
|
|
|
timecode_time (_transport_frame, t);
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
Session::jack_sync_callback (jack_transport_state_t state,
|
|
|
|
|
jack_position_t* pos)
|
|
|
|
|
{
|
|
|
|
|
bool slave = synced_to_jack();
|
|
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
|
case JackTransportStopped:
|
2009-11-08 16:28:21 +00:00
|
|
|
if (slave && _transport_frame != pos->frame && post_transport_work() == 0) {
|
2009-10-14 16:10:01 +00:00
|
|
|
request_locate (pos->frame, false);
|
2008-06-02 21:41:35 +00:00
|
|
|
// cerr << "SYNC: stopped, locate to " << pos->frame << " from " << _transport_frame << endl;
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2009-10-14 16:10:01 +00:00
|
|
|
|
2008-06-02 21:41:35 +00:00
|
|
|
case JackTransportStarting:
|
2009-11-08 16:28:21 +00:00
|
|
|
// cerr << "SYNC: starting @ " << pos->frame << " a@ " << _transport_frame << " our work = " << post_transport_work() << " pos matches ? " << (_transport_frame == pos->frame) << endl;
|
2008-06-02 21:41:35 +00:00
|
|
|
if (slave) {
|
2009-11-08 16:28:21 +00:00
|
|
|
return _transport_frame == pos->frame && post_transport_work() == 0;
|
2008-06-02 21:41:35 +00:00
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case JackTransportRolling:
|
|
|
|
|
// cerr << "SYNC: rolling slave = " << slave << endl;
|
|
|
|
|
if (slave) {
|
|
|
|
|
start_transport ();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
error << string_compose (_("Unknown JACK transport state %1 in sync callback"), state)
|
|
|
|
|
<< endmsg;
|
2009-10-14 16:10:01 +00:00
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2009-07-21 15:55:17 +00:00
|
|
|
Session::jack_timebase_callback (jack_transport_state_t /*state*/,
|
2010-12-03 22:26:29 +00:00
|
|
|
pframes_t /*nframes*/,
|
2008-06-02 21:41:35 +00:00
|
|
|
jack_position_t* pos,
|
2009-07-21 15:55:17 +00:00
|
|
|
int /*new_position*/)
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2010-12-14 18:13:37 +00:00
|
|
|
Timecode::BBT_Time bbt;
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2010-12-14 18:13:37 +00:00
|
|
|
if (pos->frame != _transport_frame) {
|
|
|
|
|
cerr << "ARDOUR says " << _transport_frame << " JACK says " << pos->frame << endl;
|
|
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
/* BBT info */
|
2009-10-14 16:10:01 +00:00
|
|
|
|
2008-06-02 21:41:35 +00:00
|
|
|
if (_tempo_map) {
|
|
|
|
|
|
2009-10-27 02:24:56 +00:00
|
|
|
TempoMetric metric (_tempo_map->metric_at (_transport_frame));
|
2012-01-02 23:32:33 +00:00
|
|
|
|
2012-01-05 05:05:31 +00:00
|
|
|
try {
|
|
|
|
|
_tempo_map->bbt_time_rt (_transport_frame, bbt);
|
|
|
|
|
|
|
|
|
|
pos->bar = bbt.bars;
|
|
|
|
|
pos->beat = bbt.beats;
|
|
|
|
|
pos->tick = bbt.ticks;
|
|
|
|
|
|
|
|
|
|
// XXX still need to set bar_start_tick
|
|
|
|
|
|
|
|
|
|
pos->beats_per_bar = metric.meter().divisions_per_bar();
|
|
|
|
|
pos->beat_type = metric.meter().note_divisor();
|
2012-01-06 16:39:40 +00:00
|
|
|
pos->ticks_per_beat = Timecode::BBT_Time::ticks_per_beat;
|
2012-01-05 05:05:31 +00:00
|
|
|
pos->beats_per_minute = metric.tempo().beats_per_minute();
|
|
|
|
|
|
|
|
|
|
pos->valid = jack_position_bits_t (pos->valid | JackPositionBBT);
|
|
|
|
|
|
|
|
|
|
} catch (...) {
|
2012-01-06 20:29:18 +00:00
|
|
|
/* no message */
|
2012-01-05 05:05:31 +00:00
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_JACK_VIDEO_SUPPORT
|
|
|
|
|
//poke audio video ratio so Ardour can track Video Sync
|
2009-10-26 14:38:58 +00:00
|
|
|
pos->audio_frames_per_video_frame = frame_rate() / timecode_frames_per_second();
|
2008-06-02 21:41:35 +00:00
|
|
|
pos->valid = jack_position_bits_t (pos->valid | JackAudioVideoRatio);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if 0
|
2009-10-26 14:38:58 +00:00
|
|
|
/* Timecode info */
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2010-12-01 20:49:22 +00:00
|
|
|
pos->timecode_offset = config.get_timecode_offset();
|
2009-10-26 14:38:58 +00:00
|
|
|
t.timecode_frame_rate = timecode_frames_per_second();
|
2010-08-31 16:01:42 +00:00
|
|
|
pos->valid = jack_position_bits_t (pos->valid | JackPositionTimecode;
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
if (_transport_speed) {
|
|
|
|
|
|
|
|
|
|
if (play_loop) {
|
|
|
|
|
|
|
|
|
|
Location* location = _locations.auto_loop_location();
|
|
|
|
|
|
|
|
|
|
if (location) {
|
|
|
|
|
|
|
|
|
|
t.transport_state = JackTransportLooping;
|
|
|
|
|
t.loop_start = location->start();
|
|
|
|
|
t.loop_end = location->end();
|
|
|
|
|
t.valid = jack_transport_bits_t (t.valid | JackTransportLoop);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
t.loop_start = 0;
|
|
|
|
|
t.loop_end = 0;
|
|
|
|
|
t.transport_state = JackTransportRolling;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
t.loop_start = 0;
|
|
|
|
|
t.loop_end = 0;
|
|
|
|
|
t.transport_state = JackTransportRolling;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-14 16:10:01 +00:00
|
|
|
}
|
|
|
|
|
#endif
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
2010-12-03 22:26:29 +00:00
|
|
|
ARDOUR::framecnt_t
|
2010-12-30 15:45:48 +00:00
|
|
|
Session::convert_to_frames (AnyTime const & position)
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
|
|
|
|
double secs;
|
2009-10-14 16:10:01 +00:00
|
|
|
|
2010-12-30 15:45:48 +00:00
|
|
|
switch (position.type) {
|
2008-06-02 21:41:35 +00:00
|
|
|
case AnyTime::BBT:
|
2012-01-05 15:56:14 +00:00
|
|
|
return _tempo_map->frame_time (position.bbt);
|
2008-06-02 21:41:35 +00:00
|
|
|
break;
|
|
|
|
|
|
2009-10-26 14:38:58 +00:00
|
|
|
case AnyTime::Timecode:
|
2008-06-02 21:41:35 +00:00
|
|
|
/* XXX need to handle negative values */
|
2010-12-30 15:45:48 +00:00
|
|
|
secs = position.timecode.hours * 60 * 60;
|
|
|
|
|
secs += position.timecode.minutes * 60;
|
|
|
|
|
secs += position.timecode.seconds;
|
|
|
|
|
secs += position.timecode.frames / timecode_frames_per_second();
|
2010-12-01 20:49:22 +00:00
|
|
|
if (config.get_timecode_offset_negative()) {
|
2010-12-03 22:26:29 +00:00
|
|
|
return (framecnt_t) floor (secs * frame_rate()) - config.get_timecode_offset();
|
2010-12-01 20:49:22 +00:00
|
|
|
} else {
|
2010-12-03 22:26:29 +00:00
|
|
|
return (framecnt_t) floor (secs * frame_rate()) + config.get_timecode_offset();
|
2010-12-01 17:51:17 +00:00
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AnyTime::Seconds:
|
2010-12-30 15:45:48 +00:00
|
|
|
return (framecnt_t) floor (position.seconds * frame_rate());
|
2008-06-02 21:41:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AnyTime::Frames:
|
2010-12-30 15:45:48 +00:00
|
|
|
return position.frames;
|
2008-06-02 21:41:35 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-30 15:45:48 +00:00
|
|
|
return position.frames;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ARDOUR::framecnt_t
|
|
|
|
|
Session::any_duration_to_frames (framepos_t position, AnyTime const & duration)
|
|
|
|
|
{
|
|
|
|
|
double secs;
|
|
|
|
|
|
|
|
|
|
switch (duration.type) {
|
|
|
|
|
case AnyTime::BBT:
|
|
|
|
|
return (framecnt_t) ( _tempo_map->framepos_plus_bbt (position, duration.bbt) - position);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AnyTime::Timecode:
|
|
|
|
|
/* XXX need to handle negative values */
|
|
|
|
|
secs = duration.timecode.hours * 60 * 60;
|
|
|
|
|
secs += duration.timecode.minutes * 60;
|
|
|
|
|
secs += duration.timecode.seconds;
|
|
|
|
|
secs += duration.timecode.frames / timecode_frames_per_second();
|
|
|
|
|
if (config.get_timecode_offset_negative()) {
|
|
|
|
|
return (framecnt_t) floor (secs * frame_rate()) - config.get_timecode_offset();
|
|
|
|
|
} else {
|
|
|
|
|
return (framecnt_t) floor (secs * frame_rate()) + config.get_timecode_offset();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AnyTime::Seconds:
|
|
|
|
|
return (framecnt_t) floor (duration.seconds * frame_rate());
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AnyTime::Frames:
|
|
|
|
|
return duration.frames;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return duration.frames;
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|