summaryrefslogtreecommitdiff
path: root/libs/ardour/transport_master.cc
blob: f3aad266fe46116bb72798a8f10c162de86805a3 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
    Copyright (C) 2002 Paul Davis

    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.

 */

#include <vector>

#include "pbd/debug.h"
#include "pbd/i18n.h"

#include "ardour/audioengine.h"
#include "ardour/midi_port.h"
#include "ardour/session.h"
#include "ardour/transport_master.h"
#include "ardour/transport_master_manager.h"
#include "ardour/types_convert.h"
#include "ardour/utils.h"

namespace ARDOUR {
	namespace Properties {
		PBD::PropertyDescriptor<bool> fr2997;
		PBD::PropertyDescriptor<bool> sclock_synced;
		PBD::PropertyDescriptor<bool> collect;
		PBD::PropertyDescriptor<bool> connected;
		PBD::PropertyDescriptor<TransportRequestType> allowed_transport_requests;
	}
}

using namespace ARDOUR;
using namespace PBD;

void
TransportMaster::make_property_quarks ()
{
	Properties::fr2997.property_id = g_quark_from_static_string (X_("fr2997"));
	DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fr2997 = %1\n", Properties::fr2997.property_id));
	Properties::sclock_synced.property_id = g_quark_from_static_string (X_("sclock_synced"));
	DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for sclock_synced = %1\n", Properties::sclock_synced.property_id));
	Properties::collect.property_id = g_quark_from_static_string (X_("collect"));
	DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for collect = %1\n", Properties::collect.property_id));
	Properties::connected.property_id = g_quark_from_static_string (X_("connected"));
	DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for connected = %1\n", Properties::connected.property_id));
	Properties::allowed_transport_requests.property_id = g_quark_from_static_string (X_("allowed_transport_requests"));
	DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for allowed_transport_requests = %1\n", Properties::allowed_transport_requests.property_id));
}

const std::string TransportMaster::state_node_name = X_("TransportMaster");

TransportMaster::TransportMaster (SyncSource t, std::string const & name)
	: _type (t)
	, _name (Properties::name, name)
	, _session (0)
	, _current_delta (0)
	, _pending_collect (true)
	, _request_mask (Properties::allowed_transport_requests, TransportRequestType (0))
	, _locked (Properties::locked, false)
	, _sclock_synced (Properties::sclock_synced, false)
	, _collect (Properties::collect, true)
	, _connected (Properties::connected, false)
{
	register_properties ();

	ARDOUR::AudioEngine::instance()->PortConnectedOrDisconnected.connect_same_thread (port_connection, boost::bind (&TransportMaster::connection_handler, this, _1, _2, _3, _4, _5));
	ARDOUR::AudioEngine::instance()->Running.connect_same_thread (backend_connection, boost::bind (&TransportMaster::check_backend, this));
}

TransportMaster::~TransportMaster()
{
	delete _session;
}

void
TransportMaster::register_properties ()
{
	_xml_node_name = state_node_name;

	add_property (_name);
	add_property (_locked);
	add_property (_collect);
	add_property (_sclock_synced);
	add_property (_request_mask);

	/* we omit _connected since it is derived from port state, and merely
	 * used for signalling
	 */
}

void
TransportMaster::set_name (std::string const & str)
{
	if (_name != str) {
		_name = str;
		PropertyChange (Properties::name);
	}
}

bool
TransportMaster::connection_handler (boost::weak_ptr<ARDOUR::Port>, std::string name1, boost::weak_ptr<ARDOUR::Port>, std::string name2, bool yn)
{
	if (!_port) {
		return false;
	}

	const std::string fqn = ARDOUR::AudioEngine::instance()->make_port_name_non_relative (_port->name());

	if (fqn == name1 || fqn == name2) {

		/* it's about us */

		/* XXX technically .. if the user makes an N->1 connection to
		 * this transport master's port, this simple minded logic is
		 * not sufficient. But the user shouldn't do that ...
		 */

		if (yn) {
			_connected = true;
		} else {
			_connected = false;
		}

		PropertyChanged (Properties::connected);

		return true;
	}

	return false;
}

bool
TransportMaster::check_collect()
{
	if (!_connected) {
		return false;
	}

	/* XXX should probably use boost::atomic something or other here */

	if (_pending_collect != _collect) {
		if (_pending_collect) {
			init ();
		} else {
			if (TransportMasterManager::instance().current().get() == this) {
				if (_session) {
					_session->config.set_external_sync (false);
				}
			}
		}
		_collect = _pending_collect;
		PropertyChanged (Properties::collect);
	}

	return _collect;
}

