Debian Codegraph @

src/state.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * Copyright (C) 2003 Steve Harris
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 * $Id: state.c,v 1.73 2008/02/04 14:23:34 esaracco Exp $
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <assert.h>
21 #include <libgen.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <gtk/gtk.h>
27 #include <libxml/parser.h>
28
29 #include "config.h"
30 #include "main.h"
31 #include "callbacks.h"
32 #include "geq.h"
33 #include "spectrum.h"
34 #include "intrim.h"
35 #include "state.h"
36 #include "io.h"
37 #include "process.h"
38 #include "scenes.h"
39 #include "hdeq.h"
40 #include "compressor-ui.h"
41 #include "help.h"
42 #include "preferences.h"
43
44 /* A scene value to indicate that loading failed */
45 #define LOAD_ERROR -2
46
47
48 /* The smallest value that counts as a change, should be approximately
49 * epsilon+delta */
50 #define MIN_CHANGE (FLT_EPSILON + FLT_EPSILON)
51
52 float s_value[S_SIZE];
53 static float s_target[S_SIZE];
54 static int s_duration[S_SIZE];
55 static int s_changed[S_SIZE];
56 static GtkAdjustment *s_adjustment[S_SIZE];
57 static s_callback_func s_callback[S_SIZE];
58 static char *errstr = NULL;
59
60
61 static s_state *last_state = NULL;
62 static int last_changed = S_NONE;
63
64 static GList *history = NULL;
65 static GList *undo_pos = NULL;
66
67 static int suppress_feedback = 0;
68 static int saved_scene;
69 static float crossfade_time = 1.0;
70 static gboolean override_limiter_default = FALSE;
71
72 static void s_set_events(int id, float value);
73 void s_update_title();
74 void s_history_add(const char *description);
75 void s_save_global_int(xmlDocPtr doc, char *symbol, int value);
76 void s_save_global_float(xmlDocPtr doc, char *symbol, float value);
77 void s_save_global_gang(xmlDocPtr doc, char *p, int band, gboolean value);
78
79 gchar *session_filename = NULL;
80
81 /* global session parameters read from the XML file */
82
83 typedef struct {
84 int scene;
85 int mode;
86 float freq;
87 float lgain;
88 float hgain;
89 float ct;
90 float inwl;
91 float outwl;
92 int eq_bypass;
93 int comp_bypass[3];
94 int limiter_bypass;
95 gboolean out_meter_peak_pref;
96 gboolean rms_meter_peak_pref;
97 int rms_time_slice;
98 int limiter_plugin;
99 float xo_delay_time[XO_BANDS - 1];
100 int xo_delay_state[XO_BANDS - 1];
101 int gang_at[XO_BANDS];
102 int gang_re[XO_BANDS];
103 int gang_th[XO_BANDS];
104 int gang_ra[XO_BANDS];
105 int gang_kn[XO_BANDS];
106 int gang_ma[XO_BANDS];
107 int iir_xover;
108 } xml_global_params;
109
110 void state_init()
111 {
112 unsigned int i;
113
114 for (i=0; i<S_SIZE; i++) {
115 s_value[i] = 0.0f;
116 s_target[i] = 0.0f;
117 s_duration[i] = 0;
118 s_changed[i] = 0;
119 s_adjustment[i] = NULL;
120 s_callback[i] = NULL;
121 }
122
123
124 /* Defaults for notches. */
125
126 s_value[S_NOTCH_Q(1)] = 5.0;
127 s_value[S_NOTCH_Q(2)] = 5.0;
128 s_value[S_NOTCH_Q(3)] = 5.0;
129
130 s_value[S_NOTCH_FREQ(0)] = hdeq_get_notch_default_freq (0);
131 s_value[S_NOTCH_FREQ(1)] = hdeq_get_notch_default_freq (1);
132 s_value[S_NOTCH_FREQ(2)] = hdeq_get_notch_default_freq (2);
133 s_value[S_NOTCH_FREQ(3)] = hdeq_get_notch_default_freq (3);
134 s_value[S_NOTCH_FREQ(4)] = hdeq_get_notch_default_freq (4);
135
136
137 s_history_add("Initial state");
138 }
139
140 void s_suppress_push()
141 {
142 suppress_feedback++;
143 }
144
145 void s_suppress_pop()
146 {
147 suppress_feedback--;
148 }
149
150 void s_set_callback(int id, s_callback_func callback)
151 {
152 assert(id >= 0 && id < S_SIZE);
153
154 s_callback[id] = callback;
155 }
156
157 void s_set_adjustment(int id, GtkAdjustment *adjustment)
158 {
159 assert(id >= 0 && id < S_SIZE);
160
161 s_adjustment[id] = adjustment;
162 }
163
164 void s_set_value_ui(int id, float value)
165 {
166 s_value[id] = value;
167
168 if (suppress_feedback) {
169 return;
170 }
171 assert(id >= 0 && id < S_SIZE);
172
173
174 if (last_changed != id) {
175 s_history_add(g_strdup_printf("%s = %f", s_description[id],
176 s_value[id]));
177 }
178 last_state->value[id] = value;
179
180 #if 0
181 /* This code is confusing in use, so I've removed it - swh */
182
183 if (value - MIN_CHANGE < last_state->value[id] &&
184 value + MIN_CHANGE > last_state->value[id]) {
185 last_changed = S_NONE;
186 } else {
187 last_changed = id;
188 }
189 #else
190 last_changed = id;
191 #endif
192
193 if (s_callback[id]) {
194 (*s_callback[id])(id, value);
195 }
196 }
197
198 void s_set_value(int id, float value, int duration)
199 {
200 /* We dont want to call this yet... s_set_value_ui(id, value); */
201 s_duration[id] = duration;
202 s_target[id] = value;
203 if (s_adjustment[id]) {
204 gtk_adjustment_set_value(s_adjustment[id], value);
205 }
206 }
207
208 void s_set_value_block(float *values, int base, int count)
209 {
210 int i;
211
212 for (i = 0 ; i < count ; i++) {
213 s_value[base + i] = values[i];
214 }
215 last_changed = base;
216 s_set_events(base, values[i]);
217 }
218
219 void s_set_value_no_history(int id, float value)
220 {
221 suppress_feedback++;
222 s_value[id] = value;
223 s_target[id] = value;
224 s_set_events(id, value);
225 suppress_feedback--;
226 }
227
228 void s_clear_history()
229 {
230 GList *p;
231
232 for (p=history; p; p=p->next) {
233 free(p->data);
234 }
235 g_list_free(history);
236 history = NULL;
237 s_history_add("Initial state");
238 undo_pos = history->next;
239 s_restore_state((s_state *)history->data);
240 last_changed = S_LOAD;
241 }
242
243 void s_history_add(const char *description)
244 {
245 s_state *ns;
246 GList *it;
247
248 ns = malloc(sizeof(s_state));
249 ns->description = (char *)description;
250 memcpy(ns->value, s_value, S_SIZE * sizeof(float));
251
252 if (undo_pos) {
253 it = undo_pos->next;
254 while (it) {
255 free(it->data);
256 it = it->next;
257 }
258 undo_pos->next = NULL;
259 }
260
261 history = g_list_append(history, ns);
262 undo_pos = g_list_last(history);
263 /* printf("add %s\n", description); */
264 last_state = ns;
265 }
266
267 void s_history_add_state(s_state state)
268 {
269 s_state *ns;
270 GList *it;
271
272 ns = malloc(sizeof(s_state));
273 ns->description = (char *)state.description;
274 memcpy(ns->value, state.value, S_SIZE * sizeof(float));
275
276 if (undo_pos) {
277 it = undo_pos->next;
278 while (it) {
279 free(it->data);
280 it = it->next;
281 }
282 undo_pos->next = NULL;
283 }
284
285 history = g_list_append(history, ns);
286 undo_pos = g_list_last(history);
287 /* printf("add %s\n", description); */
288 last_state = ns;
289 }
290
291 static unsigned int compute_state_crc (s_state *state)
292 {
293 unsigned int checksum, i;
294 unsigned char *buf;
295 unsigned int crc_table[256] =
296 {0x00000000,0x77073096,0xEE0E612C,0x990951BA,0x076DC419,0x706AF48F,
297 0xE963A535,0x9E6495A3,0x0EDB8832,0x79DCB8A4,0xE0D5E91E,0x97D2D988,
298 0x09B64C2B,0x7EB17CBD,0xE7B82D07,0x90BF1D91,0x1DB71064,0x6AB020F2,
299 0xF3B97148,0x84BE41DE,0x1ADAD47D,0x6DDDE4EB,0xF4D4B551,0x83D385C7,
300 0x136C9856,0x646BA8C0,0xFD62F97A,0x8A65C9EC,0x14015C4F,0x63066CD9,
301 0xFA0F3D63,0x8D080DF5,0x3B6E20C8,0x4C69105E,0xD56041E4,0xA2677172,
302 0x3C03E4D1,0x4B04D447,0xD20D85FD,0xA50AB56B,0x35B5A8FA,0x42B2986C,
303 0xDBBBC9D6,0xACBCF940,0x32D86CE3,0x45DF5C75,0xDCD60DCF,0xABD13D59,
304 0x26D930AC,0x51DE003A,0xC8D75180,0xBFD06116,0x21B4F4B5,0x56B3C423,
305 0xCFBA9599,0xB8BDA50F,0x2802B89E,0x5F058808,0xC60CD9B2,0xB10BE924,
306 0x2F6F7C87,0x58684C11,0xC1611DAB,0xB6662D3D,0x76DC4190,0x01DB7106,
307 0x98D220BC,0xEFD5102A,0x71B18589,0x06B6B51F,0x9FBFE4A5,0xE8B8D433,
308 0x7807C9A2,0x0F00F934,0x9609A88E,0xE10E9818,0x7F6A0DBB,0x086D3D2D,
309 0x91646C97,0xE6635C01,0x6B6B51F4,0x1C6C6162,0x856530D8,0xF262004E,
310 0x6C0695ED,0x1B01A57B,0x8208F4C1,0xF50FC457,0x65B0D9C6,0x12B7E950,
311 0x8BBEB8EA,0xFCB9887C,0x62DD1DDF,0x15DA2D49,0x8CD37CF3,0xFBD44C65,
312 0x4DB26158,0x3AB551CE,0xA3BC0074,0xD4BB30E2,0x4ADFA541,0x3DD895D7,
313 0xA4D1C46D,0xD3D6F4FB,0x4369E96A,0x346ED9FC,0xAD678846,0xDA60B8D0,
314 0x44042D73,0x33031DE5,0xAA0A4C5F,0xDD0D7CC9,0x5005713C,0x270241AA,
315 0xBE0B1010,0xC90C2086,0x5768B525,0x206F85B3,0xB966D409,0xCE61E49F,
316 0x5EDEF90E,0x29D9C998,0xB0D09822,0xC7D7A8B4,0x59B33D17,0x2EB40D81,
317 0xB7BD5C3B,0xC0BA6CAD,0xEDB88320,0x9ABFB3B6,0x03B6E20C,0x74B1D29A,
318 0xEAD54739,0x9DD277AF,0x04DB2615,0x73DC1683,0xE3630B12,0x94643B84,
319 0x0D6D6A3E,0x7A6A5AA8,0xE40ECF0B,0x9309FF9D,0x0A00AE27,0x7D079EB1,
320 0xF00F9344,0x8708A3D2,0x1E01F268,0x6906C2FE,0xF762575D,0x806567CB,
321 0x196C3671,0x6E6B06E7,0xFED41B76,0x89D32BE0,0x10DA7A5A,0x67DD4ACC,
322 0xF9B9DF6F,0x8EBEEFF9,0x17B7BE43,0x60B08ED5,0xD6D6A3E8,0xA1D1937E,
323 0x38D8C2C4,0x4FDFF252,0xD1BB67F1,0xA6BC5767,0x3FB506DD,0x48B2364B,
324 0xD80D2BDA,0xAF0A1B4C,0x36034AF6,0x41047A60,0xDF60EFC3,0xA867DF55,
325 0x316E8EEF,0x4669BE79,0xCB61B38C,0xBC66831A,0x256FD2A0,0x5268E236,
326 0xCC0C7795,0xBB0B4703,0x220216B9,0x5505262F,0xC5BA3BBE,0xB2BD0B28,
327 0x2BB45A92,0x5CB36A04,0xC2D7FFA7,0xB5D0CF31,0x2CD99E8B,0x5BDEAE1D,
328 0x9B64C2B0,0xEC63F226,0x756AA39C,0x026D930A,0x9C0906A9,0xEB0E363F,
329 0x72076785,0x05005713,0x95BF4A82,0xE2B87A14,0x7BB12BAE,0x0CB61B38,
330 0x92D28E9B,0xE5D5BE0D,0x7CDCEFB7,0x0BDBDF21,0x86D3D2D4,0xF1D4E242,
331 0x68DDB3F8,0x1FDA836E,0x81BE16CD,0xF6B9265B,0x6FB077E1,0x18B74777,
332 0x88085AE6,0xFF0F6A70,0x66063BCA,0x11010B5C,0x8F659EFF,0xF862AE69,
333 0x616BFFD3,0x166CCF45,0xA00AE278,0xD70DD2EE,0x4E048354,0x3903B3C2,
334 0xA7672661,0xD06016F7,0x4969474D,0x3E6E77DB,0xAED16A4A,0xD9D65ADC,
335 0x40DF0B66,0x37D83BF0,0xA9BCAE53,0xDEBB9EC5,0x47B2CF7F,0x30B5FFE9,
336 0xBDBDF21C,0xCABAC28A,0x53B39330,0x24B4A3A6,0xBAD03605,0xCDD70693,
337 0x54DE5729,0x23D967BF,0xB3667A2E,0xC4614AB8,0x5D681B02,0x2A6F2B94,
338 0xB40BBE37,0xC30C8EA1,0x5A05DF1B,0x2D02EF8D};
339
340
341 checksum = ~0;
342
343 buf = (unsigned char *) state->value;
344
345 for (i = 0 ; i < S_SIZE * sizeof (float) ; i++)
346 checksum = crc_table[(checksum ^ buf[i]) & 0xff] ^ (checksum >> 8);
347
348 checksum ^= ~0;
349
350
351 return (checksum);
352 }
353
354 void s_undo()
355 {
356 GList *undo_next;
357 int scene, crc[2];
358 s_state *st[2];
359
360
361 if (!undo_pos) {
362 return;
363 }
364 undo_next = g_list_previous(undo_pos);
365 if (!undo_next) {
366 return;
367 }
368 undo_pos = undo_next;
369 s_restore_state((s_state *)undo_pos->data);
370
371 scene = get_previous_scene_num ();
372 if (scene >= 0)
373 {
374 st[0] = get_scene (scene);
375 st[1] = (s_state *) undo_pos->data;
376
377 crc[0] = compute_state_crc (st[0]);
378 crc[1] = compute_state_crc (st[1]);
379
380 if (crc[0] == crc[1]) set_scene_button (scene);
381 }
382
383 set_EQ_curve_values (0, 0.0);
384 last_changed = S_LOAD;
385 }
386
387 void s_redo()
388 {
389 gboolean restore;
390 int scene, crc[2];
391 s_state *st[2];
392
393
394 restore = FALSE;
395 if (undo_pos) {
396 if (undo_pos->next) {
397 undo_pos = g_list_next(undo_pos);
398 restore = TRUE;
399 }
400 } else if (history->next) {
401 undo_pos = history;
402 undo_pos = g_list_next(undo_pos);
403 restore = TRUE;
404 }
405
406 if (restore)
407 {
408 s_restore_state((s_state *)undo_pos->data);
409
410 scene = get_previous_scene_num ();
411 if (scene >= 0)
412 {
413 st[0] = get_scene (scene);
414 st[1] = (s_state *) undo_pos->data;
415
416 crc[0] = compute_state_crc (st[0]);
417 crc[1] = compute_state_crc (st[1]);
418
419 if (crc[0] == crc[1])
420 {
421 set_scene_button (scene);
422 }
423 else
424 {
425 set_scene_warning_button (scene);
426 }
427 }
428 set_EQ_curve_values (0, 0.0);
429 }
430 }
431
432
433 /* Negative time will use the default "crossfade time" which may come from the
434 command line -c option. */
435
436 void s_crossfade_to_state(s_state *state, float time)
437 {
438 int i, duration, milliseconds;
439
440 if (time < 0.0) time = crossfade_time;
441
442
443 suspend_ganging ();
444 milliseconds = time * 1000.0 + 50;
445 g_timeout_add (milliseconds, unsuspend_ganging, NULL);
446
447
448 /* printf("restore %s\n", state->description); */
449 duration = (int)(sample_rate * time);
450 suppress_feedback++;
451 for (i=0; i<S_SIZE; i++) {
452 /* set the target and duration for crosssfade, but set the controls to
453 * the endpoint */
454 s_target[i] = state->value[i];
455 s_duration[i] = duration;
456 s_set_events(i, state->value[i]);
457 }
458 suppress_feedback--;
459 }
460
461 void s_restore_state(s_state *state)
462 {
463 s_crossfade_to_state (state, 0.003);
464 }
465
466 static void s_set_events(int id, float value)
467 {
468 if (s_callback[id]) {
469 (*s_callback[id])(id, value);
470 }
471 if (s_adjustment[id]) {
472 gtk_adjustment_set_value(s_adjustment[id], value);
473 }
474 }
475
476 void s_set_description(int id, const char *desc)
477 {
478 if (last_changed != id) {
479 s_history_add(desc);
480 }
481 last_changed = id;
482 }
483
484 void s_save_session_from_ui (GtkWidget *w, gpointer user_data)
485 {
486 #if GTK_VERSION_GE(2, 4)
487
488 gchar *fname = NULL;
489 GtkFileChooser *file_selector = (GtkFileChooser *) user_data;
490
491 fname = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_selector));
492 s_save_session (fname);
493 g_free (fname);
494
495 #else
496
497 GtkFileSelection *file_selector = (GtkFileSelection *) user_data;
498
499 s_save_session(gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_selector)));
500
501 #endif
502 }
503
504 void s_save_session (const gchar *fname)
505 {
506 xmlDocPtr doc;
507 xmlNodePtr rootnode, node, sc_node;
508 unsigned int i, j;
509 int curr_scene;
510 char tmp[256];
511
512
513 /* Check to see if we have been passed a filename, if not fall back to
514 * previous one */
515
516 if (fname) {
517 s_set_session_filename (fname);
518 s_update_title();
519 }
520 if (!s_have_session_filename ()) {
521 errstr = g_strdup_printf("No session filename found at %s:%d, not saving\n",
522 __FILE__, __LINE__);
523 message (GTK_MESSAGE_WARNING, errstr);
524 free(errstr);
525 }
526
527
528 /* Need to save this scene number. */
529
530 curr_scene = get_current_scene ();
531
532 xmlSetCompressMode(5);
533 doc = xmlNewDoc("1.0");
534 rootnode = xmlNewDocRawNode(doc, NULL, "jam-param-list", NULL);
535 xmlSetProp(rootnode, "version", VERSION);
536 xmlDocSetRootElement(doc, rootnode);
537 node = xmlNewText("\n");
538 xmlAddChild(rootnode, node);
539
540
541 s_save_global_int(doc, "mode", process_get_spec_mode());
542 s_save_global_int(doc, "freq", get_spectrum_freq());
543 s_save_global_float(doc, "ct", crossfade_time);
544 s_save_global_float(doc, "inwl", intrim_inmeter_get_warn());
545 s_save_global_float(doc, "outwl", intrim_outmeter_get_warn());
546 s_save_global_float(doc, "lgain", hdeq_get_lower_gain());
547 s_save_global_float(doc, "hgain", hdeq_get_upper_gain());
548
549 s_save_global_int(doc, "eq bypass", process_get_bypass_state (EQ_BYPASS));
550 s_save_global_int(doc, "comp bypass0", process_get_bypass_state (LOW_COMP_BYPASS));
551 s_save_global_int(doc, "comp bypass1", process_get_bypass_state (MID_COMP_BYPASS));
552 s_save_global_int(doc, "comp bypass2", process_get_bypass_state (HIGH_COMP_BYPASS));
553 s_save_global_int(doc, "limiter bypass", process_get_bypass_state (LIMITER_BYPASS));
554
555 s_save_global_int(doc, "output meter peak pref", (int) intrim_get_out_meter_peak_pref ());
556 s_save_global_int(doc, "rms meter peak pref", (int) intrim_get_rms_meter_peak_pref ());
557
558 s_save_global_int(doc, "rms time slice", process_get_rms_time_slice ());
559
560 s_save_global_int(doc, "limiter plugin", process_get_limiter_plugin ());
561
562
563 s_save_global_float(doc, "low delay time", process_get_xo_delay_time(XO_LOW));
564 s_save_global_int(doc, "low delay state", process_get_xo_delay_state (XO_LOW));
565 s_save_global_float(doc, "mid delay time", process_get_xo_delay_time(XO_MID));
566 s_save_global_int(doc, "mid delay state", process_get_xo_delay_state (XO_MID));
567
568 s_save_global_int (doc, "crossover type", process_get_crossover_type ());
569
570 /* record the current gang state of the compressor controls */
571
572 for (i = 0 ; i < XO_BANDS ; i++) {
573 s_save_global_gang(doc, "at", i, comp_at_ganged(i));
574 s_save_global_gang(doc, "re", i, comp_re_ganged(i));
575 s_save_global_gang(doc, "th", i, comp_th_ganged(i));
576 s_save_global_gang(doc, "ra", i, comp_ra_ganged(i));
577 s_save_global_gang(doc, "kn", i, comp_kn_ganged(i));
578 s_save_global_gang(doc, "ma", i, comp_ma_ganged(i));
579 }
580
581
582 /* Save current active state */
583
584 for (i=0; i<S_SIZE; i++) {
585 node = xmlNewDocRawNode(doc, NULL, "parameter", NULL);
586 snprintf(tmp, 255, "%g", s_value[i]);
587 xmlSetProp(node, "name", s_symbol[i]);
588 xmlSetProp(node, "value", tmp);
589 xmlAddChild(rootnode, node);
590 node = xmlNewText("\n");
591 xmlAddChild(rootnode, node);
592 }
593
594
595 /* Save scenes */
596
597 for (j=0; j<NUM_SCENES; j++) {
598 s_state *st = get_scene(j);
599 sc_node = xmlNewDocRawNode(doc, NULL, "scene", NULL);
600 snprintf(tmp, 255, "%d", j);
601 xmlSetProp(sc_node, "number", tmp);
602 if (!st) {
603 xmlAddChild(rootnode, sc_node);
604 node = xmlNewText("\n");
605 xmlAddChild(rootnode, node);
606 continue;
607 }
608 xmlSetProp(sc_node, "name", get_scene_name(j));
609 if (curr_scene == j) {
610 xmlSetProp(sc_node, "active", "true");
611 xmlSetProp(sc_node, "changed", "false");
612 } else if (curr_scene == changed_scene_no(j)) {
613 xmlSetProp(sc_node, "active", "true");
614 xmlSetProp(sc_node, "changed", "true");
615 }
616 node = xmlNewText("\n");
617 xmlAddChild(sc_node, node);
618 xmlAddChild(rootnode, sc_node);
619 node = xmlNewText("\n");
620 xmlAddChild(rootnode, node);
621
622 for (i=0; i<S_SIZE; i++) {
623 node = xmlNewDocRawNode(doc, NULL, "parameter", NULL);
624 snprintf(tmp, 255, "%g", st->value[i]);
625 xmlSetProp(node, "name", s_symbol[i]);
626 xmlSetProp(node, "value", tmp);
627 xmlAddChild(sc_node, node);
628 node = xmlNewText("\n");
629 xmlAddChild(sc_node, node);
630 }
631 }
632 xmlSaveFile((const char *) s_get_session_filename (), doc);
633 xmlFreeDoc(doc);
634 }
635
636 /* declare SAX handlers */
637 void s_startElement(void *user_data, const xmlChar *name,
638 const xmlChar **attrs);
639
640 static void s_warning(void *user_data, const char *msg, ...);
641
642 static void s_error(void *user_data, const char *msg, ...);
643
644
645 void s_load_session_from_ui (GtkWidget *w, gpointer user_data)
646 {
647 #if GTK_VERSION_GE(2, 4)
648
649 gchar *fname = NULL;
650 GtkFileChooser *file_selector = (GtkFileChooser *) user_data;
651
652 fname = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_selector));
653 s_load_session (fname);
654 g_free (fname);
655
656 #else
657
658 GtkFileSelection *file_selector = (GtkFileSelection *) user_data;
659
660 s_load_session(gtk_file_selection_get_filename (GTK_FILE_SELECTION
661 (file_selector)));
662
663 #endif
664 }
665
666 void s_load_session (const gchar *fname)
667 {
668 xmlSAXHandlerPtr handler;
669 int scene = -1;
670 int fd;
671 int i;
672 xml_global_params gp;
673 gchar *session_filename;
674
675 saved_scene = -1;
676 unset_scene_buttons ();
677
678 if (fname) {
679 s_set_session_filename (fname);
680 }
681 if (!s_have_session_filename ()) {
682 s_set_session_filename (default_session);
683 }
684
685 session_filename = s_get_session_filename ();
686
687 /* Check to see if file is readable */
688 if ((fd = open((const char *) session_filename, O_RDONLY)) >= 0) {
689 close(fd);
690 } else {
691 errstr = g_strdup_printf("Error opening '%s'", session_filename);
692 message (GTK_MESSAGE_WARNING, errstr);
693 perror(errstr);
694 free(errstr);
695
696 if (fname == NULL) {
697 exit(1);
698 }
699 }
700
701 s_update_title();
702
703 handler = calloc(1, sizeof(xmlSAXHandler));
704 handler->startElement = s_startElement;
705 handler->warning = s_warning;
706 handler->error = s_error;
707
708 /* set the gp struct to some sensible defaults in case values aren't set in
709 * the XML file */
710 gp.scene = scene;
711 gp.mode = SPEC_POST_EQ;
712 gp.freq = 10;
713 gp.lgain = -12.0;
714 gp.hgain = 12.0;
715 gp.inwl = -6.0;
716 gp.outwl = -6.0;
717 gp.ct = 1.0;
718 gp.eq_bypass = 0;
719 gp.comp_bypass[0] = gp.comp_bypass[1] = gp.comp_bypass[2] = 0;
720 gp.limiter_bypass = 0;
721 gp.out_meter_peak_pref = TRUE;
722 gp.rms_meter_peak_pref = TRUE;
723 gp.rms_time_slice = 50;
724 gp.limiter_plugin = FAST;
725 gp.xo_delay_time[XO_LOW] = 2.0;
726 gp.xo_delay_state[XO_LOW] = 0;
727 gp.xo_delay_time[XO_MID] = 0.5;
728 gp.xo_delay_state[XO_MID] = 0;
729 gp.iir_xover = FFT;
730 for (i = 0 ; i < XO_BANDS ; i++) {
731 gp.gang_at[i] = FALSE;
732 gp.gang_re[i] = FALSE;
733 gp.gang_th[i] = FALSE;
734 gp.gang_ra[i] = FALSE;
735 gp.gang_kn[i] = FALSE;
736 gp.gang_ma[i] = FALSE;
737 }
738
739
740 /* run the SAX parser */
741 scene_init();
742 xmlSAXUserParseFile(handler, &gp, (const char *) session_filename);
743
744 if (gp.scene == LOAD_ERROR) {
745 errstr = g_strdup_printf("Loading file '%s' failed", session_filename);
746 message (GTK_MESSAGE_WARNING, errstr);
747 perror(errstr);
748 free(errstr);
749 return;
750 }
751
752 s_history_add(g_strdup_printf("Load %s", session_filename));
753 last_changed = S_LOAD;
754 free(handler);
755
756 /* global params are read into the gp struct, and set in the UI code from
757 * here */
758
759 process_set_spec_mode (gp.mode);
760 set_spectrum_freq (gp.freq);
761 hdeq_set_upper_gain (gp.hgain);
762 geq_set_range (gtk_adjustment_get_lower(geq_get_adjustment(0)), gp.hgain);
763 hdeq_set_lower_gain (gp.lgain);
764 geq_set_range (gp.lgain, gtk_adjustment_get_upper(geq_get_adjustment(0)));
765 s_set_crossfade_time (gp.ct);
766 intrim_inmeter_set_warn (gp.inwl);
767 intrim_outmeter_set_warn (gp.outwl);
768
769
770 for (i = 0 ; i < XO_NBANDS ; i++)
771 {
772 if (gp.comp_bypass[i] != 2)
773 {
774 callbacks_set_comp_bypass_button_state (i, FALSE);
775 }
776 else
777 {
778 callbacks_set_comp_bypass_button_state (i, TRUE);
779 }
780 }
781
782 callbacks_set_eq_bypass_button_state (gp.eq_bypass);
783 callbacks_set_limiter_bypass_button_state (gp.limiter_bypass);
784
785 process_set_xo_delay_time (XO_LOW, gp.xo_delay_time[XO_LOW]);
786 process_set_xo_delay_time (XO_MID, gp.xo_delay_time[XO_MID]);
787 process_set_xo_delay_state (XO_LOW, gp.xo_delay_state[XO_LOW]);
788 process_set_xo_delay_state (XO_MID, gp.xo_delay_state[XO_MID]);
789
790 callbacks_set_low_delay_button_state (gp.xo_delay_state[XO_LOW]);
791 callbacks_set_mid_delay_button_state (gp.xo_delay_state[XO_MID]);
792
793 intrim_set_out_meter_peak_pref (gp.out_meter_peak_pref);
794 intrim_set_rms_meter_peak_pref (gp.rms_meter_peak_pref);
795
796 process_set_rms_time_slice (gp.rms_time_slice);
797
798 process_set_crossover_type (gp.iir_xover);
799
800 /* This is the active scene. */
801
802 if (saved_scene < 100)
803 {
804 set_scene (saved_scene);
805 }
806 else
807 {
808 set_num_scene_warning_button(saved_scene);
809 }
810
811 if (!fname) {
812 s_set_session_filename (NULL);
813 }
814
815
816 /* Set the ganging after the scene otherwise it will try to gang move
817 the scene values. */
818
819 for (i = 0 ; i < XO_BANDS ; i++) {
820 if (gp.gang_at[i]) comp_gang_at (i);
821 if (gp.gang_re[i]) comp_gang_re (i);
822 if (gp.gang_th[i]) comp_gang_th (i);
823 if (gp.gang_ra[i]) comp_gang_ra (i);
824 if (gp.gang_kn[i]) comp_gang_kn (i);
825 if (gp.gang_ma[i]) comp_gang_ma (i);
826 }
827
828
829 /* If we set the limiter plugin on the command line don't set
830 it from the defaults. */
831
832 if (!override_limiter_default) process_set_limiter_plugin (gp.limiter_plugin);
833 override_limiter_default = FALSE;
834
835 if(gui_mode == 0) // Default mode
836 hdeq_set_xover ();
837 set_EQ_curve_values (0, 0.0);
838
839 s_clear_history();
840 pref_set_all_values ();
841 }
842
843 void s_startElement(void *user_data, const xmlChar *name, const xmlChar **attrs)
844 {
845 const xmlChar **p;
846 unsigned int i, found = 0;
847 const char *symbol = NULL, *value = NULL, *index = NULL;
848 xml_global_params *gp = user_data;
849 int active = 0;
850 int changed = 0;
851
852 if (!strcmp(name, "jam-param-list")) {
853 return;
854 }
855
856 if (!strcmp(name, "scene")) {
857 const char *sname = NULL;
858
859 for (p=attrs; p && *p; p+=2) {
860 if (!strcmp(*p, "name")) {
861 sname = *(p+1);
862 } else if (!strcmp(*p, "number")) {
863 gp->scene = atoi(*(p+1));
864 } else if (!strcmp(*p, "active") && !strcmp(*(p+1), "true")) {
865 active = 1;
866 } else if (!strcmp(*p, "changed") && !strcmp(*(p+1), "true")) {
867 changed = 1;
868 }
869 }
870
871
872 /* Set the active scene number to be set after parsing all of the
873 scenes in the XML file. */
874
875 if (active) {
876 saved_scene = gp->scene;
877 set_scene(gp->scene);
878 }
879 if (changed) {
880 saved_scene = gp->scene + 100;
881 set_num_scene_warning_button(changed_scene_no(gp->scene));
882 }
883
884 if (sname && gp->scene > -1) {
885 set_scene(gp->scene);
886 set_scene_name(gp->scene, sname);
887 }
888
889 return;
890 }
891
892 /* if its a global setting */
893 if (!strcmp(name, "global")) {
894 /* find the name, value and index attributes */
895 for (p=attrs; p && *p; p+=2) {
896 if (!strcmp(*p, "name")) {
897 symbol = *(p+1);
898 } else if (!strcmp(*p, "value")) {
899 value = *(p+1);
900 } else if (!strcmp(*p, "index")) {
901 index = *(p+1);
902 }
903 }
904
905 if (!strcmp(symbol, "mode")) {
906 gp->mode = atoi(value);
907 } else if (!strcmp(symbol, "freq")) {
908 gp->freq = atof(value);
909 } else if (!strcmp(symbol, "lgain")) {
910 gp->lgain = atof(value);
911 } else if (!strcmp(symbol, "hgain")) {
912 gp->hgain = atof(value);
913 } else if (!strcmp(symbol, "ct")) {
914 gp->ct = atof(value);
915 } else if (!strcmp(symbol, "inwl")) {
916 gp->inwl = atof(value);
917 } else if (!strcmp(symbol, "outwl")) {
918 gp->outwl = atof(value);
919 } else if (!strcmp(symbol, "eq bypass")) {
920 gp->eq_bypass = atof(value);
921 } else if (!strcmp(symbol, "comp bypass0")) {
922 gp->comp_bypass[0] = atoi(value);
923 } else if (!strcmp(symbol, "comp bypass1")) {
924 gp->comp_bypass[1] = atoi(value);
925 } else if (!strcmp(symbol, "comp bypass2")) {
926 gp->comp_bypass[2] = atoi(value);
927 } else if (!strcmp(symbol, "limiter bypass")) {
928 gp->limiter_bypass = atoi(value);
929 } else if (!strcmp(symbol, "output meter peak pref")) {
930 gp->out_meter_peak_pref = (gboolean) atoi(value);
931 } else if (!strcmp(symbol, "rms meter peak pref")) {
932 gp->rms_meter_peak_pref = (gboolean) atoi(value);
933 } else if (!strcmp(symbol, "rms time slice")) {
934 gp->rms_time_slice = atoi(value);
935 } else if (!strcmp(symbol, "limiter plugin")) {
936 gp->limiter_plugin = atoi(value);
937 } else if (!strcmp(symbol, "low delay time")) {
938 gp->xo_delay_time[XO_LOW] = atof(value);
939 } else if (!strcmp(symbol, "mid delay time")) {
940 gp->xo_delay_time[XO_MID] = atof(value);
941 } else if (!strcmp(symbol, "low delay state")) {
942 gp->xo_delay_state[XO_LOW] = atoi(value);
943 } else if (!strcmp(symbol, "mid delay state")) {
944 gp->xo_delay_state[XO_MID] = atoi(value);
945 } else if (!strcmp(symbol, "crossover type")) {
946 gp->iir_xover = atoi(value);
947 } else if (!strcmp(symbol, "mid delay state")) {
948 } else if ((const char *)strstr(symbol, "gang_") == symbol) {
949 int ind = index ? atoi(index) : -1;
950 int val = atoi(value);
951
952 if (ind >= 0 && ind < XO_BANDS) {
953 if (!strcmp(symbol, "gang_at")) {
954 gp->gang_at[ind] = val;
955 } else if (!strcmp(symbol, "gang_re")) {
956 gp->gang_re[ind] = val;
957 } else if (!strcmp(symbol, "gang_th")) {
958 gp->gang_th[ind] = val;
959 } else if (!strcmp(symbol, "gang_ra")) {
960 gp->gang_ra[ind] = val;
961 } else if (!strcmp(symbol, "gang_kn")) {
962 gp->gang_kn[ind] = val;
963 } else if (!strcmp(symbol, "gang_ma")) {
964 gp->gang_ma[ind] = val;
965 } else {
966 errstr = g_strdup_printf("Unhandled gang: %s\n", symbol);
967 message (GTK_MESSAGE_WARNING, errstr);
968 free(errstr);
969 }
970 } else {
971 errstr = g_strdup_printf("Unhandled index: %s\n", index);
972 message (GTK_MESSAGE_WARNING, errstr);
973 free(errstr);
974 }
975 } else {
976 errstr = g_strdup_printf("Unhandled global paramter: %s\n", symbol);
977 message (GTK_MESSAGE_WARNING, errstr);
978 free(errstr);
979 }
980
981 return;
982 }
983
984 /* Check its a parameter element */
985 if (strcmp(name, "parameter")) {
986 errstr = g_strdup_printf("Unhandled element: %s\n", name);
987 message (GTK_MESSAGE_WARNING, errstr);
988 free(errstr);
989 }
990
991 /* Find the name and value attributes */
992 for (p=attrs; p && *p; p+=2) {
993 if (!strcmp(*p, "name")) {
994 symbol = *(p+1);
995 } else if (!strcmp(*p, "value")) {
996 value = *(p+1);
997 }
998 }
999
1000 /* Find the matching symbol, this is horribly inefficient */
1001 for (i=0; i<S_SIZE && !found; i++) {
1002 if (!strcmp(symbol, s_symbol[i])) {
1003 if (gp->scene == -1) {
1004 s_value[i] = atof(value);
1005 suppress_feedback++;
1006 s_set_events(i, s_value[i]);
1007 suppress_feedback--;
1008 } else {
1009 s_state *st = get_scene(gp->scene);
1010 if (st) {
1011 st->value[i] = atof(value);
1012 } else {
1013 errstr = g_strdup_printf("Bad scene number %d\n", gp->scene);
1014 message (GTK_MESSAGE_WARNING, errstr);
1015 free(errstr);
1016 }
1017 }
1018 //printf("load %s = %g\n", symbol, s_value[i]);
1019 found = 1;
1020 break;
1021 }
1022 }
1023 if (!found) {
1024
1025 /* We need to disregard stereo-balance settings as these have been removed
1026 but may still be in old .jam files. */
1027
1028 if (!strstr (symbol ,"stereo-balance"))
1029 {
1030 errstr = g_strdup_printf("Unknown symbol: %s in element %s\n",
1031 symbol, name);
1032 message (GTK_MESSAGE_WARNING, errstr);
1033 free(errstr);
1034 }
1035 }
1036 }
1037
1038 static void s_warning(void *user_data, const char *msg, ...)
1039 {
1040 va_list args;
1041 char *fmt;
1042
1043 va_start(args, msg);
1044 fmt = g_strdup_printf("XML parser warning: %s", msg);
1045 vfprintf(stderr, fmt, args);
1046 free(fmt);
1047 va_end(args);
1048 }
1049
1050 static void s_error(void *user_data, const char *msg, ...)
1051 {
1052 va_list args;
1053 char *fmt;
1054
1055 va_start(args, msg);
1056 fmt = g_strdup_printf("XML parser error: %s", msg);
1057 vfprintf(stderr, fmt, args);
1058 free(fmt);
1059 va_end(args);
1060 *((int *)user_data) = LOAD_ERROR;
1061 }
1062
1063 void s_crossfade(const int nframes)
1064 {
1065 unsigned int i;
1066
1067
1068 for (i=0; i<S_SIZE; i++) {
1069 if (s_duration[i] != 0) {
1070 /* debug crap if (i == S_IN_GAIN) printf("%d\t%f\n", s_duration[i], s_value[i]); */
1071 s_duration[i] -= nframes;
1072 if (s_duration[i] > nframes) {
1073 s_value[i] += ((float)nframes / (float)s_duration[i]) *
1074 (s_target[i] - s_value[i]);
1075 } else {
1076 s_value[i] = s_target[i];
1077 s_duration[i] = 0;
1078 }
1079 s_changed[i] = 1;
1080 }
1081 }
1082 }
1083
1084 void s_crossfade_ui()
1085 {
1086 unsigned int i;
1087
1088
1089 suppress_feedback++;
1090 for (i=0; i<S_SIZE; i++) {
1091 if (s_changed[i]) {
1092 s_changed[i] = 0;
1093 s_set_events(i, s_value[i]);
1094 }
1095 }
1096 suppress_feedback--;
1097 }
1098
1099 int s_have_session_filename()
1100 {
1101 return (session_filename != NULL);
1102 }
1103
1104 gchar *s_get_session_filename()
1105 {
1106 return ((gchar *) session_filename);
1107 }
1108
1109 void s_update_title()
1110 {
1111 char *title;
1112 char *base;
1113 gchar *tmp;
1114
1115 /* name for title bar */
1116 char *title_name = (client_name? client_name: PACKAGE);
1117
1118 tmp = g_strdup (s_get_session_filename ());
1119 base = basename (tmp);
1120 title = g_strdup_printf ("%s - %s - " VERSION, title_name, base);
1121 g_free (tmp);
1122 gtk_window_set_title ((GtkWindow *) main_window, title);
1123 g_free (title);
1124 }
1125
1126 void s_set_session_filename(const gchar *fname)
1127 {
1128 if (session_filename != NULL)
1129 g_free (session_filename);
1130
1131 if (fname != NULL) {
1132 session_filename = g_strdup (fname);
1133 } else {
1134 session_filename = NULL;
1135 }
1136 }
1137
1138 void s_set_crossfade_time(float ct)
1139 {
1140 /* We're faking them out here. 0.0 isn't really allowable but
1141 most people would rather put in 0.0 instead of 0.001. */
1142
1143 if (ct == 0.0) ct = 0.001;
1144 crossfade_time = ct;
1145 }
1146
1147 float s_get_crossfade_time()
1148 {
1149 return (crossfade_time);
1150 }
1151
1152 void s_save_global_int(xmlDocPtr doc, char *symbol, int value)
1153 {
1154 xmlNodePtr root = xmlDocGetRootElement(doc);
1155 xmlNodePtr node = xmlNewDocRawNode(doc, NULL, "global", NULL);
1156 char tmp[256];
1157
1158 snprintf(tmp, 255, "%d", value);
1159 xmlSetProp(node, "name", symbol);
1160 xmlSetProp(node, "value", tmp);
1161 xmlAddChild(root, node);
1162 node = xmlNewText("\n");
1163 xmlAddChild(root, node);
1164 }
1165
1166 void s_save_global_float(xmlDocPtr doc, char *symbol, float value)
1167 {
1168 xmlNodePtr root = xmlDocGetRootElement(doc);
1169 xmlNodePtr node = xmlNewDocRawNode(doc, NULL, "global", NULL);
1170 char tmp[256];
1171
1172 snprintf(tmp, 255, "%f", value);
1173 xmlSetProp(node, "name", symbol);
1174 xmlSetProp(node, "value", tmp);
1175 xmlAddChild(root, node);
1176 node = xmlNewText("\n");
1177 xmlAddChild(root, node);
1178 }
1179
1180 void s_save_global_gang(xmlDocPtr doc, char *p, int band, gboolean value)
1181 {
1182 xmlNodePtr root = xmlDocGetRootElement(doc);
1183 xmlNodePtr node = xmlNewDocRawNode(doc, NULL, "global", NULL);
1184 char tmp[256];
1185
1186 snprintf(tmp, 255, "gang_%s", p);
1187 xmlSetProp(node, "name", tmp);
1188 snprintf(tmp, 255, "%d", band);
1189 xmlSetProp(node, "index", tmp);
1190 snprintf(tmp, 255, "%d", value);
1191 xmlSetProp(node, "value", tmp);
1192 xmlAddChild(root, node);
1193 node = xmlNewText("\n");
1194 xmlAddChild(root, node);
1195 }
1196
1197
1198 /* Set a boolean if we put the limiter in on the command line. */
1199
1200 void s_set_override_limiter_default ()
1201 {
1202 override_limiter_default = TRUE;
1203 }
1204
1205
1206 /* vi:set ts=8 sts=4 sw=4: */