summaryrefslogtreecommitdiff
path: root/libs/backends/jack/jack_session.cc
blob: f3c5c2553f74975771b8039232ae392cab611adf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright (C) 2013-2017 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
 * Copyright (C) 2015-2016 Nick Mainsbridge <mainsbridge@gmail.com>
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include <time.h>

#include <glibmm/miscutils.h>

#include "pbd/localtime_r.h"

#include "ardour/audioengine.h"
#include "ardour/filename_extensions.h"
#include "ardour/session.h"
#include "ardour/session_directory.h"
#include "ardour/tempo.h"

#include "jack_session.h"

using namespace ARDOUR;
using std::string;

JACKSession::JACKSession (Session* s)
	: SessionHandlePtr (s)
{
}

JACKSession::~JACKSession ()
{
}

void
JACKSession::session_event (jack_session_event_t* event)
{
        char timebuf[128], *tmp;
        time_t n;
        struct tm local_time;

        time (&n);
        localtime_r (&n, &local_time);
        strftime (timebuf, sizeof(timebuf), "JS_%FT%T", &local_time);

        while ((tmp = strchr(timebuf, ':'))) { *tmp = '.'; }

        if (event->type == JackSessionSaveTemplate)
        {
                if (_session->save_template( timebuf )) {
                        event->flags = JackSessionSaveError;
                } else {
                        string cmd ("ardour3 -P -U ");
                        cmd += event->client_uuid;
                        cmd += " -T ";
                        cmd += timebuf;

                        event->command_line = strdup (cmd.c_str());
                }
        }
        else
        {
                if (_session->save_state (timebuf)) {
                        event->flags = JackSessionSaveError;
                } else {
			std::string xml_path (_session->session_directory().root_path());
			std::string legalized_filename = legalize_for_path (timebuf) + statefile_suffix;
			xml_path = Glib::build_filename (xml_path, legalized_filename);

                        string cmd ("ardour3 -P -U ");
                        cmd += event->client_uuid;
                        cmd += " \"";
                        cmd += xml_path;
                        cmd += '\"';

                        event->command_line = strdup (cmd.c_str());
                }
        }

	/* this won't be called if the port engine in use is not JACK, so we do
	   not have to worry about the type of PortEngine::private_handle()
	*/

	jack_client_t* jack_client = (jack_client_t*) AudioEngine::instance()->port_engine().private_handle();

	if (jack_client) {
		jack_session_reply (jack_client, event);
	}

	if (event->type == JackSessionSaveAndQuit) {
		_session->Quit (); /* EMIT SIGNAL */
	}

	jack_session_event_free (event);
}

void
JACKSession::timebase_callback (jack_transport_state_t /*state*/,
				 pframes_t /*nframes*/,
				 jack_position_t* pos,
				 int /*new_position*/)
{
	Timecode::BBT_Time bbt;
	TempoMap& tempo_map (_session->tempo_map());
	samplepos_t tf;

	/* see commit msg for e2c26e1b9 and Session::start_locate() for
	   details.
	*/

	tf = _session->nominal_jack_transport_sample().value_or (_session->transport_sample());

	/* BBT info */

	TempoMetric metric (tempo_map.metric_at (tf));

	try {
		bbt = tempo_map.bbt_at_sample (tf);

		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();
		pos->ticks_per_beat = Timecode::BBT_Time::ticks_per_beat;
		pos->beats_per_minute = metric.tempo().note_types_per_minute();

		pos->valid = jack_position_bits_t (pos->valid | JackPositionBBT);

	} catch (...) {
		/* no message */
	}

#ifdef HAVE_JACK_VIDEO_SUPPORT
	//poke audio video ratio so Ardour can track Video Sync
	pos->audio_frames_per_video_frame = _session->sample_rate() / _session->timecode_frames_per_second();
	pos->valid = jack_position_bits_t (pos->valid | JackAudioVideoRatio);
#endif

#ifdef HAVE_JACK_TIMCODE_SUPPORT
	/* This is not yet defined in JACK */

	/* Timecode info */

	pos->timecode_offset = _session->config.get_timecode_offset();
	t.timecode_frame_rate = _session->timecode_frames_per_second();
	pos->valid = jack_position_bits_t (pos->valid | JackPositionTimecode);
#endif

#ifdef HAVE_JACK_LOOPING_SUPPORT
	/* This is not yet defined in JACK */
	if (_transport_speed) {

		if (play_loop) {

			Location* location = _session->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;

		}

	}
#endif
}