1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@videolan.org>
8 * code from projectM http://xmms-projectm.sourceforge.net
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
32 #include "param_types.h"
35 #include "expr_types.h"
38 #include "splaytree_types.h"
39 #include "splaytree.h"
40 #include "tree_types.h"
42 #include "per_frame_eqn_types.h"
43 #include "per_frame_eqn.h"
45 #include "init_cond_types.h"
46 #include "init_cond.h"
48 #include "preset_types.h"
50 #include "custom_wave_types.h"
51 #include "custom_wave.h"
53 #include "init_cond_types.h"
54 #include "init_cond.h"
56 #include "engine_vars.h"
57 #define MAX_SAMPLE_SIZE 4096
61 custom_wave_t * interface_wave = NULL;
63 extern preset_t * active_preset;
64 static inline void eval_custom_wave_init_conds(custom_wave_t * custom_wave);
65 void load_unspec_init_cond(param_t * param);
66 void destroy_per_point_eqn_tree(splaytree_t * tree);
67 void destroy_param_db_tree(splaytree_t * tree);
68 void destroy_per_frame_eqn_tree(splaytree_t * tree);
69 void destroy_per_frame_init_eqn_tree(splaytree_t * tree);
70 void destroy_init_cond_tree(splaytree_t * tree);
71 static inline void evalPerPointEqn(per_point_eqn_t * per_point_eqn);
73 custom_wave_t * new_custom_wave(int id) {
75 custom_wave_t * custom_wave;
78 if ((custom_wave = (custom_wave_t*)malloc(sizeof(custom_wave_t))) == NULL)
82 custom_wave->per_frame_count = 0;
84 custom_wave->samples = 512;
85 custom_wave->bSpectrum = 0;
86 custom_wave->enabled = 1;
88 custom_wave->smoothing = 0.0;
89 custom_wave->bUseDots = 0;
90 custom_wave->bAdditive = 0;
91 custom_wave->r = custom_wave->g = custom_wave->b = custom_wave->a = 0.0;
92 custom_wave->scaling = 1.0;
93 custom_wave->per_frame_eqn_string_index = 0;
94 custom_wave->per_frame_init_eqn_string_index = 0;
95 custom_wave->per_point_eqn_string_index = 0;
97 custom_wave->r_mesh = malloc(MAX_SAMPLE_SIZE*sizeof(double));
98 custom_wave->g_mesh = malloc(MAX_SAMPLE_SIZE*sizeof(double));
99 custom_wave->b_mesh = malloc(MAX_SAMPLE_SIZE*sizeof(double));
100 custom_wave->a_mesh = malloc(MAX_SAMPLE_SIZE*sizeof(double));
101 custom_wave->x_mesh = malloc(MAX_SAMPLE_SIZE*sizeof(double));
102 custom_wave->y_mesh = malloc(MAX_SAMPLE_SIZE*sizeof(double));
103 custom_wave->value1 = malloc(MAX_SAMPLE_SIZE*sizeof(double));
104 custom_wave->value2 = malloc(MAX_SAMPLE_SIZE*sizeof(double));
105 custom_wave->sample_mesh = malloc(MAX_SAMPLE_SIZE*sizeof(double));
107 /* Initialize tree data structures */
109 if ((custom_wave->param_tree =
110 create_splaytree(compare_string, copy_string, free_string)) == NULL) {
111 free_custom_wave(custom_wave);
115 if ((custom_wave->per_point_eqn_tree =
116 create_splaytree(compare_int, copy_int, free_int)) == NULL) {
117 free_custom_wave(custom_wave);
121 if ((custom_wave->per_frame_eqn_tree =
122 create_splaytree(compare_int, copy_int, free_int)) == NULL) {
123 free_custom_wave(custom_wave);
127 if ((custom_wave->init_cond_tree =
128 create_splaytree(compare_string, copy_string, free_string)) == NULL) {
129 free_custom_wave(custom_wave);
133 if ((custom_wave->per_frame_init_eqn_tree =
134 create_splaytree(compare_string, copy_string, free_string)) == NULL) {
135 free_custom_wave(custom_wave);
140 /* Start: Load custom wave parameters */
142 if ((param = new_param_double("r", P_FLAG_DONT_FREE_MATRIX | P_FLAG_PER_POINT, &custom_wave->r, custom_wave->r_mesh, 1.0, 0.0, .5)) == NULL) {
143 free_custom_wave(custom_wave);
147 if (insert_param(param, custom_wave->param_tree) < 0) {
148 free_custom_wave(custom_wave);
152 if ((param = new_param_double("g", P_FLAG_DONT_FREE_MATRIX | P_FLAG_PER_POINT, &custom_wave->g, custom_wave->g_mesh, 1.0, 0.0, .5)) == NULL){
153 free_custom_wave(custom_wave);
157 if (insert_param(param, custom_wave->param_tree) < 0) {
158 free_custom_wave(custom_wave);
162 if ((param = new_param_double("b", P_FLAG_DONT_FREE_MATRIX | P_FLAG_PER_POINT, &custom_wave->b, custom_wave->b_mesh, 1.0, 0.0, .5)) == NULL){
163 free_custom_wave(custom_wave);
167 if (insert_param(param, custom_wave->param_tree) < 0) {
168 free_custom_wave(custom_wave);
172 if ((param = new_param_double("a", P_FLAG_DONT_FREE_MATRIX | P_FLAG_PER_POINT, &custom_wave->a, custom_wave->a_mesh, 1.0, 0.0, .5)) == NULL){
173 free_custom_wave(custom_wave);
177 if (insert_param(param, custom_wave->param_tree) < 0) {
178 free_custom_wave(custom_wave);
182 if ((param = new_param_double("x", P_FLAG_DONT_FREE_MATRIX | P_FLAG_PER_POINT, &custom_wave->x, custom_wave->x_mesh, 1.0, 0.0, .5)) == NULL) {
183 free_custom_wave(custom_wave);
187 if (insert_param(param, custom_wave->param_tree) < 0) {
188 free_custom_wave(custom_wave);
192 if ((param = new_param_double("y", P_FLAG_DONT_FREE_MATRIX | P_FLAG_PER_POINT, &custom_wave->y, custom_wave->y_mesh, 1.0, 0.0, .5)) == NULL) {
193 free_custom_wave(custom_wave);
197 if (insert_param(param, custom_wave->param_tree) < 0) {
198 free_custom_wave(custom_wave);
202 if ((param = new_param_bool("enabled", P_FLAG_NONE, &custom_wave->enabled, 1, 0, 0)) == NULL) {
203 free_custom_wave(custom_wave);
207 if (insert_param(param, custom_wave->param_tree) < 0) {
208 free_custom_wave(custom_wave);
212 if ((param = new_param_int("sep", P_FLAG_NONE, &custom_wave->sep, 100, -100, 0)) == NULL) {
213 free_custom_wave(custom_wave);
217 if (insert_param(param, custom_wave->param_tree) < 0) {
218 free_custom_wave(custom_wave);
222 if ((param = new_param_bool("bSpectrum", P_FLAG_NONE, &custom_wave->bSpectrum, 1, 0, 0)) == NULL) {
223 free_custom_wave(custom_wave);
227 if (insert_param(param, custom_wave->param_tree) < 0) {
228 free_custom_wave(custom_wave);
232 if ((param = new_param_bool("bDrawThick", P_FLAG_NONE, &custom_wave->bDrawThick, 1, 0, 0)) == NULL) {
233 free_custom_wave(custom_wave);
237 if (insert_param(param, custom_wave->param_tree) < 0) {
238 free_custom_wave(custom_wave);
242 if ((param = new_param_bool("bUseDots", P_FLAG_NONE, &custom_wave->bUseDots, 1, 0, 0)) == NULL) {
243 free_custom_wave(custom_wave);
247 if (insert_param(param, custom_wave->param_tree) < 0) {
248 free_custom_wave(custom_wave);
252 if ((param = new_param_bool("bAdditive", P_FLAG_NONE, &custom_wave->bAdditive, 1, 0, 0)) == NULL) {
253 free_custom_wave(custom_wave);
257 if (insert_param(param, custom_wave->param_tree) < 0) {
258 free_custom_wave(custom_wave);
262 if ((param = new_param_int("samples", P_FLAG_NONE, &custom_wave->samples, 2048, 1, 512)) == NULL) {
263 free_custom_wave(custom_wave);
267 if (insert_param(param, custom_wave->param_tree) < 0) {
268 free_custom_wave(custom_wave);
272 if ((param = new_param_double("sample", P_FLAG_READONLY | P_FLAG_DONT_FREE_MATRIX | P_FLAG_ALWAYS_MATRIX | P_FLAG_PER_POINT,
273 &custom_wave->sample, custom_wave->sample_mesh, 1.0, 0.0, 0.0)) == NULL) {
274 free_custom_wave(custom_wave);
278 if (insert_param(param, custom_wave->param_tree) < 0) {
279 printf("failed to insert sample\n");
280 free_custom_wave(custom_wave);
284 if ((param = new_param_double("value1", P_FLAG_READONLY | P_FLAG_DONT_FREE_MATRIX | P_FLAG_ALWAYS_MATRIX | P_FLAG_PER_POINT, &custom_wave->v1, custom_wave->value1, 1.0, -1.0, 0.0)) == NULL) {
285 free_custom_wave(custom_wave);
289 if (insert_param(param, custom_wave->param_tree) < 0) {
290 free_custom_wave(custom_wave);
294 if ((param = new_param_double("value2", P_FLAG_READONLY | P_FLAG_DONT_FREE_MATRIX | P_FLAG_ALWAYS_MATRIX | P_FLAG_PER_POINT, &custom_wave->v2, custom_wave->value2, 1.0, -1.0, 0.0)) == NULL) {
295 free_custom_wave(custom_wave);
299 if (insert_param(param, custom_wave->param_tree) < 0) {
300 free_custom_wave(custom_wave);
304 if ((param = new_param_double("smoothing", P_FLAG_NONE, &custom_wave->smoothing, NULL, 1.0, 0.0, 0.0)) == NULL) {
305 free_custom_wave(custom_wave);
309 if (insert_param(param, custom_wave->param_tree) < 0) {
310 free_custom_wave(custom_wave);
314 if ((param = new_param_double("scaling", P_FLAG_NONE, &custom_wave->scaling, NULL, MAX_DOUBLE_SIZE, 0.0, 1.0)) == NULL) {
315 free_custom_wave(custom_wave);
319 if (insert_param(param, custom_wave->param_tree) < 0) {
320 free_custom_wave(custom_wave);
324 if ((param = new_param_double("t1", P_FLAG_PER_POINT | P_FLAG_TVAR, &custom_wave->t1, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
325 free_custom_wave(custom_wave);
329 if (insert_param(param, custom_wave->param_tree) < 0) {
330 free_custom_wave(custom_wave);
334 if ((param = new_param_double("t2", P_FLAG_PER_POINT |P_FLAG_TVAR, &custom_wave->t2, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
335 free_custom_wave(custom_wave);
339 if (insert_param(param, custom_wave->param_tree) < 0) {
340 free_custom_wave(custom_wave);
344 if ((param = new_param_double("t3", P_FLAG_PER_POINT |P_FLAG_TVAR, &custom_wave->t3, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
345 free_custom_wave(custom_wave);
349 if (insert_param(param, custom_wave->param_tree) < 0) {
350 free_custom_wave(custom_wave);
353 if ((param = new_param_double("t4", P_FLAG_PER_POINT |P_FLAG_TVAR, &custom_wave->t4, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
354 free_custom_wave(custom_wave);
358 if (insert_param(param, custom_wave->param_tree) < 0) {
359 free_custom_wave(custom_wave);
362 if ((param = new_param_double("t5", P_FLAG_TVAR, &custom_wave->t5, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
363 free_custom_wave(custom_wave);
367 if (insert_param(param, custom_wave->param_tree) < 0) {
368 free_custom_wave(custom_wave);
371 if ((param = new_param_double("t6", P_FLAG_TVAR | P_FLAG_PER_POINT, &custom_wave->t6, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
372 free_custom_wave(custom_wave);
376 if (insert_param(param, custom_wave->param_tree) < 0) {
377 free_custom_wave(custom_wave);
380 if ((param = new_param_double("t7", P_FLAG_TVAR | P_FLAG_PER_POINT, &custom_wave->t7, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
381 free_custom_wave(custom_wave);
385 if (insert_param(param, custom_wave->param_tree) < 0) {
386 free_custom_wave(custom_wave);
390 if ((param = new_param_double("t8", P_FLAG_TVAR | P_FLAG_PER_POINT, &custom_wave->t8, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) {
391 free_custom_wave(custom_wave);
395 if (insert_param(param, custom_wave->param_tree) < 0) {
396 free_custom_wave(custom_wave);
400 /* End of parameter loading. Note that the read only parameters associated
401 with custom waves (ie, sample) are global variables, and not specific to
402 the custom wave datastructure. */
409 void destroy_per_frame_init_eqn_tree(splaytree_t * tree) {
414 splay_traverse(free_init_cond, tree);
415 destroy_splaytree(tree);
420 void destroy_per_point_eqn_tree(splaytree_t * tree) {
425 splay_traverse(free_per_point_eqn, tree);
426 destroy_splaytree(tree);
430 void destroy_init_cond_tree(splaytree_t * tree) {
435 splay_traverse(free_init_cond, tree);
436 destroy_splaytree(tree);
440 void destroy_per_frame_eqn_tree(splaytree_t * tree) {
446 splay_traverse(free_per_frame_eqn, tree);
447 destroy_splaytree(tree);
452 void destroy_param_db_tree(splaytree_t * tree) {
457 splay_traverse(free_param, tree);
458 destroy_splaytree(tree);
462 /* Frees a custom wave form object */
463 void free_custom_wave(custom_wave_t * custom_wave) {
465 if (custom_wave == NULL)
468 if (custom_wave->param_tree == NULL)
471 destroy_per_point_eqn_tree(custom_wave->per_point_eqn_tree);
472 destroy_per_frame_eqn_tree(custom_wave->per_frame_eqn_tree);
473 destroy_init_cond_tree(custom_wave->init_cond_tree);
474 destroy_param_db_tree(custom_wave->param_tree);
475 destroy_per_frame_init_eqn_tree(custom_wave->per_frame_init_eqn_tree);
477 free(custom_wave->r_mesh);
478 free(custom_wave->g_mesh);
479 free(custom_wave->b_mesh);
480 free(custom_wave->a_mesh);
481 free(custom_wave->x_mesh);
482 free(custom_wave->y_mesh);
483 free(custom_wave->value1);
484 free(custom_wave->value2);
485 free(custom_wave->sample_mesh);
495 int add_per_point_eqn(char * name, gen_expr_t * gen_expr, custom_wave_t * custom_wave) {
497 per_point_eqn_t * per_point_eqn;
499 param_t * param = NULL;
501 /* Argument checks */
502 if (custom_wave == NULL)
504 if (gen_expr == NULL)
509 if (CUSTOM_WAVE_DEBUG) printf("add_per_point_eqn: per pixel equation (name = \"%s\")\n", name);
511 /* Search for the parameter so we know what matrix the per pixel equation is referencing */
513 if ((param = find_param_db(name, custom_wave->param_tree, TRUE)) == NULL) {
514 if (CUSTOM_WAVE_DEBUG) printf("add_per_point_eqn: failed to allocate a new parameter!\n");
519 /* Find most largest index in the splaytree */
520 if ((per_point_eqn = splay_find_max(custom_wave->per_point_eqn_tree)) == NULL)
523 index = per_point_eqn->index+1;
525 /* Create the per pixel equation given the index, parameter, and general expression */
526 if ((per_point_eqn = new_per_point_eqn(index, param, gen_expr)) == NULL)
528 if (CUSTOM_WAVE_DEBUG)
529 printf("add_per_point_eqn: created new equation (index = %d) (name = \"%s\")\n", per_point_eqn->index, per_point_eqn->param->name);
530 /* Insert the per pixel equation into the preset per pixel database */
531 if (splay_insert(per_point_eqn, &per_point_eqn->index, custom_wave->per_point_eqn_tree) < 0) {
532 free_per_point_eqn(per_point_eqn);
540 per_point_eqn_t * new_per_point_eqn(int index, param_t * param, gen_expr_t * gen_expr) {
542 per_point_eqn_t * per_point_eqn;
546 if (gen_expr == NULL)
549 if ((per_point_eqn = (per_point_eqn_t*)malloc(sizeof(per_point_eqn_t))) == NULL)
553 per_point_eqn->index = index;
554 per_point_eqn->gen_expr = gen_expr;
555 per_point_eqn->param = param;
556 return per_point_eqn;
560 void free_per_point_eqn(per_point_eqn_t * per_point_eqn) {
562 if (per_point_eqn == NULL)
565 free_gen_expr(per_point_eqn->gen_expr);
572 custom_wave_t * find_custom_wave(int id, preset_t * preset, int create_flag) {
574 custom_wave_t * custom_wave = NULL;
579 if ((custom_wave = splay_find(&id, preset->custom_wave_tree)) == NULL) {
581 if (CUSTOM_WAVE_DEBUG) { printf("find_custom_wave: creating custom wave (id = %d)...", id);fflush(stdout);}
583 if (create_flag == FALSE) {
584 if (CUSTOM_WAVE_DEBUG) printf("you specified not to (create flag = false), returning null\n");
588 if ((custom_wave = new_custom_wave(id)) == NULL) {
589 if (CUSTOM_WAVE_DEBUG) printf("failed...out of memory?\n");
593 if (CUSTOM_WAVE_DEBUG) {printf("success.Inserting..."); fflush(stdout);}
595 if (splay_insert(custom_wave, &custom_wave->id, preset->custom_wave_tree) < 0) {
596 if (CUSTOM_WAVE_DEBUG) printf("failed!\n");
597 free_custom_wave(custom_wave);
601 if (CUSTOM_WAVE_DEBUG) printf("done.\n");
607 /* Interface function. Makes another custom wave the current
608 concern for per frame / point equations */
609 custom_wave_t * nextCustomWave() {
611 if ((interface_wave = splay_find(&interface_id, active_preset->custom_wave_tree)) == NULL) {
618 /* Evaluate all per frame equations associated with this wave */
619 splay_traverse(eval_per_frame_eqn, interface_wave->per_frame_eqn_tree);
620 return interface_wave;
624 void evalPerPointEqns() {
628 for (x = 0; x < interface_wave->samples; x++)
629 interface_wave->r_mesh[x] = interface_wave->r;
630 for (x = 0; x < interface_wave->samples; x++)
631 interface_wave->g_mesh[x] = interface_wave->g;
632 for (x = 0; x < interface_wave->samples; x++)
633 interface_wave->b_mesh[x] = interface_wave->b;
634 for (x = 0; x < interface_wave->samples; x++)
635 interface_wave->a_mesh[x] = interface_wave->a;
636 for (x = 0; x < interface_wave->samples; x++)
637 interface_wave->x_mesh[x] = interface_wave->x;
638 for (x = 0; x < interface_wave->samples; x++)
639 interface_wave->y_mesh[x] = interface_wave->y;
642 /* Evaluate per pixel equations */
643 splay_traverse(evalPerPointEqn, interface_wave->per_point_eqn_tree);
649 /* Evaluates a per point equation for the current custom wave given by interface_wave ptr */
650 static inline void evalPerPointEqn(per_point_eqn_t * per_point_eqn) {
654 double * param_matrix;
655 gen_expr_t * eqn_ptr;
657 samples = interface_wave->samples;
658 eqn_ptr = per_point_eqn->gen_expr;
660 if (per_point_eqn->param->matrix == NULL) {
661 if ((param_matrix = per_point_eqn->param->matrix = malloc(size = samples*sizeof(double))) == NULL)
663 memset(param_matrix, 0, size);
666 param_matrix = (double*)per_point_eqn->param->matrix;
668 for (mesh_i = 0; mesh_i < samples; mesh_i++) {
669 param_matrix[mesh_i] = eval_gen_expr(eqn_ptr);
672 /* Now that this parameter has been referenced with a per
673 point equation, we let the evaluator know by setting
675 per_point_eqn->param->matrix_flag = 1;
680 void load_unspecified_init_conds(custom_wave_t * custom_wave) {
682 interface_wave = custom_wave;
683 splay_traverse(load_unspec_init_cond, interface_wave->param_tree);
684 interface_wave = NULL;
688 void load_unspec_init_cond(param_t * param) {
690 init_cond_t * init_cond;
693 /* Don't count these parameters as initial conditions */
694 if (param->flags & P_FLAG_READONLY)
696 if (param->flags & P_FLAG_QVAR)
698 if (param->flags & P_FLAG_TVAR)
700 if (param->flags & P_FLAG_USERDEF)
703 /* If initial condition was not defined by the preset file, force a default one
704 with the following code */
705 if ((init_cond = splay_find(param->name, interface_wave->init_cond_tree)) == NULL) {
707 /* Make sure initial condition does not exist in the set of per frame initial equations */
708 if ((init_cond = splay_find(param->name, interface_wave->per_frame_init_eqn_tree)) != NULL)
711 if (param->type == P_TYPE_BOOL)
712 init_val.bool_val = 0;
714 else if (param->type == P_TYPE_INT)
715 init_val.int_val = *(int*)param->engine_val;
717 else if (param->type == P_TYPE_DOUBLE)
718 init_val.double_val = *(double*)param->engine_val;
720 //printf("%s\n", param->name);
721 /* Create new initial condition */
722 if ((init_cond = new_init_cond(param, init_val)) == NULL)
725 /* Insert the initial condition into this presets tree */
726 if (splay_insert(init_cond, init_cond->param->name, interface_wave->init_cond_tree) < 0) {
727 free_init_cond(init_cond);
735 void evalCustomWaveInitConditions() {
736 splay_traverse(eval_custom_wave_init_conds, active_preset->custom_wave_tree);