use libsndfile for all audio file i/o, and rename DiskStream AudioDiskStream
[ardour.git] / libs / ardour / configuration.cc
1 /*
2     Copyright (C) 1999-2006 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 <unistd.h>
22 #include <cstdio> /* for snprintf, grrr */
23
24 #include <pbd/failed_constructor.h>
25 #include <pbd/xml++.h>
26
27 #include <ardour/ardour.h>
28 #include <ardour/configuration.h>
29 #include <ardour/audio_diskstream.h>
30 #include <ardour/destructive_filesource.h>
31 #include <ardour/control_protocol_manager.h>
32
33 #include "i18n.h"
34
35 using namespace ARDOUR;
36 using namespace std;
37
38 /* this is global so that we do not have to indirect through an object pointer
39    to reference it.
40 */
41
42 namespace ARDOUR {
43     float speed_quietning = 0.251189; // -12dB reduction for ffwd or rewind
44 }
45
46 Configuration::Configuration ()
47         :
48 /* construct variables */
49 #undef  CONFIG_VARIABLE
50 #undef  CONFIG_VARIABLE_SPECIAL 
51 #define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
52 #define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
53 #include "ardour/configuration_vars.h"
54 #undef  CONFIG_VARIABLE
55 #undef  CONFIG_VARIABLE_SPECIAL 
56
57         user_configuration (false)
58 {
59         _control_protocol_state = 0;
60 }
61
62 Configuration::~Configuration ()
63 {
64 }
65
66 int
67 Configuration::load_state ()
68 {
69         string rcfile;
70         
71         /* load system configuration first */
72
73         rcfile = find_config_file ("ardour_system.rc");
74
75         if (rcfile.length()) {
76
77                 XMLTree tree;
78
79                 cerr << string_compose (_("loading system configuration file %1"), rcfile) << endl;
80                 
81                 if (!tree.read (rcfile.c_str())) {
82                         error << string_compose(_("Ardour: cannot read system configuration file \"%1\""), rcfile) << endmsg;
83                         return -1;
84                 }
85
86                 if (set_state (*tree.root())) {
87                         error << string_compose(_("Ardour: system configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
88                         return -1;
89                 }
90         }
91
92         /* from this point on, all configuration changes are user driven */
93
94         user_configuration = true;
95
96         /* now load configuration file for user */
97         
98         rcfile = find_config_file ("ardour.rc");
99
100         if (rcfile.length()) {
101
102                 XMLTree tree;
103                 
104                 cerr << string_compose (_("loading user configuration file %1"), rcfile) << endl;
105
106                 if (!tree.read (rcfile)) {
107                         error << string_compose(_("Ardour: cannot read configuration file \"%1\""), rcfile) << endmsg;
108                         return -1;
109                 }
110
111                 if (set_state (*tree.root())) {
112                         error << string_compose(_("Ardour: user configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
113                         return -1;
114                 }
115         }
116
117         return 0;
118 }
119
120 int
121 Configuration::save_state()
122 {
123         XMLTree tree;
124         string rcfile;
125
126         /* Note: this only writes the per-user file, and therefore
127            only saves variables marked as user-set or modified
128         */
129
130         rcfile = get_user_ardour_path ();
131         rcfile += "ardour.rc";
132
133         if (rcfile.length()) {
134                 tree.set_root (&state (true));
135                 if (!tree.write (rcfile.c_str())){
136                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
137                         return -1;
138                 }
139         }
140
141         return 0;
142 }
143
144 XMLNode&
145 Configuration::get_state ()
146 {
147         return state (false);
148 }
149
150 XMLNode&
151 Configuration::state (bool user_only)
152 {
153         XMLNode* root = new XMLNode("Ardour");
154         LocaleGuard lg (X_("POSIX"));
155
156         typedef map<string, MidiPortDescriptor*>::const_iterator CI;
157         for(CI m = midi_ports.begin(); m != midi_ports.end(); ++m){
158                 root->add_child_nocopy(m->second->get_state());
159         }
160
161         XMLNode* node = new XMLNode("Config");
162         
163 #undef  CONFIG_VARIABLE
164 #undef  CONFIG_VARIABLE_SPECIAL 
165 #define CONFIG_VARIABLE(type,var,name,value) \
166          if (!user_only || var.is_user()) var.add_to_node (*node);
167 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
168          if (!user_only || var.is_user()) var.add_to_node (*node);
169 #include "ardour/configuration_vars.h"
170 #undef  CONFIG_VARIABLE
171 #undef  CONFIG_VARIABLE_SPECIAL 
172
173         root->add_child_nocopy (*node);
174
175         if (_extra_xml) {
176                 root->add_child_copy (*_extra_xml);
177         }
178
179         root->add_child_nocopy (ControlProtocolManager::instance().get_state());
180
181         return *root;
182 }
183
184 int
185 Configuration::set_state (const XMLNode& root)
186 {
187         if (root.name() != "Ardour") {
188                 return -1;
189         }
190
191         XMLNodeList nlist = root.children();
192         XMLNodeConstIterator niter;
193         XMLNode *node;
194
195         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
196
197                 node = *niter;
198
199                 if (node->name() == "MIDI-port") {
200
201                         try {
202                                 pair<string,MidiPortDescriptor*> newpair;
203                                 newpair.second = new MidiPortDescriptor (*node);
204                                 newpair.first = newpair.second->tag;
205                                 midi_ports.insert (newpair);
206                         }
207
208                         catch (failed_constructor& err) {
209                                 warning << _("ill-formed MIDI port specification in ardour rcfile (ignored)") << endmsg;
210                         }
211
212                 } else if (node->name() == "Config") {
213
214 #undef  CONFIG_VARIABLE
215 #undef  CONFIG_VARIABLE_SPECIAL 
216 #define CONFIG_VARIABLE(type,var,name,value) \
217          var.set_from_node (*node); \
218          var.set_is_user (user_configuration);
219 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
220          var.set_from_node (*node); \
221          var.set_is_user (user_configuration);
222 #include "ardour/configuration_vars.h"
223 #undef  CONFIG_VARIABLE
224 #undef  CONFIG_VARIABLE_SPECIAL 
225                         
226                 } else if (node->name() == "extra") {
227                         _extra_xml = new XMLNode (*node);
228
229                 } else if (node->name() == ControlProtocolManager::state_node_name) {
230                         _control_protocol_state = new XMLNode (*node);
231                 }
232         }
233
234         AudioDiskstream::set_disk_io_chunk_frames (minimum_disk_io_bytes.get() / sizeof (Sample));
235
236         return 0;
237 }
238
239 Configuration::MidiPortDescriptor::MidiPortDescriptor (const XMLNode& node)
240 {
241         const XMLProperty *prop;
242         bool have_tag = false;
243         bool have_device = false;
244         bool have_type = false;
245         bool have_mode = false;
246
247         if ((prop = node.property ("tag")) != 0) {
248                 tag = prop->value();
249                 have_tag = true;
250         }
251
252         if ((prop = node.property ("device")) != 0) {
253                 device = prop->value();
254                 have_device = true;
255         }
256
257         if ((prop = node.property ("type")) != 0) {
258                 type = prop->value();
259                 have_type = true;
260         }
261
262         if ((prop = node.property ("mode")) != 0) {
263                 mode = prop->value();
264                 have_mode = true;
265         }
266
267         if (!have_tag || !have_device || !have_type || !have_mode) {
268                 throw failed_constructor();
269         }
270 }
271
272 XMLNode&
273 Configuration::MidiPortDescriptor::get_state()
274 {
275         XMLNode* root = new XMLNode("MIDI-port");
276
277         root->add_property("tag", tag);
278         root->add_property("device", device);
279         root->add_property("type", type);
280         root->add_property("mode", mode);
281
282         return *root;
283 }
284