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 } else if (obj_T == typeid (Evoral::Curve).name() || obj_T == typeid (AutomationList).name()) {
110 std::map<PBD::ID, AutomationList*>::iterator i = automation_lists.find(id);
111 if (i != automation_lists.end()) {
112 return new MementoCommand<AutomationList>(*i->second, before, after);
114 } else if (registry.count(id)) { // For Editor and AutomationLine which are off-limits here
115 return new MementoCommand<PBD::StatefulThingWithGoingAway>(*registry[id], before, after);
119 error << string_compose (_("could not reconstitute MementoCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s()) << endmsg;
125 Session::global_state_command_factory (const XMLNode& node)
127 const XMLProperty* prop;
128 Command* command = 0;
130 if ((prop = node.property ("type")) == 0) {
131 error << _("GlobalRouteStateCommand has no \"type\" node, ignoring") << endmsg;
137 if (prop->value() == "solo") {
138 command = new GlobalSoloStateCommand (*this, node);
139 } else if (prop->value() == "mute") {
140 command = new GlobalMuteStateCommand (*this, node);
141 } else if (prop->value() == "rec-enable") {
142 command = new GlobalRecordEnableStateCommand (*this, node);
143 } else if (prop->value() == "metering") {
144 command = new GlobalMeteringStateCommand (*this, node);
146 error << string_compose (_("unknown type of GlobalRouteStateCommand (%1), ignored"), prop->value()) << endmsg;
150 catch (failed_constructor& err) {
157 Session::GlobalRouteStateCommand::GlobalRouteStateCommand (Session& s, void* p)
162 Session::GlobalRouteStateCommand::GlobalRouteStateCommand (Session& s, const XMLNode& node)
163 : sess (s), src (this)
165 if (set_state (node)) {
166 throw failed_constructor ();
171 Session::GlobalRouteStateCommand::set_state (const XMLNode& node)
173 GlobalRouteBooleanState states;
175 const XMLProperty* prop;
177 XMLNodeConstIterator niter;
183 for (loop = 0; loop < 2; ++loop) {
193 if ((child = node.child (str)) == 0) {
194 warning << string_compose (_("global route state command has no \"%1\" node, ignoring entire command"), str) << endmsg;
198 nlist = child->children();
200 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
202 RouteBooleanState rbs;
203 boost::shared_ptr<Route> route;
206 prop = (*niter)->property ("id");
209 if ((route = sess.route_by_id (id)) == 0) {
210 warning << string_compose (_("cannot find track/bus \"%1\" while rebuilding a global route state command, ignored"), id.to_s()) << endmsg;
214 rbs.first = boost::weak_ptr<Route> (route);
216 prop = (*niter)->property ("yn");
217 rbs.second = (prop->value() == "1");
220 after.push_back (rbs);
222 before.push_back (rbs);
231 Session::GlobalRouteStateCommand::get_state ()
233 XMLNode* node = new XMLNode (X_("GlobalRouteStateCommand"));
234 XMLNode* nbefore = new XMLNode (X_("before"));
235 XMLNode* nafter = new XMLNode (X_("after"));
237 for (Session::GlobalRouteBooleanState::iterator x = before.begin(); x != before.end(); ++x) {
238 XMLNode* child = new XMLNode ("s");
239 boost::shared_ptr<Route> r = x->first.lock();
242 child->add_property (X_("id"), r->id().to_s());
243 child->add_property (X_("yn"), (x->second ? "1" : "0"));
244 nbefore->add_child_nocopy (*child);
248 for (Session::GlobalRouteBooleanState::iterator x = after.begin(); x != after.end(); ++x) {
249 XMLNode* child = new XMLNode ("s");
250 boost::shared_ptr<Route> r = x->first.lock();
253 child->add_property (X_("id"), r->id().to_s());
254 child->add_property (X_("yn"), (x->second ? "1" : "0"));
255 nafter->add_child_nocopy (*child);
259 node->add_child_nocopy (*nbefore);
260 node->add_child_nocopy (*nafter);
267 Session::GlobalSoloStateCommand::GlobalSoloStateCommand(Session &sess, void *src)
268 : GlobalRouteStateCommand (sess, src)
270 after = before = sess.get_global_route_boolean(&Route::soloed);
273 Session::GlobalSoloStateCommand::GlobalSoloStateCommand (Session& sess, const XMLNode& node)
274 : Session::GlobalRouteStateCommand (sess, node)
279 Session::GlobalSoloStateCommand::mark()
281 after = sess.get_global_route_boolean(&Route::soloed);
285 Session::GlobalSoloStateCommand::operator()()
287 sess.set_global_solo(after, src);
291 Session::GlobalSoloStateCommand::undo()
293 sess.set_global_solo(before, src);
297 Session::GlobalSoloStateCommand::get_state()
299 XMLNode& node = GlobalRouteStateCommand::get_state();
300 node.add_property ("type", "solo");
305 Session::GlobalMuteStateCommand::GlobalMuteStateCommand(Session &sess, void *src)
306 : GlobalRouteStateCommand (sess, src)
308 after = before = sess.get_global_route_boolean(&Route::muted);
311 Session::GlobalMuteStateCommand::GlobalMuteStateCommand (Session& sess, const XMLNode& node)
312 : Session::GlobalRouteStateCommand (sess, node)
317 Session::GlobalMuteStateCommand::mark()
319 after = sess.get_global_route_boolean(&Route::muted);
323 Session::GlobalMuteStateCommand::operator()()
325 sess.set_global_mute(after, src);
329 Session::GlobalMuteStateCommand::undo()
331 sess.set_global_mute(before, src);
335 Session::GlobalMuteStateCommand::get_state()
337 XMLNode& node = GlobalRouteStateCommand::get_state();
338 node.add_property ("type", "mute");
343 Session::GlobalRecordEnableStateCommand::GlobalRecordEnableStateCommand(Session &sess, void *src)
344 : GlobalRouteStateCommand (sess, src)
346 after = before = sess.get_global_route_boolean(&Route::record_enabled);
349 Session::GlobalRecordEnableStateCommand::GlobalRecordEnableStateCommand (Session& sess, const XMLNode& node)
350 : Session::GlobalRouteStateCommand (sess, node)
355 Session::GlobalRecordEnableStateCommand::mark()
357 after = sess.get_global_route_boolean(&Route::record_enabled);
361 Session::GlobalRecordEnableStateCommand::operator()()
363 sess.set_global_record_enable(after, src);
367 Session::GlobalRecordEnableStateCommand::undo()
369 sess.set_global_record_enable(before, src);
373 Session::GlobalRecordEnableStateCommand::get_state()
375 XMLNode& node = GlobalRouteStateCommand::get_state();
376 node.add_property ("type", "rec-enable");
381 Session::GlobalMeteringStateCommand::GlobalMeteringStateCommand(Session &s, void *p)
384 after = before = sess.get_global_route_metering();
387 Session::GlobalMeteringStateCommand::GlobalMeteringStateCommand (Session& s, const XMLNode& node)
388 : sess (s), src (this)
390 if (set_state (node)) {
391 throw failed_constructor();
396 Session::GlobalMeteringStateCommand::mark()
398 after = sess.get_global_route_metering();
402 Session::GlobalMeteringStateCommand::operator()()
404 sess.set_global_route_metering(after, src);
408 Session::GlobalMeteringStateCommand::undo()
410 sess.set_global_route_metering(before, src);
414 Session::GlobalMeteringStateCommand::get_state()
416 XMLNode* node = new XMLNode (X_("GlobalRouteStateCommand"));
417 XMLNode* nbefore = new XMLNode (X_("before"));
418 XMLNode* nafter = new XMLNode (X_("after"));
420 for (Session::GlobalRouteMeterState::iterator x = before.begin(); x != before.end(); ++x) {
421 XMLNode* child = new XMLNode ("s");
422 boost::shared_ptr<Route> r = x->first.lock();
425 child->add_property (X_("id"), r->id().to_s());
427 const char* meterstr = 0;
431 meterstr = X_("input");
434 meterstr = X_("pre");
437 meterstr = X_("post");
440 fatal << string_compose (_("programming error: %1") , "no meter state in Session::GlobalMeteringStateCommand::get_state") << endmsg;
443 child->add_property (X_("meter"), meterstr);
444 nbefore->add_child_nocopy (*child);
448 for (Session::GlobalRouteMeterState::iterator x = after.begin(); x != after.end(); ++x) {
449 XMLNode* child = new XMLNode ("s");
450 boost::shared_ptr<Route> r = x->first.lock();
453 child->add_property (X_("id"), r->id().to_s());
455 const char* meterstr;
459 meterstr = X_("input");
462 meterstr = X_("pre");
465 meterstr = X_("post");
467 default: meterstr = "";
470 child->add_property (X_("meter"), meterstr);
471 nafter->add_child_nocopy (*child);
475 node->add_child_nocopy (*nbefore);
476 node->add_child_nocopy (*nafter);
478 node->add_property ("type", "metering");
484 Session::GlobalMeteringStateCommand::set_state (const XMLNode& node)
486 GlobalRouteBooleanState states;
488 const XMLProperty* prop;
490 XMLNodeConstIterator niter;
496 for (loop = 0; loop < 2; ++loop) {
506 if ((child = node.child (str)) == 0) {
507 warning << string_compose (_("global route meter state command has no \"%1\" node, ignoring entire command"), str) << endmsg;
511 nlist = child->children();
513 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
516 boost::shared_ptr<Route> route;
519 prop = (*niter)->property ("id");
522 if ((route = sess.route_by_id (id)) == 0) {
523 warning << string_compose (_("cannot find track/bus \"%1\" while rebuilding a global route state command, ignored"), id.to_s()) << endmsg;
527 rms.first = boost::weak_ptr<Route> (route);
529 prop = (*niter)->property ("meter");
531 if (prop->value() == X_("pre")) {
532 rms.second = MeterPreFader;
533 } else if (prop->value() == X_("post")) {
534 rms.second = MeterPostFader;
536 rms.second = MeterInput;
540 after.push_back (rms);
542 before.push_back (rms);