File: zytrax.cpp

package info (click to toggle)
zytrax 0%2Bgit20201215-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,488 kB
  • sloc: cpp: 41,800; ansic: 3,387; makefile: 8; sh: 3
file content (269 lines) | stat: -rw-r--r-- 7,749 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <gtkmm.h>

#ifdef VST2_ENABLED
#include "drivers/vst2/factory_wrapper_vst2.h"
#endif

#ifdef LV2_ENABLED
#include "drivers/lv2/audio_effect_provider_lv2.h"
#include "drivers/lv2/effect_editor_lv2.h"
#endif

#include "effects/effects.h"
#include "engine/song.h"
#include "globals/json_file.h"
#include "gui/interface.h"

#ifdef RTAUDIO_ENABLED
#include "drivers/rtaudio/sound_driver_rtaudio.h"
#endif

#ifdef RTMIDI_ENABLED
#include "drivers/rtmidi/midi_driver_rtmidi.h"
#endif
int main(int argc, char *argv[]) {

	AudioEffectFactory effect_factory;

	printf("one\n");
#ifdef VST2_ENABLED
	AudioEffectProvider *provider_vst2 = create_vst2_provider();
	effect_factory.add_provider(provider_vst2);
#endif

	printf("two\n");
#ifdef LV2_ENABLED
	AudioEffectProviderLV2 provider_lv2(&argc, &argv);
	effect_factory.add_provider(&provider_lv2);
	EffectEditorLV2::initialize_lv2_editor();
#endif
	printf("three\n");
#ifdef RTAUDIO_ENABLED
	register_rtaudio_driver();
#endif

#ifdef RTMIDI_ENABLED
	register_rtmidi_driver();
#endif
	auto app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");

	Theme theme;
	KeyBindings key_bindings;

	/* Time to load the Settings */
	{
		String path = SettingsDialog::get_settings_path() + "/settings.json";
		JSON::Node node;
		int use_driver_index = SoundDriverManager::get_driver_count() ? 0 : -1;
		int use_midi_in_driver_index = MIDIDriverManager::get_input_driver_count() ? 0 : -1;

		if (load_json(path, node) == OK) {

			if (node.has("audio")) { //audio
				JSON::Node audio_node = node.get("audio");
				std::string driver_id = audio_node.get("id").toString();

				for (int i = 0; i < SoundDriverManager::get_driver_count(); i++) {
					SoundDriver *driver = SoundDriverManager::get_driver(i);
					if (driver->get_id() == driver_id.c_str()) {
						use_driver_index = i;
					}

					break;
				}

				int mixing_hz = audio_node.get("mixing_hz");
				int buffer_size = audio_node.get("buffer_size");
				int block_size = audio_node.get("block_size");

				if (mixing_hz >= 0 && mixing_hz < SoundDriverManager::MIX_FREQ_MAX) {
					SoundDriverManager::set_mix_frequency(SoundDriverManager::MixFrequency(mixing_hz));
				}
				if (buffer_size >= 0 && buffer_size < SoundDriverManager::BUFFER_SIZE_MAX) {
					SoundDriverManager::set_buffer_size(SoundDriverManager::BufferSize(buffer_size));
				}

				if (block_size >= 0 && block_size < SoundDriverManager::BUFFER_SIZE_MAX) {
					SoundDriverManager::set_step_buffer_size(SoundDriverManager::BufferSize(block_size));
				}

				std::string midi_driver_id = audio_node.get("midi_in_id").toString();

				for (int i = 0; i < MIDIDriverManager::get_input_driver_count(); i++) {
					MIDIInputDriver *driver = MIDIDriverManager::get_input_driver(i);
					if (driver->get_id() == midi_driver_id.c_str()) {
						use_midi_in_driver_index = i;
					}

					break;
				}
			}

			if (node.has("plugins")) { //plugins
				JSON::Node plugin_node = node.get("plugins");
				for (int i = 0; i < AudioEffectProvider::MAX_SCAN_PATHS; i++) {
					std::string key = String::num(i).ascii().get_data();
					if (plugin_node.has(key)) {
						std::string path = plugin_node.get(key).toString();
						String pathu;
						pathu.parse_utf8(path.c_str());
						AudioEffectProvider::set_scan_path(i, pathu);
					}
				}
			}

			if (node.has("theme")) { //theme
				JSON::Node theme_node = node.get("theme");
				if (theme_node.has("font")) {

					theme.font.parse_utf8(theme_node.get("font").toString().c_str());
				}

				if (theme_node.has("colors")) {

					JSON::Node colors_node = theme_node.get("colors");
					for (int i = 0; i < Theme::COLOR_MAX; i++) {
						if (colors_node.has(theme.color_names[i])) {

							JSON::Node array = colors_node.get(theme.color_names[i]);
							theme.colors[i].set_red(array.get(0).toFloat());
							theme.colors[i].set_green(array.get(1).toFloat());
							theme.colors[i].set_blue(array.get(2).toFloat());
						}
					}

					if (theme_node.has("use_dark_theme") && bool(theme_node.get("use_dark_theme").toBool())) {
						theme.color_scheme = Theme::COLOR_SCHEME_DARK;
					}
				}
			}

			if (node.has("key_bindings")) { //key bindings

				JSON::Node bindings = node.get("key_bindings");
				if (bindings.has("keys")) {

					JSON::Node array = bindings.get("keys");

					for (int i = 0; i < array.getCount(); i++) {

						JSON::Node bind = array.get(i);
						std::string name = bind.get("name").toString();

						KeyBindings::KeyBind name_index = KeyBindings::BIND_MAX;
						for (int j = 0; j < KeyBindings::BIND_MAX; j++) {
							KeyBindings::KeyBind b = KeyBindings::KeyBind(j);
							if (name == key_bindings.get_keybind_name(b)) {
								name_index = b;

								break;
							}
						}

						if (name_index != KeyBindings::BIND_MAX) {

							int key = bind.get("key").toInt();
							int state = bind.get("mods").toInt();

							key_bindings.set_keybind(name_index, key, state);
						}
					}
				}
			}

			if (node.has("default_commands")) { //default commands

				JSON::Node def_commands = node.get("default_commands");

				for (int i = 0; i < def_commands.getCount(); i++) {

					JSON::Node command = def_commands.get(i);

					int index = command.get("index").toInt();
					String name;
					name.parse_utf8(command.get("identifier").toString().c_str());
					char c = char(command.get("command").toInt());

					SettingsDialog::set_default_command(index, name, c);
				}
			}
		}
		SoundDriverManager::init_driver(use_driver_index);
		MIDIDriverManager::init_input_driver(use_midi_in_driver_index);
	}

	printf("regfx\n");
	register_effects(&effect_factory);

	/* make it dark */
	if (theme.color_scheme == Theme::COLOR_SCHEME_DARK) {
		g_object_set(gtk_settings_get_default(),
				"gtk-application-prefer-dark-theme", TRUE,
				NULL);
	}

	/* Load the cached plugins */

	{ //plugins

		String path = SettingsDialog::get_settings_path() + "/plugins.json";
		JSON::Node node;

		if (load_json(path, node) == OK) {

			JSON::Node plugin_array = node.get("plugins");

			for (int i = 0; i < plugin_array.getCount(); i++) {

				JSON::Node plugin_node = plugin_array.get(i);
				AudioEffectInfo info;

				info.caption.parse_utf8(plugin_node.get("caption").toString().c_str());
				info.description.parse_utf8(plugin_node.get("description").toString().c_str());
				info.author.parse_utf8(plugin_node.get("author").toString().c_str());
				info.category.parse_utf8(plugin_node.get("category").toString().c_str());
				info.unique_ID.parse_utf8(plugin_node.get("unique_id").toString().c_str());
				info.icon_string.parse_utf8(plugin_node.get("icon_string").toString().c_str());
				info.version.parse_utf8(plugin_node.get("version").toString().c_str());
				info.provider_caption.parse_utf8(plugin_node.get("provider_caption").toString().c_str());
				info.provider_id.parse_utf8(plugin_node.get("provider_id").toString().c_str());
				info.path.parse_utf8(plugin_node.get("path").toString().c_str());

				info.synth = plugin_node.get("synth").toBool();
				info.has_ui = plugin_node.get("has_ui").toBool();

				effect_factory.add_audio_effect(info);
			}
		}
	}

	/* Initialize the UI */

	printf("go\n");

	Interface window(app.operator->(), &effect_factory, &theme, &key_bindings);
	window.set_default_size(1280, 720);
#ifdef VST2_ENABLED
	window.add_editor_plugin_function(get_vst2_editor_function());
#endif
#ifdef LV2_ENABLED
	window.add_editor_plugin_function(get_lv2_editor_function());
#endif
	int ret = app->run(window);

	printf("bye\n");

	SoundDriverManager::finish_driver();

#ifdef VST2_ENABLED
	delete provider_vst2;
#endif

#ifdef RTAUDIO_ENABLED
	cleanup_rtaudio_driver();
#endif
#ifdef LV2_ENABLED
	EffectEditorLV2::finalize_lv2_editor();
#endif
	return ret;
}