Restored pattern editor functionality.
[epichord.git] / src / uihelper.cpp
bloba630fc4a0c64a414749032c70cb77e595c22b5ef
1 /*
2 Epichord - a midi sequencer
3 Copyright (C) 2008 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to
18 The Free Software Foundation, Inc.
19 51 Franklin Street, Fifth Floor
20 Boston, MA 02110-1301, USA
23 #include <stdlib.h>
24 #include <vector>
25 #include <fstream>
26 #include <string.h>
27 #include <math.h>
29 #include <limits>
31 #include <fltk/run.h>
33 #include "seq.h"
34 #include "ui.h"
35 #include "backend.h"
37 #include "uihelper.h"
40 #define CONFIG_FILENAME ".epichordrc"
42 extern UI* ui;
43 extern std::vector<track*> tracks;
45 struct conf config;
47 using namespace std;
49 char* config_filename;
53 void load_config(){
55 //linux dependent
56 char* homepath = getenv("HOME");
57 asprintf(&config_filename,"%s/"CONFIG_FILENAME,homepath);
59 fstream f;
60 f.open(config_filename,fstream::in);
61 if(!f.is_open()){
62 printf("load_config: Unable to open config file for reading.\n");
63 config.beats_per_measure = 4;
64 config.measures_per_phrase = 4;
65 config.measures_until_record = 1;
66 config.alwayscopy = 0;
67 config.autotrackname = 0;
68 config.passthru = 1;
69 config.playinsert = 1;
70 config.recordonchan = 0;
71 config.playmove = 1;
72 config.follow = 1;
73 config.recordmode = 0;
74 config.robmode = 0;
75 config.defaultvelocity = 96;
77 load_default_keymap();
78 update_config_gui();
79 return;
82 config.beats_per_measure = 4;
83 config.measures_per_phrase = 4;
85 std::string word;
87 while(!f.eof()){
88 word = "";
89 f >> word;
90 if(word == "leadin"){f>>config.measures_until_record;}
91 else if(word == "alwayscopy"){f>>config.alwayscopy;}
92 else if(word == "autotrackname"){f>>config.autotrackname;}
93 else if(word == "passthru"){f>>config.passthru;}
94 else if(word == "playinsert"){f>>config.playinsert;}
95 else if(word == "recordonchan"){f>>config.recordonchan;}
96 else if(word == "playmove"){f>>config.playmove;}
97 else if(word == "follow"){f>>config.follow;}
98 else if(word == "recordmode"){f>>config.recordmode;}
99 else if(word == "robmode"){f>>config.robmode;}
100 else if(word == "keymap"){load_keymap(f);}
101 else if(word == "defaultvelocity"){f>>config.defaultvelocity;}
102 else{
103 f.ignore(std::numeric_limits<streamsize>::max(),'\n');
106 update_config_gui();
107 f.close();
110 void save_config(){
111 fstream f;
112 f.open(config_filename,fstream::out);
113 if(!f.is_open()){
114 printf("save_config: Unable to open config file %s for saving.\n", config_filename);
115 return;
118 f << "leadin " << config.measures_until_record << endl;
119 f << "alwayscopy " << config.alwayscopy << endl;
120 f << "autotrackname " << config.autotrackname << endl;
121 f << "passthru " << config.passthru << endl;
122 f << "playinsert " << config.playinsert << endl;
123 f << "recordonchan " << config.recordonchan << endl;
124 f << "playmove " << config.playmove << endl;
125 f << "follow " << config.follow << endl;
126 //f << "quantizedur " << config.quantizedur << endl;
127 f << "recordmode " << config.recordmode << endl;
128 f << "robmode " << config.robmode << endl;
129 f << "defaultvelocity " << config.defaultvelocity << endl;
130 f << endl;
131 save_keymap(f);
132 f.close();
135 void update_config_gui(){
136 ui->beats_per_measure->value(config.beats_per_measure);
137 ui->measures_per_phrase->value(config.measures_per_phrase);
138 ui->measures_until_record->value(config.measures_until_record);
140 ui->bpm_wheel->value(config.beats_per_minute);
141 ui->bpm_output->value(config.beats_per_minute);
143 ui->check_alwayscopy->state(config.alwayscopy);
144 ui->check_autotrackname->state(config.autotrackname);
145 ui->check_passthru->state(config.passthru);
146 ui->check_playinsert->state(config.playinsert);
147 ui->check_recordonchan->state(config.recordonchan);
148 ui->check_playmove->state(config.playmove);
149 ui->check_follow->state(config.follow);
151 ui->menu_recordmode->value(config.recordmode);
152 ui->menu_rob->value(config.robmode);
154 ui->default_velocity->value(config.defaultvelocity);
156 ui->config_window->redraw();
161 seqpat* rob_check(seqpat* s){
162 seqpat* prev = s->prev;
163 Command* c;
164 if(config.robmode == 0){
165 return NULL;
167 else if(config.robmode == 1 || prev == NULL){
168 int pos = get_play_position();
169 int M = config.measures_per_phrase*config.beats_per_measure*128;
170 int P1 = pos/M*M;
171 int P2 = P1 + M;
172 int T = P1;
173 int R = s->tick+s->dur;
174 if(R > P1){
175 T = R;
177 int W = P2 - T;
178 if(s->next){
179 int L = s->next->tick;
180 if(L < P2){
181 W = L - T;
184 c = new CreateSeqpatBlank(s->track,T,W);
185 set_undo(c);
186 undo_push(1);
187 return s->next;
189 else if(config.robmode == 2){
190 int pos = get_play_position();
191 int M = config.measures_per_phrase*config.beats_per_measure*128;
192 int P = pos/M*M + M;//tick at next phrase boundary
193 int W = P - s->tick;
194 if(s->next){
195 int W2 = s->next->tick - s->tick;
196 if(W2 < W){
197 W=W2;
200 c = new ResizeSeqpat(s,W);
201 set_undo(c);
202 undo_push(1);
203 return prev->next;
209 int last_pos=0;
210 void playing_timeout_cb(void* v){
211 int pos = get_play_position();
213 if(pos < last_pos){
214 reset_record_flags();
216 last_pos = pos;
218 if(config.follow){
219 ui->arranger->update(pos);
220 ui->piano_roll->update(pos);
222 ui->song_timeline->update(pos);
223 ui->pattern_timeline->update(pos);
224 ui->metronome->update(pos);
226 //check for midi input
227 int tick;
228 int chan;
229 int type;
230 int val1;
231 int val2;
233 track* t = tracks[get_rec_track()];
234 Command* c;
235 seqpat* s;
236 pattern* p;
238 char report[256];
240 while(recv_midi(&chan,&tick,&type,&val1,&val2)){
242 if(config.recordonchan){
243 for(int i=0; i<tracks.size(); i++){
244 if(tracks[i]->chan == chan){
245 t = tracks[i];
250 switch(type){
251 case 0x80://note off
252 snprintf(report,256,"%02x %02x %02x : note off - ch %d note %d vel %d\n",type|chan,val1,val2,chan,val1,val2);
253 scope_print(report);
255 if(!is_backend_recording())
256 break;
258 s = tfind<seqpat>(t->head,tick);
259 if(s->tick+s->dur < tick){
260 s = rob_check(s);
261 if(!s){continue;}
264 //if(s->record_flag==1 && config.recordmode>0){show_song_edit();}
265 s->record_check(config.recordmode);
266 p = s->p;
267 c=new CreateNoteOff(p,val1,val2,tick-s->tick);
268 set_undo(c);
269 undo_push(1);
270 if(ui->piano_roll->visible()){
271 ui->piano_roll->redraw();
272 ui->event_edit->redraw();
273 if(ui->event_edit->cur_seqpat == s){ui->event_edit->has[1]=1;}
274 ui->event_menu->redraw();
276 if(ui->arranger->visible())
277 ui->arranger->redraw();
278 break;
279 case 0x90://note on
280 snprintf(report,256,"%02x %02x %02x : note on - ch %d note %d vel %d\n",type|chan,val1,val2,chan,val1,val2);
281 scope_print(report);
283 if(!is_backend_recording())
284 break;
286 s = tfind<seqpat>(t->head,tick);
287 if(s->tick+s->dur < tick){
288 s = rob_check(s);
289 if(!s){continue;}
292 // if(s->record_flag==1 && config.recordmode>0){show_song_edit();}
293 s->record_check(config.recordmode);
294 p = s->p;
295 c=new CreateNoteOn(p,val1,val2,tick-s->tick,16);
296 set_undo(c);
297 undo_push(1);
298 if(ui->piano_roll->visible()){
299 ui->piano_roll->redraw();
300 ui->event_edit->redraw();
301 if(ui->event_edit->cur_seqpat == s){ui->event_edit->has[0]=1;}
302 ui->event_menu->redraw();
304 if(ui->arranger->visible())
305 ui->arranger->redraw();
306 break;
307 case 0xa0://aftertouch
308 case 0xb0://controller
309 case 0xc0://program change
310 case 0xd0://channel pressure
311 case 0xe0://pitch wheel
313 s = tfind<seqpat>(t->head,tick);
314 if(s->tick+s->dur < tick){
315 s = rob_check(s);
316 if(!s){continue;}
319 switch(type){
320 case 0xa0:
321 snprintf(report,256,"%02x %02x %02x : aftertouch - ch %d note %d %d\n",type|chan,val1,val2,chan,val1,val2);
322 if(ui->event_edit->cur_seqpat == s){ui->event_edit->has[2]=1;}
323 break;
324 case 0xb0:
325 snprintf(report,256,"%02x %02x %02x : controller change - ch %d cntr %d val %d\n",type|chan,val1,val2,chan,val1,val2);
326 if(ui->event_edit->cur_seqpat == s){
327 ui->event_edit->has[val1+6]=1;
329 break;
330 case 0xc0:
331 snprintf(report,256,"%02x %02x : program change - ch %d pgrm %d \n",type|chan,val1,chan,val1);
332 if(ui->event_edit->cur_seqpat == s){ui->event_edit->has[3]=1;}
333 break;
334 case 0xd0:
335 snprintf(report,256,"%02x %02x : channel pressure - ch %d val %d \n",type|chan,val1,chan,val1);
336 if(ui->event_edit->cur_seqpat == s){ui->event_edit->has[4]=1;}
337 break;
338 case 0xe0:
339 snprintf(report,256,"%02x %02x %02x : pitch wheel - ch %d val %d \n",type|chan,val1,val2,chan,(val2<<7)|val1);
340 if(ui->event_edit->cur_seqpat == s){ui->event_edit->has[5]=1;}
341 break;
343 scope_print(report);
345 if(!is_backend_recording())
346 break;
348 // if(s->record_flag==1 && config.recordmode>0){show_song_edit();}
349 s->record_check(config.recordmode);
350 p = s->p;
351 c=new CreateEvent(p,type,tick,val1,val2);
352 set_undo(c);
353 undo_push(1);
354 if(ui->piano_roll->visible()){
355 ui->piano_roll->redraw();
356 ui->event_edit->redraw();
357 ui->event_menu->redraw();
359 if(ui->arranger->visible())
360 ui->arranger->redraw();
361 break;
362 case 0xf0:
363 switch(chan){
364 case 1://undefined (reserved) system common message
365 snprintf(report,256,"%02x : undefined (reserved) system common message\n",type|chan);
366 break;
367 case 2://song position pointer
368 snprintf(report,256,"%02x %02x %02x : song position - %d \n",type|chan,val1,val2,(val2<<7)|val1);
369 break;
370 case 3://song select
371 snprintf(report,256,"%02x %02x : song select - %d \n",type|chan,val1,val1);
372 break;
373 case 4://undefined (reserved) system common message
374 case 5://undefined (reserved) system common message
375 snprintf(report,256,"%02x : undefined (reserved) system common message\n",type|chan);
376 break;
377 case 6://tune request
378 snprintf(report,256,"%02x : tune request\n",type|chan);
379 break;
380 case 7://end of exclusive
381 snprintf(report,256,"%02x : end of exclusive\n",type|chan);
382 break;
383 case 8://timing clock
384 snprintf(report,256,"%02x : timing clock\n",type|chan);
385 break;
386 case 9://undefined (reserved) system common message
387 snprintf(report,256,"%02x : undefined (reserved) system common message\n",type|chan);
388 break;
389 case 10://start
390 snprintf(report,256,"%02x : start\n",type|chan);
391 break;
392 case 11://continue
393 snprintf(report,256,"%02x : continue\n",type|chan);
394 break;
395 case 12://stop
396 snprintf(report,256,"%02x : stop\n",type|chan);
397 break;
398 case 13://undefined
399 snprintf(report,256,"%02x : undefined (reserved) system common message\n",type|chan);
400 break;
401 case 14://active sensing
402 snprintf(report,256,"%02x : active sensing\n",type|chan);
403 break;
404 case 15://reset
405 snprintf(report,256,"%02x : reset\n",type|chan);
406 break;
408 if(chan==0){
409 snprintf(report,256,"%02x %02x : system exclusive - id %d ; data follows\n",type|chan,val1,val1);
410 scope_print(report);
411 scope_print(getsysexbuf());
412 scope_print("\nf7 : end of sysex\n");
414 else{
415 scope_print(report);
422 //handle session events (LASH)
423 int ret;
424 char* session_string;
425 char* filename_string;
426 ret=backend_session_process();
427 while(ret != SESSION_NOMORE){
428 session_string=get_session_string();
429 filename_string = (char*)malloc(strlen(session_string)+16);
430 strcpy(filename_string,session_string);
431 strcat(filename_string,"/song.epi");
432 switch(ret){
433 case SESSION_SAVE: save(filename_string); break;
434 case SESSION_LOAD: load(filename_string); break;
435 case SESSION_QUIT: ui->main_window->hide(); break;
436 case SESSION_UNHANDLED: break;
438 free(session_string);
439 ret=backend_session_process();
443 if(is_backend_playing()){
444 fltk::repeat_timeout(0.005, playing_timeout_cb, NULL);
446 else{
447 fltk::repeat_timeout(0.1, playing_timeout_cb, NULL);
451 void start_monitor(){
452 fltk::add_timeout(0.1, playing_timeout_cb, NULL);
455 void press_play(){
456 if(!is_backend_playing()){
457 start_backend();
458 ui->play_button->label("@||");
459 //fltk::add_timeout(0.01, playing_timeout_cb, NULL);
461 else{
462 pause_backend();
463 all_notes_off();
464 ui->play_button->label("@>");
468 void press_stop(){
470 //stops playback and sets the play position to zero
471 pause_backend();
472 reset_backend(0);
473 all_notes_off();
475 ui->song_timeline->update(0);
476 ui->pattern_timeline->update(0);
478 ui->song_timeline->redraw();
479 ui->pattern_timeline->redraw();
481 ui->play_button->label("@>");
482 ui->play_button->redraw();
484 ui->metronome->update(0);
489 void set_quant(int q){
490 switch(q){
491 case 0:
492 ui->qbutton4->state(0);
493 ui->qbutton8->state(0);
494 ui->qbutton16->state(0);
495 ui->qbutton32->state(0);
496 ui->qbutton64->state(0);
497 ui->qbutton128->state(0);
498 ui->qbutton0->state(1);
499 ui->piano_roll->set_qtick(1);
500 break;
501 case 4:
502 ui->qbutton4->state(1);
503 ui->qbutton8->state(0);
504 ui->qbutton16->state(0);
505 ui->qbutton32->state(0);
506 ui->qbutton64->state(0);
507 ui->qbutton128->state(0);
508 ui->qbutton0->state(0);
509 ui->piano_roll->set_qtick(128);
510 break;
511 case 8:
512 ui->qbutton4->state(0);
513 ui->qbutton8->state(1);
514 ui->qbutton16->state(0);
515 ui->qbutton32->state(0);
516 ui->qbutton64->state(0);
517 ui->qbutton128->state(0);
518 ui->qbutton0->state(0);
519 ui->piano_roll->set_qtick(64);
520 break;
521 case 16:
522 ui->qbutton4->state(0);
523 ui->qbutton8->state(0);
524 ui->qbutton16->state(1);
525 ui->qbutton32->state(0);
526 ui->qbutton64->state(0);
527 ui->qbutton128->state(0);
528 ui->qbutton0->state(0);
529 ui->piano_roll->set_qtick(32);
530 break;
531 case 32:
532 ui->qbutton4->state(0);
533 ui->qbutton8->state(0);
534 ui->qbutton16->state(0);
535 ui->qbutton32->state(1);
536 ui->qbutton64->state(0);
537 ui->qbutton128->state(0);
538 ui->qbutton0->state(0);
539 ui->piano_roll->set_qtick(16);
540 break;
541 case 64:
542 ui->qbutton4->state(0);
543 ui->qbutton8->state(0);
544 ui->qbutton16->state(0);
545 ui->qbutton32->state(0);
546 ui->qbutton64->state(1);
547 ui->qbutton128->state(0);
548 ui->qbutton0->state(0);
549 ui->piano_roll->set_qtick(8);
550 break;
551 case 128:
552 ui->qbutton4->state(0);
553 ui->qbutton8->state(0);
554 ui->qbutton16->state(0);
555 ui->qbutton32->state(0);
556 ui->qbutton64->state(0);
557 ui->qbutton128->state(1);
558 ui->qbutton0->state(0);
559 ui->piano_roll->set_qtick(4);
560 break;
565 void set_beats_per_measure(int n){
566 config.beats_per_measure = n;
567 ui->metronome->set_N(n);
568 ui->piano_roll->redraw();
569 ui->arranger->redraw();
570 ui->arranger->q_tick = n*TICKS_PER_BEAT;
571 ui->song_timeline->redraw();
572 ui->pattern_timeline->redraw();
575 void set_measures_per_phrase(int n){
576 config.measures_per_phrase = n;
577 ui->piano_roll->redraw();
578 ui->arranger->redraw();
579 ui->song_timeline->redraw();
580 ui->pattern_timeline->redraw();
583 void set_beats_per_minute(int n){
584 config.beats_per_minute = n;
585 set_bpm(n);
588 void set_measures_until_record(int n){
589 config.measures_until_record = n;
592 void set_alwayscopy(int v){
593 config.alwayscopy = v;
596 void set_autotrackname(int v){
597 config.autotrackname = v;
600 void set_passthru(int v){
601 config.passthru = v;
602 backend_set_passthru(v);
605 void set_playinsert(int v){
606 config.playinsert = v;
609 void set_recordonchan(int v){
610 config.recordonchan = v;
613 void set_playmove(int v){
614 config.playmove = v;
617 void set_follow(int v){
618 config.follow = v;
621 void set_recordmode(int n){
622 config.recordmode = n;
625 void set_robmode(int n){
626 config.robmode = n;
629 void set_defaultvelocity(int n){
630 config.defaultvelocity = n;
634 int scopeon=0;
635 void turnonscope(){
636 scopeon=1;
639 void turnoffscope(){
640 scopeon=0;
641 fltk::TextBuffer* ptr = ui->scope->buffer();
642 ptr->remove(0,ptr->length());
643 // ui->redraw();
646 void scope_print(const char* text){
647 if(scopeon){
648 ui->scope->append(text);
649 int N = ui->scope->buffer()->length();
650 ui->scope->scroll(N,0);
656 void show_song_edit(){
657 ui->pattern_edit->hide();
658 ui->pattern_buttons->hide();
659 ui->song_edit->activate();
660 ui->song_edit->show();
661 ui->song_edit->take_focus();
662 ui->song_buttons->show();
665 void show_pattern_edit(){
666 ui->song_edit->hide();
667 ui->song_edit->deactivate();
668 ui->song_buttons->hide();
669 ui->pattern_edit->take_focus();
670 ui->pattern_edit->show();
671 ui->pattern_buttons->show();
675 static int tool = 0;
676 //switch between normal, note off, portamento, and aftertouch
677 void toggle_tool(){
678 switch(tool){
679 case 0:
680 tool=1;
681 ui->tool_button->copy_label("80");
682 ui->tool_button->state(1);
683 break;
684 case 1:
685 tool=2;
686 ui->tool_button->copy_label("A0");
687 break;
688 case 2:
689 tool=3;
690 ui->tool_button->copy_label("po");
691 break;
692 case 3:
693 tool=0;
694 ui->tool_button->copy_label("tool");
695 ui->tool_button->state(0);
696 break;
702 void reset_song(){
703 clear();
705 track* t;
706 for(int i=0; i<16; i++){
707 t = new track();
708 t->head->track = i;
709 t->chan = i;
710 add_track(t);
713 set_rec_track(0);
714 ui->track_info->set_rec(0);
715 ui->track_info->update();
716 ui->action_window->hide();
721 void add_track(track* t){
722 tracks.push_back(t);
723 ui->track_info->add_track();
726 void remove_track(int n){
732 void init_gui(){
733 ui->arranger->layout();
734 ui->song_vscroll->value(0);
736 ui->song_hscroll->minimum(0);
737 ui->song_hscroll->maximum(1<<20);
739 ui->pattern_timeline->edit_flag = 1;
740 ui->pattern_timeline->zoom = 15;
741 ui->pattern_vscroll->minimum(12*75);
742 ui->pattern_vscroll->maximum(0);
743 ui->pattern_vscroll->value(300);
744 ui->pattern_vscroll->slider_size(50);
745 ui->pattern_hscroll->minimum(0);
746 ui->pattern_hscroll->maximum(1<<20);
747 ui->pattern_hscroll->value(0);