HAAS Stereo Enhancer
[calf.git] / src / calf / preset.h
blobc2c7b2c765118b2d5a7495e63519374cd1fa5f3c
1 /* Calf DSP Library
2 * Preset management
4 * Copyright (C) 2007 Krzysztof Foltman
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02111-1307, USA.
21 #ifndef __CALF_PRESET_H
22 #define __CALF_PRESET_H
24 #include <vector>
25 #include <string.h>
26 #include "utils.h"
28 namespace calf_plugins {
30 class plugin_ctl_iface;
32 /// Contents of single preset
33 struct plugin_preset
35 /// Bank the preset belongs to (not used yet)
36 int bank;
37 /// Program number of the preset (not used yet)
38 int program;
39 /// Name of the preset
40 std::string name;
41 /// Name of the plugin the preset is for
42 std::string plugin;
43 /// Names of parameters in values array (for each item in param_names there should be a counterpart in values)
44 std::vector<std::string> param_names;
45 /// Values of parameters
46 std::vector<float> values;
47 /// DSSI configure-style variables
48 std::map<std::string, std::string> variables;
50 plugin_preset() : bank(0), program(0) {}
51 /// Export preset as XML
52 std::string to_xml();
53 /// "Upload" preset content to the plugin
54 void activate(plugin_ctl_iface *plugin);
55 /// "Download" preset content from the plugin
56 void get_from(plugin_ctl_iface *plugin);
58 std::string get_safe_name();
61 /// Exception thrown by preset system
62 struct preset_exception
64 std::string message, param, fulltext;
65 int error;
66 preset_exception(const std::string &_message, const std::string &_param, int _error)
67 : message(_message), param(_param), error(_error)
70 const char *what() {
71 if (error)
72 fulltext = message + " " + param + " (" + strerror(error) + ")";
73 else
74 fulltext = message + " " + param;
75 return fulltext.c_str();
77 ~preset_exception()
82 /// A vector of presets
83 typedef std::vector<plugin_preset> preset_vector;
85 /// A single list of presets (usually there are two - @see get_builtin_presets(), get_user_presets() )
86 struct preset_list
88 /// Plugin list item
89 struct plugin_snapshot
91 /// Preset offset
92 int preset_offset;
93 /// Plugin type
94 std::string type;
95 /// Instance name
96 std::string instance_name;
97 /// Index of the first input port
98 int input_index;
99 /// Index of the first output port
100 int output_index;
101 /// Index of the first MIDI port
102 int midi_index;
103 /// Automation assignments for this plugin
104 std::vector<std::pair<std::string, std::string> > automation_entries;
106 /// Reset to initial values
107 void reset();
110 /// Parser states
111 enum parser_state
113 START, ///< Beginning of parsing process (before root element)
114 LIST, ///< Inside root element
115 PRESET, ///< Inside preset definition
116 VALUE, ///< Inside (empty) param tag
117 VAR, ///< Inside (non-empty) var tag
118 PLUGIN, ///< Inside plugin element (calfjackhost snapshots only)
119 RACK, ///< Inside rack element (calfjackhost snapshots only)
120 AUTOMATION_ENTRY, ///< inside automation element (calfjackhost snapshots only, always an empty element)
121 } state;
123 /// Contained presets (usually for all plugins)
124 preset_vector presets;
125 /// Temporary preset used during parsing process
126 plugin_preset parser_preset;
127 /// Temporary plugin desc used during parsing process
128 plugin_snapshot parser_plugin;
129 /// Preset number counters for DSSI (currently broken)
130 std::map<std::string, int> last_preset_ids;
131 /// The key used in current <var name="key"> tag (for state == VAR)
132 std::string current_key;
133 /// The file is loaded in rack mode (and rack/plugin elements are expected)
134 bool rack_mode;
135 /// List of plugin states for rack mode
136 std::vector<plugin_snapshot> plugins;
138 /// Return the name of the built-in or user-defined preset file
139 static std::string get_preset_filename(bool builtin);
140 /// Load default preset list (built-in or user-defined)
141 bool load_defaults(bool builtin);
142 /// Load preset list from an in-memory XML string
143 void parse(const std::string &data, bool in_rack_mode);
144 /// Load preset list from XML file
145 void load(const char *filename, bool in_rack_mode);
146 /// Save preset list as XML file
147 void save(const char *filename);
148 /// Append or replace a preset (replaces a preset with the same plugin and preset name)
149 void add(const plugin_preset &sp);
150 /// Get a sublist of presets for a given plugin (those with plugin_preset::plugin == plugin)
151 void get_for_plugin(preset_vector &vec, const char *plugin);
153 protected:
154 /// Internal function: start element handler for expat
155 static void xml_start_element_handler(void *user_data, const char *name, const char *attrs[]);
156 /// Internal function: end element handler for expat
157 static void xml_end_element_handler(void *user_data, const char *name);
158 /// Internal function: character data (tag text content) handler for expat
159 static void xml_character_data_handler(void *user_data, const char *data, int len);
162 /// Return the current list of built-in (factory) presets (these are loaded from system-wide file)
163 extern preset_list &get_builtin_presets();
165 /// Return the current list of user-defined presets (these are loaded from ~/.calfpresets)
166 extern preset_list &get_user_presets();
170 #endif