Fixed division by zero when using extend block.
[epichord.git] / src / uihelper.cpp
blob2f5367bf3d2293862579e94e488b4b56d5aef6e3
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;
170 if(M!=0){
171 M = M*config.beats_per_measure*128;
173 else{
174 M = 4*config.beats_per_measure*128;
176 int P1 = pos/M*M;
177 int P2 = P1 + M;
178 int T = P1;
179 int R = s->tick+s->dur;
180 if(R > P1){
181 T = R;
183 int W = P2 - T;
184 if(s->next){
185 int L = s->next->tick;
186 if(L < P2){
187 W = L - T;
190 c = new CreateSeqpatBlank(s->track,T,W);
191 set_undo(c);
192 undo_push(1);
193 return s->next;
195 else if(config.robmode == 2){
196 int pos = get_play_position();
197 int M = config.measures_per_phrase;
198 if(M!=0){
199 M = M*config.beats_per_measure*128;
201 else{
202 M = 4*config.beats_per_measure*128;
204 int P = pos/M*M + M;//tick at next phrase boundary
205 int W = P - s->tick;
206 if(s->next){
207 int W2 = s->next->tick - s->tick;
208 if(W2 < W){
209 W=W2;
212 c = new ResizeSeqpat(s,W);
213 set_undo(c);
214 undo_push(1);
215 return prev->next;
221 int last_pos=0;
222 void playing_timeout_cb(void* v){
223 int pos = get_play_position();
225 if(pos < last_pos){
226 reset_record_flags();
228 last_pos = pos;
230 if(config.follow){
231 ui->arranger->update(pos);
232 ui->piano_roll->update(pos);
234 ui->song_timeline->update(pos);
235 ui->pattern_timeline->update(pos);
236 ui->metronome->update(pos);
238 //check for midi input
239 int tick;
240 int chan;
241 int type;
242 int val1;
243 int val2;
245 track* t = tracks[get_rec_track()];
246 Command* c;
247 seqpat* s;
248 pattern* p;
250 char report[256];
252 while(recv_midi(&chan,&tick,&type,&val1,&val2)){
254 if(config.recordonchan){
255 for(int i=0; i<tracks.size(); i++){
256 if(tracks[i]->chan == chan){
257 t = tracks[i];
262 switch(type){
263 case 0x80://note off
264 snprintf(report,256,"%02x %02x %02x : note off - ch %d note %d vel %d\n",type|chan,val1,val2,chan,val1,val2);
265 scope_print(report);
267 if(!is_backend_recording())
268 break;
270 s = tfind<seqpat>(t->head,tick);
271 if(s->tick+s->dur < tick){
272 s = rob_check(s);
273 if(!s){continue;}
276 //if(s->record_flag==1 && config.recordmode>0){show_song_edit();}
277 s->record_check(config.recordmode);
278 p = s->p;
279 c=new CreateNoteOff(p,val1,val2,tick-s->tick);
280 set_undo(c);
281 undo_push(1);
282 if(ui->piano_roll->visible()){
283 ui->piano_roll->redraw();
284 ui->event_edit->redraw();
285 if(ui->event_edit->cur_seqpat == s){ui->event_edit->has[1]=1;}
286 ui->event_menu->redraw();
288 if(ui->arranger->visible())
289 ui->arranger->redraw();
290 break;
291 case 0x90://note on
292 snprintf(report,256,"%02x %02x %02x : note on - ch %d note %d vel %d\n",type|chan,val1,val2,chan,val1,val2);
293 scope_print(report);
295 if(!is_backend_recording())
296 break;
298 s = tfind<seqpat>(t->head,tick);
299 if(s->tick+s->dur < tick){
300 s = rob_check(s);
301 if(!s){continue;}
304 // if(s->record_flag==1 && config.recordmode>0){show_song_edit();}
305 s->record_check(config.recordmode);
306 p = s->p;
307 c=new CreateNoteOn(p,val1,val2,tick-s->tick,16);
308 set_undo(c);
309 undo_push(1);
310 if(ui->piano_roll->visible()){
311 ui->piano_roll->redraw();
312 ui->event_edit->redraw();
313 if(ui->event_edit->cur_seqpat == s){ui->event_edit->has[0]=1;}
314 ui->event_menu->redraw();
316 if(ui->arranger->visible())
317 ui->arranger->redraw();
318 break;
319 case 0xa0://aftertouch
320 case 0xb0://controller
321 case 0xc0://program change
322 case 0xd0://channel pressure
323 case 0xe0://pitch wheel
325 s = tfind<seqpat>(t->head,tick);
326 if(s->tick+s->dur < tick){
327 s = rob_check(s);
328 if(!s){continue;}
331 switch(type){
332 case 0xa0:
333 snprintf(report,256,"%02x %02x %02x : aftertouch - ch %d note %d %d\n",type|chan,val1,val2,chan,val1,val2);
334 if(ui->event_edit->cur_seqpat == s){ui->event_edit->has[2]=1;}
335 break;
336 case 0xb0:
337 snprintf(report,256,"%02x %02x %02x : controller change - ch %d cntr %d val %d\n",type|chan,val1,val2,chan,val1,val2);
338 if(ui->event_edit->cur_seqpat == s){
339 ui->event_edit->has[val1+6]=1;
341 break;
342 case 0xc0:
343 snprintf(report,256,"%02x %02x : program change - ch %d pgrm %d \n",type|chan,val1,chan,val1);
344 if(ui->event_edit->cur_seqpat == s){ui->event_edit->has[3]=1;}
345 break;
346 case 0xd0:
347 snprintf(report,256,"%02x %02x : channel pressure - ch %d val %d \n",type|chan,val1,chan,val1);
348 if(ui->event_edit->cur_seqpat == s){ui->event_edit->has[4]=1;}
349 break;
350 case 0xe0:
351 snprintf(report,256,"%02x %02x %02x : pitch wheel - ch %d val %d \n",type|chan,val1,val2,chan,(val2<<7)|val1);
352 if(ui->event_edit->cur_seqpat == s){ui->event_edit->has[5]=1;}
353 break;
355 scope_print(report);
357 if(!is_backend_recording())
358 break;
360 // if(s->record_flag==1 && config.recordmode>0){show_song_edit();}
361 s->record_check(config.recordmode);
362 p = s->p;
363 c=new CreateEvent(p,type,tick,val1,val2);
364 set_undo(c);
365 undo_push(1);
366 if(ui->piano_roll->visible()){
367 ui->piano_roll->redraw();
368 ui->event_edit->redraw();
369 ui->event_menu->redraw();
371 if(ui->arranger->visible())
372 ui->arranger->redraw();
373 break;
374 case 0xf0:
375 switch(chan){
376 case 1://undefined (reserved) system common message
377 snprintf(report,256,"%02x : undefined (reserved) system common message\n",type|chan);
378 break;
379 case 2://song position pointer
380 snprintf(report,256,"%02x %02x %02x : song position - %d \n",type|chan,val1,val2,(val2<<7)|val1);
381 break;
382 case 3://song select
383 snprintf(report,256,"%02x %02x : song select - %d \n",type|chan,val1,val1);
384 break;
385 case 4://undefined (reserved) system common message
386 case 5://undefined (reserved) system common message
387 snprintf(report,256,"%02x : undefined (reserved) system common message\n",type|chan);
388 break;
389 case 6://tune request
390 snprintf(report,256,"%02x : tune request\n",type|chan);
391 break;
392 case 7://end of exclusive
393 snprintf(report,256,"%02x : end of exclusive\n",type|chan);
394 break;
395 case 8://timing clock
396 snprintf(report,256,"%02x : timing clock\n",type|chan);
397 break;
398 case 9://undefined (reserved) system common message
399 snprintf(report,256,"%02x : undefined (reserved) system common message\n",type|chan);
400 break;
401 case 10://start
402 snprintf(report,256,"%02x : start\n",type|chan);
403 break;
404 case 11://continue
405 snprintf(report,256,"%02x : continue\n",type|chan);
406 break;
407 case 12://stop
408 snprintf(report,256,"%02x : stop\n",type|chan);
409 break;
410 case 13://undefined
411 snprintf(report,256,"%02x : undefined (reserved) system common message\n",type|chan);
412 break;
413 case 14://active sensing
414 snprintf(report,256,"%02x : active sensing\n",type|chan);
415 break;
416 case 15://reset
417 snprintf(report,256,"%02x : reset\n",type|chan);
418 break;
420 if(chan==0){
421 snprintf(report,256,"%02x %02x : system exclusive - id %d ; data follows\n",type|chan,val1,val1);
422 scope_print(report);
423 scope_print(getsysexbuf());
424 scope_print("\nf7 : end of sysex\n");
426 else{
427 scope_print(report);
434 //handle session events (LASH)
435 int ret;
436 char* session_string;
437 char* filename_string;
438 ret=backend_session_process();
439 while(ret != SESSION_NOMORE){
440 session_string=get_session_string();
441 filename_string = (char*)malloc(strlen(session_string)+16);
442 strcpy(filename_string,session_string);
443 strcat(filename_string,"/song.epi");
444 switch(ret){
445 case SESSION_SAVE: save(filename_string); break;
446 case SESSION_LOAD: load(filename_string); break;
447 case SESSION_QUIT: ui->main_window->hide(); break;
448 case SESSION_UNHANDLED: break;
450 free(session_string);
451 ret=backend_session_process();
455 if(is_backend_playing()){
456 fltk::repeat_timeout(0.005, playing_timeout_cb, NULL);
458 else{
459 fltk::repeat_timeout(0.1, playing_timeout_cb, NULL);
463 void start_monitor(){
464 fltk::add_timeout(0.1, playing_timeout_cb, NULL);
467 void press_play(){
468 if(!is_backend_playing()){
469 start_backend();
470 ui->play_button->label("@||");
471 //fltk::add_timeout(0.01, playing_timeout_cb, NULL);
473 else{
474 pause_backend();
475 all_notes_off();
476 ui->play_button->label("@>");
480 void press_stop(){
482 //stops playback and sets the play position to zero
483 pause_backend();
484 reset_backend(0);
485 all_notes_off();
487 ui->song_timeline->update(0);
488 ui->pattern_timeline->update(0);
490 ui->song_timeline->redraw();
491 ui->pattern_timeline->redraw();
493 ui->play_button->label("@>");
494 ui->play_button->redraw();
496 ui->metronome->update(0);
501 void set_quant(int q){
502 switch(q){
503 case 0:
504 ui->qbutton4->state(0);
505 ui->qbutton8->state(0);
506 ui->qbutton16->state(0);
507 ui->qbutton32->state(0);
508 ui->qbutton64->state(0);
509 ui->qbutton128->state(0);
510 ui->qbutton0->state(1);
511 ui->piano_roll->set_qtick(1);
512 break;
513 case 4:
514 ui->qbutton4->state(1);
515 ui->qbutton8->state(0);
516 ui->qbutton16->state(0);
517 ui->qbutton32->state(0);
518 ui->qbutton64->state(0);
519 ui->qbutton128->state(0);
520 ui->qbutton0->state(0);
521 ui->piano_roll->set_qtick(128);
522 break;
523 case 8:
524 ui->qbutton4->state(0);
525 ui->qbutton8->state(1);
526 ui->qbutton16->state(0);
527 ui->qbutton32->state(0);
528 ui->qbutton64->state(0);
529 ui->qbutton128->state(0);
530 ui->qbutton0->state(0);
531 ui->piano_roll->set_qtick(64);
532 break;
533 case 16:
534 ui->qbutton4->state(0);
535 ui->qbutton8->state(0);
536 ui->qbutton16->state(1);
537 ui->qbutton32->state(0);
538 ui->qbutton64->state(0);
539 ui->qbutton128->state(0);
540 ui->qbutton0->state(0);
541 ui->piano_roll->set_qtick(32);
542 break;
543 case 32:
544 ui->qbutton4->state(0);
545 ui->qbutton8->state(0);
546 ui->qbutton16->state(0);
547 ui->qbutton32->state(1);
548 ui->qbutton64->state(0);
549 ui->qbutton128->state(0);
550 ui->qbutton0->state(0);
551 ui->piano_roll->set_qtick(16);
552 break;
553 case 64:
554 ui->qbutton4->state(0);
555 ui->qbutton8->state(0);
556 ui->qbutton16->state(0);
557 ui->qbutton32->state(0);
558 ui->qbutton64->state(1);
559 ui->qbutton128->state(0);
560 ui->qbutton0->state(0);
561 ui->piano_roll->set_qtick(8);
562 break;
563 case 128:
564 ui->qbutton4->state(0);
565 ui->qbutton8->state(0);
566 ui->qbutton16->state(0);
567 ui->qbutton32->state(0);
568 ui->qbutton64->state(0);
569 ui->qbutton128->state(1);
570 ui->qbutton0->state(0);
571 ui->piano_roll->set_qtick(4);
572 break;
577 void set_beats_per_measure(int n){
578 config.beats_per_measure = n;
579 ui->metronome->set_N(n);
580 ui->piano_roll->redraw();
581 ui->arranger->redraw();
582 ui->arranger->q_tick = n*TICKS_PER_BEAT;
583 ui->song_timeline->redraw();
584 ui->pattern_timeline->redraw();
587 void set_measures_per_phrase(int n){
588 config.measures_per_phrase = n;
589 ui->piano_roll->redraw();
590 ui->arranger->redraw();
591 ui->song_timeline->redraw();
592 ui->pattern_timeline->redraw();
595 void set_beats_per_minute(int n){
596 config.beats_per_minute = n;
597 set_bpm(n);
600 void set_measures_until_record(int n){
601 config.measures_until_record = n;
604 void set_alwayscopy(int v){
605 config.alwayscopy = v;
608 void set_autotrackname(int v){
609 config.autotrackname = v;
612 void set_passthru(int v){
613 config.passthru = v;
614 backend_set_passthru(v);
617 void set_playinsert(int v){
618 config.playinsert = v;
621 void set_recordonchan(int v){
622 config.recordonchan = v;
625 void set_playmove(int v){
626 config.playmove = v;
629 void set_follow(int v){
630 config.follow = v;
633 void set_recordmode(int n){
634 config.recordmode = n;
637 void set_robmode(int n){
638 config.robmode = n;
641 void set_defaultvelocity(int n){
642 config.defaultvelocity = n;
646 int scopeon=0;
647 void turnonscope(){
648 scopeon=1;
651 void turnoffscope(){
652 scopeon=0;
653 fltk::TextBuffer* ptr = ui->scope->buffer();
654 ptr->remove(0,ptr->length());
655 // ui->redraw();
658 void scope_print(const char* text){
659 if(scopeon){
660 ui->scope->append(text);
661 int N = ui->scope->buffer()->length();
662 ui->scope->scroll(N,0);
668 void show_song_edit(){
669 ui->pattern_edit->hide();
670 ui->pattern_buttons->hide();
671 ui->song_edit->activate();
672 ui->song_edit->show();
673 ui->song_edit->take_focus();
674 ui->song_buttons->show();
677 void show_pattern_edit(){
678 ui->song_edit->hide();
679 ui->song_edit->deactivate();
680 ui->song_buttons->hide();
681 ui->pattern_edit->take_focus();
682 ui->pattern_edit->show();
683 ui->pattern_buttons->show();
687 static int tool = 0;
688 //switch between normal, note off, portamento, and aftertouch
689 void toggle_tool(){
690 switch(tool){
691 case 0:
692 tool=1;
693 ui->tool_button->copy_label("80");
694 ui->tool_button->state(1);
695 break;
696 case 1:
697 tool=2;
698 ui->tool_button->copy_label("A0");
699 break;
700 case 2:
701 tool=3;
702 ui->tool_button->copy_label("po");
703 break;
704 case 3:
705 tool=0;
706 ui->tool_button->copy_label("tool");
707 ui->tool_button->state(0);
708 break;
714 void reset_song(){
715 clear();
717 track* t;
718 for(int i=0; i<16; i++){
719 t = new track();
720 t->head->track = i;
721 t->chan = i;
722 add_track(t);
725 set_rec_track(0);
726 ui->track_info->set_rec(0);
727 ui->track_info->update();
728 ui->action_window->hide();
733 void add_track(track* t){
734 tracks.push_back(t);
735 ui->track_info->add_track();
738 void remove_track(int n){
744 void init_gui(){
745 ui->arranger->layout();
746 ui->song_vscroll->value(0);
748 ui->song_hscroll->minimum(0);
749 ui->song_hscroll->maximum(1<<20);
751 ui->pattern_timeline->edit_flag = 1;
752 ui->pattern_timeline->zoom = 15;
753 ui->pattern_vscroll->minimum(12*75);
754 ui->pattern_vscroll->maximum(0);
755 ui->pattern_vscroll->value(300);
756 ui->pattern_vscroll->slider_size(50);
757 ui->pattern_hscroll->minimum(0);
758 ui->pattern_hscroll->maximum(1<<20);
759 ui->pattern_hscroll->value(0);