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>
27 #include <fltk/Scrollbar.h>
29 #include <fltk/events.h>
42 extern std::vector
<track
*> tracks
;
44 extern struct conf config
;
48 #define SWAP(X,Y) tmp=X; X=Y; Y=tmp;
50 PianoRoll::PianoRoll(int x
, int y
, int w
, int h
, const char* label
= 0) : fltk::Widget(x
, y
, w
, h
, label
) {
72 resize_handle_width
= 4;
76 if(fakeh
< h
){fakeh
= h
;}
82 int PianoRoll::handle(int event
){
96 if(event_key()==fltk::DeleteKey
){
101 ui
->event_edit
->redraw();
104 if(event_state(CTRL
) && event_key()=='c'){
105 //printf("roll copy\n");
108 if(zoom_out_key(event_key(),event_state())){
111 set_zoom(30*(1<<zoom_n
)/16);
112 ui
->pattern_timeline
->zoom
= zoom
;
113 ui
->pattern_timeline
->update(get_play_position());
114 ui
->pattern_timeline
->redraw();
115 ui
->event_edit
->zoom
= zoom
;
116 ui
->event_edit
->redraw();
121 if(zoom_in_key(event_key(),event_state())){
124 set_zoom(30*(1<<zoom_n
)/16);
125 ui
->pattern_timeline
->zoom
= zoom
;
126 ui
->pattern_timeline
->update(get_play_position());
127 ui
->pattern_timeline
->redraw();
128 ui
->event_edit
->zoom
= zoom
;
129 ui
->event_edit
->redraw();
138 if(event_button()==1){//left mouse
139 if(e
==NULL
){//new note init
140 if(event_state()&fltk::SHIFT
){//begin box
146 box_t1
=xpix2tick(X
+scrollx
);
148 box_n1
=ypix2note(Y
+scrolly
,1);
153 insert_torig
= quantize(xpix2tick(X
+scrollx
));
154 insert_toffset
= q_tick
;
155 //new_orig_t = new_left_t;
156 insert_note
= ypix2note(Y
+scrolly
,1);
158 last_note
= insert_note
;
159 if(config
.playinsert
){
160 ui
->keyboard
->play_note(last_note
,0);
167 if(!(e
->selected
) && !(event_state()&fltk::SHIFT
)){
171 resize_arrow_color
= fltk::color(128,128,0);
173 if(over_rhandle(e
)){//resize
175 rresize_torig
= e
->tick
+e
->dur
;
178 else if(over_lhandle(e
)){//resize move
180 lresize_torig
= e
->tick
;
186 move_torig
= e
->tick
;
187 move_qoffset
= e
->tick
- quantize(e
->tick
);
189 move_toffset
= -move_qoffset
;
191 //move_offset = quantize(xpix2tick(X)) - move_torig - move_qoffset;
193 move_offset
= X
- tick2xpix(e
->tick
);
194 move_norig
= ypix2note(Y
+scrolly
,1);
197 last_note
= move_norig
;
199 ui
->keyboard
->play_note(last_note
,0);
204 else if(event_button()==2){//middle mouse
205 //button initiates paste
207 else if(event_button()==3){//right mouse
211 ui
->event_edit
->redraw();
213 else{//set up for deletion
214 if(!(e
->selected
) && !(event_state()&fltk::SHIFT
)){
219 resize_arrow_color
= fltk::color(120,60,58);
229 box_t2
= xpix2tick(X
+scrollx
);
230 box_n2
= ypix2note(Y
+scrolly
,1);
232 else if(insert_flag
){
233 insert_toffset
= quantize(xpix2tick(X
+scrollx
)+q_tick
) - insert_torig
;
234 if(insert_toffset
<=0){
235 insert_toffset
-= q_tick
;
237 insert_note
= ypix2note(Y
+scrolly
,1);
238 if(insert_note
!= last_note
){
239 if(config
.playinsert
){//play on insert
240 ui
->keyboard
->release_note(last_note
,0);
241 ui
->keyboard
->play_note(insert_note
,0);
243 last_note
= insert_note
;
247 move_toffset
= quantize(xpix2tick(X
- move_offset
)) - move_torig
;
248 move_noffset
= ypix2note(Y
+scrolly
,1) - move_norig
;
249 int N
= move_norig
+move_noffset
;
251 if(config
.playmove
){//play on move
252 ui
->keyboard
->release_note(last_note
,0);
253 ui
->keyboard
->play_note(N
,0);
258 else if(rresize_flag
){
259 rresize_toffset
= quantize(xpix2tick(X
+scrollx
))+q_tick
-rresize_torig
;
261 else if(lresize_flag
){
262 lresize_toffset
= quantize(xpix2tick(X
+scrollx
)) - lresize_torig
;
268 if(event_button()==1){
271 ui
->event_edit
->redraw();
274 else if(rresize_flag
){
278 ui
->event_edit
->redraw();
280 else if(lresize_flag
){
284 ui
->event_edit
->redraw();
286 else if(insert_flag
){
291 ui
->keyboard
->release_note(insert_note
,0);
292 ui
->keyboard
->redraw();
293 ui
->event_edit
->has
[0]=1;
294 ui
->event_edit
->has
[1]=1;
295 ui
->event_edit
->redraw();
296 ui
->event_menu
->redraw();
302 midi_track_off(cur_seqpat
->track
);
303 ui
->keyboard
->release_note(last_note
,0);
304 ui
->keyboard
->release_note(move_norig
+move_noffset
,0);
305 ui
->keyboard
->redraw();
306 ui
->event_edit
->redraw();
311 if(event_button()==3){
312 mevent
* over_n
= over_note();
313 if(delete_flag
&& over_n
){
314 if(over_n
->selected
){
316 midi_track_off(cur_seqpat
->track
);
317 ui
->event_edit
->redraw();
331 if(resize_e
!= e
|| resize_arrow
!= 1){
332 if(e
->selected
){resize_arrow_color
= fltk::color(128,128,0);}
333 else{resize_arrow_color
= fltk::color(95,58,119);}
336 resize_x
= tick2xpix(e
->tick
+ e
->dur
)-scrollx
-resize_handle_width
;
337 resize_y
= note2ypix(e
->value1
)-scrolly
;
341 else if(over_lhandle(e
)){
342 if(resize_e
!= e
|| resize_arrow
!= 1){
343 if(e
->selected
){resize_arrow_color
= fltk::color(128,128,0);}
344 else{resize_arrow_color
= fltk::color(95,58,119);}
347 resize_x
= tick2xpix(e
->tick
)+1 - scrollx
;
348 resize_y
= note2ypix(e
->value1
) - scrolly
;
353 if(resize_e
!= e
|| resize_arrow
!= 0){
361 if(resize_arrow
!= 0){
372 void PianoRoll::draw(){
374 fltk::push_clip(0,0,w(),h());
376 fltk::setcolor(fltk::GRAY05
);
377 fltk::fillrect(0,0,w(),h());
379 fltk::setcolor(fltk::GRAY20
);
380 for(int i
=12-scrolly
; i
<h(); i
+=12){
382 fltk::fillrect(0,i
,w(),1);
385 for(int i
=zoom
-scrollx
; i
<w(); i
+=zoom
){
387 fltk::fillrect(i
,0,1,h());
391 fltk::setcolor(fltk::GRAY30
);
392 for(int i
=12*5-scrolly
; i
<h(); i
+=12*7){
394 fltk::fillrect(0,i
,w(),1);
398 fltk::setcolor(fltk::GRAY50
);
399 for(int i
=zoom
*4-scrollx
; i
<w(); i
+=zoom
*4){
401 fltk::fillrect(i
,0,1,h());
405 fltk::setcolor(fltk::WHITE
);
406 int M
= config
.beats_per_measure
;
407 for(int i
=zoom
*4*M
-scrollx
; i
<w(); i
+=zoom
*4*M
){
409 fltk::fillrect(i
,0,1,h());
413 fltk::setcolor(fltk::color(128,0,0));
414 int rightend
= tick2xpix(cur_seqpat
->dur
)-scrollx
;
415 if(rightend
>=0 && rightend
< w()){
416 fltk::fillrect(rightend
,0,1,h());
419 fltk::setcolor(fltk::color(128,128,0));
420 fltk::fillrect(0,12*40-scrolly
,w(),1);
424 fltk::setcolor(fltk::BLUE
);
425 int T1
= insert_torig
;
426 int T2
= T1
+ insert_toffset
;
427 if(T1
>T2
){SWAP(T1
,T2
);}
428 int X
= tick2xpix(T1
)+1 - scrollx
;
429 int Y
= note2ypix(insert_note
) - scrolly
;
430 int W
= tick2xpix(T2
)-scrollx
- X
;
431 fltk::fillrect(X
,Y
,W
,11);
435 fltk::setcolor(fltk::MAGENTA
);
436 mevent
* ptr
= cur_seqpat
->p
->events
->next
;
438 if(ptr
->type
== MIDI_NOTE_ON
&& ptr
->selected
){
439 int X
= tick2xpix(ptr
->tick
+move_toffset
)+1-scrollx
;
440 int Y
= note2ypix(ptr
->value1
+move_noffset
)-scrolly
;
441 int W
= tick2xpix(ptr
->dur
);
442 fltk::fillrect(X
,Y
,W
-1,1);
443 fltk::fillrect(X
,Y
+11,W
-1,1);
444 fltk::fillrect(X
,Y
,1,11);
445 fltk::fillrect(X
+W
-2,Y
,1,11);
454 mevent
* e
= cur_seqpat
->p
->events
->next
;
456 fltk::Color c1
,c2
,c3
;
459 if(e
->type
== MIDI_NOTE_ON
){
460 //fltk::fillrect(tick2xpix(e->tick),note2ypix(e->value),e->dur,11);
462 int R1
= rresize_flag
&&e
->selected
? rresize_toffset
: 0;
463 int R2
= lresize_flag
&&e
->selected
? lresize_toffset
: 0;
465 int T1
= e
->tick
+ R2
;
466 int T2
= e
->tick
+e
->dur
+ R1
;
468 if(T1
>= T2
-q_tick
&& e
->selected
){
473 else if(lresize_flag
){
474 T2
= e
->tick
+ e
->dur
;
479 int X
= tick2xpix(T1
) + 1 - scrollx
;
480 int Y
= note2ypix(e
->value1
) - scrolly
;
482 int W
= tick2xpix(T2
)-scrollx
- X
;
483 get_event_color(e
,&c1
,&c2
,&c3
);
485 if(!(X
+W
<0 || X
> w())){
488 fltk::fillrect(X
+1,Y
+1,W
-1,10);
491 fltk::fillrect(X
,Y
+11,W
,1);
492 fltk::fillrect(X
+W
-1,Y
+1,1,11);
495 fltk::fillrect(X
,Y
,W
,1);
496 fltk::fillrect(X
,Y
,1,11);
503 if(!rresize_flag
&& !lresize_flag
){
504 if(resize_arrow
> 0){
505 setcolor(resize_arrow_color
);
507 int W
= resize_handle_width
;
514 addvertex(X
+W
,Y
+H
/2);
517 else if(resize_arrow
< 0){
518 setcolor(resize_arrow_color
);
520 int W
= resize_handle_width
;
534 fltk::setcolor(fltk::GREEN
);
552 fltk::fillrect(X1
,Y1
,X2
-X1
,1);
553 fltk::fillrect(X1
,Y1
,1,Y2
-Y1
);
554 fltk::fillrect(X2
,Y1
,1,Y2
-Y1
);
555 fltk::fillrect(X1
,Y2
,X2
-X1
,1);
564 void PianoRoll::scrollTo(int X
, int Y
){
570 if(is_backend_playing() && config
.follow
){
571 int pos
= tick2xpix(get_play_position()) - cur_seqpat
->tick
;
572 if(pos
< X
|| pos
> X
+ w() - 30 - 30){
573 ui
->pattern_hscroll
->value(scrollx
);
580 cur_seqpat
->scrollx
= X
;
581 cur_seqpat
->scrolly
= Y
;
584 ui
->pattern_hscroll
->value(X
);
585 ui
->pattern_hscroll
->redraw();
586 ui
->pattern_vscroll
->value(Y
);
587 ui
->pattern_timeline
->scroll
= X
;
588 ui
->pattern_timeline
->redraw();
589 ui
->event_edit
->scroll
= X
;
590 ui
->event_edit
->redraw();
591 ui
->keyboard
->scroll
= Y
;
592 ui
->keyboard
->redraw();
596 void PianoRoll::layout(){
606 fltk::Scrollbar
* sb
= ui
->pattern_vscroll
;
609 sb
->minimum(fakeh
-h());
611 if(sb
->value() > sb
->minimum()){
612 scrollTo(scrollx
,900-h());
615 int newsize
= M
-(fakeh
-h());
619 ui
->song_vscroll
->slider_size(60);
622 void PianoRoll::load(seqpat
* s
){
624 scrollTo(s
->scrollx
,s
->scrolly
);
625 cur_track
= tracks
[s
->track
];
626 ui
->pattern_timeline
->ticks_offset
= s
->tick
;
631 int PianoRoll::note2ypix(int note
){
632 int udy
= 6*(note
+ (note
+7)/12 + note
/12) + 12;
633 return 900 - udy
+ 1;
636 int PianoRoll::tick2xpix(int tick
){
637 return tick
*zoom
*4 / 128;
640 int PianoRoll::xpix2tick(int xpix
){
641 return xpix
*128 / (zoom
*4);
644 int PianoRoll::quantize(int tick
){
645 return tick
/q_tick
* q_tick
;
649 void PianoRoll::set_zoom(int z
){
655 mevent
* PianoRoll::over_note(){
656 mevent
* e
= cur_seqpat
->p
->events
->next
;
658 int X
= event_x()+scrollx
;
659 int Y
= event_y()+scrolly
;
663 if(e
->type
== MIDI_NOTE_ON
){
664 cy
= note2ypix(e
->value1
);
665 lx
= tick2xpix(e
->tick
);
666 rx
= tick2xpix(e
->tick
+e
->dur
);
667 if(X
> lx
&& X
< rx
&&
668 Y
< cy
+12 && Y
> cy
){
680 void PianoRoll::update(int pos
){
681 if(!is_backend_playing() || !cur_seqpat
){
684 int X1
= tick2xpix(pos
-cur_seqpat
->tick
);
685 int X2
= X1
- scrollx
;
687 scrollTo(X1
-50<0?0:X1
-50,scrolly
);
690 scrollTo(X1
-50,scrolly
);
695 void PianoRoll::unselect_all(){
696 mevent
* e
= cur_seqpat
->p
->events
;
698 if(e
->type
== MIDI_NOTE_ON
&& e
->selected
==1){
707 void PianoRoll::get_event_color(mevent
* e
, fltk::Color
* c1
, fltk::Color
* c2
, fltk::Color
* c3
){
713 *c1
= fltk::color(229,79,75);
714 *c2
= fltk::color(120,60,58);
715 *c3
= fltk::color(225,131,109);
726 if(T1
>T2
){SWAP(T1
,T2
);}
727 if(N1
<N2
){SWAP(N1
,N2
);}
728 if(e
->tick
+e
->dur
> T1
&& e
->tick
< T2
&& N
>= N2
&& N
<= N1
){
729 *c1
= fltk::color(108,229,75);
730 *c2
= fltk::color(71,120,59);
731 *c3
= fltk::color(108,229,75);
737 *c1
= fltk::color(255,248,47);
738 *c2
= fltk::color(140,137,46);
739 *c3
= fltk::color(232,255,37);
743 *c1
= fltk::color(169,75,229);
744 *c2
= fltk::color(95,58,119);
745 *c3
= fltk::color(198,109,225);
749 void PianoRoll::apply_box(){
750 mevent
* e
= cur_seqpat
->p
->events
->next
;
757 if(T1
>T2
){SWAP(T1
,T2
);}
758 if(N1
<N2
){SWAP(N1
,N2
);}
761 if(e
->type
== MIDI_NOTE_ON
&&
762 e
->tick
+e
->dur
> T1
&& e
->tick
< T2
&&
770 void PianoRoll::apply_insert(){
771 if(insert_note
> 127 || insert_note
< 0){
776 int T1
= insert_torig
;
777 int T2
= T1
+ insert_toffset
;
778 if(T1
>T2
){SWAP(T1
,T2
);}
784 pattern
* p
= cur_seqpat
->p
;
785 Command
* c
=new CreateNote(p
,insert_note
,config
.defaultvelocity
,T1
,T2
-T1
);
789 cur_track
->restate();
792 void PianoRoll::apply_delete(){
796 pattern
* p
= cur_seqpat
->p
;
799 e
= cur_seqpat
->p
->events
->next
;
802 if(e
->selected
&& e
->type
== MIDI_NOTE_ON
){
803 c
=new DeleteNote(p
,e
);
811 cur_track
->restate();
814 void PianoRoll::apply_move(){
815 if(move_toffset
==0 && move_noffset
==0){
819 pattern
* p
= cur_seqpat
->p
;
820 mevent
* e
= p
->events
->next
;
822 int K
= e
->value1
+move_noffset
;
823 int T
= e
->tick
+move_toffset
;
824 if(e
->type
== MIDI_NOTE_ON
&& e
->selected
&& (T
<0 || K
< 0 || K
> 127)){
836 for(int i
=0; i
<tracks
.size(); i
++){
840 if(e
->selected
&& e
->modified
== 0){
841 int K
= e
->value1
+ move_noffset
;
842 int T
= e
->tick
+ move_toffset
;
844 c
=new MoveNote(p
,e
,T
,K
);
855 if(e
->modified
){e
->modified
=0;}
859 cur_track
->restate();
862 void PianoRoll::apply_paste(){
868 void PianoRoll::apply_rresize(){
869 if(rresize_toffset
==0){
876 pattern
* p
= cur_seqpat
->p
;
883 if(e
->type
== MIDI_NOTE_ON
&& e
->selected
&& e
->modified
== 0){
886 int R
= rresize_toffset
;
890 c
=new ResizeNote(p
,e
,W
+R
);
899 if(e
->modified
){e
->modified
=0;}
903 cur_track
->restate();
907 void PianoRoll::apply_lresize(){
908 if(lresize_toffset
==0){
915 pattern
* p
= cur_seqpat
->p
;
921 if(e
->type
== MIDI_NOTE_ON
&& e
->selected
){
922 if(e
->tick
+ lresize_toffset
< 0){
932 if(e
->type
== MIDI_NOTE_ON
&& e
->selected
&& e
->modified
== 0){
936 int R
= lresize_toffset
;
942 mevent
* etmp
= e
->prev
;
943 c
=new ResizeNote(p
,e
,W
-R
);
947 c
=new MoveNote(p
,e
,T
+R
,e
->value1
);
957 if(e
->modified
){e
->modified
=0;}
961 cur_track
->restate();
973 int PianoRoll::over_rhandle(mevent
* e
){
974 int X
= event_x()+scrollx
;
975 int Y
= event_y()+scrolly
;
976 int X1
= tick2xpix(e
->tick
);
977 int X2
= X1
+ tick2xpix(e
->dur
);
978 int Y1
= note2ypix(e
->value1
);
981 if(X2
-X1
< resize_handle_width
*3){
985 return (Y
> Y1
&& Y
< Y2
&& X
< X2
&& X
> X2
- resize_handle_width
);
988 int PianoRoll::over_lhandle(mevent
* e
){
989 int X
= event_x()+scrollx
;
990 int Y
= event_y()+scrolly
;
991 int X1
= tick2xpix(e
->tick
);
992 int X2
= X1
+ tick2xpix(e
->dur
);
993 int Y1
= note2ypix(e
->value1
);
996 if(X2
-X1
< resize_handle_width
*3){
1000 return (Y
> Y1
&& Y
< Y2
&& X
< X1
+ resize_handle_width
+1 && X
> X1
+ 1);