Optimize automation-event process splitting
[ardour.git] / libs / ardour / session_command.cc
1 /*
2     Copyright (C) 2000-2007 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <string>
21
22 #include "ardour/automation_list.h"
23 #include "ardour/location.h"
24 #include "ardour/midi_automation_list_binder.h"
25 #include "ardour/playlist.h"
26 #include "ardour/region.h"
27 #include "ardour/region_factory.h"
28 #include "ardour/route.h"
29 #include "ardour/session.h"
30 #include "ardour/session_playlists.h"
31 #include "ardour/source.h"
32 #include "ardour/tempo.h"
33 #include "evoral/Curve.hpp"
34 #include "pbd/error.h"
35 #include "pbd/failed_constructor.h"
36 #include "pbd/id.h"
37 #include "pbd/memento_command.h"
38 #include "pbd/stateful_diff_command.h"
39 #include "pbd/statefuldestructible.h"
40 #include "pbd/types_convert.h"
41
42 class Command;
43
44 using namespace PBD;
45 using namespace ARDOUR;
46
47 #include "pbd/i18n.h"
48
49 void Session::register_with_memento_command_factory(PBD::ID id, PBD::StatefulDestructible *ptr)
50 {
51     registry[id] = ptr;
52 }
53
54 Command *
55 Session::memento_command_factory(XMLNode *n)
56 {
57     PBD::ID id;
58     XMLNode *before = 0, *after = 0;
59     XMLNode *child = 0;
60
61     /* XXX: HACK! */
62     bool have_id = n->get_property ("obj-id", id);
63
64     /* get before/after */
65
66     if (n->name() == "MementoCommand") {
67             before = new XMLNode(*n->children().front());
68             after = new XMLNode(*n->children().back());
69             child = before;
70     } else if (n->name() == "MementoUndoCommand") {
71             before = new XMLNode(*n->children().front());
72             child = before;
73     } else if (n->name() == "MementoRedoCommand") {
74             after = new XMLNode(*n->children().front());
75             child = after;
76     } else if (n->name() == "PlaylistCommand") {
77             before = new XMLNode(*n->children().front());
78             after = new XMLNode(*n->children().back());
79             child = before;
80     }
81
82     if (!child) {
83             info << string_compose (_("Tried to reconstitute a MementoCommand with no contents, failing. id=%1"), id.to_s()) << endmsg;
84             return 0;
85     }
86
87     /* create command */
88     std::string type_name;
89     n->get_property ("type-name", type_name);
90
91     if (type_name == "ARDOUR::AudioRegion" || type_name == "ARDOUR::MidiRegion" || type_name == "ARDOUR::Region") {
92             boost::shared_ptr<Region> r = RegionFactory::region_by_id (id);
93             if (r) {
94                     return new MementoCommand<Region>(*r, before, after);
95             }
96
97     } else if (type_name == "ARDOUR::AudioSource" || type_name == "ARDOUR::MidiSource") {
98             if (sources.count(id))
99                     return new MementoCommand<Source>(*sources[id], before, after);
100
101     } else if (type_name == "ARDOUR::Location") {
102             Location* loc = _locations->get_location_by_id(id);
103             if (loc) {
104                     return new MementoCommand<Location>(*loc, before, after);
105             }
106
107     } else if (type_name == "ARDOUR::Locations") {
108             return new MementoCommand<Locations>(*_locations, before, after);
109
110     } else if (type_name == "ARDOUR::TempoMap") {
111             return new MementoCommand<TempoMap>(*_tempo_map, before, after);
112
113     } else if (type_name == "ARDOUR::Playlist" || type_name == "ARDOUR::AudioPlaylist" || type_name == "ARDOUR::MidiPlaylist") {
114             if (boost::shared_ptr<Playlist> pl = playlists->by_name(child->property("name")->value())) {
115                     return new MementoCommand<Playlist>(*(pl.get()), before, after);
116             }
117
118     } else if (type_name == "ARDOUR::Route" || type_name == "ARDOUR::AudioTrack" || type_name == "ARDOUR::MidiTrack") {
119                 if (boost::shared_ptr<Route> r = route_by_id(id)) {
120                         return new MementoCommand<Route>(*r, before, after);
121                 } else {
122                         error << string_compose (X_("Route %1 not found in session"), id) << endmsg;
123                 }
124
125     } else if (type_name == "Evoral::Curve" || type_name == "ARDOUR::AutomationList") {
126             if (have_id) {
127                     std::map<PBD::ID, AutomationList*>::iterator i = automation_lists.find(id);
128                     if (i != automation_lists.end()) {
129                             return new MementoCommand<AutomationList>(*i->second, before, after);
130                     }
131             } else {
132                     return new MementoCommand<AutomationList> (
133                             new MidiAutomationListBinder (n, sources),
134                             before, after
135                             );
136             }
137
138             std::cerr << "Alist " << id << " not found\n";
139
140     } else if (registry.count(id)) { // For Editor and AutomationLine which are off-limits herea
141             return new MementoCommand<PBD::StatefulDestructible>(*registry[id], before, after);
142     }
143
144     /* we failed */
145     info << string_compose (_("Could not reconstitute MementoCommand from XMLNode. object type = %1 id = %2"), type_name, id.to_s()) << endmsg;
146
147     return 0 ;
148 }
149
150 Command *
151 Session::stateful_diff_command_factory (XMLNode* n)
152 {
153         PBD::ID id;
154         std::string type_name;
155         if (!n->get_property ("obj-id", id) || !n->get_property ("type-name", type_name)) {
156                 error << _("Could get object ID and type name for StatefulDiffCommand from XMLNode.")
157                       << endmsg;
158                       return 0;
159         }
160
161         if ((type_name == "ARDOUR::AudioRegion" || type_name == "ARDOUR::MidiRegion")) {
162                 boost::shared_ptr<Region> r = RegionFactory::region_by_id (id);
163                 if (r) {
164                         return new StatefulDiffCommand (r, *n);
165                 }
166
167         } else if (type_name == "ARDOUR::AudioPlaylist" ||  type_name == "ARDOUR::MidiPlaylist") {
168                 boost::shared_ptr<Playlist> p = playlists->by_id (id);
169                 if (p) {
170                         return new StatefulDiffCommand (p, *n);
171                 } else {
172                         std::cerr << "Playlist with ID = " << id << " not found\n";
173                 }
174         }
175
176         /* we failed */
177
178         info << string_compose (
179                 _("Could not reconstitute StatefulDiffCommand from XMLNode. object type = %1 id = %2"), type_name, id.to_s())
180               << endmsg;
181
182         return 0;
183 }