2 Copyright (C) 2000-2007 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "ardour/session.h"
21 #include "ardour/route.h"
22 #include "pbd/memento_command.h"
23 #include "ardour/diskstream.h"
24 #include "ardour/playlist.h"
25 #include "ardour/audioplaylist.h"
26 #include "ardour/audio_track.h"
27 #include "ardour/midi_playlist.h"
28 #include "ardour/midi_track.h"
29 #include "ardour/tempo.h"
30 #include "ardour/audiosource.h"
31 #include "ardour/audioregion.h"
32 #include "ardour/midi_source.h"
33 #include "ardour/midi_region.h"
34 #include "pbd/error.h"
36 #include "pbd/statefuldestructible.h"
37 #include "pbd/failed_constructor.h"
38 #include "evoral/Curve.hpp"
41 using namespace ARDOUR;
45 void Session::register_with_memento_command_factory(PBD::ID id, PBD::StatefulThingWithGoingAway *ptr)
51 Session::memento_command_factory(XMLNode *n)
54 XMLNode *before = 0, *after = 0;
58 id = PBD::ID(n->property("obj-id")->value());
60 /* get before/after */
62 if (n->name() == "MementoCommand") {
63 before = new XMLNode(*n->children().front());
64 after = new XMLNode(*n->children().back());
66 } else if (n->name() == "MementoUndoCommand") {
67 before = new XMLNode(*n->children().front());
69 } else if (n->name() == "MementoRedoCommand") {
70 after = new XMLNode(*n->children().front());
72 } else if (n->name() == "PlaylistCommand") {
73 before = new XMLNode(*n->children().front());
74 after = new XMLNode(*n->children().back());
79 error << _("Tried to reconstitute a MementoCommand with no contents, failing. id=") << id.to_s() << endmsg;
84 string obj_T = n->property ("type-name")->value();
85 if (obj_T == typeid (AudioRegion).name() || obj_T == typeid (MidiRegion).name() || obj_T == typeid (Region).name()) {
86 if (regions.count(id)) {
87 return new MementoCommand<Region>(*regions[id], before, after);
89 } else if (obj_T == typeid (AudioSource).name() || obj_T == typeid (MidiSource).name()) {
90 if (sources.count(id))
91 return new MementoCommand<Source>(*sources[id], before, after);
92 } else if (obj_T == typeid (Location).name()) {
93 Location* loc = _locations.get_location_by_id(id);
95 return new MementoCommand<Location>(*loc, before, after);
97 } else if (obj_T == typeid (Locations).name()) {
98 return new MementoCommand<Locations>(_locations, before, after);
99 } else if (obj_T == typeid (TempoMap).name()) {
100 return new MementoCommand<TempoMap>(*_tempo_map, before, after);
101 } else if (obj_T == typeid (Playlist).name() || obj_T == typeid (AudioPlaylist).name() || obj_T == typeid (MidiPlaylist).name()) {
102 if (boost::shared_ptr<Playlist> pl = playlist_by_name(child->property("name")->value())) {
103 return new MementoCommand<Playlist>(*(pl.get()), before, after);
105 } else if (obj_T == typeid (Route).name() || obj_T == typeid (AudioTrack).name() || obj_T == typeid(MidiTrack).name()) {
106 if (boost::shared_ptr<Route> r = route_by_id(id)) {
107 return new MementoCommand<Route>(*r, before, after);
109 error << string_compose (X_("Route %1 not found in session"), id) << endmsg;
111 } else if (obj_T == typeid (Evoral::Curve).name() || obj_T == typeid (AutomationList).name()) {
112 std::map<PBD::ID, AutomationList*>::iterator i = automation_lists.find(id);
113 if (i != automation_lists.end()) {
114 return new MementoCommand<AutomationList>(*i->second, before, after);
116 } else if (registry.count(id)) { // For Editor and AutomationLine which are off-limits here
117 return new MementoCommand<PBD::StatefulThingWithGoingAway>(*registry[id], before, after);
121 error << string_compose (_("could not reconstitute MementoCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s()) << endmsg;
127 Session::global_state_command_factory (const XMLNode& node)
129 const XMLProperty* prop;
130 Command* command = 0;
132 if ((prop = node.property ("type")) == 0) {
133 error << _("GlobalRouteStateCommand has no \"type\" node, ignoring") << endmsg;
139 if (prop->value() == "solo") {
140 command = new GlobalSoloStateCommand (*this, node);
141 } else if (prop->value() == "mute") {
142 command = new GlobalMuteStateCommand (*this, node);
143 } else if (prop->value() == "rec-enable") {
144 command = new GlobalRecordEnableStateCommand (*this, node);
145 } else if (prop->value() == "metering") {
146 command = new GlobalMeteringStateCommand (*this, node);
148 error << string_compose (_("unknown type of GlobalRouteStateCommand (%1), ignored"), prop->value()) << endmsg;
152 catch (failed_constructor& err) {
159 Session::GlobalRouteStateCommand::GlobalRouteStateCommand (Session& s, void* p)
164 Session::GlobalRouteStateCommand::GlobalRouteStateCommand (Session& s, const XMLNode& node)
165 : sess (s), src (this)
167 if (set_state (node)) {
168 throw failed_constructor ();
173 Session::GlobalRouteStateCommand::set_state (const XMLNode& node)
175 GlobalRouteBooleanState states;
177 const XMLProperty* prop;
179 XMLNodeConstIterator niter;
185 for (loop = 0; loop < 2; ++loop) {
195 if ((child = node.child (str)) == 0) {
196 warning << string_compose (_("global route state command has no \"%1\" node, ignoring entire command"), str) << endmsg;
200 nlist = child->children();
202 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
204 RouteBooleanState rbs;
205 boost::shared_ptr<Route> route;
208 prop = (*niter)->property ("id");
211 if ((route = sess.route_by_id (id)) == 0) {
212 warning << string_compose (_("cannot find track/bus \"%1\" while rebuilding a global route state command, ignored"), id.to_s()) << endmsg;
216 rbs.first = boost::weak_ptr<Route> (route);
218 prop = (*niter)->property ("yn");
219 rbs.second = (prop->value() == "1");
222 after.push_back (rbs);
224 before.push_back (rbs);
233 Session::GlobalRouteStateCommand::get_state ()
235 XMLNode* node = new XMLNode (X_("GlobalRouteStateCommand"));
236 XMLNode* nbefore = new XMLNode (X_("before"));
237 XMLNode* nafter = new XMLNode (X_("after"));
239 for (Session::GlobalRouteBooleanState::iterator x = before.begin(); x != before.end(); ++x) {
240 XMLNode* child = new XMLNode ("s");
241 boost::shared_ptr<Route> r = x->first.lock();
244 child->add_property (X_("id"), r->id().to_s());
245 child->add_property (X_("yn"), (x->second ? "1" : "0"));
246 nbefore->add_child_nocopy (*child);
250 for (Session::GlobalRouteBooleanState::iterator x = after.begin(); x != after.end(); ++x) {
251 XMLNode* child = new XMLNode ("s");
252 boost::shared_ptr<Route> r = x->first.lock();
255 child->add_property (X_("id"), r->id().to_s());
256 child->add_property (X_("yn"), (x->second ? "1" : "0"));
257 nafter->add_child_nocopy (*child);
261 node->add_child_nocopy (*nbefore);
262 node->add_child_nocopy (*nafter);
269 Session::GlobalSoloStateCommand::GlobalSoloStateCommand(Session &sess, void *src)
270 : GlobalRouteStateCommand (sess, src)
272 after = before = sess.get_global_route_boolean(&Route::soloed);
275 Session::GlobalSoloStateCommand::GlobalSoloStateCommand (Session& sess, const XMLNode& node)
276 : Session::GlobalRouteStateCommand (sess, node)
281 Session::GlobalSoloStateCommand::mark()
283 after = sess.get_global_route_boolean(&Route::soloed);
287 Session::GlobalSoloStateCommand::operator()()
289 sess.set_global_solo(after, src);
293 Session::GlobalSoloStateCommand::undo()
295 sess.set_global_solo(before, src);
299 Session::GlobalSoloStateCommand::get_state()
301 XMLNode& node = GlobalRouteStateCommand::get_state();
302 node.add_property ("type", "solo");
307 Session::GlobalMuteStateCommand::GlobalMuteStateCommand(Session &sess, void *src)
308 : GlobalRouteStateCommand (sess, src)
310 after = before = sess.get_global_route_boolean(&Route::muted);
313 Session::GlobalMuteStateCommand::GlobalMuteStateCommand (Session& sess, const XMLNode& node)
314 : Session::GlobalRouteStateCommand (sess, node)
319 Session::GlobalMuteStateCommand::mark()
321 after = sess.get_global_route_boolean(&Route::muted);
325 Session::GlobalMuteStateCommand::operator()()
327 sess.set_global_mute(after, src);
331 Session::GlobalMuteStateCommand::undo()
333 sess.set_global_mute(before, src);
337 Session::GlobalMuteStateCommand::get_state()
339 XMLNode& node = GlobalRouteStateCommand::get_state();
340 node.add_property ("type", "mute");
345 Session::GlobalRecordEnableStateCommand::GlobalRecordEnableStateCommand(Session &sess, void *src)
346 : GlobalRouteStateCommand (sess, src)
348 after = before = sess.get_global_route_boolean(&Route::record_enabled);
351 Session::GlobalRecordEnableStateCommand::GlobalRecordEnableStateCommand (Session& sess, const XMLNode& node)
352 : Session::GlobalRouteStateCommand (sess, node)
357 Session::GlobalRecordEnableStateCommand::mark()
359 after = sess.get_global_route_boolean(&Route::record_enabled);
363 Session::GlobalRecordEnableStateCommand::operator()()
365 sess.set_global_record_enable(after, src);
369 Session::GlobalRecordEnableStateCommand::undo()
371 sess.set_global_record_enable(before, src);
375 Session::GlobalRecordEnableStateCommand::get_state()
377 XMLNode& node = GlobalRouteStateCommand::get_state();
378 node.add_property ("type", "rec-enable");
383 Session::GlobalMeteringStateCommand::GlobalMeteringStateCommand(Session &s, void *p)
386 after = before = sess.get_global_route_metering();
389 Session::GlobalMeteringStateCommand::GlobalMeteringStateCommand (Session& s, const XMLNode& node)
390 : sess (s), src (this)
392 if (set_state (node)) {
393 throw failed_constructor();
398 Session::GlobalMeteringStateCommand::mark()
400 after = sess.get_global_route_metering();
404 Session::GlobalMeteringStateCommand::operator()()
406 sess.set_global_route_metering(after, src);
410 Session::GlobalMeteringStateCommand::undo()
412 sess.set_global_route_metering(before, src);
416 Session::GlobalMeteringStateCommand::get_state()
418 XMLNode* node = new XMLNode (X_("GlobalRouteStateCommand"));
419 XMLNode* nbefore = new XMLNode (X_("before"));
420 XMLNode* nafter = new XMLNode (X_("after"));
422 for (Session::GlobalRouteMeterState::iterator x = before.begin(); x != before.end(); ++x) {
423 XMLNode* child = new XMLNode ("s");
424 boost::shared_ptr<Route> r = x->first.lock();
427 child->add_property (X_("id"), r->id().to_s());
429 const char* meterstr = 0;
433 meterstr = X_("input");
436 meterstr = X_("pre");
439 meterstr = X_("post");
442 fatal << string_compose (_("programming error: %1") , "no meter state in Session::GlobalMeteringStateCommand::get_state") << endmsg;
445 child->add_property (X_("meter"), meterstr);
446 nbefore->add_child_nocopy (*child);
450 for (Session::GlobalRouteMeterState::iterator x = after.begin(); x != after.end(); ++x) {
451 XMLNode* child = new XMLNode ("s");
452 boost::shared_ptr<Route> r = x->first.lock();
455 child->add_property (X_("id"), r->id().to_s());
457 const char* meterstr;
461 meterstr = X_("input");
464 meterstr = X_("pre");
467 meterstr = X_("post");
469 default: meterstr = "";
472 child->add_property (X_("meter"), meterstr);
473 nafter->add_child_nocopy (*child);
477 node->add_child_nocopy (*nbefore);
478 node->add_child_nocopy (*nafter);
480 node->add_property ("type", "metering");
486 Session::GlobalMeteringStateCommand::set_state (const XMLNode& node)
488 GlobalRouteBooleanState states;
490 const XMLProperty* prop;
492 XMLNodeConstIterator niter;
498 for (loop = 0; loop < 2; ++loop) {
508 if ((child = node.child (str)) == 0) {
509 warning << string_compose (_("global route meter state command has no \"%1\" node, ignoring entire command"), str) << endmsg;
513 nlist = child->children();
515 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
518 boost::shared_ptr<Route> route;
521 prop = (*niter)->property ("id");
524 if ((route = sess.route_by_id (id)) == 0) {
525 warning << string_compose (_("cannot find track/bus \"%1\" while rebuilding a global route state command, ignored"), id.to_s()) << endmsg;
529 rms.first = boost::weak_ptr<Route> (route);
531 prop = (*niter)->property ("meter");
533 if (prop->value() == X_("pre")) {
534 rms.second = MeterPreFader;
535 } else if (prop->value() == X_("post")) {
536 rms.second = MeterPostFader;
538 rms.second = MeterInput;
542 after.push_back (rms);
544 before.push_back (rms);