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