-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
2938 lines (2482 loc) · 88 KB
/
main.c
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//FIXME /usr/lib/ladspa/fm_osc_1415.so fmOsc crashes -- maybe need to check that input buffer has ok values??? stupid shit -- it should be the one checking these
//FIXME maybe add effects turned off (some ladspa are buggy with defaults)
#include <dlfcn.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <alsa/asoundlib.h>
#include <ladspa.h>
#include <pthread.h>
#include <gtk/gtk.h>
#include <time.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include <libgen.h>
#include "common.h"
#include "common-string.h"
#include "builtin-effect.h"
#include "tuneit.h"
#include "main.h"
#include "sound.h"
#define LADSPA_PLUGIN_DIR "/usr/lib/ladspa"
#define MAX_LINE 1024
#define DEFAULT_CONFIG ".gtrfx"
#define MAX_EFFECT_BANKS 256
#define EFFECT_BUILTIN 0
#define EFFECT_LADSPA 1
#define MIX_REPLACE 0
#define MIX_ADD 1
/////////////////////////////////////////
#define OSCIL_WIDTH 300
#define OSCIL_HEIGHT 200
static builtin_effect_t builtin_effects[] = {
{ FX_GAIN, be_gain_init },
{ FX_LINEAR_GAIN, be_linear_gain_init },
{ FX_HARD_CLIP, be_hard_clip_init },
{ FX_SOFT_CLIP, be_soft_clip_init },
{ FX_HARD_GATE, be_hard_gate_init },
{ FX_CLIP_DETECT, be_clip_detect_init },
{ FX_MONO_TO_STEREO, be_mono_to_stereo_init },
{ FX_WAV_FILE_INPUT, be_wav_file_input_init },
{ FX_WAV_FILE_OUTPUT, be_wav_file_output_init },
{ FX_OGG_FILE_OUTPUT, be_ogg_file_output_init },
{ FX_SOUND_PIPE_INPUT, be_sound_pipe_input_init },
{ FX_TRIANGLE, be_triangle_init },
{ FX_SAW, be_saw_init },
{ FX_ONLYMINMAX, be_onlyminmax_init },
{ FX_ARCTAN_DISTORTION, be_arctan_distortion_init },
{ FX_BANANA_DISTORTION, be_banana_distortion_init },
{ FX_DCOFFSET, be_dcoffset_init },
{ FX_VALVE_DISTORTION, be_valve_distortion_init },
{ FX_FALLING_CLIP, be_falling_clip_init },
{ FX_CONV, be_conv_init },
};
static int numBuiltinEffects;
typedef struct gui_s gui_t;
struct gui_s
{
GtkWidget *window;
GtkWidget *swindow;
GtkWidget *effectVbox;
//GtkWidget *darea;
GtkWidget *addEffectButton;
GtkWidget *writeConfigButton;
GtkWidget *clampLadspaInputButton;
GtkWidget *tunerButton;
GtkWidget *muteButton;
GtkWidget *effectListWindow;
};
static gui_t gui;
typedef struct effect_s effect_t;
struct effect_s
{
int valid;
int on; // on or off
int type; // builtin, ladspa, etc..
int mixType; // add or replace
float mixvaldb; // if mixType == MIX_ADD
int id; // for builtin effects
int nInPorts; // control ports doesn't include audio ports,
int nOutPorts;
int inChannels;
int outChannels;
float ifval[256]; //FIXME dynamic?
//void **ival;
//int **itype;
float iDefaultVal[256];
int ihasmin[256];
int ihasmax[256];
float imin[256];
float imax[256];
float itoggled[256];
float iIsLog[256];
float iIsInt[256];
float ofval[256];
int inputToPort[256];
int outputToPort[256];
int inChannelToPort[256];
int outChannelToPort[256];
int inputBufferIsBounded[256];
float inputBufferMax[256];
float inputBufferMin[256];
//FIXME
char strings[MAX_CHARS][16];
const LADSPA_Descriptor *pdesc;
LADSPA_Handle instanceHandle;
void *libHandle;
char libFname[MAX_CHARS];
char pluginName[MAX_CHARS];
char label[MAX_CHARS];
char name[MAX_CHARS];
effect_t *prev;
effect_t *next;
void *data;
void (*init)(void);
void (*cleanup)(void);
};
static effect_t *effects[MAX_EFFECT_BANKS];
//FIXME gui notebook form for diff banks
//FIXME mutex lock for currentEffectBank?
static int currentEffectBank = 0;
static GMutex *effects_lock;
typedef struct gui_effect_s gui_effect_t;
struct gui_effect_s {
effect_t *effect;
GtkWidget *label;
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *hsep;
GtkWidget *closeButton;
GtkWidget *onOffButton;
GtkWidget *moveUpButton;
GtkWidget *moveDownButton;
GtkWidget *mixTypeButton;
GtkWidget *mixEntry;
};
typedef struct gui_entry_float_s gui_entry_float_t;
struct gui_entry_float_s {
GtkWidget *nameLabel;
GtkWidget *valueLabel;
GtkWidget *entry;
GtkWidget *hbox;
GtkWidget *hscale;
float *val;
};
typedef struct gui_entry_file_s gui_entry_file_t;
struct gui_entry_file_s {
GtkWidget *entry;
GtkWidget *file_select;
char *s;
};
typedef struct plugin_list_s plugin_list_t;
struct plugin_list_s {
int type;
int id;
char libname[MAX_CHARS];
char name[MAX_CHARS];
char description[MAX_CHARS];
plugin_list_t *prev;
plugin_list_t *next;
};
static plugin_list_t *plugins = NULL;
static plugin_list_t *plugin_last = NULL;
typedef struct oscil_s oscil_t;
struct oscil_s {
GtkWidget *darea;
guchar *rgb_buffer;
int width;
int height;
};
typedef struct visuals_s visuals_t;
struct visuals_s {
oscil_t oscil_in;
//FIXME tuner
oscil_t oscil_out;
};
//FIXME don't use PERIODSIZE and PERIODS
// the final audio
float ldata[PERIODSIZE * PERIODS];
float rdata[PERIODSIZE * PERIODS];
float bufDataIn[BUFDATA_SIZE];
int bufDataPosIn;
float bufDataOut[BUFDATA_SIZE];
int bufDataPosOut;
// this is a dummy channel for plugins that have more than left right and we
// want to ignore
static float dummydata[PERIODSIZE * PERIODS];
// these are the tmp buffers for filters which then are mixed
static float lindata[PERIODSIZE * PERIODS];
static float rindata[PERIODSIZE * PERIODS];
static float loutdata[PERIODSIZE * PERIODS];
static float routdata[PERIODSIZE * PERIODS];
//FIXME take out
unsigned int SampleRate = 48000;
//unsigned int SampleRate = 44100;
#define FREQ_TABLE_MAX 96
static float freq_table[FREQ_TABLE_MAX];
int programExit = FALSE;
static int optionClampLadspaInput = FALSE;
int optionTuner = FALSE;
static int optionMute = FALSE;
int EffectsReady = FALSE;
char title[MAX_CHARS];
///////////////////////////////////////
static void get_ladspa_default_input_value_bounds_and_type (const LADSPA_Descriptor *desc, int inPort, effect_t *eff);
static effect_t *ladspa_effect_new (const char *libname, const char *label);
static void gui_add_ladspa_effect (effect_t *e, int pos);
static void gui_add_builtin_effect (effect_t *e, int pos);
static effect_t *builtin_effect_new (int id);
// returns TRUE even for stupid errors -- fuck that plugin
static int ladspa_plugin_bad (const LADSPA_Descriptor *pdesc)
{
int i;
if (!pdesc->Name) {
fprintf(stderr, "warning no name given for ladspa plugin\n");
return TRUE;
} else {
printf("Name: %s\n", pdesc->Name);
}
if (!pdesc->Maker) {
fprintf(stderr, "warning maker not given for ladspa plugin\n");
return TRUE;
} else {
printf("Maker: %s\n", pdesc->Maker);
}
if (!pdesc->Copyright) {
fprintf(stderr, "warning no copyright for ladspa plugin\n");
return TRUE;
} else {
printf("Copyright: %s\n", pdesc->Copyright);
}
if (pdesc->PortCount <= 0) {
fprintf(stderr, "error, PortCount <= 0 %ld\n", pdesc->PortCount);
return TRUE;
} else {
printf("PortCount: %ld\n", pdesc->PortCount);
}
if (!pdesc->PortDescriptors) {
fprintf(stderr, "error no port descriptors\n");
return TRUE;
} else {
for (i = 0; i < pdesc->PortCount; i++) {
printf("port descriptor %d %d\n", i, pdesc->PortDescriptors[i]);
}
}
if (!pdesc->PortNames) {
fprintf(stderr, "warning no port names\n");
return TRUE;
} else {
for (i = 0; i < pdesc->PortCount; i++) {
if (!pdesc->PortNames[i]) {
fprintf(stderr, "warning port %d has no name\n", i);
return TRUE;
} else {
printf("port %d: %s\n", i, pdesc->PortNames[i]);
}
}
}
if (!pdesc->PortRangeHints) {
fprintf(stderr, "error no port range hints\n");
return TRUE;
} else {
for (i = 0; i < pdesc->PortCount; i++) {
printf("port %d range hint (%d %f -> %f)\n", i, pdesc->PortRangeHints[i].HintDescriptor, pdesc->PortRangeHints[i].LowerBound, pdesc->PortRangeHints[i].UpperBound);
}
}
if (pdesc->ImplementationData) {
//printf("ImplementationData %p don't know what the fuck it's for -- ignoring plugin\n", pdesc->ImplementationData);
//return TRUE;
printf("ImplementationData %p\n", pdesc->ImplementationData);
} else {
printf("ImplementationData %p\n", pdesc->ImplementationData);
}
if (!pdesc->instantiate) {
fprintf(stderr, "error no instantiate()\n");
return TRUE;
} else {
printf("instantiate() %p\n", pdesc->instantiate);
}
if (!pdesc->connect_port) {
fprintf(stderr, "error no connect_port()\n");
return TRUE;
} else {
printf("connect_port() %p\n", pdesc->connect_port);
}
printf("activate() %p\n", pdesc->activate);
if (!pdesc->run) {
fprintf(stderr, "no run()\n");
return TRUE;
} else {
printf("run() %p\n", pdesc->run);
}
printf("run_adding() %p\n", pdesc->run_adding);
printf("set_run_adding_gain() %p\n", pdesc->set_run_adding_gain);
if (pdesc->run_adding && !pdesc->set_run_adding_gain) {
fprintf(stderr, "run_adding() without set_run_adding_gain()\n");
// even though we don't use this, bail anyway
return TRUE;
}
printf("deactivate() %p\n", pdesc->deactivate);
printf("cleanup() %p\n", pdesc->cleanup);
return FALSE;
}
//FIXME maybe pass in the type
#if 1
static effect_t *effect_new (void)
{
effect_t *e;
e = calloc(1, sizeof(effect_t));
if (!e) {
xerror("couldn't allocate effect");
}
return e;
}
#endif
static void effect_prepend_to_bank (effect_t *eff, int bank)
{
effect_t *elist;
effect_t *old;
if (eff == NULL) {
xerror();
}
if (bank >= MAX_EFFECT_BANKS) {
xerror("bank:%d >= MAX_EFFECT_BANKS", bank);
}
g_mutex_lock(effects_lock);
elist = effects[bank];
if (elist == NULL) {
// this will be the first effect
effects[bank] = eff;
eff->prev = NULL;
eff->next = NULL;
g_mutex_unlock(effects_lock);
return;
}
//for (; elist->next != NULL; elist = elist->next) {
// ;
//}
old = elist;
effects[bank] = eff;
eff->prev = NULL;
eff->next = old;
old->prev = eff;
g_mutex_unlock(effects_lock);
}
static void effect_append_to_bank (effect_t *eff, int bank)
{
effect_t *elist;
if (eff == NULL) {
xerror();
}
if (bank >= MAX_EFFECT_BANKS) {
xerror("bank:%d >= MAX_EFFECT_BANKS", bank);
}
g_mutex_lock(effects_lock);
elist = effects[bank];
if (elist == NULL) {
// this will be the first effect
effects[bank] = eff;
eff->prev = NULL;
eff->next = NULL;
g_mutex_unlock(effects_lock);
return;
}
for (; elist->next != NULL; elist = elist->next) {
;
}
elist->next = eff;
eff->prev = elist;
eff->next = NULL; // should already be
g_mutex_unlock(effects_lock);
}
static void effect_remove (effect_t *eff)
{
effect_t *prev, *next;
int i;
if (eff == NULL) {
xerror();
}
g_mutex_lock(effects_lock);
prev = eff->prev;
next = eff->next;
if (prev == NULL) {
for (i = 0; i < MAX_EFFECT_BANKS; i++) {
if (effects[i] == eff) {
printf("reseting bank base %p < %p\n", eff, eff->next);
effects[i] = eff->next;
break;
}
}
if (i >= MAX_EFFECT_BANKS) {
xerror();
}
} else {
prev->next = next;
}
if (next) {
next->prev = prev;
}
g_mutex_unlock(effects_lock);
}
static void effect_free (effect_t *eff)
{
//FIXME free other shit
//printf("FIXME -- free other shit -- pdesc, effect data, etc.\n");
if (eff->type == EFFECT_LADSPA) {
if (eff->pdesc->deactivate) {
eff->pdesc->deactivate(eff->instanceHandle);
}
if (eff->pdesc->cleanup) {
eff->pdesc->cleanup(eff->instanceHandle);
} else {
//printf("no cleanup?? %s\n", eff->name);
}
} else if (eff->type == EFFECT_BUILTIN) {
builtin_effects[eff->id].deactivate(eff->instanceHandle);
builtin_effects[eff->id].cleanup(eff->instanceHandle);
}
if (eff->cleanup) {
eff->cleanup();
}
if (eff->type == EFFECT_LADSPA) {
dlclose(eff->libHandle);
}
if (eff->data) {
free(eff->data);
}
free(eff);
}
static void effect_move_back (effect_t *eff, int bank)
{
effect_t *zero, *one, *two, *three;
if (!eff) {
xerror();
}
g_mutex_lock(effects_lock);
printf("moving back %s\n", eff->name);
if (eff->prev == NULL) { // already at beginning
printf("effect_move_back: at beg, skipping\n");
g_mutex_unlock(effects_lock);
return;
}
if (eff->prev->prev == NULL) { // we will now be the beginning of the list
printf("setting to main head\n");
effects[bank] = eff;
}
// 0 3
// 1 2
zero = one = two = three = NULL;
two = eff; // us
one = eff->prev;
three = eff->next;
zero = one->prev; // one checked for NULL at beginning of function
// 0 3
// 2 1
if (zero) {
zero->next = two;
}
one->prev = two;
one->next = three;
two->prev = zero;
two->next = one;
if (three) {
three->prev = one;
}
g_mutex_unlock(effects_lock);
}
static void effect_move_forward (effect_t *eff, int bank)
{
effect_t *zero, *one, *two, *three;
if (!eff) {
xerror();
}
g_mutex_lock(effects_lock);
printf("moving forward %s\n", eff->name);
if (eff->next == NULL) { // already at beginning
printf("effect_move_forward: at end, skipping\n");
g_mutex_unlock(effects_lock);
return;
}
if (eff->prev == NULL) { // we will now be the beginning of the list
printf("setting next to main head\n");
effects[bank] = eff->next;
}
// 0 3
// 1 2
zero = one = two = three = NULL;
one = eff; // us
zero = one->prev;
two = one->next;
three = two->next; // two checked for NULL at beginning of function
// 0 3
// 2 1
if (zero) {
zero->next = two;
}
one->prev = two;
one->next = three;
two->prev = zero;
two->next = one;
if (three) {
three->prev = one;
}
g_mutex_unlock(effects_lock);
}
// for ladspa name is shortened 'pdesc->Label', and
// description is 'pdesc->Name'
static void plugin_append (const char *libname, const char *name, const char *description, int type, int id)
{
plugin_list_t *p;
p = calloc(1, sizeof(plugin_list_t));
if (!p) {
xerror("couldn't allocate plugin");
}
strncpy(p->libname, libname, MAX_CHARS);
strncpy(p->name, name, MAX_CHARS);
strncpy(p->description, description, MAX_CHARS);
p->type = type;
p->id = id;
if (plugins == NULL) {
p->prev = NULL;
p->next = NULL;
plugins = p;
plugin_last = p;
} else {
plugin_last->next = p;
p->next = NULL;
p->prev = plugin_last;
plugin_last = p;
}
}
static float dB (float d)
{
return(powf(10, d / 20.0f));
}
static void entry_float_activate_cb (GtkWidget *widget, gpointer data)
{
const gchar *etext;
float f;
gui_entry_float_t *entryData;
entryData = (gui_entry_float_t *)data;
etext = gtk_entry_get_text(GTK_ENTRY(widget));
printf("new text: %s\n", etext);
f = (float)strtod(etext, NULL);
//*((float *)data) = f;
*entryData->val = f;
//FIXME the other stuff
gtk_label_set_text(GTK_LABEL(entryData->valueLabel), etext);
//gtk_entry_set_text(GTK_ENTRY(widget), "");
gtk_range_set_value(GTK_RANGE(entryData->hscale), (gdouble)f);
}
static void entry_string_activate_cb (GtkWidget *widget, gpointer data)
{
const gchar *etext;
//float f;
etext = gtk_entry_get_text(GTK_ENTRY(widget));
printf("new text: %s\n", etext);
//f = (float)strtod(etext, NULL);
//*((float *)data) = f;
strncpy((char *)data, etext, MAX_CHARS);
}
static void file_select_ok_button_clicked_cb (GtkWidget *button, gpointer data)
{
gui_entry_file_t *entryData = (gui_entry_file_t *)data;
const gchar *newText;
if (!entryData) {
xerror();
}
newText = gtk_file_selection_get_filename(GTK_FILE_SELECTION(entryData->file_select));
strncpy(entryData->s, newText, MAX_CHARS);
gtk_entry_set_text(GTK_ENTRY(entryData->entry), newText);
}
static void entry_file_select_button_clicked_cb (GtkWidget *widget, gpointer data)
{
gui_entry_file_t *entryData = (gui_entry_file_t *)data;
GtkWidget *fs;
if (!entryData) {
xerror();
}
fs = gtk_file_selection_new("Please select a file");
entryData->file_select = fs;
gtk_file_selection_set_filename(GTK_FILE_SELECTION(fs), gtk_entry_get_text(GTK_ENTRY(entryData->entry)));
gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(fs)->ok_button), "clicked", GTK_SIGNAL_FUNC(file_select_ok_button_clicked_cb), data);
// ensure that the dialog box is destroyed when the user clicks a button
g_signal_connect_swapped (GTK_FILE_SELECTION (fs)->ok_button,
"clicked",
G_CALLBACK (gtk_widget_destroy),
fs);
g_signal_connect_swapped (GTK_FILE_SELECTION (fs)->cancel_button,
"clicked",
G_CALLBACK (gtk_widget_destroy),
fs);
gtk_widget_show(fs);
}
static void close_button_clicked_cb (GtkWidget *button, gpointer data)
{
gui_effect_t *ge = (gui_effect_t *)data;
effect_remove(ge->effect);
effect_free(ge->effect);
gtk_widget_destroy(GTK_WIDGET(ge->vbox));
free(ge);
}
static void on_off_toggle_button_clicked_cb (GtkWidget *button, gpointer data)
{
gui_effect_t *ge = (gui_effect_t *)data;
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
ge->effect->on = TRUE;
gtk_button_set_label(GTK_BUTTON(button), " on");
} else {
ge->effect->on = FALSE;
gtk_button_set_label(GTK_BUTTON(button), "off");
}
}
static void move_up_button_clicked_cb (GtkWidget *button, gpointer data)
{
gui_effect_t *ge = (gui_effect_t *)data;
GList *g;
int i;
int found = FALSE;
effect_move_back(ge->effect, currentEffectBank);
for (i = 0, g = g_list_first(GTK_BOX(gui.effectVbox)->children); g != NULL; g = g_list_next(g), i++) {
printf("%p\n", g->data);
if (((struct _GtkBoxChild *)(g->data))->widget == ge->vbox) {
printf("found at %d\n", i);
found = TRUE;
break;
}
}
if (!found) {
xerror();
}
if (i == 0) { // already at beginning
return;
}
gtk_box_reorder_child(GTK_BOX(gui.effectVbox), ge->vbox, i - 1);
}
static void move_down_button_clicked_cb (GtkWidget *button, gpointer data)
{
gui_effect_t *ge = (gui_effect_t *)data;
GList *g;
int i;
int found = FALSE;
effect_move_forward(ge->effect, currentEffectBank);
g = g_list_last(GTK_BOX(gui.effectVbox)->children);
if (!g) {
xerror();
}
if (((struct _GtkBoxChild *)(g->data))->widget == ge->vbox) {
// already at end
printf("gui already at end of list\n");
return;
}
for (i = 0, g = g_list_first(GTK_BOX(gui.effectVbox)->children); g != NULL; g = g_list_next(g), i++) {
printf("%p\n", g->data);
if (((struct _GtkBoxChild *)(g->data))->widget == ge->vbox) {
printf("found at %d\n", i);
found = TRUE;
break;
}
}
if (!found) {
xerror();
}
gtk_box_reorder_child(GTK_BOX(gui.effectVbox), ge->vbox, i + 1);
}
static void mix_type_button_clicked_cb (GtkWidget *button, gpointer data)
{
gui_effect_t *ge = (gui_effect_t *)data;
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
ge->effect->mixType = MIX_ADD;
gtk_button_set_label(GTK_BUTTON(button), "add");
gtk_widget_show(ge->mixEntry);
} else {
ge->effect->mixType = MIX_REPLACE;
gtk_button_set_label(GTK_BUTTON(button), "rep");
gtk_widget_hide(ge->mixEntry);
}
}
static void mix_entry_activate_cb (GtkWidget *entry, gpointer data)
{
gui_effect_t *ge = (gui_effect_t *)data;
const gchar *etext;
float f;
etext = gtk_entry_get_text(GTK_ENTRY(entry));
printf("new text for mix entry: %s\n", etext);
f = (float)strtod(etext, NULL);
ge->effect->mixvaldb = f;
}
static gui_effect_t *gui_effect_new (effect_t *eff, int pos)
{
gui_effect_t *ge;
GtkWidget *vsep;
char text[MAX_CHARS];
if (!eff) {
xerror();
}
ge = calloc(1, sizeof(gui_effect_t));
if (!ge) {
xerror("couldn't allocate gui effect");
}
ge->effect = eff;
ge->vbox = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(gui.effectVbox), ge->vbox, FALSE, FALSE, 0);
if (pos == -1) { // append
} else if (pos == 0) { // prepend
gtk_box_reorder_child(GTK_BOX(gui.effectVbox), ge->vbox, 0);
} else {
printf("%s:%s FIXME\n", __FILE__, __func__);
}
ge->hbox = gtk_hbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(ge->vbox), ge->hbox, FALSE, FALSE, 0);
ge->closeButton = gtk_button_new_with_label("X");
gtk_box_pack_start(GTK_BOX(ge->hbox), ge->closeButton, FALSE, FALSE, 0);
gtk_signal_connect(GTK_OBJECT(ge->closeButton), "clicked", GTK_SIGNAL_FUNC(close_button_clicked_cb), ge);
vsep = gtk_vseparator_new();
gtk_box_pack_start(GTK_BOX(ge->hbox), vsep, FALSE, TRUE, 10);
//FIXME, pixmaps or something
ge->onOffButton = gtk_toggle_button_new_with_label(" on");
//ge->onOffButton = gtk_check_button_new_with_label(" on");
gtk_box_pack_start(GTK_BOX(ge->hbox), ge->onOffButton, FALSE, FALSE, 0);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ge->onOffButton), ge->effect->on);
gtk_signal_connect(GTK_OBJECT(ge->onOffButton), "clicked", GTK_SIGNAL_FUNC(on_off_toggle_button_clicked_cb), ge);
if (!ge->effect->on) {
gtk_button_set_label(GTK_BUTTON(ge->onOffButton), "off");
}
vsep = gtk_vseparator_new();
gtk_box_pack_start(GTK_BOX(ge->hbox), vsep, FALSE, TRUE, 10);
ge->moveUpButton = gtk_button_new_with_label("^");
gtk_box_pack_start(GTK_BOX(ge->hbox), ge->moveUpButton, FALSE, FALSE, 0);
gtk_signal_connect(GTK_OBJECT(ge->moveUpButton), "clicked", GTK_SIGNAL_FUNC(move_up_button_clicked_cb), ge);
vsep = gtk_vseparator_new();
gtk_box_pack_start(GTK_BOX(ge->hbox), vsep, FALSE, TRUE, 10);
ge->moveDownButton = gtk_button_new_with_label("\\/");
gtk_box_pack_start(GTK_BOX(ge->hbox), ge->moveDownButton, FALSE, FALSE, 0);
gtk_signal_connect(GTK_OBJECT(ge->moveDownButton), "clicked", GTK_SIGNAL_FUNC(move_down_button_clicked_cb), ge);
vsep = gtk_vseparator_new();
gtk_box_pack_start(GTK_BOX(ge->hbox), vsep, FALSE, TRUE, 10);
if (ge->effect->mixType == MIX_REPLACE) {
ge->mixTypeButton = gtk_toggle_button_new_with_label("rep");
} else {
ge->mixTypeButton = gtk_toggle_button_new_with_label("add");
}
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ge->mixTypeButton), ge->effect->mixType == MIX_ADD);
gtk_box_pack_start(GTK_BOX(ge->hbox), ge->mixTypeButton, FALSE, FALSE, 0);
gtk_signal_connect(GTK_OBJECT(ge->mixTypeButton), "clicked", GTK_SIGNAL_FUNC(mix_type_button_clicked_cb), ge);
ge->mixEntry = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(ge->hbox), ge->mixEntry, FALSE, FALSE, 0);
snprintf(text, MAX_CHARS, "%f", ge->effect->mixvaldb);
gtk_entry_set_text(GTK_ENTRY(ge->mixEntry), text);
gtk_signal_connect(GTK_OBJECT(ge->mixEntry), "activate", GTK_SIGNAL_FUNC(mix_entry_activate_cb), ge);
ge->hsep = gtk_hseparator_new();
gtk_box_pack_end(GTK_BOX(ge->vbox), ge->hsep, FALSE, TRUE, 10);
gtk_widget_show_all(ge->hsep);
gtk_widget_show_all(ge->vbox);
//FIXME hack
if (ge->effect->mixType == MIX_REPLACE) {
gtk_widget_hide(ge->mixEntry);
}
return ge;
}
static void gui_add_label (gui_effect_t *ge, const char *name)
{
if (!ge) {
xerror();
}
strncpy(ge->effect->name, name, MAX_CHARS);
ge->label = gtk_label_new(name);
gtk_box_pack_start(GTK_BOX(ge->vbox), ge->label, TRUE, FALSE, 0);
//gtk_box_pack_start(GTK_BOX(ge->hbox), ge->label, TRUE, FALSE, 0);
gtk_widget_show_all(ge->label);
}
static gint hscale_float_value_changed_cb (GtkWidget *widget, gpointer data)
{
gui_entry_float_t *entryData;
float oldVal;
float newVal;
//float adjVal;
entryData = (gui_entry_float_t *)data;
oldVal = *entryData->val;
newVal = (float)gtk_range_get_value(GTK_RANGE(entryData->hscale));
printf("hscale value changed: %f (from %f)\n", newVal, oldVal);
return TRUE;
}
static gint entry_float_destroy_cb (GtkWidget *widget, gpointer data)
{
printf("float entry destroy cb %p %p\n", widget, data);
g_free(data);
return FALSE;
}
static gint entry_file_select_button_destroy_cb (GtkWidget *widget, gpointer data)
{
//printf("entry file select button destroy cb\n");
g_free(data);
return FALSE;
}
static void gui_add_entry_float (gui_effect_t *ge, const char *name, float *val, int portNum)
{
//GtkWidget *label;
//GtkWidget *entry;
//GtkWidget *hbox;
//double min, max;
char text[MAX_CHARS];
gui_entry_float_t *entryData = NULL;
if (!ge) {
xerror();
}
entryData = g_malloc(sizeof(gui_entry_float_t));
if (!entryData) {
printf("couldn't allocate entryData\n");