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