Genericificationalizeified AudioFilter (now Filter).
[ardour.git] / gtk2_ardour / midi_region_view.cc
1 /*
2     Copyright (C) 2001-2007 Paul Davis 
3     Author: Dave Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
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.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <cmath>
21 #include <cassert>
22 #include <algorithm>
23
24 #include <gtkmm.h>
25
26 #include <gtkmm2ext/gtk_ui.h>
27
28 #include <sigc++/signal.h>
29
30 #include <ardour/playlist.h>
31 #include <ardour/tempo.h>
32 #include <ardour/midi_region.h>
33 #include <ardour/midi_source.h>
34 #include <ardour/midi_diskstream.h>
35 #include <ardour/midi_events.h>
36 #include <ardour/midi_model.h>
37
38 #include "streamview.h"
39 #include "midi_region_view.h"
40 #include "midi_streamview.h"
41 #include "midi_time_axis.h"
42 #include "simplerect.h"
43 #include "simpleline.h"
44 #include "canvas-hit.h"
45 #include "public_editor.h"
46 #include "ghostregion.h"
47 #include "midi_time_axis.h"
48 #include "utils.h"
49 #include "midi_util.h"
50 #include "gui_thread.h"
51 #include "keyboard.h"
52
53 #include "i18n.h"
54
55 using namespace sigc;
56 using namespace ARDOUR;
57 using namespace PBD;
58 using namespace Editing;
59 using namespace ArdourCanvas;
60
61 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu,
62                                   Gdk::Color& basic_color)
63         : RegionView (parent, tv, r, spu, basic_color)
64         , _default_note_length(0.0)
65         , _active_notes(0)
66         , _delta_command(NULL)
67         , _command_mode(None)
68 {
69 }
70
71 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, 
72                                   Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
73         : RegionView (parent, tv, r, spu, basic_color, visibility)
74         , _default_note_length(0.0)
75         , _active_notes(0)
76         , _delta_command(NULL)
77         , _command_mode(None)
78 {
79 }
80
81 void
82 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
83 {
84         if (wfd)
85                 midi_region()->midi_source(0)->load_model();
86         
87         const Meter& m = trackview.session().tempo_map().meter_at(_region->start());
88         const Tempo& t = trackview.session().tempo_map().tempo_at(_region->start());
89         _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
90                         / m.beats_per_bar();
91
92         _model = midi_region()->midi_source(0)->model();
93         _enable_display = false;
94         
95         RegionView::init(basic_color, false);
96
97         compute_colors (basic_color);
98
99         reset_width_dependent_items ((double) _region->length() / samples_per_unit);
100
101         set_y_position_and_height (0, trackview.height);
102
103         region_muted ();
104         region_resized (BoundsChanged);
105         region_locked ();
106
107         _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
108
109         set_colors ();
110
111         _enable_display = true;
112
113         if (_model) {
114                 if (wfd) {
115                         redisplay_model();
116                 }
117                 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
118         }
119         
120         group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
121
122 }
123
124 bool
125 MidiRegionView::canvas_event(GdkEvent* ev)
126 {
127         enum State { None, Pressed, SelectDragging, AddDragging };
128         static int press_button = 0;
129         static State _state;
130
131         static double drag_start_x, drag_start_y;
132         static double last_x, last_y;
133         double event_x, event_y;
134
135         static ArdourCanvas::SimpleRect* drag_rect = NULL;
136         
137         if (trackview.editor.current_mouse_mode() != MouseNote)
138                 return false;
139
140         switch (ev->type) {
141         case GDK_KEY_PRESS:
142                 if (ev->key.keyval == GDK_Delete)
143                         start_remove_command();
144                 break;
145         
146         case GDK_KEY_RELEASE:
147                 if (_command_mode == Remove && ev->key.keyval == GDK_Delete) {
148                         delete_selection();
149                         apply_command();
150                 }
151                 break;
152
153         case GDK_BUTTON_PRESS:
154                 //group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK, ev->button.time);
155                 _state = Pressed;
156                 press_button = ev->button.button;
157                 //cerr << "PRESSED: " << press_button << endl;
158                 return true;
159         
160         case GDK_ENTER_NOTIFY:
161                 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
162                 Keyboard::magic_widget_grab_focus();
163                 group->grab_focus();
164                 break;
165         
166         case GDK_MOTION_NOTIFY:
167                 event_x = ev->motion.x;
168                 event_y = ev->motion.y;
169                 group->w2i(event_x, event_y);
170
171                 switch (_state) {
172                 case Pressed: // Drag start
173
174                         if (press_button == 1 && _command_mode != Remove) { // Select rect start
175                                 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
176                                                 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
177                                 last_x = event_x;
178                                 last_y = event_y;
179                                 drag_start_x = event_x;
180                                 drag_start_y = event_y;
181
182                                 drag_rect = new ArdourCanvas::SimpleRect(*group);
183                                 drag_rect->property_x1() = event_x;
184                                 drag_rect->property_y1() = event_y;
185                                 drag_rect->property_x2() = event_x;
186                                 drag_rect->property_y2() = event_y;
187                                 drag_rect->property_outline_what() = 0xFF;
188                                 drag_rect->property_outline_color_rgba()
189                                         = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
190                                 drag_rect->property_fill_color_rgba()
191                                         = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
192
193                                 _state = SelectDragging;
194                                 return true;
195
196                         } else if (press_button == 3) { // Add note drag start
197                                 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
198                                                 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
199                                 last_x = event_x;
200                                 last_y = event_y;
201                                 drag_start_x = event_x;
202                                 drag_start_y = event_y;
203                                 
204                                 nframes_t event_frame = midi_view()->editor.pixel_to_frame(event_x);
205                                 midi_view()->editor.snap_to(event_frame);
206
207                                 drag_rect = new ArdourCanvas::SimpleRect(*group);
208                                 drag_rect->property_x1() = midi_view()->editor.frame_to_pixel(event_frame);
209                                 
210                                 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
211                                 drag_rect->property_x2() = event_x;
212                                 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
213                                 drag_rect->property_outline_what() = 0xFF;
214                                 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
215
216                                 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
217
218                                 _state = AddDragging;
219                                 return true;
220                         }
221
222                         break;
223
224                 case SelectDragging: // Select rect motion
225                 case AddDragging: // Add note rect motion
226                         if (ev->motion.is_hint) {
227                                 int t_x;
228                                 int t_y;
229                                 GdkModifierType state;
230                                 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
231                                 event_x = t_x;
232                                 event_y = t_y;
233                         }
234
235                         if (_state == AddDragging) {
236                                 nframes_t event_frame = midi_view()->editor.pixel_to_frame(event_x);
237                                 midi_view()->editor.snap_to(event_frame);
238                                 event_x = midi_view()->editor.frame_to_pixel(event_frame);
239                         }
240
241                         if (drag_rect)
242                                 drag_rect->property_x2() = event_x;
243
244                         if (drag_rect && _state == SelectDragging)
245                                 drag_rect->property_y2() = event_y;
246
247                         last_x = event_x;
248                         last_y = event_y;
249
250                         return true;
251                 default:
252                         _state = None;
253                         break;
254                 }
255                 break;
256         
257         case GDK_BUTTON_RELEASE:
258                 event_x = ev->motion.x;
259                 event_y = ev->motion.y;
260                 group->w2i(event_x, event_y);
261                 group->ungrab(ev->button.time);
262                 switch (_state) {
263                 case Pressed: // Clicked
264                         if (ev->button.button == 1) {
265                                 clear_selection();
266                         } else if (ev->button.button == 3) {
267                                 nframes_t event_frame = midi_view()->editor.pixel_to_frame(event_x);
268                                 midi_view()->editor.snap_to(event_frame);
269                                 event_x = midi_view()->editor.frame_to_pixel(event_frame);
270                                 create_note_at(event_x, event_y, _default_note_length);
271                         }
272                         _state = None;
273                         return true;
274                 case SelectDragging: // Select drag done
275                         _state = None;
276                         delete drag_rect;
277                         drag_rect = NULL;
278                         return true;
279                 case AddDragging: // Add drag done
280                         _state = None;
281                         if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
282                                 create_note_at(drag_rect->property_x1(), drag_rect->property_y1(),
283                                                 trackview.editor.pixel_to_frame(
284                                                 drag_rect->property_x2() - drag_rect->property_x1()));
285                         }
286
287                         delete drag_rect;
288                         drag_rect = NULL;
289                         return true;
290                 default:
291                         break;
292                 }
293         
294         default:
295                 break;
296         }
297
298         return false;
299 }
300
301
302 /** Add a note to the model, and the view, at a canvas (click) coordinate */
303 void
304 MidiRegionView::create_note_at(double x, double y, double dur)
305 {
306         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
307         MidiStreamView* const view = mtv->midi_view();
308
309         double note = midi_stream_view()->y_to_note(y);
310
311         assert(note >= 0.0);
312         assert(note <= 127.0);
313
314         const nframes_t stamp = trackview.editor.pixel_to_frame (x);
315         assert(stamp >= 0);
316         //assert(stamp <= _region->length());
317
318         //const Meter& m = trackview.session().tempo_map().meter_at(stamp);
319         //const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
320         //double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
321
322         // Add a 1 beat long note (for now)
323         const MidiModel::Note new_note(stamp, dur, (uint8_t)note, 0x40);
324         
325         view->update_bounds(new_note.note());
326
327         MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
328         cmd->add(new_note);
329         _model->apply_command(cmd);
330         
331         //add_note(new_note);
332 }
333
334
335 void
336 MidiRegionView::clear_events()
337 {
338         for (std::vector<ArdourCanvas::Item*>::iterator i = _events.begin(); i != _events.end(); ++i)
339                 delete *i;
340         
341         _events.clear();
342 }
343
344
345 void
346 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
347 {
348         _model = model;
349
350         if (_enable_display)
351                 redisplay_model();
352 }
353
354
355 void
356 MidiRegionView::redisplay_model()
357 {
358         clear_events();
359         
360         if (_model) {
361                 begin_write();
362
363                 for (size_t i=0; i < _model->n_notes(); ++i)
364                         add_note(_model->note_at(i));
365
366                 end_write();
367         } else {
368                 warning << "MidiRegionView::redisplay_model called without a model" << endmsg;
369         }
370 }
371
372
373 MidiRegionView::~MidiRegionView ()
374 {
375         in_destructor = true;
376         end_write();
377
378         RegionViewGoingAway (this); /* EMIT_SIGNAL */
379 }
380
381
382 void
383 MidiRegionView::region_resized (Change what_changed)
384 {
385         RegionView::region_resized(what_changed);
386
387         if (what_changed & ARDOUR::PositionChanged) {
388         
389                 if (_enable_display)
390                         redisplay_model();
391         
392         } else if (what_changed & Change (StartChanged)) {
393
394                 //cerr << "MIDI RV START CHANGED" << endl;
395
396         } else if (what_changed & Change (LengthChanged)) {
397                 
398                 //cerr << "MIDI RV LENGTH CHANGED" << endl;
399         
400         }
401 }
402
403 void
404 MidiRegionView::reset_width_dependent_items (double pixel_width)
405 {
406         RegionView::reset_width_dependent_items(pixel_width);
407         assert(_pixel_width == pixel_width);
408                 
409         if (_enable_display)
410                 redisplay_model();
411 }
412
413 void
414 MidiRegionView::set_y_position_and_height (double y, double h)
415 {
416         RegionView::set_y_position_and_height(y, h - 1);
417
418         if (_enable_display)
419                 redisplay_model();
420
421         if (name_text) {
422                 name_text->raise_to_top();
423         }
424 }
425
426 GhostRegion*
427 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
428 {
429         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
430         assert(rtv);
431
432         double unit_position = _region->position () / samples_per_unit;
433         GhostRegion* ghost = new GhostRegion (atv, unit_position);
434
435         ghost->set_height ();
436         ghost->set_duration (_region->length() / samples_per_unit);
437         ghosts.push_back (ghost);
438
439         ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
440
441         return ghost;
442 }
443
444
445 /** Begin tracking note state for successive calls to add_event
446  */
447 void
448 MidiRegionView::begin_write()
449 {
450         _active_notes = new CanvasNote*[128];
451         for (unsigned i=0; i < 128; ++i)
452                 _active_notes[i] = NULL;
453 }
454
455
456 /** Destroy note state for add_event
457  */
458 void
459 MidiRegionView::end_write()
460 {
461         delete[] _active_notes;
462         _active_notes = NULL;
463 }
464
465
466 /** Add a MIDI event.
467  *
468  * This is used while recording, and handles displaying still-unresolved notes.
469  * Displaying an existing model is simpler, and done with add_note.
470  */
471 void
472 MidiRegionView::add_event (const MidiEvent& ev)
473 {
474         /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
475         for (size_t i=0; i < ev.size; ++i) {
476                 printf("%X ", ev.buffer[i]);
477         }
478         printf("\n\n");*/
479
480         ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
481         
482         if (midi_view()->note_mode() == Sustained) {
483                 if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
484                         const Byte& note = ev.buffer()[1];
485                         const double y1 = midi_stream_view()->note_to_y(note);
486
487                         CanvasNote* ev_rect = new CanvasNote(*this, *group);
488                         ev_rect->property_x1() = trackview.editor.frame_to_pixel (
489                                         (nframes_t)ev.time());
490                         ev_rect->property_y1() = y1;
491                         ev_rect->property_x2() = trackview.editor.frame_to_pixel (
492                                         _region->length());
493                         ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
494                         ev_rect->property_fill_color_rgba() = note_fill_color(ev.velocity());
495                         ev_rect->property_outline_color_rgba() = note_outline_color(ev.velocity());
496                         /* outline all but right edge */
497                         ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
498
499                         ev_rect->raise_to_top();
500
501                         _events.push_back(ev_rect);
502                         if (_active_notes)
503                                 _active_notes[note] = ev_rect;
504
505                 } else if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
506                         const Byte& note = ev.buffer()[1];
507                         if (_active_notes && _active_notes[note]) {
508                                 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time());
509                                 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
510                                 _active_notes[note] = NULL;
511                         }
512                 }
513         
514         } else if (midi_view()->note_mode() == Percussive) {
515                 const Byte& note = ev.buffer()[1];
516                 const double diamond_size = midi_stream_view()->note_height() / 2.0;
517                 const double x = trackview.editor.frame_to_pixel((nframes_t)ev.time());
518                 const double y = midi_stream_view()->note_to_y(note) + ((diamond_size-2) / 4.0);
519
520                 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
521                 ev_diamond->move(x, y);
522                 ev_diamond->show();
523                 ev_diamond->property_fill_color_rgba() = note_fill_color(ev.velocity());
524                 ev_diamond->property_outline_color_rgba() = note_outline_color(ev.velocity());
525                 
526                 _events.push_back(ev_diamond);
527         }
528 }
529
530
531 /** Extend active notes to rightmost edge of region (if length is changed)
532  */
533 void
534 MidiRegionView::extend_active_notes()
535 {
536         if (!_active_notes)
537                 return;
538
539         for (unsigned i=0; i < 128; ++i)
540                 if (_active_notes[i])
541                         _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
542 }
543
544
545 /** Add a MIDI note to the view (with duration).
546  *
547  * This does no 'realtime' note resolution, notes from a MidiModel have a
548  * duration so they can be drawn in full immediately.
549  */
550 void
551 MidiRegionView::add_note (const MidiModel::Note& note)
552 {
553         assert(note.time() >= 0);
554         assert(note.time() < _region->length());
555
556         ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
557         
558         if (midi_view()->note_mode() == Sustained) {
559                 const double y1 = midi_stream_view()->note_to_y(note.note());
560
561                 ArdourCanvas::SimpleRect * ev_rect = new CanvasNote(*this, *group, &note);
562                 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note.time());
563                 ev_rect->property_y1() = y1;
564                 ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note.end_time()));
565                 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
566
567                 ev_rect->property_fill_color_rgba() = note_fill_color(note.velocity());
568                 ev_rect->property_outline_color_rgba() = note_outline_color(note.velocity());
569                 ev_rect->property_outline_what() = (guint32) 0xF; // all edges
570
571                 ev_rect->show();
572                 _events.push_back(ev_rect);
573
574         } else if (midi_view()->note_mode() == Percussive) {
575                 const double diamond_size = midi_stream_view()->note_height() / 2.0;
576                 const double x = trackview.editor.frame_to_pixel((nframes_t)note.time());
577                 const double y = midi_stream_view()->note_to_y(note.note()) + ((diamond_size-2) / 4.0);
578
579                 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
580                 ev_diamond->move(x, y);
581                 ev_diamond->show();
582                 ev_diamond->property_fill_color_rgba() = note_fill_color(note.velocity());
583                 ev_diamond->property_outline_color_rgba() = note_outline_color(note.velocity());
584                 _events.push_back(ev_diamond);
585         }
586 }
587
588 void
589 MidiRegionView::delete_selection()
590 {
591         assert(_delta_command);
592
593         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
594                 if ((*i)->selected())
595                         _delta_command->remove(*(*i)->note());
596
597         _selection.clear();
598 }
599
600 void
601 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasMidiEvent* ev)
602 {
603         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
604                 if ((*i)->selected() && (*i) != ev)
605                         (*i)->selected(false);
606
607         _selection.clear();
608 }
609
610 void
611 MidiRegionView::unique_select(ArdourCanvas::CanvasMidiEvent* ev)
612 {
613         for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
614                 if ((*i) != ev)
615                         (*i)->selected(false);
616
617         _selection.clear();
618         _selection.insert(ev);
619         ev->selected(true);
620 }
621         
622 void
623 MidiRegionView::note_selected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
624 {
625         if ( ! add)
626                 clear_selection_except(ev);
627         
628         _selection.insert(ev);
629         ev->selected(true);
630 }
631
632
633 void
634 MidiRegionView::note_deselected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
635 {
636         if ( ! add)
637                 clear_selection_except(ev);
638         
639         _selection.erase(ev);
640         ev->selected(false);
641 }
642
643