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
24 #include <fltk/Group.h>
25 #include <fltk/Widget.h>
26 #include <fltk/events.h>
39 extern std::vector
<track
*> tracks
;
41 extern struct conf config
;
45 #define SWAP(X,Y) tmp=X; X=Y; Y=tmp;
47 PianoRoll::PianoRoll(int x
, int y
, int w
, int h
, const char* label
= 0) : fltk::Widget(x
, y
, w
, h
, label
) {
67 resize_handle_width
= 4;
70 int PianoRoll::handle(int event
){
84 if(event_key()==fltk::DeleteKey
){
89 ui
->event_edit
->redraw();
92 if(event_state(CTRL
) && event_key()=='c'){
93 //printf("roll copy\n");
96 if(zoom_out_key(event_key(),event_state())){
99 set_zoom(30*(1<<zoom_n
)/16);
100 ui
->pattern_timeline
->zoom
= zoom
;
101 ui
->pattern_timeline
->update(get_play_position());
102 ui
->pattern_timeline
->redraw();
103 ui
->event_edit
->zoom
= zoom
;
104 ui
->event_edit
->redraw();
109 if(zoom_in_key(event_key(),event_state())){
112 set_zoom(30*(1<<zoom_n
)/16);
113 ui
->pattern_timeline
->zoom
= zoom
;
114 ui
->pattern_timeline
->update(get_play_position());
115 ui
->pattern_timeline
->redraw();
116 ui
->event_edit
->zoom
= zoom
;
117 ui
->event_edit
->redraw();
126 if(event_button()==1){//left mouse
127 if(e
==NULL
){//new note init
128 if(event_state()&fltk::SHIFT
){//begin box
136 box_n1
=ypix2note(Y
,1);
141 insert_torig
= quantize(xpix2tick(event_x()));
142 insert_toffset
= q_tick
;
143 //new_orig_t = new_left_t;
144 insert_note
= ypix2note(event_y(),1);
146 last_note
= insert_note
;
147 if(config
.playinsert
){
148 ui
->keyboard
->play_note(last_note
,0);
155 if(!(e
->selected
) && !(event_state()&fltk::SHIFT
)){
159 resize_arrow_color
= fltk::color(128,128,0);
163 if(over_rhandle(e
,X
,Y
)){//resize
172 else if(over_lhandle(e
,X
,Y
)){//resize move
184 move_torig
= e
->tick
;
185 move_qoffset
= e
->tick
- quantize(e
->tick
);
187 move_toffset
= -move_qoffset
;
189 //move_offset = quantize(xpix2tick(X)) - move_torig - move_qoffset;
191 move_offset
= X
- tick2xpix(e
->tick
);
192 move_norig
= ypix2note(event_y(),1);
195 last_note
= move_norig
;
197 ui
->keyboard
->play_note(last_note
,0);
202 else if(event_button()==2){//middle mouse
203 //button initiates paste
205 else if(event_button()==3){//right mouse
209 ui
->event_edit
->redraw();
211 else{//set up for deletion
214 resize_arrow_color
= fltk::color(120,60,58);
225 box_t2
= xpix2tick(X
);
226 box_n2
= ypix2note(Y
,1);
228 else if(insert_flag
){
229 insert_toffset
= quantize(xpix2tick(X
)+q_tick
) - insert_torig
;
230 if(insert_toffset
<=0){
231 insert_toffset
-= q_tick
;
233 insert_note
= ypix2note(Y
,1);
234 if(insert_note
!= last_note
){
235 if(config
.playinsert
){//play on insert
236 ui
->keyboard
->release_note(last_note
,0);
237 ui
->keyboard
->play_note(insert_note
,0);
239 last_note
= insert_note
;
243 move_toffset
= quantize(xpix2tick(X
- move_offset
)) - move_torig
;
244 move_noffset
= ypix2note(Y
,1) - move_norig
;
245 int N
= move_norig
+move_noffset
;
247 if(config
.playmove
){//play on move
248 ui
->keyboard
->release_note(last_note
,0);
249 ui
->keyboard
->play_note(N
,0);
254 else if(rresize_flag
){
257 else if(lresize_flag
){
264 if(event_button()==1){
267 ui
->event_edit
->redraw();
270 else if(rresize_flag
){
273 else if(insert_flag
){
278 cur_seqpat
->restate();
279 ui
->keyboard
->release_note(insert_note
,0);
280 ui
->keyboard
->redraw();
281 ui
->event_edit
->has
[0]=1;
282 ui
->event_edit
->has
[1]=1;
283 ui
->event_edit
->redraw();
284 ui
->event_menu
->redraw();
290 int play_pos = get_play_position();
291 mevent* e = main_sel;
292 track* tr = tracks[cur_seqpat->track];
293 if(play_pos > e->tick && play_pos < e->tick + e->dur){
294 midi_note_off(e->value1,tr->chan,tr->port);
297 c=new MoveNote(cur_seqpat->p,main_sel,move_t,move_note);
301 //int old_note = e->value1;
302 int cur_chan
= tracks
[cur_seqpat
->track
]->chan
;
303 int cur_port
= tracks
[cur_seqpat
->track
]->port
;
304 //midi_note_off(old_note,cur_chan,cur_port);
306 cur_seqpat
->restate();
307 midi_track_off(cur_seqpat
->track
);
308 ui
->keyboard
->release_note(last_note
,0);
309 ui
->keyboard
->release_note(move_norig
+move_noffset
,0);
310 ui
->keyboard
->redraw();
311 ui
->event_edit
->redraw();
316 if(event_button()==3){
317 mevent
* over_n
= over_note();
318 if(delete_flag
&& over_n
){
319 if(over_n
->selected
){
321 midi_track_off(cur_seqpat
->track
);
322 cur_seqpat
->restate();
323 ui
->event_edit
->redraw();
336 if(over_rhandle(e
,X
,Y
)){
337 if(resize_e
!= e
|| resize_arrow
!= 1){
338 if(e
->selected
){resize_arrow_color
= fltk::color(128,128,0);}
339 else{resize_arrow_color
= fltk::color(95,58,119);}
342 resize_x
= tick2xpix(e
->tick
+ e
->dur
) - resize_handle_width
;
343 resize_y
= note2ypix(e
->value1
);
347 else if(over_lhandle(e
,X
,Y
)){
348 if(resize_e
!= e
|| resize_arrow
!= 1){
349 if(e
->selected
){resize_arrow_color
= fltk::color(128,128,0);}
350 else{resize_arrow_color
= fltk::color(95,58,119);}
353 resize_x
= tick2xpix(e
->tick
)+1;
354 resize_y
= note2ypix(e
->value1
);
359 if(resize_e
!= e
|| resize_arrow
!= 0){
367 if(resize_arrow
!= 0){
378 void PianoRoll::draw(){
379 fltk::setcolor(fltk::GRAY05
);
380 fltk::fillrect(0,0,w(),h());
382 fltk::setcolor(fltk::GRAY20
);
383 for(int i
=12; i
<h(); i
+=12){
384 fltk::drawline(0,i
,w(),i
);
386 for(int i
=zoom
; i
<w(); i
+=zoom
){
387 fltk::drawline(i
,0,i
,h());
390 fltk::setcolor(fltk::GRAY30
);
391 for(int i
=12*5; i
<h(); i
+=12*7){
392 fltk::drawline(0,i
,w(),i
);
395 fltk::setcolor(fltk::GRAY50
);
396 for(int i
=zoom
*4; i
<w(); i
+=zoom
*4){
397 fltk::drawline(i
,0,i
,h());
400 fltk::setcolor(fltk::WHITE
);
401 int M
= config
.beats_per_measure
;
402 for(int i
=zoom
*4*M
; i
<w(); i
+=zoom
*4*M
){
403 fltk::fillrect(i
,0,1,h());
406 fltk::setcolor(fltk::color(128,0,0));
407 int rightend
= tick2xpix(cur_seqpat
->dur
);
408 fltk::fillrect(rightend
,0,1,h());
410 fltk::setcolor(fltk::color(128,128,0));
411 fltk::drawline(0,12*40,w(),12*40);
415 fltk::setcolor(fltk::BLUE
);
416 int T1
= insert_torig
;
417 int T2
= T1
+ insert_toffset
;
418 if(T1
>T2
){SWAP(T1
,T2
);}
419 int X
= tick2xpix(T1
)+1;
420 int Y
= note2ypix(insert_note
);
421 int W
= tick2xpix(T2
) - X
;
422 fltk::fillrect(X
,Y
,W
,11);
426 fltk::setcolor(fltk::MAGENTA
);
427 mevent
* ptr
= cur_seqpat
->p
->events
->next
;
429 if(ptr
->type
== MIDI_NOTE_ON
&& ptr
->selected
){
430 int X
= tick2xpix(ptr
->tick
+move_toffset
)+1;
431 int Y
= note2ypix(ptr
->value1
+move_noffset
);
432 int W
= tick2xpix(ptr
->dur
);
433 fltk::fillrect(X
,Y
,W
-1,1);
434 fltk::fillrect(X
,Y
+11,W
-1,1);
435 fltk::fillrect(X
,Y
,1,11);
436 fltk::fillrect(X
+W
-2,Y
,1,11);
445 mevent
* e
= cur_seqpat
->p
->events
->next
;
447 fltk::Color c1
,c2
,c3
;
450 if(e
->type
== MIDI_NOTE_ON
){
451 //fltk::fillrect(tick2xpix(e->tick),note2ypix(e->value),e->dur,11);
452 int X
= tick2xpix(e
->tick
) + 1;
453 int Y
= note2ypix(e
->value1
);
454 int W
= tick2xpix(e
->tick
+e
->dur
) - X
;
455 get_event_color(e
,&c1
,&c2
,&c3
);
458 fltk::fillrect(X
+1,Y
+1,W
-1,10);
461 fltk::fillrect(X
,Y
+11,W
,1);
462 fltk::fillrect(X
+W
-1,Y
+1,1,11);
465 fltk::fillrect(X
,Y
,W
,1);
466 fltk::fillrect(X
,Y
,1,11);
472 if(!rresize_flag
&& !lresize_flag
){
473 if(resize_arrow
> 0){
474 setcolor(resize_arrow_color
);
476 int W
= resize_handle_width
;
483 addvertex(X
+W
,Y
+H
/2);
486 else if(resize_arrow
< 0){
487 setcolor(resize_arrow_color
);
489 int W
= resize_handle_width
;
503 fltk::setcolor(fltk::GREEN
);
521 fltk::fillrect(X1
,Y1
,X2
-X1
,1);
522 fltk::fillrect(X1
,Y1
,1,Y2
-Y1
);
523 fltk::fillrect(X2
,Y1
,1,Y2
-Y1
);
524 fltk::fillrect(X1
,Y2
,X2
-X1
,1);
530 static int kludge
= 4; //very powerful magic
531 void PianoRoll::layout(){
533 /* the kludge is used so the fltk::ScrollGroup can update
534 widgets not contained within it. Better solution, the
535 scrollgroup could do its callback if it scrolls.
536 Subclassing fltk::ScrollGroup to add this behavior failed. */
542 ui
->pattern_timeline
->zoom
= zoom
;
543 ui
->event_edit
->zoom
= zoom
;
546 int W
= tick2xpix(cur_seqpat
->dur
);
550 int wp
= ui
->pattern_scroll
->w();
555 int hp
= ui
->pattern_scroll
->h();
560 int xp
= ui
->pattern_scroll
->xposition();
561 int yp
= ui
->pattern_scroll
->yposition();
565 ui
->pattern_scroll
->scrollTo(xp
,yp
);
568 ui
->pattern_timeline
->scroll
= xp
;
569 ui
->event_edit
->scroll
= xp
;
570 ui
->keyboard
->scroll
= yp
;
573 cur_seqpat
->scrolly
= yp
;
574 cur_seqpat
->scrollx
= xp
;
578 ui
->pattern_timeline
->redraw();
579 ui
->event_edit
->redraw();
582 ui
->keyboard
->redraw();
591 void PianoRoll::load(seqpat
* s
){
593 cur_track
= tracks
[s
->track
];
594 int W
= tick2xpix(s
->dur
);
597 ui
->pattern_timeline
->ticks_offset
= s
->tick
;
600 int PianoRoll::note2ypix(int note
){
601 int udy
= 6*(note
+ (note
+7)/12 + note
/12) + 12;
602 return h() - udy
+ 1;
605 int PianoRoll::tick2xpix(int tick
){
606 return tick
*zoom
*4 / 128;
609 int PianoRoll::xpix2tick(int xpix
){
610 return xpix
*128 / (zoom
*4);
613 int PianoRoll::quantize(int tick
){
614 return tick
/q_tick
* q_tick
;
618 void PianoRoll::set_zoom(int z
){
621 //int W = tick2xpix(cur_seqpat->dur);
626 mevent
* PianoRoll::over_note(){
627 mevent
* e
= cur_seqpat
->p
->events
->next
;
631 if(e
->type
== MIDI_NOTE_ON
){
632 cy
= note2ypix(e
->value1
);
633 lx
= tick2xpix(e
->tick
);
634 rx
= tick2xpix(e
->tick
+e
->dur
);
635 if(event_x() > lx
&& event_x() < rx
&&
636 event_y() < cy
+12 && event_y() > cy
){
648 void PianoRoll::update(int pos
){
649 if(!is_backend_playing() || !cur_seqpat
){
652 int wp
= ui
->pattern_scroll
->w();
653 int xp
= ui
->pattern_scroll
->xposition();
654 int yp
= ui
->pattern_scroll
->yposition();
655 int X1
= tick2xpix(pos
-cur_seqpat
->tick
);
661 ui
->pattern_scroll
->scrollTo(X1
-50<0?0:X1
-50,yp
);
664 ui
->pattern_scroll
->scrollTo(X1
-50,yp
);
669 void PianoRoll::unselect_all(){
670 mevent
* e
= cur_seqpat
->p
->events
;
672 if(e
->type
== MIDI_NOTE_ON
&& e
->selected
==1){
681 void PianoRoll::get_event_color(mevent
* e
, fltk::Color
* c1
, fltk::Color
* c2
, fltk::Color
* c3
){
687 *c1
= fltk::color(229,79,75);
688 *c2
= fltk::color(120,60,58);
689 *c3
= fltk::color(225,131,109);
700 if(T1
>T2
){SWAP(T1
,T2
);}
701 if(N1
<N2
){SWAP(N1
,N2
);}
702 if(e
->tick
+e
->dur
> T1
&& e
->tick
< T2
&& N
>= N2
&& N
<= N1
){
703 *c1
= fltk::color(108,229,75);
704 *c2
= fltk::color(71,120,59);
705 *c3
= fltk::color(108,229,75);
711 *c1
= fltk::color(255,248,47);
712 *c2
= fltk::color(140,137,46);
713 *c3
= fltk::color(232,255,37);
717 *c1
= fltk::color(169,75,229);
718 *c2
= fltk::color(95,58,119);
719 *c3
= fltk::color(198,109,225);
723 void PianoRoll::apply_box(){
724 mevent
* e
= cur_seqpat
->p
->events
->next
;
731 if(T1
>T2
){SWAP(T1
,T2
);}
732 if(N1
<N2
){SWAP(N1
,N2
);}
735 if(e
->type
== MIDI_NOTE_ON
&&
736 e
->tick
+e
->dur
> T1
&& e
->tick
< T2
&&
744 void PianoRoll::apply_insert(){
745 if(insert_note
> 127 || insert_note
< 0){
750 int T1
= insert_torig
;
751 int T2
= T1
+ insert_toffset
;
752 if(T1
>T2
){SWAP(T1
,T2
);}
758 pattern
* p
= cur_seqpat
->p
;
759 Command
* c
=new CreateNote(p
,insert_note
,127,T1
,T2
-T1
);
764 void PianoRoll::apply_delete(){
768 pattern
* p
= cur_seqpat
->p
;
771 e
= cur_seqpat
->p
->events
->next
;
774 if(e
->selected
&& e
->type
== MIDI_NOTE_ON
){
775 c
=new DeleteNote(p
,e
);
785 void PianoRoll::apply_move(){
786 if(move_toffset
==0 && move_noffset
==0){
790 pattern
* p
= cur_seqpat
->p
;
791 mevent
* e
= p
->events
->next
;
793 int K
= e
->value1
+move_noffset
;
794 int T
= e
->tick
+move_toffset
;
795 if(e
->type
== MIDI_NOTE_ON
&& e
->selected
&& (T
<0 || K
< 0 || K
> 127)){
807 for(int i
=0; i
<tracks
.size(); i
++){
811 if(e
->selected
&& e
->modified
== 0){
812 int K
= e
->value1
+ move_noffset
;
813 int T
= e
->tick
+ move_toffset
;
815 c
=new MoveNote(p
,e
,T
,K
);
826 if(e
->modified
){e
->modified
=0;}
831 void PianoRoll::apply_paste(){
835 void PianoRoll::apply_rresize(){
839 void PianoRoll::apply_lresize(){
844 int PianoRoll::over_rhandle(mevent
* e
, int X
, int Y
){
845 int X1
= tick2xpix(e
->tick
);
846 int X2
= X1
+ tick2xpix(e
->dur
);
847 int Y1
= note2ypix(e
->value1
);
850 if(X2
-X1
< resize_handle_width
*3){
854 return (Y
> Y1
&& Y
< Y2
&& X
< X2
&& X
> X2
- resize_handle_width
);
857 int PianoRoll::over_lhandle(mevent
* e
, int X
, int Y
){
858 int X1
= tick2xpix(e
->tick
);
859 int X2
= X1
+ tick2xpix(e
->dur
);
860 int Y1
= note2ypix(e
->value1
);
863 if(X2
-X1
< resize_handle_width
*3){
867 return (Y
> Y1
&& Y
< Y2
&& X
< X1
+ resize_handle_width
+1 && X
> X1
+ 1);