/
gneutronica.c
5888 lines (5215 loc) · 192 KB
/
gneutronica.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
/*
(C) Copyright 2005,2006, Stephen M. Cameron.
This file is part of Gneutronica.
Gneutronica is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Gneutronica is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Gneutronica; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define COPYRIGHT "(c) Copyright 2005,2006, Stephen M. Cameron\n\n" \
"This program is free software; you can redistribute it and/or modify it under the terms\n" \
"of the GNU General Public License as published by the Free Software Foundation; either \n" \
"version 2 of the License, or (at your option) any later version.\n" \
"\n" \
"This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;\n" \
"without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" \
"See the GNU General Public License for more details.\n\n" \
"You should have received a copy of the GNU General Public License along with this program;\n" \
"if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,\n" \
"Boston, MA 02111-1307 USA\n"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#define UNUSED __attribute__((unused))
extern double trunc(double x); /* math.h doesn't have this? What? */
#include <signal.h>
#include <setjmp.h>
#include <netinet/in.h> /* . . . just for for htons() */
#include <sys/mman.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
// #define GNEUTRONICA_FRENCH 1
#include "lang.h"
#define INSTANTIATE_GNEUTRONICA_GLOBALS
#include "gneutronica.h"
#include "sched.h"
#include "old_fileformats.h"
#include "fractions.h"
#include "drumtab.h"
#include "midi_reader.h"
#include "midioutput.h"
#include "midioutput_raw.h"
#include "midioutput_alsa.h"
/* struct midi_method *midi = &midi_method_raw; */
struct midi_method *midi = &midi_method_alsa;
struct midi_handle *midi_handle = NULL;
#define MIDI_CHANNEL (drumkit[kit].midi_channel & 0x0f)
#include "version.h"
/* commands to send player process */
#define PLAY_ONCE 0
#define PLAY_LOOP 1
#define PLAYER_QUIT 2
#define PERFORM_PATCH_CHANGE 4
void load_button_clicked(GtkWidget *widget, gpointer data);
void save_button_clicked(GtkWidget *widget, gpointer data);
void import_patterns_button_clicked(GtkWidget *widget, gpointer data);
void import_drumtab_button_clicked(GtkWidget *widget, gpointer data);
void factor_drumtab_button_clicked(GtkWidget *widget, gpointer data);
static void paste_drumtab_clicked(GtkWidget *widget, gpointer data);
int about_activate(GtkWidget *widget, gpointer data);
void export_midi_button_clicked(GtkWidget *widget, gpointer data);
void remap_drumkit_clicked(UNUSED GtkWidget *widget, UNUSED gpointer data);
void trigger_about_activate(GtkWidget *widget, gpointer data);
void destroy_event(GtkWidget *widget, gpointer data);
void widget_exclude_keypress(GtkWidget *w);
int unflatten_pattern(int ckit, int cpattern);
int translate_drumtab_data(int factor);
void pattern_record_button_clicked(GtkWidget *widget, gpointer data);
void pattern_play_button_clicked(GtkWidget *widget, gpointer data);
int import_patterns_from_file(const char *filename);
int save_to_file(const char *filename);
int export_to_midi_file(const char *filename);
int find_tempo(int measure);
static void remove_tempo_change(int index);
static void redraw_arranger(void);
int insert_tempo_change(int measure, int tempo);
static int get_drawing_width(void)
{
if (pattern_scroller)
return (int) (pattern_scroller->allocation.width * 0.8);
return 800;
}
#define DRAW_WIDTH (get_drawing_width())
/* Main menu items. Almost all of this menu code was taken verbatim from the
gtk tutorial at http://www.gtk.org/tutorial/sec-itemfactoryexample.html
I tweaked it tweaked a bit for style and menu content, but that's about it.*/
static GtkItemFactoryEntry menu_items[] = {
{ "/" FILE_LABEL, NULL, NULL, 0, "<Branch>", NULL },
{ "/" FILE_LABEL "/_" OPEN_LABEL, "<control>O", load_button_clicked, 0, "<StockItem>", GTK_STOCK_OPEN },
{ "/" FILE_LABEL "/_" SAVE_LABEL, "<control>S", save_button_clicked, 0, "<StockItem>", GTK_STOCK_SAVE },
{ "/" FILE_LABEL "/" SAVE_AS_LABEL, NULL, save_button_clicked, 0, "<Item>", NULL },
{ "/" FILE_LABEL "/sep1", NULL, NULL, 0, "<Separator>", NULL },
{ "/" FILE_LABEL "/" IMPORT_PATTERNS_LABEL, NULL, import_patterns_button_clicked, 0, "<Item>", NULL },
{ "/" FILE_LABEL "/" IMPORT_DRUM_TAB_LABEL, NULL, import_drumtab_button_clicked, 0, "<Item>", NULL },
{ "/" FILE_LABEL "/" EXPORT_TO_MIDI_FILE_LABEL, NULL, export_midi_button_clicked, 0, "<Item>", NULL },
/* { "/File/_Quit", "<CTRL>Q", gtk_main_quit, 0, "<StockItem>", GTK_STOCK_QUIT }, */
{ "/" FILE_LABEL "/" QUIT_LABEL, "<CTRL>Q", destroy_event, 0, "<StockItem>", GTK_STOCK_QUIT },
{ "/" EDIT_LABEL, NULL, NULL, 0, "<Branch>", NULL },
{ "/" EDIT_LABEL "/" PASTE_DRUM_TAB_LABEL, NULL, paste_drumtab_clicked, 0, "<Item>", NULL },
{ "/" EDIT_LABEL "/" REMAP_DRUM_KIT_MENU_LABEL, NULL, remap_drumkit_clicked, 0, "<Item>", NULL },
{ "/" HELP_LABEL, NULL, NULL, 0, "<LastBranch>", NULL },
{ "/" HELP_LABEL "/" ABOUT_LABEL, NULL, trigger_about_activate, 0, "<Item>", NULL },
};
static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
GtkWidget *main_menubar, *main_popup_button, *main_option_menu;
/* Returns a menubar widget made from the above menu */
static GtkWidget *get_menubar_menu( GtkWidget *window )
{
GtkItemFactory *item_factory;
GtkAccelGroup *accel_group;
/* Make an accelerator group (shortcut keys) */
accel_group = gtk_accel_group_new ();
/* Make an ItemFactory (that makes a menubar) */
item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>",
accel_group);
/* This function generates the menu items. Pass the item factory,
the number of items in the array, the array itself, and any
callback data for the the menu items. */
gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);
/* Attach the new accelerator group to the window. */
gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
/* Finally, return the actual menu bar created by the item factory. */
return gtk_item_factory_get_widget (item_factory, "<main>");
}
/* Popup the menu when the popup button is pressed */
static gboolean popup_cb(UNUSED GtkWidget *widget, GdkEvent *event, GtkWidget *menu)
{
GdkEventButton *bevent = (GdkEventButton *)event;
/* Only take button presses */
if (event->type != GDK_BUTTON_PRESS)
return FALSE;
/* Show the menu */
gtk_menu_popup (GTK_MENU(menu), NULL, NULL,
NULL, NULL, bevent->button, bevent->time);
return TRUE;
}
/* Same as with get_menubar_menu() but just return a button with a signal to
call a popup menu */
GtkWidget *get_popup_menu( void )
{
GtkItemFactory *item_factory;
GtkWidget *button, *menu;
/* Same as before but don't bother with the accelerators */
item_factory = gtk_item_factory_new (GTK_TYPE_MENU, "<main>", NULL);
gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);
menu = gtk_item_factory_get_widget (item_factory, "<main>");
/* Make a button to activate the popup menu */
button = gtk_button_new_with_label ("Popup");
/* Make the menu popup when clicked */
g_signal_connect (G_OBJECT(button), "event", G_CALLBACK(popup_cb),
(gpointer) menu);
return button;
}
/* Same again but return an option menu */
GtkWidget *get_option_menu( void )
{
GtkItemFactory *item_factory;
GtkWidget *option_menu;
/* Same again, not bothering with the accelerators */
item_factory = gtk_item_factory_new (GTK_TYPE_OPTION_MENU, "<main>", NULL);
gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);
option_menu = gtk_item_factory_get_widget (item_factory, "<main>");
return option_menu;
}
/* . . . End of code copied from gtk tutorial. */
struct filedata {
char *filename;
FILE *f;
int linecount;
char buffer[1024];
char *str;
};
FILE *open_file(struct filedata *f, char *filename, char *mode)
{
f->filename = filename;
memset(f->buffer, 0, sizeof(f->buffer));
f->str = NULL;
f->linecount = 0;
f->f = fopen(f->filename, mode);
return f->f;
}
void close_file(struct filedata *f)
{
fclose(f->f);
f->filename = NULL;
f->f = NULL;
f->linecount = 0;
memset(f->buffer, 0, sizeof(f->buffer));
f->str = NULL;
}
char *get_next_line(struct filedata *f)
{
errno = 0;
do {
f->str = fgets(f->buffer, sizeof(f->buffer), f->f);
if (!f->str) {
if (errno == EINTR)
continue;
if (!feof(f->f) || errno != 0) /* end of file */
fprintf(stderr, "Error reading '%s': %s\n", f->filename, strerror(errno));
f->str = NULL;
return NULL;
}
f->linecount++;
} while (f->buffer[0] == '#'); /* skip comments */
f->str = f->buffer;
return f->str;
}
int read_drumkit_fileformat_1_or_2(struct filedata *f, int *ndrumkits,
struct drumkit_struct *drumkit, int format)
{
struct drumkit_struct *dk;
int i;
int rc, n;
if (format == 1)
fprintf(stderr, "Drumkit file %s is old file format version %d\n"
"and contains no instrument mapping info\n",
f->filename, format);
dk = &drumkit[*ndrumkits];
dk->ninsts = 0;
f->str = get_next_line(f);
rc = sscanf(f->str, "%[^,]%*c %[^,]%*c %[^\n]%*c", dk->make, dk->model, dk->name);
/* g_print("rc = %d\n", rc); */
if (rc != 3) {
fprintf(stderr, "Error in %s (make/model/name) at line %d, rc = %d\n", f->filename, f->linecount, rc);
close_file(f);
return -1;
}
fprintf(stderr, "Reading drumkit %s %s %s\n", dk->make, dk->model, dk->name);
dk->instrument = malloc(sizeof(struct instrument_struct)*MAXINSTS);
if (dk->instrument == NULL) {
fprintf(stderr, "Out of memory\n");
close_file(f);
return -1;
}
memset(dk->instrument, 0, sizeof(struct instrument_struct)*MAXINSTS);
for (i=0;i<MAXINSTS;i++) {
strcpy(dk->instrument[i].name, "x");
strcpy(dk->instrument[i].type, "x");
dk->instrument[i].midivalue = i;
dk->instrument[i].gm_equivalent = i;
dk->instrument[i].hit = NULL;
dk->instrument[i].button = NULL;
dk->instrument[i].hidebutton = NULL;
dk->instrument[i].canvas = NULL;
dk->instrument[i].instrument_num = i;
}
n = 0;
while (!feof(f->f)) {
f->str = get_next_line(f);
if (!f->str)
break;
switch(format) {
case 2: rc = sscanf(f->str, "%[^,]%*c %[^,]%*c %hhu %d\n",
dk->instrument[n].name,
dk->instrument[n].type,
&dk->instrument[n].midivalue,
&dk->instrument[n].gm_equivalent);
if (rc != 4) {
fprintf(stderr, "Error in %s instrument at line %d, rc = %d\n", f->filename, f->linecount, rc);
close_file(f);
return -1;
}
break;
case 1: rc = sscanf(f->str, "%[^,]%*c %[^,]%*c %hhu\n",
dk->instrument[n].name,
dk->instrument[n].type,
&dk->instrument[n].midivalue);
if (rc != 3) {
fprintf(stderr, "Error in %s at line %d\n", f->filename, f->linecount);
close_file(f);
return -1;
}
dk->instrument[n].gm_equivalent = -1;
break;
default:fprintf(stderr, "Error in %s at line %d\n", f->filename, f->linecount);
close_file(f);
return -1;
}
dk->instrument[n].instrument_num = n;
n++;
if (n < MAXINSTS) {
dk->instrument[n].hit = NULL;
dk->instrument[n].button = NULL;
dk->instrument[n].hidebutton = NULL;
dk->instrument[n].canvas = NULL;
}
}
dk->ninsts = n;
(*ndrumkits)++;
close_file(f);
return 0;
}
int make_default_drumkit(int *ndrumkits, struct drumkit_struct *drumkit)
{
struct drumkit_struct *dk;
int i;
dk = &drumkit[*ndrumkits];
dk->ninsts = MAXINSTS;
strcpy(dk->make, "No name");
strcpy(dk->model, "No name");
strcpy(dk->name, "No name");
if (dk->instrument != NULL)
free(dk->instrument);
dk->instrument = malloc(sizeof(struct instrument_struct)*MAXINSTS);
if (dk->instrument == NULL) {
fprintf(stderr, "Out of memory\n");
return -1;
}
memset(dk->instrument, 0, sizeof(struct instrument_struct)*MAXINSTS);
for (i=0;i<MAXINSTS;i++) {
/* No space in name so editing later is easier, double click instead of triple click
to highlight and replace with real name. */
sprintf(dk->instrument[i].name, "Instrument%d", i);
sprintf(dk->instrument[i].type, "description");
dk->instrument[i].midivalue = i;
dk->instrument[i].instrument_num = i;
dk->instrument[i].gm_equivalent = -1;
dk->instrument[i].hit = NULL;
dk->instrument[i].button = NULL;
dk->instrument[i].hidebutton = NULL;
dk->instrument[i].canvas = NULL;
}
(*ndrumkits)++;
return 0;
}
#define DEFAULT_DRUMKIT_DIR "/usr/local/share/gneutronica/drumkits"
int read_drumkit(char *filename, int *ndrumkits, struct drumkit_struct *drumkit)
{
int rc;
int fileformat;
char realfilename[PATH_MAX + 1];
struct stat statbuf;
struct filedata f;
strncpy(realfilename, filename, PATH_MAX);
rc = stat(realfilename, &statbuf);
if (rc != 0 && (strlen(DEFAULT_DRUMKIT_DIR) + strlen(filename) + 2) < PATH_MAX) {
/* try putting DEFAULT_DRUMKIT_DIR n the front and see if that helps. */
sprintf(realfilename, "%s/%s", DEFAULT_DRUMKIT_DIR, filename);
rc = stat(realfilename, &statbuf);
if (rc != 0) /* Nope... put it back, and let it fail normally */
strncpy(realfilename, filename, PATH_MAX);
}
if (*ndrumkits >= MAXKITS)
return -1;
f.f = open_file(&f, realfilename, "r");
if (f.f == NULL) {
fprintf(stderr, "Can't open %s: %s\n", f.filename, strerror(errno));
return -1;
}
get_next_line(&f);
if (!f.str)
return -1;
rc = sscanf(f.str, "Gneutronica drumkit file format %d\n", &fileformat);
if (rc != 1) {
printf("%s does not appear to be a Gneutronica drumkit file\n", filename);
close_file(&f);
return -1;
}
switch (fileformat) {
case 1:
case 2: rc = read_drumkit_fileformat_1_or_2(&f, ndrumkits, drumkit, fileformat);
break;
default: printf("Unknown drumkit fileformat version %d\n", fileformat);
rc = -1;
break;
}
return rc;
}
static int save_drumkit_to_file(const char *filename)
{
struct drumkit_struct *dk;
FILE *f;
int i, fileformat = 2;
printf("save_drumkit_to_file called\n");
dk = &drumkit[kit];
f = fopen(filename, "w");
if (f == NULL)
return -1;
fprintf(f, "Gneutronica drumkit file format %d\n", fileformat);
fprintf(f, "%s, %s, %s\n", dk->make, dk->model, dk->name);
for (i=0;i<dk->ninsts;i++) {
fprintf(f, "%s, %s, %d %d\n",
dk->instrument[i].name,
dk->instrument[i].type,
dk->instrument[i].midivalue,
dk->instrument[i].gm_equivalent);
}
fclose(f);
return 0;
}
void destroy_event(GtkWidget *widget, gpointer data);
void cleanup_tempo_changes(void);
void tempo_change_ok_button(UNUSED GtkWidget *widget, UNUSED gpointer data)
{
int new_tempo;
printf("Tempo change ok button\n");
new_tempo = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(TempoChBPM));
insert_tempo_change(changing_tempo_measure, new_tempo);
cleanup_tempo_changes();
gtk_widget_queue_draw(Tempo_da);
gtk_widget_hide(TempoChWin);
}
void tempo_change_cancel_button(UNUSED GtkWidget *widget, UNUSED gpointer data)
{
printf("Tempo change cancel button\n");
cleanup_tempo_changes();
gtk_widget_hide(TempoChWin);
gtk_widget_queue_draw(Tempo_da);
}
void tempo_change_delete_button(UNUSED GtkWidget *widget, UNUSED gpointer data)
{
int index;
printf("Tempo change delete button\n");
index = insert_tempo_change(changing_tempo_measure, -1);
if (index > 0)
tempo_change[index].beats_per_minute = tempo_change[index-1].beats_per_minute;
cleanup_tempo_changes();
gtk_widget_hide(TempoChWin);
gtk_widget_queue_draw(Tempo_da);
}
void destroy_means_hide(GtkWidget *widget, UNUSED gpointer data)
{
/* hmm, this doesn't really work, the next time the widget is activated... boom! */
gtk_widget_hide(widget);
}
void make_tempo_change_editor(void)
{
TempoChWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
TempoChvbox1 = gtk_vbox_new(FALSE, 0);
TempoChhbox1 = gtk_hbox_new(FALSE, 0);
TempoChhbox2 = gtk_hbox_new(FALSE, 0);
TempoChLabel = gtk_label_new("Measure:xxx Beats/Minute:");
TempoChBPM = gtk_spin_button_new_with_range(MINTEMPO, MAXTEMPO, 1);
widget_exclude_keypress(TempoChBPM);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(TempoChBPM), (gdouble) 120);
TempoChOk = gtk_button_new_with_label("Ok");
g_signal_connect(G_OBJECT (TempoChOk), "clicked",
G_CALLBACK (tempo_change_ok_button), NULL);
TempoChCancel = gtk_button_new_with_label("Cancel");
g_signal_connect(G_OBJECT (TempoChCancel), "clicked",
G_CALLBACK (tempo_change_cancel_button), NULL);
TempoChDelete = gtk_button_new_with_label("Delete tempo change");
g_signal_connect(G_OBJECT (TempoChDelete), "clicked",
G_CALLBACK (tempo_change_delete_button), NULL);
g_signal_connect(G_OBJECT (TempoChWin), "delete_event",
// G_CALLBACK (delete_event), NULL);
G_CALLBACK (destroy_means_hide), NULL);
g_signal_connect(G_OBJECT (TempoChWin), "destroy",
G_CALLBACK (destroy_means_hide), NULL);
gtk_container_set_border_width(GTK_CONTAINER (TempoChWin), 15);
gtk_container_add(GTK_CONTAINER (TempoChWin), TempoChvbox1);
gtk_box_pack_start(GTK_BOX(TempoChvbox1), TempoChhbox1, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(TempoChvbox1), TempoChhbox2, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(TempoChhbox1), TempoChLabel, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(TempoChhbox1), TempoChBPM, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(TempoChhbox2), TempoChOk, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(TempoChhbox2), TempoChCancel, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(TempoChhbox2), TempoChDelete, TRUE, TRUE, 0);
gtk_widget_show(TempoChOk);
gtk_widget_show(TempoChCancel);
gtk_widget_show(TempoChDelete);
gtk_widget_show(TempoChhbox2);
gtk_widget_show(TempoChLabel);
gtk_widget_show(TempoChBPM);
gtk_widget_show(TempoChhbox1);
gtk_widget_show(TempoChvbox1);
// gtk_widget_show(TempoChWin);
}
static int arr_darea_clicked(GtkWidget *w, GdkEventButton *event,
struct pattern_struct *data);
static int arr_darea_event(GtkWidget *w, GdkEvent *event, struct pattern_struct *p);
static int arr_darea_button_press(GtkWidget *w, GdkEventButton *event,
struct pattern_struct *data);
void clear_hitpattern(struct hitpattern *p);
void free_pattern(struct pattern_struct *p)
{
if (p == NULL)
return;
if (p->arr_darea != NULL) {
gtk_widget_destroy(p->arr_darea);
// gtk_object_unref(GTK_OBJECT(p->arr_darea));
}
if (p->arr_button != NULL)
gtk_widget_destroy(GTK_WIDGET(p->arr_button));
if (p->copy_button != NULL)
gtk_widget_destroy(GTK_WIDGET(p->copy_button));
if (p->del_button != NULL)
gtk_widget_destroy(GTK_WIDGET(p->del_button));
if (p->ins_button != NULL)
gtk_widget_destroy(GTK_WIDGET(p->ins_button));
// gtk_object_unref(GTK_OBJECT(p->arr_button));
/* FIXME: shouldn't we call clear_hitpattern(p->hitpattern) here? */
clear_hitpattern(p->hitpattern);
free(p);
}
void edit_pattern(int new_pattern);
void edit_pattern_clicked(GtkWidget *widget,
struct pattern_struct *data);
void ins_pattern_button_pressed(GtkWidget *widget,
struct pattern_struct *data);
void del_pattern_button_pressed(GtkWidget *widget,
struct pattern_struct *data);
void copy_pattern_button_pressed(GtkWidget *widget,
struct pattern_struct *data);
void make_new_pattern_widgets(int new_pattern, int total_rows)
{
struct pattern_struct *p = pattern[new_pattern];
int top;
int new_arranger_scroller_size;
char msg[256];
if (p == NULL)
return;
total_rows = total_rows + 6; /* tempo/del/ins/copy/paste/transport */
top = total_rows -1;
p->arr_button = gtk_button_new_with_label(p->patname);
gtk_tooltips_set_tip(tooltips, p->arr_button, EDIT_PATTERN_TIP, NULL);
p->copy_button = gtk_button_new_with_label("Sel");
sprintf(msg, SELECT_PATTERN_TIP, p->patname);
gtk_tooltips_set_tip(tooltips, p->copy_button, msg, NULL);
p->ins_button = gtk_button_new_with_label("Ins");
gtk_tooltips_set_tip(tooltips, p->ins_button,
INSERT_PATTERN_TIP,NULL);
p->del_button = gtk_button_new_with_label("Del");
gtk_tooltips_set_tip(tooltips, p->del_button,
DELETE_PATTERN_TIP, NULL);
p->arr_darea = gtk_drawing_area_new();
gtk_widget_modify_bg(p->arr_darea, GTK_STATE_NORMAL, &whitecolor);
gtk_tooltips_set_tip(tooltips, p->arr_darea, ASSIGN_P_TO_M_TIP, NULL);
g_signal_connect(G_OBJECT (p->arr_darea), "expose_event",
G_CALLBACK (arr_darea_event), p);
gtk_widget_add_events(p->arr_darea, GDK_BUTTON_PRESS_MASK);
gtk_widget_add_events(p->arr_darea, GDK_BUTTON_RELEASE_MASK);
gtk_widget_add_events(p->arr_darea, GDK_DRAG_ENTER);
gtk_widget_add_events(p->arr_darea, GDK_DRAG_LEAVE);
//g_signal_connect(G_OBJECT (p->arr_darea), "button-release-event",
g_signal_connect(G_OBJECT (p->arr_darea), "button-release-event",
G_CALLBACK (arr_darea_clicked), p);
g_signal_connect(G_OBJECT (p->arr_darea), "button-press-event",
G_CALLBACK (arr_darea_button_press), p);
gtk_widget_set_size_request(p->arr_darea, ARRANGER_WIDTH, ARRANGER_HEIGHT);
new_arranger_scroller_size = (total_rows+2) * ARRANGER_HEIGHT;
if (new_arranger_scroller_size > 200)
new_arranger_scroller_size = 200;
gtk_widget_set_size_request (arranger_scroller, 750, new_arranger_scroller_size);
gtk_table_resize(GTK_TABLE(arranger_table), total_rows, ARRANGER_COLS);
/* How do I move the things in the table around? For now,
just add at the end. */
gtk_table_attach(GTK_TABLE(arranger_table), p->ins_button,
0, 1, top, top+1, 0, 0, 0, 0);
gtk_table_attach(GTK_TABLE(arranger_table), p->del_button,
1, 2, top, top+1, 0, 0, 0, 0);
gtk_table_attach(GTK_TABLE(arranger_table), p->copy_button,
2, 3, top, top+1, 0, 0, 0, 0);
gtk_table_attach(GTK_TABLE(arranger_table), p->arr_button,
3, 4, top, top+1, GTK_FILL, 0, 0, 0);
g_signal_connect(G_OBJECT (p->arr_button), "clicked",
G_CALLBACK (edit_pattern_clicked), p);
// 0, 1, top, top+1, GTK_FILL, GTK_FILL, 0, 0);
gtk_table_attach(GTK_TABLE(arranger_table), p->arr_darea,
4, 5, top, top+1, 0,0,0,0);
g_signal_connect(G_OBJECT (p->ins_button), "clicked",
G_CALLBACK (ins_pattern_button_pressed), p);
g_signal_connect(G_OBJECT (p->del_button), "clicked",
G_CALLBACK (del_pattern_button_pressed), p);
g_signal_connect(G_OBJECT (p->copy_button), "clicked",
G_CALLBACK (copy_pattern_button_pressed), p);
gtk_widget_show(p->ins_button);
gtk_widget_show(p->del_button);
gtk_widget_show(p->copy_button);
gtk_widget_show(p->arr_button);
gtk_widget_show(p->arr_darea);
/* gtk_widget_show_all(arranger_window); */
gtk_widget_queue_draw(arranger_table);
return;
}
int lowlevel_add_hit(struct hitpattern **hit,
int dkit, int pattern, int instnum,
int beat, int beats_per_measure,
int noteoff_beat, int noteoff_beats_per_measure,
unsigned char velocity, int change_velocity)
{
struct hitpattern *prev, *this, *next;
double percent;
percent = (double) beat / (double) beats_per_measure;
/* g_print("Adding hit to %s at beat %d of %d beats, percent = %g\n",
drumkit[dkit].instrument[instnum].name,
beat+1, beats_per_measure, percent); */
/* adding to the very beginning */
if (*hit == NULL) {
this = (struct hitpattern *) malloc(sizeof(struct hitpattern));
this->next = NULL;
this->h.velocity = velocity;
this->h.time = percent; /* as a percentage of the measure */
this->h.beat = beat;
this->h.beats_per_measure = beats_per_measure;
this->h.noteoff_beat = noteoff_beat;
this->h.noteoff_beats_per_measure = noteoff_beats_per_measure;
this->h.noteoff_time = (double) noteoff_beat / (double) noteoff_beats_per_measure;
this->h.drumkit = dkit;
this->h.pattern = pattern;
this->h.instrument_num = instnum;
*hit = this;
return 0;
}
/* First, see if this hit is already there... */
for (this = *hit; this != NULL; this = this->next) {
if (this->h.beat == beat &&
this->h.beats_per_measure == beats_per_measure &&
this->h.instrument_num == instnum &&
this->h.drumkit == dkit) {
// printf("lowlevel add hit rejecting duplicate\n");
if (change_velocity) { /* just a velocity change */
this->h.velocity = velocity;
return 0;
} else
return -2;
}
}
/* search through... */
prev = NULL;
for (this = *hit; this != NULL; this = this->next) {
if (this->h.time > percent) {
/* add somewhere in the middle */
next = this;
this = (struct hitpattern *) malloc(sizeof(struct hitpattern));
this->next = next;
this->h.velocity = velocity;
this->h.time = percent; /* as a percentage of the measure */
this->h.beat = beat;
this->h.beats_per_measure = beats_per_measure;
this->h.noteoff_beat = noteoff_beat;
this->h.noteoff_beats_per_measure = noteoff_beats_per_measure;
this->h.noteoff_time = (double) noteoff_beat / (double) noteoff_beats_per_measure;
this->h.drumkit = dkit;
this->h.pattern = pattern;
this->h.instrument_num = instnum;
if (prev == NULL)
*hit = this;
else
prev->next = this;
return 0;
}
prev = this;
}
/* must be adding to the end. */
this = (struct hitpattern *) malloc(sizeof(struct hitpattern));
this->next = NULL;
this->h.velocity = velocity;
this->h.time = percent; /* as a percentage of the measure */
this->h.beat = beat;
this->h.beats_per_measure = beats_per_measure;
this->h.noteoff_beat = noteoff_beat;
this->h.noteoff_beats_per_measure = noteoff_beats_per_measure;
this->h.noteoff_time = (double) noteoff_beat / (double) noteoff_beats_per_measure;
this->h.drumkit = dkit;
this->h.pattern = pattern;
this->h.instrument_num = instnum;
prev->next = this;
return 0;
}
void remove_hit(struct hitpattern **hit,
double thetime, double measurelength,
UNUSED int dkit, UNUSED int instnum, UNUSED int pattern)
{
struct hitpattern *prev, *this, *next, *matchprev;
double distance, mindist, percent;
percent = thetime / measurelength;
matchprev = prev = NULL;
mindist = DRAW_WIDTH * 2.0;
for (this = *hit; this != NULL; this = next) {
next = this->next;
distance = this->h.time * measurelength - percent * measurelength;
if (distance < 0)
distance = -distance;
if (distance < mindist) {
mindist = distance;
matchprev = prev;
}
prev = this;
}
if (mindist < 15.0) {
if (matchprev == NULL) {
this = *hit;
*hit = this->next;
free(this);
return;
} else {
this = matchprev->next;
if (this == NULL)
return;
matchprev->next = this->next;
free(this);
}
}
}
void find_bestbeat(double percent, double measurelength,
int *bestbeat, int *bestdivision)
{
int i;
double zero, diff, bestdiff;
double target, divlen, x;
*bestbeat = -1;
*bestdivision = -1;
bestdiff = 1e100; /* absurdly large initial value to shut scan-build up. */
for (i=0;i<ndivisions;i++) {
zero = gtk_spin_button_get_value_as_float(GTK_SPIN_BUTTON(timediv[i].spin));
if (zero < 0.0)
zero = -zero;
if (zero < 0.00001)
continue;
divlen = measurelength / zero;
x = percent * measurelength / divlen;
/* g_print("x = %g, == %g * %g / %g, zero = %G\n", percent, measurelength, divlen, zero);
g_print("x = %g\n", x); */
if (x - trunc(x) > 0.5) {
target = (trunc(x) + 1.0);
diff = (target - x) * divlen;
} else {
target = trunc(x);
diff = (x - target) * divlen;
}
/* g_print("%d: diff = %g, target= %g, x = %g\n", i, diff, target, x); */
if (i == 0 || diff < bestdiff) {
bestdiff = diff;
*bestbeat = (int) target;
*bestdivision = (int) zero;
/* bestpercent = (target / zero); */
/* g_print("i=%d, bestdiff = %g\n", i, bestdiff); */
}
}
return;
}
int add_hit(struct hitpattern **hit,
double thetime, double duration, double measurelength,
int dkit, int instnum, int tpattern, unsigned char velocity,
int change_velocity)
{
/* figure out the nearest place */
int noteoff_bestbeat = -1;
int bestbeat = -1;
int noteoff_best_division, best_division;
int stg;
double noteoff_percent, percent;
if (thetime > measurelength || thetime < 0)
return 0;
percent = thetime / measurelength;
noteoff_percent = (thetime+duration) / measurelength;
stg = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (snap_to_grid));
/* g_print("percent = %g\n", percent); */
if (stg) {
find_bestbeat(percent, measurelength, &bestbeat, &best_division);
if (bestbeat == -1) {
bestbeat = (int) thetime;
best_division = (int) measurelength;
}
find_bestbeat(noteoff_percent, measurelength,
¬eoff_bestbeat, ¬eoff_best_division);
if (noteoff_bestbeat == -1) {
noteoff_bestbeat = (int) (thetime + duration);
noteoff_best_division = (int) measurelength;
}
} else {
bestbeat = (int) thetime; /* cast should be ok, it's mouse coord */
best_division = (int) measurelength;
noteoff_bestbeat = (int) (thetime + duration);
noteoff_best_division = best_division;
}
reduce_fraction(&bestbeat, &best_division);
reduce_fraction(¬eoff_bestbeat, ¬eoff_best_division);
/* If there are 16 beats, that's 0 thru 15, beat 16 belongs to the next measure... */
if (bestbeat == best_division) {
/* g_print("Rejecting beat %d of %d beats\n",
bestbeat+1, best_division); */
return 0;
}
return (lowlevel_add_hit(hit, dkit, tpattern, instnum, bestbeat, best_division,
noteoff_bestbeat, noteoff_best_division,
velocity, change_velocity));
}
void channel_spin_change(GtkSpinButton *spinbutton, UNUSED void *data)
{
pattern[cpattern]->channel =
gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spinbutton));
}
void track_spin_change(GtkSpinButton *spinbutton, UNUSED void *data)
{
pattern[cpattern]->tracknum =
gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spinbutton));
}
void timediv_spin_change(UNUSED GtkSpinButton *spinbutton, UNUSED struct division_struct *data)
{
int i;
/* Make the timing lines redraw . . . */
for (i=0;i<NINSTS;i++)
gtk_widget_queue_draw(drumkit[kit].instrument[i].canvas);
}
static void mark_selection_endpoint(int m, int end)
{
int s, e;
s = start_copy_measure;
e = end_copy_measure;
if (!end) {
start_copy_measure = m;
if (m > end_copy_measure || end_copy_measure == -1)
end_copy_measure = m;
} else {
end_copy_measure = m;
if (start_copy_measure == -1)
start_copy_measure = m;
if (end_copy_measure < start_copy_measure) {
int tmp = end_copy_measure;
end_copy_measure = start_copy_measure;
start_copy_measure = tmp;
}
}
if (s != start_copy_measure || e != end_copy_measure)
redraw_arranger();
}
static int arr_darea_button_press(UNUSED GtkWidget *w, GdkEventButton *event,
UNUSED struct pattern_struct *data)
{
int m;
m = (int) trunc((0.0 + event->x) / (0.0 + MEASUREWIDTH));
if (event->button == 1)
start_paint_measure = m;
else if (event->button == 3)
mark_selection_endpoint(m, 0);
return 1;
}
void redraw_measure_op_buttons(void)
{
/* I should really make a measure_ops_da[] array, or something . . . */
gtk_widget_queue_draw(Tempo_da);
gtk_widget_queue_draw(Copy_da);
gtk_widget_queue_draw(Paste_da);
gtk_widget_queue_draw(Insert_da);
gtk_widget_queue_draw(Delete_da);
}
void drag_spin_button_change(GtkSpinButton *spinbutton, struct instrument_struct *inst)
{
int i = inst->instrument_num;
pattern[cpattern]->drag[i] = gtk_spin_button_get_value_as_float(GTK_SPIN_BUTTON(spinbutton));
}
void integer_spin_button_change(GtkSpinButton *spinbutton, int *value)
{
*value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spinbutton));
}
void unsigned_char_spin_button_change(GtkSpinButton *spinbutton, unsigned char *value)
{
*value = (unsigned char) gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spinbutton));
}
static void redraw_arranger(void)
{
int i;
for (i=0;i<npatterns;i++) {
gtk_widget_queue_draw(GTK_WIDGET(pattern[i]->arr_darea));
}