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 return new MementoCommand<Route>(*route_by_id(id), before, after);
107 } else if (obj_T == typeid (Evoral::Curve).name() || obj_T == typeid (AutomationList).name()) {
108 std::map<PBD::ID, AutomationList*>::iterator i = automation_lists.find(id);
109 if (i != automation_lists.end()) {
110 return new MementoCommand<AutomationList>(*i->second, before, after);
112 } else if (registry.count(id)) { // For Editor and AutomationLine which are off-limits here
113 return new MementoCommand<PBD::StatefulThingWithGoingAway>(*registry[id], before, after);
117 error << string_compose (_("could not reconstitute MementoCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s()) << endmsg;
123 Session::global_state_command_factory (const XMLNode& node)
125 const XMLProperty* prop;
126 Command* command = 0;
128 if ((prop = node.property ("type")) == 0) {
129 error << _("GlobalRouteStateCommand has no \"type\" node, ignoring") << endmsg;
135 if (prop->value() == "solo") {
136 command = new GlobalSoloStateCommand (*this, node);
137 } else if (prop->value() == "mute") {
138 command = new GlobalMuteStateCommand (*this, node);
139 } else if (prop->value() == "rec-enable") {
140 command = new GlobalRecordEnableStateCommand (*this, node);
141 } else if (prop->value() == "metering") {
142 command = new GlobalMeteringStateCommand (*this, node);
144 error << string_compose (_("unknown type of GlobalRouteStateCommand (%1), ignored"), prop->value()) << endmsg;
148 catch (failed_constructor& err) {
155 Session::GlobalRouteStateCommand::GlobalRouteStateCommand (Session& s, void* p)
160 Session::GlobalRouteStateCommand::GlobalRouteStateCommand (Session& s, const XMLNode& node)
161 : sess (s), src (this)
163 if (set_state (node)) {
164 throw failed_constructor ();
169 Session::GlobalRouteStateCommand::set_state (const XMLNode& node)
171 GlobalRouteBooleanState states;
173 const XMLProperty* prop;
175 XMLNodeConstIterator niter;
181 for (loop = 0; loop < 2; ++loop) {
191 if ((child = node.child (str)) == 0) {
192 warning << string_compose (_("global route state command has no \"%1\" node, ignoring entire command"), str) << endmsg;
196 nlist = child->children();
198 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
200 RouteBooleanState rbs;
201 boost::shared_ptr<Route> route;
204 prop = (*niter)->property ("id");
207 if ((route = sess.route_by_id (id)) == 0) {
208 warning << string_compose (_("cannot find track/bus \"%1\" while rebuilding a global route state command, ignored"), id.to_s()) << endmsg;
212 rbs.first = boost::weak_ptr<Route> (route);
214 prop = (*niter)->property ("yn");
215 rbs.second = (prop->value() == "1");
218 after.push_back (rbs);
220 before.push_back (rbs);
229 Session::GlobalRouteStateCommand::get_state ()
231 XMLNode* node = new XMLNode (X_("GlobalRouteStateCommand"));
232 XMLNode* nbefore = new XMLNode (X_("before"));
233 XMLNode* nafter = new XMLNode (X_("after"));
235 for (Session::GlobalRouteBooleanState::iterator x = before.begin(); x != before.end(); ++x) {
236 XMLNode* child = new XMLNode ("s");
237 boost::shared_ptr<Route> r = x->first.lock();
240 child->add_property (X_("id"), r->id().to_s());
241 child->add_property (X_("yn"), (x->second ? "1" : "0"));
242 nbefore->add_child_nocopy (*child);
246 for (Session::GlobalRouteBooleanState::iterator x = after.begin(); x != after.end(); ++x) {
247 XMLNode* child = new XMLNode ("s");
248 boost::shared_ptr<Route> r = x->first.lock();
251 child->add_property (X_("id"), r->id().to_s());
252 child->add_property (X_("yn"), (x->second ? "1" : "0"));
253 nafter->add_child_nocopy (*child);
257 node->add_child_nocopy (*nbefore);
258 node->add_child_nocopy (*nafter);
265 Session::GlobalSoloStateCommand::GlobalSoloStateCommand(Session &sess, void *src)
266 : GlobalRouteStateCommand (sess, src)
268 after = before = sess.get_global_route_boolean(&Route::soloed);
271 Session::GlobalSoloStateCommand::GlobalSoloStateCommand (Session& sess, const XMLNode& node)
272 : Session::GlobalRouteStateCommand (sess, node)
277 Session::GlobalSoloStateCommand::mark()
279 after = sess.get_global_route_boolean(&Route::soloed);
283 Session::GlobalSoloStateCommand::operator()()
285 sess.set_global_solo(after, src);
289 Session::GlobalSoloStateCommand::undo()
291 sess.set_global_solo(before, src);
295 Session::GlobalSoloStateCommand::get_state()
297 XMLNode& node = GlobalRouteStateCommand::get_state();
298 node.add_property ("type", "solo");
303 Session::GlobalMuteStateCommand::GlobalMuteStateCommand(Session &sess, void *src)
304 : GlobalRouteStateCommand (sess, src)
306 after = before = sess.get_global_route_boolean(&Route::muted);
309 Session::GlobalMuteStateCommand::GlobalMuteStateCommand (Session& sess, const XMLNode& node)
310 : Session::GlobalRouteStateCommand (sess, node)
315 Session::GlobalMuteStateCommand::mark()
317 after = sess.get_global_route_boolean(&Route::muted);
321 Session::GlobalMuteStateCommand::operator()()
323 sess.set_global_mute(after, src);
327 Session::GlobalMuteStateCommand::undo()
329 sess.set_global_mute(before, src);
333 Session::GlobalMuteStateCommand::get_state()
335 XMLNode& node = GlobalRouteStateCommand::get_state();
336 node.add_property ("type", "mute");
341 Session::GlobalRecordEnableStateCommand::GlobalRecordEnableStateCommand(Session &sess, void *src)
342 : GlobalRouteStateCommand (sess, src)
344 after = before = sess.get_global_route_boolean(&Route::record_enabled);
347 Session::GlobalRecordEnableStateCommand::GlobalRecordEnableStateCommand (Session& sess, const XMLNode& node)
348 : Session::GlobalRouteStateCommand (sess, node)
353 Session::GlobalRecordEnableStateCommand::mark()
355 after = sess.get_global_route_boolean(&Route::record_enabled);
359 Session::GlobalRecordEnableStateCommand::operator()()
361 sess.set_global_record_enable(after, src);
365 Session::GlobalRecordEnableStateCommand::undo()
367 sess.set_global_record_enable(before, src);
371 Session::GlobalRecordEnableStateCommand::get_state()
373 XMLNode& node = GlobalRouteStateCommand::get_state();
374 node.add_property ("type", "rec-enable");
379 Session::GlobalMeteringStateCommand::GlobalMeteringStateCommand(Session &s, void *p)
382 after = before = sess.get_global_route_metering();
385 Session::GlobalMeteringStateCommand::GlobalMeteringStateCommand (Session& s, const XMLNode& node)
386 : sess (s), src (this)
388 if (set_state (node)) {
389 throw failed_constructor();
394 Session::GlobalMeteringStateCommand::mark()
396 after = sess.get_global_route_metering();
400 Session::GlobalMeteringStateCommand::operator()()
402 sess.set_global_route_metering(after, src);
406 Session::GlobalMeteringStateCommand::undo()
408 sess.set_global_route_metering(before, src);
412 Session::GlobalMeteringStateCommand::get_state()
414 XMLNode* node = new XMLNode (X_("GlobalRouteStateCommand"));
415 XMLNode* nbefore = new XMLNode (X_("before"));
416 XMLNode* nafter = new XMLNode (X_("after"));
418 for (Session::GlobalRouteMeterState::iterator x = before.begin(); x != before.end(); ++x) {
419 XMLNode* child = new XMLNode ("s");
420 boost::shared_ptr<Route> r = x->first.lock();
423 child->add_property (X_("id"), r->id().to_s());
425 const char* meterstr = 0;
429 meterstr = X_("input");
432 meterstr = X_("pre");
435 meterstr = X_("post");
438 fatal << string_compose (_("programming error: %1") , "no meter state in Session::GlobalMeteringStateCommand::get_state") << endmsg;
441 child->add_property (X_("meter"), meterstr);
442 nbefore->add_child_nocopy (*child);
446 for (Session::GlobalRouteMeterState::iterator x = after.begin(); x != after.end(); ++x) {
447 XMLNode* child = new XMLNode ("s");
448 boost::shared_ptr<Route> r = x->first.lock();
451 child->add_property (X_("id"), r->id().to_s());
453 const char* meterstr;
457 meterstr = X_("input");
460 meterstr = X_("pre");
463 meterstr = X_("post");
465 default: meterstr = "";
468 child->add_property (X_("meter"), meterstr);
469 nafter->add_child_nocopy (*child);
473 node->add_child_nocopy (*nbefore);
474 node->add_child_nocopy (*nafter);
476 node->add_property ("type", "metering");
482 Session::GlobalMeteringStateCommand::set_state (const XMLNode& node)
484 GlobalRouteBooleanState states;
486 const XMLProperty* prop;
488 XMLNodeConstIterator niter;
494 for (loop = 0; loop < 2; ++loop) {
504 if ((child = node.child (str)) == 0) {
505 warning << string_compose (_("global route meter state command has no \"%1\" node, ignoring entire command"), str) << endmsg;
509 nlist = child->children();
511 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
514 boost::shared_ptr<Route> route;
517 prop = (*niter)->property ("id");
520 if ((route = sess.route_by_id (id)) == 0) {
521 warning << string_compose (_("cannot find track/bus \"%1\" while rebuilding a global route state command, ignored"), id.to_s()) << endmsg;
525 rms.first = boost::weak_ptr<Route> (route);
527 prop = (*niter)->property ("meter");
529 if (prop->value() == X_("pre")) {
530 rms.second = MeterPreFader;
531 } else if (prop->value() == X_("post")) {
532 rms.second = MeterPostFader;
534 rms.second = MeterInput;
538 after.push_back (rms);
540 before.push_back (rms);