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