Note canvas event handling testing stuff.
[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 "diamond.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         // FIXME: Some redundancy here with RegionView::init.  Need to figure out
78         // where order is important and where it isn't...
79         
80         // FIXME
81         RegionView::init(basic_color, /*wfd*/false);
82
83         compute_colors (basic_color);
84
85         reset_width_dependent_items ((double) _region->length() / samples_per_unit);
86
87         set_y_position_and_height (0, trackview.height);
88
89         region_muted ();
90         region_resized (BoundsChanged);
91         region_locked ();
92
93         _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
94
95         set_colors ();
96
97         if (wfd) {
98                 midi_region()->midi_source(0)->load_model();
99                 display_events();
100         }
101                 
102         midi_region()->midi_source(0)->model()->ContentsChanged.connect(sigc::mem_fun(
103                         this, &MidiRegionView::redisplay_model));
104         
105         group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event));
106 }
107
108 bool
109 MidiRegionView::canvas_event(GdkEvent* ev)
110 {
111         if (trackview.editor.current_mouse_mode() == MouseNote) {
112                 if (ev->type == GDK_BUTTON_PRESS) {
113                         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
114                         MidiStreamView* const view = mtv->midi_view();
115                         
116                         const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
117                         const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
118                         const double roll_height = trackview.height - footer_height;
119                         
120                         double x = ev->button.x;
121                         double y = ev->button.y;
122                         get_canvas_group()->w2i(x, y);
123
124                         double note = floor((roll_height - y) / roll_height * (double)note_range) + view->lowest_note();
125                         assert(note >= 0.0);
126                         assert(note <= 127.0);
127
128                         const nframes_t stamp = trackview.editor.pixel_to_frame (x);
129                         assert(stamp >= 0);
130                         //assert(stamp <= _region->length());
131                         
132                         const Meter& m = trackview.session().tempo_map().meter_at(stamp);
133                         const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
134                         double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
135                         
136                         MidiModel* model = midi_region()->midi_source(0)->model();
137                         
138                         // Add a 1 beat long note (for now)
139                         const MidiModel::Note new_note(stamp, dur, (uint8_t)note, 0x40);
140                         
141                         model->begin_command();
142                         model->add_note(new_note);
143                         model->finish_command();
144
145                         view->update_bounds(new_note.note);
146
147                         add_note(new_note);
148                 }
149         }
150         
151         return false;
152 }
153
154
155 bool
156 MidiRegionView::note_canvas_event(GdkEvent* ev)
157 {
158         cerr << "NOTE CANVAS EVENT" << endl;
159
160         return true;
161 }
162
163
164 void
165 MidiRegionView::redisplay_model()
166 {
167         clear_events();
168         display_events();
169 }
170
171
172 void
173 MidiRegionView::clear_events()
174 {
175         for (std::vector<ArdourCanvas::Item*>::iterator i = _events.begin(); i != _events.end(); ++i)
176                 delete *i;
177         
178         _events.clear();
179 }
180
181
182 void
183 MidiRegionView::display_events()
184 {
185         clear_events();
186         
187         begin_write();
188
189         for (size_t i=0; i < midi_region()->midi_source(0)->model()->n_notes(); ++i)
190                 add_note(midi_region()->midi_source(0)->model()->note_at(i));
191
192         end_write();
193 }
194
195
196 MidiRegionView::~MidiRegionView ()
197 {
198         in_destructor = true;
199         end_write();
200
201         RegionViewGoingAway (this); /* EMIT_SIGNAL */
202 }
203
204 boost::shared_ptr<ARDOUR::MidiRegion>
205 MidiRegionView::midi_region() const
206 {
207         // "Guaranteed" to succeed...
208         return boost::dynamic_pointer_cast<MidiRegion>(_region);
209 }
210
211 void
212 MidiRegionView::region_resized (Change what_changed)
213 {
214         RegionView::region_resized(what_changed);
215
216         if (what_changed & ARDOUR::PositionChanged) {
217         
218                 display_events();
219         
220         } else if (what_changed & Change (StartChanged)) {
221
222                 //cerr << "MIDI RV START CHANGED" << endl;
223
224         } else if (what_changed & Change (LengthChanged)) {
225                 
226                 //cerr << "MIDI RV LENGTH CHANGED" << endl;
227         
228         }
229 }
230
231 void
232 MidiRegionView::reset_width_dependent_items (double pixel_width)
233 {
234         RegionView::reset_width_dependent_items(pixel_width);
235         assert(_pixel_width == pixel_width);
236                 
237         display_events();
238 }
239
240 void
241 MidiRegionView::set_y_position_and_height (double y, double h)
242 {
243         RegionView::set_y_position_and_height(y, h - 1);
244
245         display_events();
246
247         if (name_text) {
248                 name_text->raise_to_top();
249         }
250 }
251
252 void
253 MidiRegionView::show_region_editor ()
254 {
255         cerr << "No MIDI region editor." << endl;
256 }
257
258 GhostRegion*
259 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
260 {
261         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
262         assert(rtv);
263
264         double unit_position = _region->position () / samples_per_unit;
265         GhostRegion* ghost = new GhostRegion (atv, unit_position);
266
267         ghost->set_height ();
268         ghost->set_duration (_region->length() / samples_per_unit);
269         ghosts.push_back (ghost);
270
271         ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
272
273         return ghost;
274 }
275
276
277 /** Begin tracking note state for successive calls to add_event
278  */
279 void
280 MidiRegionView::begin_write()
281 {
282         _active_notes = new ArdourCanvas::SimpleRect*[128];
283         for (unsigned i=0; i < 128; ++i)
284                 _active_notes[i] = NULL;
285 }
286
287
288 /** Destroy note state for add_event
289  */
290 void
291 MidiRegionView::end_write()
292 {
293         delete[] _active_notes;
294         _active_notes = NULL;
295 }
296
297
298 /** Add a MIDI event.
299  *
300  * This is used while recording, and handles displaying still-unresolved notes.
301  * Displaying an existing model is simpler, and done with add_note.
302  */
303 void
304 MidiRegionView::add_event (const MidiEvent& ev)
305 {
306         /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
307         for (size_t i=0; i < ev.size; ++i) {
308                 printf("%X ", ev.buffer[i]);
309         }
310         printf("\n\n");*/
311
312         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
313         MidiStreamView* const view = mtv->midi_view();
314         ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
315         
316         const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
317         const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
318         const double pixel_range = (trackview.height - footer_height - 5.0) / (double)note_range;
319
320         if (mtv->note_mode() == Note) {
321                 if ((ev.buffer[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
322                         const Byte& note = ev.buffer[1];
323                         const double y1 = trackview.height - (pixel_range * (note - view->lowest_note() + 1))
324                                 - footer_height - 3.0;
325
326                         ArdourCanvas::SimpleRect * ev_rect = new Gnome::Canvas::SimpleRect(*group);
327                         ev_rect->property_x1() = trackview.editor.frame_to_pixel (
328                                         (nframes_t)ev.time);
329                         ev_rect->property_y1() = y1;
330                         ev_rect->property_x2() = trackview.editor.frame_to_pixel (
331                                         _region->length());
332                         ev_rect->property_y2() = y1 + ceil(pixel_range);
333                         ev_rect->property_outline_color_rgba() = 0xFFFFFFAA;
334                         /* outline all but right edge */
335                         ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
336                         ev_rect->property_fill_color_rgba() = 0xFFFFFF66;
337
338                         ev_rect->signal_event().connect(sigc::mem_fun(this, &MidiRegionView::note_canvas_event));
339
340                         ev_rect->raise_to_top();
341
342                         _events.push_back(ev_rect);
343                         if (_active_notes)
344                                 _active_notes[note] = ev_rect;
345
346                 } else if ((ev.buffer[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
347                         const Byte& note = ev.buffer[1];
348                         if (_active_notes && _active_notes[note]) {
349                                 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time);
350                                 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
351                                 _active_notes[note] = NULL;
352                         }
353                 }
354         
355         } else if (mtv->note_mode() == Percussion) {
356                 const Byte& note = ev.buffer[1];
357                 const double x = trackview.editor.frame_to_pixel((nframes_t)ev.time);
358                 const double y = trackview.height - (pixel_range * (note - view->lowest_note() + 1))
359                         - footer_height - 3.0;
360
361                 Diamond* ev_diamond = new Diamond(*group, std::min(pixel_range, 5.0));
362                 ev_diamond->move(x, y);
363                 ev_diamond->show();
364                 ev_diamond->property_outline_color_rgba() = 0xFFFFFFDD;
365                 ev_diamond->property_fill_color_rgba() = 0xFFFFFF66;
366                         
367                 ev_diamond->signal_event().connect(sigc::mem_fun(this, &MidiRegionView::note_canvas_event));
368
369                 _events.push_back(ev_diamond);
370         }
371 }
372
373
374 /** Extend active notes to rightmost edge of region (if length is changed)
375  */
376 void
377 MidiRegionView::extend_active_notes()
378 {
379         if (!_active_notes)
380                 return;
381
382         for (unsigned i=0; i < 128; ++i)
383                 if (_active_notes[i])
384                         _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
385 }
386
387
388 /** Add a MIDI note (with duration).
389  *
390  * This does no 'realtime' note resolution, notes from a MidiModel have a
391  * duration so they can be drawn in full immediately.
392  */
393 void
394 MidiRegionView::add_note (const MidiModel::Note& note)
395 {
396         assert(note.start >= 0);
397         assert(note.start < _region->length());
398         //assert(note.start + note.duration < _region->length());
399
400         /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
401         for (size_t i=0; i < ev.size; ++i) {
402                 printf("%X ", ev.buffer[i]);
403         }
404         printf("\n\n");*/
405
406         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
407         MidiStreamView* const view = mtv->midi_view();
408         ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
409         
410         const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
411         const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
412         const double pixel_range = (trackview.height - footer_height - 5.0) / (double)note_range;
413
414         if (mtv->note_mode() == Note) {
415                 const double y1 = trackview.height - (pixel_range * (note.note - view->lowest_note() + 1))
416                         - footer_height - 3.0;
417
418                 ArdourCanvas::SimpleRect * ev_rect = new Gnome::Canvas::SimpleRect(*group);
419                 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note.start);
420                 ev_rect->property_y1() = y1;
421                 ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note.start + note.duration));
422                 ev_rect->property_y2() = y1 + ceil(pixel_range);
423
424                 ev_rect->property_fill_color_rgba() = 0xFFFFFF66;
425                 ev_rect->property_outline_color_rgba() = 0xFFFFFFAA;
426                 ev_rect->property_outline_what() = (guint32) 0xF; // all edges
427
428                 ev_rect->show();
429                 _events.push_back(ev_rect);
430
431         } else if (mtv->note_mode() == Percussion) {
432                 const double x = trackview.editor.frame_to_pixel((nframes_t)note.start);
433                 const double y = trackview.height - (pixel_range * (note.note - view->lowest_note() + 1))
434                         - footer_height - 3.0;
435
436                 Diamond* ev_diamond = new Diamond(*group, std::min(pixel_range, 5.0));
437                 ev_diamond->move(x, y);
438                 ev_diamond->show();
439                 ev_diamond->property_outline_color_rgba() = 0xFFFFFFDD;
440                 ev_diamond->property_fill_color_rgba() = 0xFFFFFF66;
441                 _events.push_back(ev_diamond);
442         }
443 }
444
445