9800c5b1a2a2b1518c6ae507a26367e641169523
[ardour.git] / libs / ardour / redirect.cc
1 /*
2     Copyright (C) 2001 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     $Id$
19 */
20
21 #include <fstream>
22 #include <algorithm>
23 #include <string>
24 #include <cerrno>
25 #include <unistd.h>
26 #include <sstream>
27
28 #include <sigc++/bind.h>
29
30 #include <pbd/xml++.h>
31
32 #include <ardour/redirect.h>
33 #include <ardour/session.h>
34 #include <ardour/utils.h>
35 #include <ardour/send.h>
36 #include <ardour/insert.h>
37
38 #include "i18n.h"
39
40 using namespace std;
41 using namespace ARDOUR;
42 using namespace PBD;
43
44 const string Redirect::state_node_name = "Redirect";
45 sigc::signal<void,Redirect*> Redirect::RedirectCreated;
46
47 Redirect::Redirect (Session& s, const string& name, Placement p,
48
49                     int input_min, int input_max, int output_min, int output_max)
50         : IO (s, name, input_min, input_max, output_min, output_max)
51 {
52         _placement = p;
53         _active = false;
54         _sort_key = 0;
55         _gui = 0;
56         _extra_xml = 0;
57 }
58
59 Redirect::~Redirect ()
60 {
61 }
62
63 Redirect*
64 Redirect::clone (const Redirect& other)
65 {
66         const Send *send;
67         const PortInsert *port_insert;
68         const PluginInsert *plugin_insert;
69
70         if ((send = dynamic_cast<const Send*>(&other)) != 0) {
71                 return new Send (*send);
72         } else if ((port_insert = dynamic_cast<const PortInsert*>(&other)) != 0) {
73                 return new PortInsert (*port_insert);
74         } else if ((plugin_insert = dynamic_cast<const PluginInsert*>(&other)) != 0) {
75                 return new PluginInsert (*plugin_insert);
76         } else {
77                 fatal << _("programming error: unknown Redirect type in Redirect::Clone!\n")
78                       << endmsg;
79                 /*NOTREACHED*/
80         }
81         return 0;
82 }
83
84 void
85 Redirect::set_sort_key (uint32_t key)
86 {
87         _sort_key = key;
88 }
89         
90 void
91 Redirect::set_placement (Placement p, void *src)
92 {
93         if (_placement != p) {
94                 _placement = p;
95                  placement_changed (this, src); /* EMIT SIGNAL */
96         }
97 }
98
99 void
100 Redirect::set_placement (const string& str, void *src)
101 {
102         if (str == _("pre")) {
103                 set_placement (PreFader, this);
104         } else if (str == _("post")) {
105                 set_placement (PostFader, this);
106         } else {
107                 error << string_compose(_("Redirect: unknown placement string \"%1\" (ignored)"), str) << endmsg;
108         }
109 }
110
111 int
112 Redirect::load_automation (string path)
113 {
114         string fullpath;
115
116         if (path[0] == '/') { // legacy
117                 fullpath = path;
118         } else {
119                 fullpath = _session.automation_dir();
120                 fullpath += path;
121         }
122         ifstream in (fullpath.c_str());
123
124         if (!in) {
125                 warning << string_compose(_("%1: cannot open %2 to load automation data (%3)"), _name, fullpath, strerror (errno)) << endmsg;
126                 return 1;
127         }
128
129         Glib::Mutex::Lock lm (_automation_lock);
130         set<uint32_t> tosave;
131         parameter_automation.clear ();
132
133         while (in) {
134                 double when;
135                 double value;
136                 uint32_t port;
137
138                 in >> port;     if (!in) break;
139                 in >> when;  if (!in) goto bad;
140                 in >> value; if (!in) goto bad;
141                 
142                 AutomationList& al = automation_list (port);
143                 al.add (when, value);
144                 tosave.insert (port);
145         }
146         
147         for (set<uint32_t>::iterator i = tosave.begin(); i != tosave.end(); ++i) {
148                 automation_list (*i).save_state (_("loaded from disk"));
149         }
150         
151         return 0;
152
153   bad:
154         error << string_compose(_("%1: cannot load automation data from %2"), _name, fullpath) << endmsg;
155         parameter_automation.clear ();
156         return -1;
157 }
158
159 int
160 Redirect::save_automation (string path)
161 {
162         Glib::Mutex::Lock lm (_automation_lock);
163         string fullpath;
164
165         if (parameter_automation.empty()) {
166                 return 1;
167         }
168
169         fullpath = _session.automation_dir();
170         fullpath += path;
171
172         ofstream out (fullpath.c_str());
173
174         if (!out) {
175                 error << string_compose(_("%1: cannot open %2 to store automation data (%3)"), _name, fullpath, strerror (errno)) << endmsg;
176                 return -1;
177         }
178
179         AutomationList::const_iterator i;
180         map<uint32_t,AutomationList*>::iterator li;
181         
182         for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
183                 for (i = (*li).second->begin(); i != (*li).second->end(); ++i) {
184                         
185                         out << (*li).first << ' ' << (*i)->when << ' ' << (*i)->value << endl;
186                         
187                         if (!out) {
188                                 break;
189                         }
190                 }
191                 
192                 if (i != (*li).second->end()) {
193                         unlink (fullpath.c_str());
194                         error << string_compose(_("%1: could not save automation state to %2"), _name, fullpath) << endmsg;
195                         return -1;
196                 }
197         }
198
199         if (li != parameter_automation.end()) {
200                 unlink (fullpath.c_str());
201                 error << string_compose(_("%1: could not save automation state to %2"), _name, fullpath) << endmsg;
202                 return -1;
203         }
204
205         return 0;
206 }
207
208 XMLNode&
209 Redirect::get_state (void)
210 {
211         return state (true);
212 }
213
214 XMLNode&
215 Redirect::state (bool full_state)
216 {
217         char buf[64];
218         XMLNode* node = new XMLNode (state_node_name);
219         stringstream sstr;
220
221         node->add_property("active", active() ? "yes" : "no");  
222         node->add_property("placement", placement_as_string (placement()));
223         node->add_child_nocopy (IO::state (full_state));
224
225         if (_extra_xml){
226                 node->add_child_copy (*_extra_xml);
227         }
228         
229         if (full_state) {
230
231                 string path;
232                 string legal_name;
233                 
234                 path = _session.snap_name();
235                 path += "-redirect-";
236                 id().print (buf);
237                 path += buf;
238                 path += ".automation";
239                 
240                 /* XXX we didn't ask for a state save, we asked for the current state.
241                    FIX ME!
242                 */
243                 
244                 switch (save_automation (path)) {
245                 case -1:
246                         error << string_compose(_("Could not get state from Redirect (%1).  Problem with save_automation"), _name) << endmsg;
247                         break;
248                         
249                 case 0:
250                         XMLNode *aevents = node->add_child("Automation");
251                         
252                         for (set<uint32_t>::iterator x = visible_parameter_automation.begin(); x != visible_parameter_automation.end(); ++x) {
253                                 if (x != visible_parameter_automation.begin()) {
254                                         sstr << ' ';
255                                 }
256                                 sstr << *x;
257                         }
258                         
259                         aevents->add_property ("path", path);
260                         aevents->add_property ("visible", sstr.str());
261                         break;
262                 }
263         }
264
265         return *node;
266 }
267
268 void
269 Redirect::what_has_automation (set<uint32_t>& s) const
270 {
271         Glib::Mutex::Lock lm (_automation_lock);
272         map<uint32_t,AutomationList*>::const_iterator li;
273         
274         for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
275                 s.insert  ((*li).first);
276         }
277 }
278
279 void
280 Redirect::what_has_visible_automation (set<uint32_t>& s) const
281 {
282         Glib::Mutex::Lock lm (_automation_lock);
283         set<uint32_t>::const_iterator li;
284         
285         for (li = visible_parameter_automation.begin(); li != visible_parameter_automation.end(); ++li) {
286                 s.insert  (*li);
287         }
288 }
289
290 int
291 Redirect::set_state (const XMLNode& node)
292 {
293         const XMLProperty *prop;
294
295         if (node.name() != state_node_name) {
296                 error << string_compose(_("incorrect XML node \"%1\" passed to Redirect object"), node.name()) << endmsg;
297                 return -1;
298         }
299
300         XMLNodeList nlist = node.children();
301         XMLNodeIterator niter;
302         bool have_io = false;
303
304         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
305
306                 if ((*niter)->name() == IO::state_node_name) {
307
308                         IO::set_state (**niter);
309                         have_io = true;
310
311                 } else if ((*niter)->name() == "Automation") {
312
313                         XMLProperty *prop;
314                         
315                         if ((prop = (*niter)->property ("path")) != 0) {
316                                 load_automation (prop->value());
317                         } else {
318                                 warning << string_compose(_("%1: Automation node has no path property"), _name) << endmsg;
319                         }
320
321                         if ((prop = (*niter)->property ("visible")) != 0) {
322                                 uint32_t what;
323                                 stringstream sstr;
324
325                                 visible_parameter_automation.clear ();
326                                 
327                                 sstr << prop->value();
328                                 while (1) {
329                                         sstr >> what;
330                                         if (sstr.fail()) {
331                                                 break;
332                                         }
333                                         mark_automation_visible (what, true);
334                                 }
335                         }
336
337                 } else if ((*niter)->name() == "extra") {
338                         _extra_xml = new XMLNode (*(*niter));
339                 }
340         }
341
342         if (!have_io) {
343                 error << _("XML node describing an IO is missing an IO node") << endmsg;
344                 return -1;
345         }
346
347         if ((prop = node.property ("active")) == 0) {
348                 error << _("XML node describing a redirect is missing the `active' field") << endmsg;
349                 return -1;
350         }
351
352         if (_active != (prop->value() == "yes")) {
353                 _active = !_active;
354                 active_changed (this, this); /* EMIT_SIGNAL */
355         }
356         
357         if ((prop = node.property ("placement")) == 0) {
358                 error << _("XML node describing a redirect is missing the `placement' field") << endmsg;
359                 return -1;
360         }
361
362         set_placement (prop->value(), this);
363
364         return 0;
365 }
366
367 AutomationList&
368 Redirect::automation_list (uint32_t parameter)
369 {
370         AutomationList* al = parameter_automation[parameter];
371
372         if (al == 0) {
373                 al = parameter_automation[parameter] = new AutomationList (default_parameter_value (parameter));
374                 /* let derived classes do whatever they need with this */
375                 automation_list_creation_callback (parameter, *al);
376         }
377
378         return *al;
379 }
380
381 string
382 Redirect::describe_parameter (uint32_t which)
383 {
384         /* derived classes will override this */
385         return "";
386 }
387
388 void
389 Redirect::can_automate (uint32_t what)
390 {
391         can_automate_list.insert (what);
392 }
393
394 void
395 Redirect::mark_automation_visible (uint32_t what, bool yn)
396 {
397         if (yn) {
398                 visible_parameter_automation.insert (what);
399         } else {
400                 set<uint32_t>::iterator i;
401
402                 if ((i = visible_parameter_automation.find (what)) != visible_parameter_automation.end()) {
403                         visible_parameter_automation.erase (i);
404                 }
405         }
406 }
407
408 bool
409 Redirect::find_next_event (jack_nframes_t now, jack_nframes_t end, ControlEvent& next_event) const
410 {
411         map<uint32_t,AutomationList*>::const_iterator li;       
412         AutomationList::TimeComparator cmp;
413
414         next_event.when = max_frames;
415         
416         for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
417                 
418                 AutomationList::const_iterator i;
419                 const AutomationList& alist (*((*li).second));
420                 ControlEvent cp (now, 0.0f);
421                 
422                 for (i = lower_bound (alist.const_begin(), alist.const_end(), &cp, cmp); i != alist.const_end() && (*i)->when < end; ++i) {
423                         if ((*i)->when > now) {
424                                 break; 
425                         }
426                 }
427                 
428                 if (i != alist.const_end() && (*i)->when < end) {
429                         
430                         if ((*i)->when < next_event.when) {
431                                 next_event.when = (*i)->when;
432                         }
433                 }
434         }
435
436         return next_event.when != max_frames;
437 }
438
439 void
440 Redirect::store_state (RedirectState& state) const
441 {
442         state.active = _active;
443 }
444
445 Change
446 Redirect::restore_state (StateManager::State& state)
447 {
448         RedirectState* rstate = dynamic_cast<RedirectState*> (&state);
449         set_active (rstate->active, this);
450         return Change (0);
451 }
452
453 StateManager::State*
454 Redirect::state_factory (std::string why) const
455 {
456         RedirectState* state = new RedirectState (why);
457
458         store_state (*state);
459
460         return state;
461 }
462
463 void
464 Redirect::set_active (bool yn, void* src)
465 {
466         _active = yn; 
467         save_state (_("active_changed"));
468         active_changed (this, src); 
469         _session.set_dirty ();
470 }
471