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 "ardour/session_playlists.h"
35 #include "pbd/error.h"
37 #include "pbd/statefuldestructible.h"
38 #include "pbd/failed_constructor.h"
39 #include "evoral/Curve.hpp"
42 using namespace ARDOUR;
46 void Session::register_with_memento_command_factory(PBD::ID id, PBD::StatefulThingWithGoingAway *ptr)
52 Session::memento_command_factory(XMLNode *n)
55 XMLNode *before = 0, *after = 0;
59 id = PBD::ID(n->property("obj-id")->value());
61 /* get before/after */
63 if (n->name() == "MementoCommand") {
64 before = new XMLNode(*n->children().front());
65 after = new XMLNode(*n->children().back());
67 } else if (n->name() == "MementoUndoCommand") {
68 before = new XMLNode(*n->children().front());
70 } else if (n->name() == "MementoRedoCommand") {
71 after = new XMLNode(*n->children().front());
73 } else if (n->name() == "PlaylistCommand") {
74 before = new XMLNode(*n->children().front());
75 after = new XMLNode(*n->children().back());
80 error << _("Tried to reconstitute a MementoCommand with no contents, failing. id=") << id.to_s() << endmsg;
85 string obj_T = n->property ("type-name")->value();
86 if (obj_T == typeid (AudioRegion).name() || obj_T == typeid (MidiRegion).name() || obj_T == typeid (Region).name()) {
87 if (regions.count(id)) {
88 return new MementoCommand<Region>(*regions[id], before, after);
90 } else if (obj_T == typeid (AudioSource).name() || obj_T == typeid (MidiSource).name()) {
91 if (sources.count(id))
92 return new MementoCommand<Source>(*sources[id], before, after);
93 } else if (obj_T == typeid (Location).name()) {
94 Location* loc = _locations.get_location_by_id(id);
96 return new MementoCommand<Location>(*loc, before, after);
98 } else if (obj_T == typeid (Locations).name()) {
99 return new MementoCommand<Locations>(_locations, before, after);
100 } else if (obj_T == typeid (TempoMap).name()) {
101 return new MementoCommand<TempoMap>(*_tempo_map, before, after);
102 } else if (obj_T == typeid (Playlist).name() || obj_T == typeid (AudioPlaylist).name() || obj_T == typeid (MidiPlaylist).name()) {
103 if (boost::shared_ptr<Playlist> pl = playlists->by_name(child->property("name")->value())) {
104 return new MementoCommand<Playlist>(*(pl.get()), before, after);
106 } else if (obj_T == typeid (Route).name() || obj_T == typeid (AudioTrack).name() || obj_T == typeid(MidiTrack).name()) {
107 if (boost::shared_ptr<Route> r = route_by_id(id)) {
108 return new MementoCommand<Route>(*r, before, after);
110 error << string_compose (X_("Route %1 not found in session"), id) << endmsg;
112 } else if (obj_T == typeid (Evoral::Curve).name() || obj_T == typeid (AutomationList).name()) {
113 std::map<PBD::ID, AutomationList*>::iterator i = automation_lists.find(id);
114 if (i != automation_lists.end()) {
115 return new MementoCommand<AutomationList>(*i->second, before, after);
117 } else if (registry.count(id)) { // For Editor and AutomationLine which are off-limits here
118 return new MementoCommand<PBD::StatefulThingWithGoingAway>(*registry[id], before, after);
122 error << string_compose (_("could not reconstitute MementoCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s()) << endmsg;
128 Session::global_state_command_factory (const XMLNode& node)
130 const XMLProperty* prop;
131 Command* command = 0;
133 if ((prop = node.property ("type")) == 0) {
134 error << _("GlobalRouteStateCommand has no \"type\" node, ignoring") << endmsg;
140 if (prop->value() == "solo") {
141 command = new GlobalSoloStateCommand (*this, node);
142 } else if (prop->value() == "mute") {
143 command = new GlobalMuteStateCommand (*this, node);
144 } else if (prop->value() == "rec-enable") {
145 command = new GlobalRecordEnableStateCommand (*this, node);
146 } else if (prop->value() == "metering") {
147 command = new GlobalMeteringStateCommand (*this, node);
149 error << string_compose (_("unknown type of GlobalRouteStateCommand (%1), ignored"), prop->value()) << endmsg;
153 catch (failed_constructor& err) {
160 Session::GlobalRouteStateCommand::GlobalRouteStateCommand (Session& s, void* p)
165 Session::GlobalRouteStateCommand::GlobalRouteStateCommand (Session& s, const XMLNode& node)
166 : sess (s), src (this)
168 if (set_state (node, Stateful::loading_state_version)) {
169 throw failed_constructor ();
174 Session::GlobalRouteStateCommand::set_state (const XMLNode& node, int /*version*/)
176 GlobalRouteBooleanState states;
178 const XMLProperty* prop;
180 XMLNodeConstIterator niter;
186 for (loop = 0; loop < 2; ++loop) {
196 if ((child = node.child (str)) == 0) {
197 warning << string_compose (_("global route state command has no \"%1\" node, ignoring entire command"), str) << endmsg;
201 nlist = child->children();
203 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
205 RouteBooleanState rbs;
206 boost::shared_ptr<Route> route;
209 prop = (*niter)->property ("id");
212 if ((route = sess.route_by_id (id)) == 0) {
213 warning << string_compose (_("cannot find track/bus \"%1\" while rebuilding a global route state command, ignored"), id.to_s()) << endmsg;
217 rbs.first = boost::weak_ptr<Route> (route);
219 prop = (*niter)->property ("yn");
220 rbs.second = (prop->value() == "1");
223 after.push_back (rbs);
225 before.push_back (rbs);
234 Session::GlobalRouteStateCommand::get_state ()
236 XMLNode* node = new XMLNode (X_("GlobalRouteStateCommand"));
237 XMLNode* nbefore = new XMLNode (X_("before"));
238 XMLNode* nafter = new XMLNode (X_("after"));
240 for (Session::GlobalRouteBooleanState::iterator x = before.begin(); x != before.end(); ++x) {
241 XMLNode* child = new XMLNode ("s");
242 boost::shared_ptr<Route> r = x->first.lock();
245 child->add_property (X_("id"), r->id().to_s());
246 child->add_property (X_("yn"), (x->second ? "1" : "0"));
247 nbefore->add_child_nocopy (*child);
251 for (Session::GlobalRouteBooleanState::iterator x = after.begin(); x != after.end(); ++x) {
252 XMLNode* child = new XMLNode ("s");
253 boost::shared_ptr<Route> r = x->first.lock();
256 child->add_property (X_("id"), r->id().to_s());
257 child->add_property (X_("yn"), (x->second ? "1" : "0"));
258 nafter->add_child_nocopy (*child);
262 node->add_child_nocopy (*nbefore);
263 node->add_child_nocopy (*nafter);
270 Session::GlobalSoloStateCommand::GlobalSoloStateCommand(Session &sess, void *src)
271 : GlobalRouteStateCommand (sess, src)
273 after = before = sess.get_global_route_boolean(&Route::soloed);
276 Session::GlobalSoloStateCommand::GlobalSoloStateCommand (Session& sess, const XMLNode& node)
277 : Session::GlobalRouteStateCommand (sess, node)
282 Session::GlobalSoloStateCommand::mark()
284 after = sess.get_global_route_boolean(&Route::soloed);
288 Session::GlobalSoloStateCommand::operator()()
290 sess.set_global_solo(after, src);
294 Session::GlobalSoloStateCommand::undo()
296 sess.set_global_solo(before, src);
300 Session::GlobalSoloStateCommand::get_state()
302 XMLNode& node = GlobalRouteStateCommand::get_state();
303 node.add_property ("type", "solo");
308 Session::GlobalMuteStateCommand::GlobalMuteStateCommand(Session &sess, void *src)
309 : GlobalRouteStateCommand (sess, src)
311 after = before = sess.get_global_route_boolean(&Route::muted);
314 Session::GlobalMuteStateCommand::GlobalMuteStateCommand (Session& sess, const XMLNode& node)
315 : Session::GlobalRouteStateCommand (sess, node)
320 Session::GlobalMuteStateCommand::mark()
322 after = sess.get_global_route_boolean(&Route::muted);
326 Session::GlobalMuteStateCommand::operator()()
328 sess.set_global_mute(after, src);
332 Session::GlobalMuteStateCommand::undo()
334 sess.set_global_mute(before, src);
338 Session::GlobalMuteStateCommand::get_state()
340 XMLNode& node = GlobalRouteStateCommand::get_state();
341 node.add_property ("type", "mute");
346 Session::GlobalRecordEnableStateCommand::GlobalRecordEnableStateCommand(Session &sess, void *src)
347 : GlobalRouteStateCommand (sess, src)
349 after = before = sess.get_global_route_boolean(&Route::record_enabled);
352 Session::GlobalRecordEnableStateCommand::GlobalRecordEnableStateCommand (Session& sess, const XMLNode& node)
353 : Session::GlobalRouteStateCommand (sess, node)
358 Session::GlobalRecordEnableStateCommand::mark()
360 after = sess.get_global_route_boolean(&Route::record_enabled);
364 Session::GlobalRecordEnableStateCommand::operator()()
366 sess.set_global_record_enable(after, src);
370 Session::GlobalRecordEnableStateCommand::undo()
372 sess.set_global_record_enable(before, src);
376 Session::GlobalRecordEnableStateCommand::get_state()
378 XMLNode& node = GlobalRouteStateCommand::get_state();
379 node.add_property ("type", "rec-enable");
384 Session::GlobalMeteringStateCommand::GlobalMeteringStateCommand(Session &s, void *p)
387 after = before = sess.get_global_route_metering();
390 Session::GlobalMeteringStateCommand::GlobalMeteringStateCommand (Session& s, const XMLNode& node)
391 : sess (s), src (this)
393 if (set_state (node, Stateful::loading_state_version)) {
394 throw failed_constructor();
399 Session::GlobalMeteringStateCommand::mark()
401 after = sess.get_global_route_metering();
405 Session::GlobalMeteringStateCommand::operator()()
407 sess.set_global_route_metering(after, src);
411 Session::GlobalMeteringStateCommand::undo()
413 sess.set_global_route_metering(before, src);
417 Session::GlobalMeteringStateCommand::get_state()
419 XMLNode* node = new XMLNode (X_("GlobalRouteStateCommand"));
420 XMLNode* nbefore = new XMLNode (X_("before"));
421 XMLNode* nafter = new XMLNode (X_("after"));
423 for (Session::GlobalRouteMeterState::iterator x = before.begin(); x != before.end(); ++x) {
424 XMLNode* child = new XMLNode ("s");
425 boost::shared_ptr<Route> r = x->first.lock();
428 child->add_property (X_("id"), r->id().to_s());
430 const char* meterstr = 0;
434 meterstr = X_("input");
437 meterstr = X_("pre");
440 meterstr = X_("post");
443 fatal << string_compose (_("programming error: %1") , "no meter state in Session::GlobalMeteringStateCommand::get_state") << endmsg;
446 child->add_property (X_("meter"), meterstr);
447 nbefore->add_child_nocopy (*child);
451 for (Session::GlobalRouteMeterState::iterator x = after.begin(); x != after.end(); ++x) {
452 XMLNode* child = new XMLNode ("s");
453 boost::shared_ptr<Route> r = x->first.lock();
456 child->add_property (X_("id"), r->id().to_s());
458 const char* meterstr;
462 meterstr = X_("input");
465 meterstr = X_("pre");
468 meterstr = X_("post");
470 default: meterstr = "";
473 child->add_property (X_("meter"), meterstr);
474 nafter->add_child_nocopy (*child);
478 node->add_child_nocopy (*nbefore);
479 node->add_child_nocopy (*nafter);
481 node->add_property ("type", "metering");
487 Session::GlobalMeteringStateCommand::set_state (const XMLNode& node, int /*version*/)
489 GlobalRouteBooleanState states;
491 const XMLProperty* prop;
493 XMLNodeConstIterator niter;
499 for (loop = 0; loop < 2; ++loop) {
509 if ((child = node.child (str)) == 0) {
510 warning << string_compose (_("global route meter state command has no \"%1\" node, ignoring entire command"), str) << endmsg;
514 nlist = child->children();
516 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
519 boost::shared_ptr<Route> route;
522 prop = (*niter)->property ("id");
525 if ((route = sess.route_by_id (id)) == 0) {
526 warning << string_compose (_("cannot find track/bus \"%1\" while rebuilding a global route state command, ignored"), id.to_s()) << endmsg;
530 rms.first = boost::weak_ptr<Route> (route);
532 prop = (*niter)->property ("meter");
534 if (prop->value() == X_("pre")) {
535 rms.second = MeterPreFader;
536 } else if (prop->value() == X_("post")) {
537 rms.second = MeterPostFader;
539 rms.second = MeterInput;
543 after.push_back (rms);
545 before.push_back (rms);