Line up percussion hits correctly.
[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 "rgb_macros.h"
50 #include "gui_thread.h"
51
52 #include "i18n.h"
53
54 using namespace sigc;
55 using namespace ARDOUR;
56 using namespace PBD;
57 using namespace Editing;
58 using namespace ArdourCanvas;
59
60 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu,
61                                   Gdk::Color& basic_color)
62         : RegionView (parent, tv, r, spu, basic_color)
63         , _default_note_length(0.0)
64         , _active_notes(0)
65         , _delta_command(NULL)
66         , _command_mode(None)
67 {
68 }
69
70 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, 
71                                   Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
72         : RegionView (parent, tv, r, spu, basic_color, visibility)
73         , _default_note_length(0.0)
74         , _active_notes(0)
75         , _delta_command(NULL)
76         , _command_mode(None)
77 {
78 }
79
80 void
81 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
82 {
83         if (wfd)
84                 midi_region()->midi_source(0)->load_model();
85         
86         const Meter& m = trackview.session().tempo_map().meter_at(_region->start());
87         const Tempo& t = trackview.session().tempo_map().tempo_at(_region->start());
88         _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
89                         / m.beats_per_bar();
90
91         _model = midi_region()->midi_source(0)->model();
92         _enable_display = false;
93         
94         RegionView::init(basic_color, false);
95
96         compute_colors (basic_color);
97
98         reset_width_dependent_items ((double) _region->length() / samples_per_unit);
99
100         set_y_position_and_height (0, trackview.height);
101
102         region_muted ();
103         region_resized (BoundsChanged);
104         region_locked ();
105
106         _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
107
108         set_colors ();
109
110         _enable_display = true;
111
112         if (_model) {
113                 if (wfd) {
114                         redisplay_model();
115                 }
116                 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
117         }
118         
119         group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
120
121 }
122
123 bool
124 MidiRegionView::canvas_event(GdkEvent* ev)
125 {
126         enum State { None, Pressed, SelectDragging, AddDragging };
127         static int press_button = 0;
128         static State _state;
129
130         static double drag_start_x, drag_start_y;
131         static double last_x, last_y;
132         double event_x, event_y;
133
134         static ArdourCanvas::SimpleRect* drag_rect = NULL;
135         
136         if (trackview.editor.current_mouse_mode() != MouseNote)
137                 return false;
138
139         switch (ev->type) {
140         case GDK_KEY_PRESS:
141                 if (ev->key.keyval == GDK_Delete)
142                         start_remove_command();
143                 break;
144         
145         case GDK_KEY_RELEASE:
146                 if (_command_mode == Remove && ev->key.keyval == GDK_Delete)
147                         apply_command();
148                 break;
149
150         case GDK_BUTTON_PRESS:
151                 //group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK, ev->button.time);
152                 _state = Pressed;
153                 press_button = ev->button.button;
154                 //cerr << "PRESSED: " << press_button << endl;
155                 return true;
156         
157         case GDK_MOTION_NOTIFY:
158                 event_x = ev->motion.x;
159                 event_y = ev->motion.y;
160                 group->w2i(event_x, event_y);
161
162                 switch (_state) {
163                 case Pressed: // Drag start
164
165                         if (press_button == 1 && _command_mode != Remove) { // Select rect start
166                                 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
167                                                 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
168                                 last_x = event_x;
169                                 last_y = event_y;
170                                 drag_start_x = event_x;
171                                 drag_start_y = event_y;
172
173                                 drag_rect = new ArdourCanvas::SimpleRect(*group);
174                                 drag_rect->property_x1() = event_x;
175                                 drag_rect->property_y1() = event_y;
176                                 drag_rect->property_x2() = event_x;
177                                 drag_rect->property_y2() = event_y;
178                                 drag_rect->property_outline_what() = 0xFF;
179                                 drag_rect->property_outline_color_rgba() = 0xFF000099;
180                                 drag_rect->property_fill_color_rgba() = 0xFFDDDD33;
181
182                                 _state = SelectDragging;
183                                 return true;
184
185                         } else if (press_button == 3) { // Add note drag start
186                                 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
187                                                 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
188                                 last_x = event_x;
189                                 last_y = event_y;
190                                 drag_start_x = event_x;
191                                 drag_start_y = event_y;
192                                 
193                                 nframes_t event_frame = midi_view()->editor.pixel_to_frame(event_x);
194                                 midi_view()->editor.snap_to(event_frame);
195
196                                 drag_rect = new ArdourCanvas::SimpleRect(*group);
197                                 drag_rect->property_x1() = midi_view()->editor.frame_to_pixel(event_frame);
198                                 
199                                 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
200                                 drag_rect->property_x2() = event_x;
201                                 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
202                                 drag_rect->property_outline_what() = 0xFF;
203                                 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
204                                 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
205
206                                 _state = AddDragging;
207                                 return true;
208                         }
209
210                         break;
211
212                 case SelectDragging: // Select rect motion
213                 case AddDragging: // Add note rect motion
214                         if (ev->motion.is_hint) {
215                                 int t_x;
216                                 int t_y;
217                                 GdkModifierType state;
218                                 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
219                                 event_x = t_x;
220                                 event_y = t_y;
221                         }
222
223                         if (_state == AddDragging) {
224                                 nframes_t event_frame = midi_view()->editor.pixel_to_frame(event_x);
225                                 midi_view()->editor.snap_to(event_frame);
226                                 event_x = midi_view()->editor.frame_to_pixel(event_frame);
227                         }
228
229                         if (drag_rect)
230                                 drag_rect->property_x2() = event_x;
231
232                         if (drag_rect && _state == SelectDragging)
233                                 drag_rect->property_y2() = event_y;
234
235                         last_x = event_x;
236                         last_y = event_y;
237
238                         return true;
239                 default:
240                         _state = None;
241                         break;
242                 }
243                 break;
244         
245         case GDK_BUTTON_RELEASE:
246                 event_x = ev->motion.x;
247                 event_y = ev->motion.y;
248                 group->w2i(event_x, event_y);
249                 group->ungrab(ev->button.time);
250                 switch (_state) {
251                 case Pressed: // Clicked
252                         if (ev->button.button == 3) {
253                                 nframes_t event_frame = midi_view()->editor.pixel_to_frame(event_x);
254                                 midi_view()->editor.snap_to(event_frame);
255                                 event_x = midi_view()->editor.frame_to_pixel(event_frame);
256                                 create_note_at(event_x, event_y, _default_note_length);
257                         }
258                         _state = None;
259                         return true;
260                 case SelectDragging: // Select drag done
261                         _state = None;
262                         delete drag_rect;
263                         drag_rect = NULL;
264                         return true;
265                 case AddDragging: // Add drag done
266                         _state = None;
267                         if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
268                                 create_note_at(drag_rect->property_x1(), drag_rect->property_y1(),
269                                                 trackview.editor.pixel_to_frame(
270                                                 drag_rect->property_x2() - drag_rect->property_x1()));
271                         }
272
273                         delete drag_rect;
274                         drag_rect = NULL;
275                         return true;
276                 default:
277                         break;
278                 }
279         
280         default:
281                 break;
282         }
283
284         return false;
285 }
286
287
288 /** Add a note to the model, and the view, at a canvas (click) coordinate */
289 void
290 MidiRegionView::create_note_at(double x, double y, double dur)
291 {
292         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
293         MidiStreamView* const view = mtv->midi_view();
294
295         double note = midi_stream_view()->y_to_note(y);
296
297         assert(note >= 0.0);
298         assert(note <= 127.0);
299
300         const nframes_t stamp = trackview.editor.pixel_to_frame (x);
301         assert(stamp >= 0);
302         //assert(stamp <= _region->length());
303
304         //const Meter& m = trackview.session().tempo_map().meter_at(stamp);
305         //const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
306         //double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
307
308         // Add a 1 beat long note (for now)
309         const MidiModel::Note new_note(stamp, dur, (uint8_t)note, 0x40);
310         
311         view->update_bounds(new_note.note());
312
313         MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
314         cmd->add(new_note);
315         _model->apply_command(cmd);
316         
317         //add_note(new_note);
318 }
319
320
321 void
322 MidiRegionView::clear_events()
323 {
324         for (std::vector<ArdourCanvas::Item*>::iterator i = _events.begin(); i != _events.end(); ++i)
325                 delete *i;
326         
327         _events.clear();
328 }
329
330
331 void
332 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
333 {
334         _model = model;
335
336         if (_enable_display)
337                 redisplay_model();
338 }
339
340
341 void
342 MidiRegionView::redisplay_model()
343 {
344         clear_events();
345         
346         if (_model) {
347                 begin_write();
348
349                 for (size_t i=0; i < _model->n_notes(); ++i)
350                         add_note(_model->note_at(i));
351
352                 end_write();
353         } else {
354                 warning << "MidiRegionView::redisplay_model called without a model" << endmsg;
355         }
356 }
357
358
359 MidiRegionView::~MidiRegionView ()
360 {
361         in_destructor = true;
362         end_write();
363
364         RegionViewGoingAway (this); /* EMIT_SIGNAL */
365 }
366
367
368 void
369 MidiRegionView::region_resized (Change what_changed)
370 {
371         RegionView::region_resized(what_changed);
372
373         if (what_changed & ARDOUR::PositionChanged) {
374         
375                 if (_enable_display)
376                         redisplay_model();
377         
378         } else if (what_changed & Change (StartChanged)) {
379
380                 //cerr << "MIDI RV START CHANGED" << endl;
381
382         } else if (what_changed & Change (LengthChanged)) {
383                 
384                 //cerr << "MIDI RV LENGTH CHANGED" << endl;
385         
386         }
387 }
388
389 void
390 MidiRegionView::reset_width_dependent_items (double pixel_width)
391 {
392         RegionView::reset_width_dependent_items(pixel_width);
393         assert(_pixel_width == pixel_width);
394                 
395         if (_enable_display)
396                 redisplay_model();
397 }
398
399 void
400 MidiRegionView::set_y_position_and_height (double y, double h)
401 {
402         RegionView::set_y_position_and_height(y, h - 1);
403
404         if (_enable_display)
405                 redisplay_model();
406
407         if (name_text) {
408                 name_text->raise_to_top();
409         }
410 }
411
412 void
413 MidiRegionView::show_region_editor ()
414 {
415         cerr << "No MIDI region editor." << endl;
416 }
417
418 GhostRegion*
419 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
420 {
421         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
422         assert(rtv);
423
424         double unit_position = _region->position () / samples_per_unit;
425         GhostRegion* ghost = new GhostRegion (atv, unit_position);
426
427         ghost->set_height ();
428         ghost->set_duration (_region->length() / samples_per_unit);
429         ghosts.push_back (ghost);
430
431         ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
432
433         return ghost;
434 }
435
436
437 /** Begin tracking note state for successive calls to add_event
438  */
439 void
440 MidiRegionView::begin_write()
441 {
442         _active_notes = new CanvasNote*[128];
443         for (unsigned i=0; i < 128; ++i)
444                 _active_notes[i] = NULL;
445 }
446
447
448 /** Destroy note state for add_event
449  */
450 void
451 MidiRegionView::end_write()
452 {
453         delete[] _active_notes;
454         _active_notes = NULL;
455 }
456
457
458 /** Add a MIDI event.
459  *
460  * This is used while recording, and handles displaying still-unresolved notes.
461  * Displaying an existing model is simpler, and done with add_note.
462  */
463 void
464 MidiRegionView::add_event (const MidiEvent& ev)
465 {
466         /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
467         for (size_t i=0; i < ev.size; ++i) {
468                 printf("%X ", ev.buffer[i]);
469         }
470         printf("\n\n");*/
471
472         ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
473         
474         if (midi_view()->note_mode() == Sustained) {
475                 if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
476                         const Byte& note = ev.buffer()[1];
477                         const double y1 = midi_stream_view()->note_to_y(note);
478
479                         CanvasNote* ev_rect = new CanvasNote(*this, *group);
480                         ev_rect->property_x1() = trackview.editor.frame_to_pixel (
481                                         (nframes_t)ev.time());
482                         ev_rect->property_y1() = y1;
483                         ev_rect->property_x2() = trackview.editor.frame_to_pixel (
484                                         _region->length());
485                         ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
486                         ev_rect->property_outline_color_rgba() = 0xFFFFFFAA;
487                         /* outline all but right edge */
488                         ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
489                         ev_rect->property_fill_color_rgba() = 0xFFFFFF66;
490
491                         ev_rect->raise_to_top();
492
493                         _events.push_back(ev_rect);
494                         if (_active_notes)
495                                 _active_notes[note] = ev_rect;
496
497                 } else if ((ev.buffer()[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
498                         const Byte& note = ev.buffer()[1];
499                         if (_active_notes && _active_notes[note]) {
500                                 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time());
501                                 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
502                                 _active_notes[note] = NULL;
503                         }
504                 }
505         
506         } else if (midi_view()->note_mode() == Percussive) {
507                 const Byte& note = ev.buffer()[1];
508                 const double diamond_size = midi_stream_view()->note_height() / 2.0;
509                 const double x = trackview.editor.frame_to_pixel((nframes_t)ev.time());
510                 const double y = midi_stream_view()->note_to_y(note) + ((diamond_size-2) / 4.0);
511
512                 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
513                 ev_diamond->move(x, y);
514                 ev_diamond->show();
515                 ev_diamond->property_outline_color_rgba() = 0xFFFFFFDD;
516                 ev_diamond->property_fill_color_rgba() = 0xFFFFFF66;
517                         
518                 _events.push_back(ev_diamond);
519         }
520 }
521
522
523 /** Extend active notes to rightmost edge of region (if length is changed)
524  */
525 void
526 MidiRegionView::extend_active_notes()
527 {
528         if (!_active_notes)
529                 return;
530
531         for (unsigned i=0; i < 128; ++i)
532                 if (_active_notes[i])
533                         _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
534 }
535
536
537 /** Add a MIDI note to the view (with duration).
538  *
539  * This does no 'realtime' note resolution, notes from a MidiModel have a
540  * duration so they can be drawn in full immediately.
541  */
542 void
543 MidiRegionView::add_note (const MidiModel::Note& note)
544 {
545         assert(note.time() >= 0);
546         assert(note.time() < _region->length());
547         //assert(note.time() + note.duration < _region->length());
548
549         ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
550         
551         const uint8_t fill_alpha = 0x20 + (uint8_t)(note.velocity() * 1.5); 
552         const uint32_t fill = RGBA_TO_UINT(0xE0 + note.velocity()/127.0 * 0x10, 0xE0, 0xE0, fill_alpha);
553         const uint8_t outline_alpha = 0x80 + (uint8_t)(note.velocity()); 
554         const uint32_t outline = RGBA_TO_UINT(0xE0 + note.velocity()/127.0 * 0x10, 0xE0, 0xE0, outline_alpha);
555         
556         //printf("Event, time = %f, note = %d\n", note.time(), note.note());
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() = fill;
568                 ev_rect->property_outline_color_rgba() = outline;
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() = fill;
583                 ev_diamond->property_outline_color_rgba() = outline;
584                 _events.push_back(ev_diamond);
585         }
586 }
587
588