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