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
40 #define CONFIG_FILENAME ".epichordrc"
43 extern std::vector
<track
*> tracks
;
49 char* config_filename
;
56 char* homepath
= getenv("HOME");
57 asprintf(&config_filename
,"%s/"CONFIG_FILENAME
,homepath
);
60 f
.open(config_filename
,fstream::in
);
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;
69 config
.playinsert
= 1;
70 config
.recordonchan
= 0;
73 config
.recordmode
= 0;
75 config
.defaultvelocity
= 96;
77 load_default_keymap();
82 config
.beats_per_measure
= 4;
83 config
.measures_per_phrase
= 4;
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
;}
103 f
.ignore(std::numeric_limits
<streamsize
>::max(),'\n');
112 f
.open(config_filename
,fstream::out
);
114 printf("save_config: Unable to open config file %s for saving.\n", config_filename
);
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
;
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
;
164 if(config
.robmode
== 0){
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;
173 int R
= s
->tick
+s
->dur
;
179 int L
= s
->next
->tick
;
184 c
= new CreateSeqpatBlank(s
->track
,T
,W
);
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
195 int W2
= s
->next
->tick
- s
->tick
;
200 c
= new ResizeSeqpat(s
,W
);
210 void playing_timeout_cb(void* v
){
211 int pos
= get_play_position();
214 reset_record_flags();
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
233 track
* t
= tracks
[get_rec_track()];
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
){
252 snprintf(report
,256,"%02x %02x %02x : note off - ch %d note %d vel %d\n",type
|chan
,val1
,val2
,chan
,val1
,val2
);
255 if(!is_backend_recording())
258 s
= tfind
<seqpat
>(t
->head
,tick
);
259 if(s
->tick
+s
->dur
< tick
){
264 //if(s->record_flag==1 && config.recordmode>0){show_song_edit();}
265 s
->record_check(config
.recordmode
);
267 c
=new CreateNoteOff(p
,val1
,val2
,tick
-s
->tick
);
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();
280 snprintf(report
,256,"%02x %02x %02x : note on - ch %d note %d vel %d\n",type
|chan
,val1
,val2
,chan
,val1
,val2
);
283 if(!is_backend_recording())
286 s
= tfind
<seqpat
>(t
->head
,tick
);
287 if(s
->tick
+s
->dur
< tick
){
292 // if(s->record_flag==1 && config.recordmode>0){show_song_edit();}
293 s
->record_check(config
.recordmode
);
295 c
=new CreateNoteOn(p
,val1
,val2
,tick
-s
->tick
,16);
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();
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
){
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;}
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;
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;}
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;}
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;}
345 if(!is_backend_recording())
348 // if(s->record_flag==1 && config.recordmode>0){show_song_edit();}
349 s
->record_check(config
.recordmode
);
351 c
=new CreateEvent(p
,type
,tick
,val1
,val2
);
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();
364 case 1://undefined (reserved) system common message
365 snprintf(report
,256,"%02x : undefined (reserved) system common message\n",type
|chan
);
367 case 2://song position pointer
368 snprintf(report
,256,"%02x %02x %02x : song position - %d \n",type
|chan
,val1
,val2
,(val2
<<7)|val1
);
371 snprintf(report
,256,"%02x %02x : song select - %d \n",type
|chan
,val1
,val1
);
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
);
377 case 6://tune request
378 snprintf(report
,256,"%02x : tune request\n",type
|chan
);
380 case 7://end of exclusive
381 snprintf(report
,256,"%02x : end of exclusive\n",type
|chan
);
383 case 8://timing clock
384 snprintf(report
,256,"%02x : timing clock\n",type
|chan
);
386 case 9://undefined (reserved) system common message
387 snprintf(report
,256,"%02x : undefined (reserved) system common message\n",type
|chan
);
390 snprintf(report
,256,"%02x : start\n",type
|chan
);
393 snprintf(report
,256,"%02x : continue\n",type
|chan
);
396 snprintf(report
,256,"%02x : stop\n",type
|chan
);
399 snprintf(report
,256,"%02x : undefined (reserved) system common message\n",type
|chan
);
401 case 14://active sensing
402 snprintf(report
,256,"%02x : active sensing\n",type
|chan
);
405 snprintf(report
,256,"%02x : reset\n",type
|chan
);
409 snprintf(report
,256,"%02x %02x : system exclusive - id %d ; data follows\n",type
|chan
,val1
,val1
);
411 scope_print(getsysexbuf());
412 scope_print("\nf7 : end of sysex\n");
422 //handle session events (LASH)
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");
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
);
447 fltk::repeat_timeout(0.1, playing_timeout_cb
, NULL
);
451 void start_monitor(){
452 fltk::add_timeout(0.1, playing_timeout_cb
, NULL
);
456 if(!is_backend_playing()){
458 ui
->play_button
->label("@||");
459 //fltk::add_timeout(0.01, playing_timeout_cb, NULL);
464 ui
->play_button
->label("@>");
470 //stops playback and sets the play position to zero
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
){
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);
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);
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);
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);
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);
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);
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);
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
;
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
){
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
){
617 void set_follow(int v
){
621 void set_recordmode(int n
){
622 config
.recordmode
= n
;
625 void set_robmode(int n
){
629 void set_defaultvelocity(int n
){
630 config
.defaultvelocity
= n
;
641 fltk::TextBuffer
* ptr
= ui
->scope
->buffer();
642 ptr
->remove(0,ptr
->length());
646 void scope_print(const char* text
){
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();
676 //switch between normal, note off, portamento, and aftertouch
681 ui
->tool_button
->copy_label("80");
682 ui
->tool_button
->state(1);
686 ui
->tool_button
->copy_label("A0");
690 ui
->tool_button
->copy_label("po");
694 ui
->tool_button
->copy_label("tool");
695 ui
->tool_button
->state(0);
706 for(int i
=0; i
<16; i
++){
714 ui
->track_info
->set_rec(0);
715 ui
->track_info
->update();
716 ui
->action_window
->hide();
721 void add_track(track
* t
){
723 ui
->track_info
->add_track();
726 void remove_track(int n
){
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);