-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrange.cpp
1659 lines (1311 loc) · 39 KB
/
arrange.cpp
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
#include <unistd.h>
#include <stdlib.h>
#include <sndfile.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <thread>
#include <vector>
#include <fstream>
#include "arrange.h"
#include <sndfile.h>
extern "C" {
#include "minilisp/minilisp.h"
#include "minilisp/reader.h"
#include "misc.c"
}
extern void x11_stuff_init();
extern void x11_exit_cleanup();
using namespace std;
enum instrument_type_t {
I_SAMPLE,
I_MIDI
};
struct Note {
int freq;
int velo;
View* view;
};
struct Instrument {
int id;
instrument_type_t type;
char* path;
char* code; // instrument source code
int note;
int midi_port;
int midi_channel;
float* pcm;
uint32_t pcm_size;
};
struct MPRegion {
int id;
int track_id;
long inpoint;
long length;
int instrument_id;
bool selected;
vector<Note> notes;
bool fired; // fired in current loop?
bool stopped; // note off sent in current loop?
View* view;
long _prev_inpoint;
};
struct Track {
int id;
track_type_t type;
string title;
int r;
int g;
int b;
View* view;
vector<MPRegion*> regions;
char label[256];
};
struct Project {
vector<Track*> tracks;
vector<Instrument*> instruments;
};
static Project active_project;
static int playback_enabled = 0;
static int bounce_enabled = 0;
#define QUANTUM_NANOSEC 10000L
static int running = 1;
#include <stdio.h>
#include <jack/jack.h>
#include <jack/midiport.h>
#include <string.h>
#define MIDI_NOTE_ON 0x90
#define MIDI_NOTE_OFF 0x80
#define NUM_MIDI_PORTS 8
#define MAX_MIDI_QUEUE_LEN 64
#define NUM_AUDIO_PORTS 16
#define JACK_BUFSIZE 1024
jack_port_t* midi_output_ports[NUM_MIDI_PORTS];
void* midi_port_buffers[NUM_MIDI_PORTS];
jack_port_t* audio_output_ports[NUM_AUDIO_PORTS];
void* audio_port_buffers[NUM_AUDIO_PORTS];
jack_client_t *jack_client = NULL;
struct MidiMessage {
jack_nframes_t time;
int len;
unsigned char data[3];
};
struct MidiEvent {
int note;
int note_on;
int port;
int channel;
int velocity;
};
void send_midi(int note, int note_on, int port, char channel, int velocity) {
unsigned char *buffer;
jack_nframes_t last_frame_time;
struct MidiMessage ev;
channel = 0;
void* port_buffer = midi_port_buffers[port];
ev.len = 3;
printf("send_midi: %d %d %d %d %d\n",note,note_on,port,channel,velocity);
if (note_on) {
ev.data[0] = MIDI_NOTE_ON + channel;
} else {
ev.data[0] = MIDI_NOTE_OFF + channel;
}
ev.data[1] = note; // c3
ev.data[2] = velocity; // velocity
if (port_buffer == NULL) {
printf("jack_port_get_buffer failed, cannot send anything.\n");
return;
}
buffer = jack_midi_event_reserve(port_buffer, 0, ev.len);
if (buffer == NULL) {
printf("jack_midi_event_reserve (1) failed, NOTE ON LOST.\n");
return;
}
memcpy(buffer, ev.data, 3);
}
static double playhead = 0;
// beat = 1/4 bar
// if bar = 2s, then beat = 0.5s; minute = 60*2 beats; 120bpm
static double bpm = 120.0;
#define TMUL 1000000.0
static double loop_start_point = 0;
static double loop_end_point = 1000;
double playhead_samples = 0;
static int playback_clean_up = 0;
void set_playhead(double ph) {
playhead = ph;
playhead_samples = playhead/TMUL*48.0;
}
void do_playback_cleanup() {
int audio_port_idx = 0;
int ti = 0;
for (Track* t : active_project.tracks) {
float* audio_out = NULL;
Instrument* default_instr = active_project.instruments[ti];
for (MPRegion* r : t->regions) {
if (r->fired && !r->stopped) {
Instrument* instr = active_project.instruments[r->instrument_id];
if (instr->type == I_MIDI) {
send_midi(instr->note,0,instr->midi_port,instr->midi_channel,127);
}
}
r->fired = false;
r->stopped = false;
}
ti++;
}
}
int jack_process_callback(jack_nframes_t nframes, void *notused)
{
for (int i=0; i<NUM_MIDI_PORTS; i++) {
jack_midi_clear_buffer(midi_port_buffers[i]);
}
//printf("out buffer: %p\n",out);
// playhead is in nanoseconds (?)
double bpm_factor = 240.0/bpm; // 2
double delta_ns = TMUL*((double)nframes/48.0); // how many ns passed?
if (playback_clean_up) {
playback_clean_up = 0;
do_playback_cleanup();
// clear audio buffers
int ti = 0;
int audio_port_idx = 0;
for (Track* t : active_project.tracks) {
float* audio_out = NULL;
Instrument* default_instr = active_project.instruments[ti];
if (default_instr->type == I_SAMPLE) {
// clear all the buffers
audio_out = (float*)jack_port_get_buffer(audio_output_ports[audio_port_idx], nframes);
memset(audio_out, 0, nframes*sizeof(float));
audio_port_idx = (audio_port_idx+1)%NUM_AUDIO_PORTS;
}
ti++;
}
}
if (playback_enabled) {
if (playhead>loop_end_point*bpm_factor*TMUL) {
set_playhead(loop_start_point*bpm_factor*TMUL);
printf("looped to %f\n",playhead);
// looped.
// clear fired flags of regions
do_playback_cleanup();
if (bounce_enabled) {
playback_enabled = 0;
bounce_enabled = 0;
printf("-- finished bounce.\n");
}
}
int audio_port_idx = 0;
int ti = 0;
for (Track* t : active_project.tracks) {
float* audio_out = NULL;
if (active_project.instruments.size()>ti) {
Instrument* default_instr = active_project.instruments[ti];
if (default_instr->type == I_SAMPLE) {
audio_out = (float*)jack_port_get_buffer(audio_output_ports[audio_port_idx], nframes);
memset(audio_out, 0, nframes*sizeof(float));
audio_port_idx = (audio_port_idx+1)%NUM_AUDIO_PORTS;
}
}
for (MPRegion* r : t->regions) {
double rstart = (long)r->inpoint * TMUL * bpm_factor;
double rstop = rstart + ((long)r->length/2 * TMUL * bpm_factor); // FIXME: why is /2 correct?!?
double rstart_smp = (rstart*48.0)/TMUL;
double rstop_smp = (rstop*48.0)/TMUL;
if (active_project.instruments.size()<=r->instrument_id) {
printf("error: track has non-existing instrument id %d\n",r->instrument_id);
} else {
Instrument* instr = active_project.instruments[r->instrument_id];
if (r->fired && !r->stopped && playhead>=rstop) {
r->stopped = true;
if (instr->type == I_MIDI) {
send_midi(instr->note,0,instr->midi_port,instr->midi_channel,127);
}
}
else if (!r->fired && playhead_samples>=rstart_smp && playhead_samples<rstop_smp) {
if (instr->type == I_SAMPLE) {
//printf("sample voice fired: %s rstart: %ld\n",instr->path,rstart);
long offset = playhead_samples - rstart_smp;
printf("offset: %ld nframes: %ld\n",offset,nframes);
if (offset<instr->pcm_size) {
int size = nframes;
if (instr->pcm_size<=offset+nframes) {
size = instr->pcm_size-offset;
r->fired = true;
}
if (audio_out) {
memcpy(audio_out, instr->pcm+offset, size*sizeof(float));
}
}
} else {
r->fired = true;
send_midi(instr->note,1,instr->midi_port,instr->midi_channel,127);
}
}
else if (!r->fired && playhead_samples<rstart_smp && (playhead_samples+nframes)>rstart_smp) {
// playback buffer reaches into beginning of frame
if (instr->type == I_SAMPLE) {
long offset = (rstart_smp - playhead_samples);
long size = nframes - (rstart_smp - playhead_samples);
printf("offset2: %ld size: %ld\n",offset,size);
/*int size = nframes - rstart_smp;
if (instr->pcm_size<=offset+nframes) {
size = instr->pcm_size-offset;
r->fired = true;
}*/
if (audio_out) {
memcpy(audio_out+offset, instr->pcm, size*sizeof(float));
}
}
}
}
} // end region loop
ti++;
} // end track loop
playhead += delta_ns;
playhead_samples += nframes;
}
return 0;
}
void init_jack() {
jack_client = jack_client_open("produce", JackNullOption, NULL);
if (jack_client == NULL) {
printf("MIDI: Could not connect to the JACK server; run jackd first?\n");
return;
}
for (int i=0; i<NUM_MIDI_PORTS; i++) {
char buf[64];
sprintf(buf,"produce_midi_out_%d",i);
midi_output_ports[i] = jack_port_register(
jack_client,
buf,
JACK_DEFAULT_MIDI_TYPE,
JackPortIsOutput,
0);
printf("JACK MIDI: output port %d %p\n",i,midi_output_ports[i]);
midi_port_buffers[i] = jack_port_get_buffer(midi_output_ports[i], JACK_BUFSIZE);
}
for (int i=0; i<NUM_AUDIO_PORTS; i++) {
char buf[64];
sprintf(buf,"produce_audio_out_%d",i);
audio_output_ports[i] = jack_port_register(
jack_client,
buf,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput,
0);
printf("JACK Audio: output port %d %p\n",i,audio_output_ports[i]);
//audio_port_buffers[i] = jack_port_get_buffer(audio_output_ports[i], JACK_BUFSIZE);
}
if (jack_set_process_callback(jack_client, jack_process_callback, 0)) {
printf("JACK: Could not register JACK process callback.\n");
exit(1);
}
if (jack_activate(jack_client)) {
printf("JACK: Cannot activate JACK client.\n");
exit(1);
}
for (int i=0; i<NUM_AUDIO_PORTS; i++) {
char buf[128];
sprintf(buf,"produce:produce_audio_out_%d",i);
jack_connect(jack_client,buf,"system:playback_1");
jack_connect(jack_client,buf,"system:playback_2");
}
}
static int load_wave_file(Instrument* instr, const char *wavename)
{
SNDFILE *infile;
SF_INFO sfinfo;
sfinfo.format = 0;
if (!(infile = sf_open(wavename, SFM_READ, &sfinfo)))
{
printf ("-- load_wave_file: failed to open file %s\n");
sf_perror (NULL);
return 1;
}
float *data = (float*)malloc(sfinfo.frames * sizeof(float));
sf_read_float(infile, data, sfinfo.frames);
instr->pcm = data;
instr->pcm_size = sfinfo.frames;
sf_close(infile);
printf ("-- load_wave_file: loaded %s %d\n", wavename, instr->pcm_size);
return 0;
}
GLV glv_root;
View* tracks_view;
View* selection_rect;
vector<View*> grid_lines;
View* header_view;
View* playhead_view;
View* loop_start_marker;
View* loop_end_marker;
bool transforming = false;
Track* selected_track = NULL;
Buttons* toolbar;
NumberDialer* bpm_dialer;
TextView* title_view;
int win_w;
int win_h;
float track_h;
float scroll_x = 500;
float zoom_x = 0.5;
void zoom_in_x() {
zoom_x *= 2.0;
}
void zoom_out_x() {
zoom_x /= 2.0;
}
void scroll_left() {
scroll_x += 1000;
}
void scroll_right() {
scroll_x -= 1000;
}
enum mouse_state_t {
MS_IDLE,
MS_MOVING,
MS_RESIZING,
MS_SELECTING
};
mouse_state_t mouse_state = MS_IDLE;
int mouse_dx = 0;
int mouse_dy = 0;
long drag_x1 = 0;
long drag_y1 = 0;
int old_track_dy = 0;
View* hover_track_view = NULL;
void toggle_playback() {
playback_enabled = 1-playback_enabled;
do_playback_cleanup();
playback_clean_up = 1;
}
MPRegion* view_to_region(View* v) {
for (Track* t : active_project.tracks) {
for (MPRegion* r : t->regions) {
if (r->view == v) {
return r;
}
}
}
return NULL;
}
Track* view_to_track(View* v) {
for (Track* t : active_project.tracks) {
if (t->view == v) {
return t;
}
}
return NULL;
}
Track* region_to_track(MPRegion* r_needle) {
for (Track* t : active_project.tracks) {
for (MPRegion* r : t->regions) {
if (r == r_needle) {
return t;
}
}
}
return NULL;
}
vector<MPRegion*> selected_regions() {
vector<MPRegion*> res;
for (Track* t : active_project.tracks) {
for (MPRegion* r : t->regions) {
if (r->selected) {
res.push_back(r);
}
}
}
return res;
}
void deselect_regions() {
vector<MPRegion*> regions = selected_regions();
for (MPRegion* r : regions) {
r->selected = false;
}
}
void delete_selected_regions() {
vector<MPRegion*> regions = selected_regions();
for (MPRegion* r : regions) {
Track* t = region_to_track(r);
if (t) {
vector<MPRegion*>& v = t->regions;
r->view->remove();
v.erase(remove(begin(v), end(v), r), end(v));
}
}
}
void select_regions_in_rect(Rect& rect) {
for (Track* t : active_project.tracks) {
for (MPRegion* r : t->regions) {
if (r->view) {
Rect shifted = Rect(*(r->view));
shifted.posAdd(t->view->left(), t->view->top());
if (shifted.intersects(rect)) {
//printf("intersects: %f %f %f %f\n",r->view->left(),r->view->top(),r->view->width(),r->view->height());
r->selected = true;
}
}
}
}
}
bool on_toolbar_mouseup(View* v, GLV& glv) {
toggle_playback();
return false;
}
bool on_root_mouseup(View * v, GLV& glv) {
//printf("on_root_mouseup\n");
if (mouse_state == MS_SELECTING) {
select_regions_in_rect(*selection_rect);
selection_rect->disable(Visible);
}
mouse_state = MS_IDLE;
return false;
}
bool on_root_mousedown(View * v, GLV& glv) {
//printf("on_root_mouseup\n");
//transforming = true;
deselect_regions();
return false;
}
bool on_region_mousedown(View * v, GLV& glv) {
mouse_state = MS_MOVING;
MPRegion* r = view_to_region(v);
printf("on_region_mousedown: %x\n",r);
if (r) {
vector<MPRegion*> regions = selected_regions();
if (!glv.keyboard().shift() && !glv.keyboard().ctrl() && regions.size()<2) {
for (MPRegion* r : regions) {
r->selected = false;
}
}
r->selected = true;
if (glv.keyboard().ctrl()) {
// clone region
for (MPRegion* sr : regions) {
Track* t = region_to_track(sr);
if (t) {
MPRegion* dup = new MPRegion;
memcpy(dup, sr, sizeof(MPRegion));
dup->view = NULL;
dup->selected = true;
t->regions.push_back(dup);
sr->selected = false;
}
}
}
mouse_state = MS_MOVING;
mouse_dx = 0;
mouse_dy = 0;
old_track_dy = 0;
drag_x1 = glv.mouse().x();
drag_y1 = glv.mouse().y();
for (MPRegion* sr : selected_regions()) {
sr->_prev_inpoint = sr->inpoint; // save state when we started dragging
}
}
return false;
}
long snap_time(long p) {
long snap = 1000/8;
long thresh = 20;
long p1=p-(p%snap);
long p2=snap+p-(p%snap);
//printf("p: %ld p1: %ld\n",p,p1);
if (abs(p-p1) < thresh) return p1;
if (abs(p-p2) < thresh) return p2;
return p;
}
bool on_loop_start_init_drag(View* v, GLV& glv) {
drag_x1 = loop_start_point;
drag_y1 = 0;
mouse_dx = 0;
return true;
}
bool on_loop_start_drag(View* v, GLV& glv) {
float bpm_factor = 240.0/bpm;
Mouse m = glv.mouse();
mouse_dx += m.dx();
loop_start_point = snap_time(drag_x1 + (mouse_dx/zoom_x/bpm_factor));
return false;
}
bool on_loop_end_init_drag(View* v, GLV& glv) {
drag_x1 = loop_end_point;
drag_y1 = 0;
mouse_dx = 0;
return true;
}
bool on_loop_end_drag(View* v, GLV& glv) {
float bpm_factor = 240.0/bpm;
Mouse m = glv.mouse();
mouse_dx += m.dx();
loop_end_point = snap_time(drag_x1 + (mouse_dx/zoom_x/bpm_factor));
return false;
}
bool on_root_mousemove(View* v, GLV& glv) {
float bpm_factor = 240.0/bpm;
Mouse m = glv.mouse();
mouse_dx += m.dx();
mouse_dy += m.dy();
int track_dy = round((float)mouse_dy/(float)track_h);
//printf("root_mousemove %d\n",mouse_state);
if (mouse_state == MS_MOVING) {
int track_ddy = track_dy-old_track_dy;
vector<MPRegion*> regions = selected_regions();
for (MPRegion* r : regions) {
r->inpoint = snap_time(r->_prev_inpoint + mouse_dx/zoom_x/bpm_factor);
// allow cross-track moving only inside bounds
if ((r->track_id+track_ddy)<0 || (r->track_id+track_ddy)>active_project.tracks.size()-1) {
track_ddy = 0;
}
}
if (track_ddy!=0) {
printf("move across tracks %d\n",track_ddy);
vector<MPRegion*> regions = selected_regions();
for (MPRegion* r : regions) {
Track* t = region_to_track(r);
vector<MPRegion*>& v = t->regions;
v.erase(remove(begin(v), end(v), r), end(v));
int new_id = t->id + track_ddy;
Track* new_track = active_project.tracks[new_id];
/*MPRegion* new_r = new MPRegion {r->id,
new_id,
r->inpoint,
r->length,
new_id};*/
//new_r->_prev_inpoint = r->_prev_inpoint;
//new_r->selected = true;
// recycle the view
//new_r->view = r->view;
//r->view = NULL;
r->track_id = new_id;
r->instrument_id = new_id;
*new_track->view << *r->view;
new_track->regions.push_back(r);
}
old_track_dy = track_dy;
}
}
return true;
}
bool on_bpm_keyup(View * v, GLV& glv) {
bpm = bpm_dialer->getValue();
}
bool on_selection_rect_drag(View* v, GLV& glv) {
if (mouse_state == MS_MOVING) return true;
float dx = glv.mouse().dx();
float dy = glv.mouse().dy();
mouse_dx += dx;
mouse_dy += dy;
mouse_state = MS_SELECTING;
float x,y,w=mouse_dx,h=mouse_dy;
if (w<0) {
x = drag_x1 + w;
w = -w;
} else {
x = drag_x1;
w = w;
}
if (h<0) {
y = drag_y1 + h;
h = -h;
} else {
y = drag_y1;
h = h;
}
selection_rect->set(x,y,w,h);
selection_rect->enable(Visible);
//printf("drag %f %f %f %f\n",x,y,w,h);
return false;
}
Cell* lisp_add_region_at_mouse(Cell* args, Cell* env) {
float bpm_factor = 240.0/bpm;
space_t x = glv_root.mouse().x() - scroll_x;
space_t y = glv_root.mouse().y();
hover_track_view = glv_root.findTarget(x, y);
printf("hover_track_view: %x\n",hover_track_view);
if (!hover_track_view) return alloc_nil();
// create note
Track* t = view_to_track(hover_track_view);
if (t) {
int inpoint = snap_time(x/zoom_x/bpm_factor);
int duration = 50;
MPRegion* r = new MPRegion {1,
t->id,
inpoint,
duration,
t->id};
t->regions.push_back(r);
}
return alloc_nil();
}
bool on_track_mousedown(View* v, GLV& glv) {
mouse_dx = 0;
mouse_dy = 0;
float x = glv.mouse().x();
float y = glv.mouse().y();
drag_x1 = x - tracks_view->left();
drag_y1 = y - tracks_view->top();
selection_rect->left(drag_x1);
selection_rect->top(drag_y1);
selection_rect->width(1);
selection_rect->height(1);
//printf("track mousedown %d %d\n",drag_x1,drag_y1);
hover_track_view = glv_root.findTarget(x, y);
selected_track = view_to_track(hover_track_view);
if (selected_track) {
//printf("selected_track: %p (id: %d)\n",selected_track,selected_track->id);
}
if (!glv.keyboard().shift()) {
deselect_regions();
}
return false;
}
Cell* external_edit_selected_region(Cell* args, Cell* env) {
vector<MPRegion*> rs = selected_regions();
MPRegion* r = rs[0];
Instrument* instr = active_project.instruments[r->instrument_id];
string cmd = "mhwaveedit --driver jack \""+string(instr->path)+"\"";
system(cmd.c_str());
// TODO: reload audio
return alloc_nil();
}
bool on_root_keydown(View * v, GLV& glv) {
char k = glv.keyboard().key();
//printf("key: %d %d %d\n",k,glv.keyboard().ctrl(),glv.keyboard().alt());
// FIXME
switch (k) {
case ' ':
toggle_playback();
break;
case '+':
zoom_in_x();
break;
case '-':
zoom_out_x();
break;
case ',':
set_playhead(playhead - 250*TMUL);
break;
case '.':
set_playhead(playhead + 250*TMUL);
break;
case 13:
// cursor left
scroll_left();
break;
case 15:
scroll_right();
break;
case 8: // backspace
delete_selected_regions();
break;
default: {
char buf[1024];
sprintf(buf,"(handle-key \"%c\")",k);
eval(read_string(buf), get_globals());
}
}
}
static int song_bars = 4*100;
char* note_to_label(Instrument* i) {
char buf[64];
int octave = i->note/12;
char* notes[] = {
"C",
"C#",
"D",
"D#",
"E",
"F",
"F#",
"G",
"G#",
"A",
"A#",
"B"
};
sprintf(buf, "%s%d", notes[i->note%12], octave);
printf("note_to_label: %s\n",buf);
return strdup(buf);
}
char* make_track_label(Track* t) {
//printf("-- make_track_label %d\n",t->id);
char tmp[128];
if (t->type == TRACK_MIDI) {
Instrument* i = active_project.instruments[t->id];
snprintf(t->label, 255, "%02d MIDI %s", t->id, note_to_label(i));
} else {
const char* ptr = t->title.c_str();
while (strstr(ptr, "/")) {
// find last slash
ptr = strstr(ptr, "/") + 1;
}
if (!ptr) ptr = t->title.c_str();
strncpy(tmp,ptr,16);
tmp[16] = 0;
snprintf(t->label, 255, "%02d %s", t->id, tmp);
}
printf("track %d label: %s\n",t->id,t->label);
return t->label;
}
void update_ui() {
Project& p = active_project;
float bpm_factor = 240.0/bpm;
win_w = 2560;
win_h = 1600;
glv_root.disable(DrawBack);
if (!tracks_view) {
tracks_view = new View(Rect(0,100, win_w, win_h));
tracks_view->colors().set(Color(0.2,0.4,1,0.8), 1.0);
tracks_view->disable(DrawBack);
tracks_view->disable(DrawBorder);
for (int i=0; i<song_bars; i++) {
// 1000px = 1 bar
// 250px = 1/4 bar (beat)
int x = (int)(250.0*zoom_x*bpm_factor*i);
View* l = new View(Rect(x,0,1,win_h));
if (i%4>0) {
l->cloneStyle().colors().set(Color(0.9,0.9,1,0.08), 1.0);
} else {
l->cloneStyle().colors().set(Color(0.9,0.9,1,0.12), 1.0);
}
l->disable(DrawBack);
grid_lines.push_back(l);
*tracks_view << l;
}
selection_rect = new View(Rect(0,0,0,0));
selection_rect->cloneStyle().colors().set(Color(1,1,1,0.3), 0.3);
tracks_view->on(Event::MouseDown, on_track_mousedown);
tracks_view->on(Event::MouseDrag, on_selection_rect_drag);
*tracks_view << selection_rect;
glv_root << tracks_view;
} else {
int i = 0;
tracks_view->height(win_h);
for (View* l : grid_lines) {
int x = (int)(scroll_x + 250.0*zoom_x*bpm_factor*(i++));
l->left(x);
l->height(win_h);
}
}
if (!header_view) {
header_view = new View(Rect(0,0,win_w*2,50));
header_view->cloneStyle().colors().set(Color(1.0,1.0,1.0,0.5), 0.9);
/*toolbar = new Buttons(Rect(0,0,200,50), 4);
toolbar->cloneStyle().colors().set(Color(1.0,1.0,1.0,0.6), 0.9);
*header_view << (View*)toolbar;*/
title_view = new TextView(Rect(150,10,340,30), 19.0);
*header_view << (View*)title_view;
title_view->setValue("untitled.l");
title_view->disable(DrawBorder);
header_view->disable(DrawBorder);
bpm_dialer = new NumberDialer(Rect(5,5,50,40), 3, 2, 999.0, 1.0);