void
TransportMaster::set_collect (bool yn)
{
	_pending_collect = yn;
}

void
TransportMaster::set_sample_clock_synced (bool yn)
{
	if (yn != _sclock_synced) {
		_sclock_synced = yn;
		PropertyChanged (Properties::sclock_synced);
	}
}

void
TransportMaster::set_session (Session* s)
{
	_session = s;
}

int
TransportMaster::set_state (XMLNode const & node, int /* version */)
{
	PropertyChange what_changed;

	what_changed = set_values (node);

	XMLNode* pnode = node.child (X_("Port"));

	if (pnode) {
		XMLNodeList const & children = pnode->children();
		for (XMLNodeList::const_iterator ci = children.begin(); ci != children.end(); ++ci) {

			XMLProperty const *prop;

			if ((*ci)->name() == X_("Connection")) {
				if ((prop = (*ci)->property (X_("other"))) == 0) {
					continue;
				}
				_port->connect (prop->value());
			}
		}
	}

	PropertyChanged (what_changed);

	return 0;
}

XMLNode&
TransportMaster::get_state ()
{
	XMLNode* node = new XMLNode (state_node_name);
	node->set_property (X_("type"), _type);

	add_properties (*node);

	if (_port) {
		std::vector<std::string> connections;

		XMLNode* pnode = new XMLNode (X_("Port"));

		if (_port->get_connections (connections)) {

			std::vector<std::string>::const_iterator ci;
			std::sort (connections.begin(), connections.end());

			for (ci = connections.begin(); ci != connections.end(); ++ci) {

				/* if its a connection to our own port,
				   return only the port name, not the
				   whole thing. this allows connections
				   to be re-established even when our
				   client name is different.
				*/

				XMLNode* cnode = new XMLNode (X_("Connection"));

				cnode->set_property (X_("other"), AudioEngine::instance()->make_port_name_relative (*ci));
				pnode->add_child_nocopy (*cnode);
			}
		}

		node->add_child_nocopy (*pnode);
	}

	return *node;
}

boost::shared_ptr<TransportMaster>
TransportMaster::factory (XMLNode const & node)
{
	if (node.name() != TransportMaster::state_node_name) {
		return boost::shared_ptr<TransportMaster>();
	}

	SyncSource type;
	std::string name;

	if (!node.get_property (X_("type"), type)) {
		return boost::shared_ptr<TransportMaster>();
	}

	if (!node.get_property (X_("name"), name)) {
		return boost::shared_ptr<TransportMaster>();
	}

	return factory (type, name);
}

boost::shared_ptr<TransportMaster>
TransportMaster::factory (SyncSource type, std::string const& name)
{
	/* XXX need to count existing sources of a given type */

	switch (type) {
	case MTC:
		return boost::shared_ptr<TransportMaster> (new MTC_TransportMaster (sync_source_to_string (type)));
	case LTC:
		return boost::shared_ptr<TransportMaster> (new LTC_TransportMaster (sync_source_to_string (type)));
	case MIDIClock:
		return boost::shared_ptr<TransportMaster> (new MIDIClock_TransportMaster (sync_source_to_string (type)));
	case Engine:
		return boost::shared_ptr<TransportMaster> (new Engine_TransportMaster (*AudioEngine::instance()));
	default:
		break;
	}

	return boost::shared_ptr<TransportMaster>();
}

boost::shared_ptr<Port>
TransportMasterViaMIDI::create_midi_port (std::string const & port_name)
{
	boost::shared_ptr<Port> p;

	if ((p = AudioEngine::instance()->register_input_port (DataType::MIDI, port_name)) == 0) {
		return boost::shared_ptr<Port> ();
	}

	_midi_port = boost::dynamic_pointer_cast<MidiPort> (p);

	return p;
}

bool
TransportMaster::allow_request (TransportRequestSource src, TransportRequestType type) const
{
	return _request_mask & type;
}

void
TransportMaster::set_request_mask (TransportRequestType t)
{
	if (_request_mask != t) {
		_request_mask = t;
		PropertyChanged (Properties::allowed_transport_requests);
	}
}

TimecodeTransportMaster::TimecodeTransportMaster (std::string const & name, SyncSource type)
	: TransportMaster (type, name)
	, _fr2997 (Properties::fr2997, false)
{
	register_properties ();
}

void
TimecodeTransportMaster::register_properties ()
{
	TransportMaster::register_properties ();
	add_property (_fr2997);
}

void
TimecodeTransportMaster::set_fr2997 (bool yn)
{
	if (yn != _fr2997) {
		_fr2997 = yn;
		PropertyChanged (Properties::fr2997);
	}
}