Selection of visible note range (full range vs fit contents, selectable from midi...
[ardour.git] / gtk2_ardour / midi_region_view.cc
1 /*
2     Copyright (C) 2001-2006 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cmath>
20 #include <cassert>
21 #include <algorithm>
22
23 #include <gtkmm.h>
24
25 #include <gtkmm2ext/gtk_ui.h>
26
27 #include <ardour/playlist.h>
28 #include <ardour/tempo.h>
29 #include <ardour/midi_region.h>
30 #include <ardour/midi_source.h>
31 #include <ardour/midi_diskstream.h>
32 #include <ardour/midi_events.h>
33 #include <ardour/midi_model.h>
34
35 #include "streamview.h"
36 #include "midi_region_view.h"
37 #include "midi_streamview.h"
38 #include "midi_time_axis.h"
39 #include "simplerect.h"
40 #include "simpleline.h"
41 #include "diamond.h"
42 #include "public_editor.h"
43 #include "ghostregion.h"
44 #include "midi_time_axis.h"
45 #include "utils.h"
46 #include "rgb_macros.h"
47 #include "gui_thread.h"
48
49 #include "i18n.h"
50
51 using namespace sigc;
52 using namespace ARDOUR;
53 using namespace PBD;
54 using namespace Editing;
55 using namespace ArdourCanvas;
56
57 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu,
58                                   Gdk::Color& basic_color)
59         : RegionView (parent, tv, r, spu, basic_color)
60         , _active_notes(0)
61 {
62 }
63
64 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, 
65                                   Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
66         : RegionView (parent, tv, r, spu, basic_color, visibility)
67         , _active_notes(0)
68 {
69 }
70
71 void
72 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
73 {
74         // FIXME: Some redundancy here with RegionView::init.  Need to figure out
75         // where order is important and where it isn't...
76         
77         // FIXME
78         RegionView::init(basic_color, /*wfd*/false);
79
80         compute_colors (basic_color);
81
82         reset_width_dependent_items ((double) _region->length() / samples_per_unit);
83
84         set_y_position_and_height (0, trackview.height);
85
86         region_muted ();
87         region_resized (BoundsChanged);
88         region_locked ();
89
90         _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
91
92         set_colors ();
93
94         if (wfd) {
95                 midi_region()->midi_source(0)->load_model();
96                 display_events();
97         }
98         
99         group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event));
100 }
101
102 bool
103 MidiRegionView::canvas_event(GdkEvent* ev)
104 {
105         if (trackview.editor.current_mouse_mode() == MouseNote) {
106                 if (ev->type == GDK_BUTTON_PRESS) {
107                         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
108                         MidiStreamView* const view = mtv->midi_view();
109                         
110                         const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
111                         const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
112                         const double roll_height = trackview.height - footer_height;
113                         
114                         double x = ev->button.x;
115                         double y = ev->button.y;
116                         get_canvas_group()->w2i(x, y);
117
118                         double note = floor((roll_height - y) / roll_height * (double)note_range) + view->lowest_note();
119                         assert(note >= 0.0);
120                         assert(note <= 127.0);
121
122                         const nframes_t stamp = trackview.editor.pixel_to_frame (x);
123                         assert(stamp >= 0);
124                         //assert(stamp <= _region->length());
125                         
126                         const Meter& m = trackview.session().tempo_map().meter_at(stamp);
127                         const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
128                         double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
129                         
130                         // Add a 1 beat long note (for now)
131                         const MidiModel::Note new_note(stamp, dur, (uint8_t)note, 0x40);
132
133                         MidiModel::Notes& notes = midi_region()->midi_source(0)->model()->notes();
134                         MidiModel::Notes::iterator i = upper_bound(notes.begin(), notes.end(), new_note,
135                                         MidiModel::NoteTimeComparator());
136                         notes.insert(i, new_note);
137                         view->update_bounds(new_note.note);
138
139                         add_note(new_note);
140                 }
141         }
142         
143         return false;
144 }
145
146
147 void
148 MidiRegionView::clear_events()
149 {
150         for (std::vector<ArdourCanvas::Item*>::iterator i = _events.begin(); i != _events.end(); ++i)
151                 delete *i;
152         
153         _events.clear();
154 }
155
156
157 void
158 MidiRegionView::display_events()
159 {
160         clear_events();
161         
162         begin_write();
163
164         for (size_t i=0; i < midi_region()->midi_source(0)->model()->n_notes(); ++i)
165                 add_note(midi_region()->midi_source(0)->model()->note_at(i));
166
167         end_write();
168 }
169
170
171 MidiRegionView::~MidiRegionView ()
172 {
173         in_destructor = true;
174         end_write();
175
176         RegionViewGoingAway (this); /* EMIT_SIGNAL */
177 }
178
179 boost::shared_ptr<ARDOUR::MidiRegion>
180 MidiRegionView::midi_region() const
181 {
182         // "Guaranteed" to succeed...
183         return boost::dynamic_pointer_cast<MidiRegion>(_region);
184 }
185
186 void
187 MidiRegionView::region_resized (Change what_changed)
188 {
189         RegionView::region_resized(what_changed);
190
191         if (what_changed & ARDOUR::PositionChanged) {
192         
193                 display_events();
194         
195         } else if (what_changed & Change (StartChanged)) {
196
197                 //cerr << "MIDI RV START CHANGED" << endl;
198
199         } else if (what_changed & Change (LengthChanged)) {
200                 
201                 //cerr << "MIDI RV LENGTH CHANGED" << endl;
202         
203         }
204 }
205
206 void
207 MidiRegionView::reset_width_dependent_items (double pixel_width)
208 {
209         RegionView::reset_width_dependent_items(pixel_width);
210         assert(_pixel_width == pixel_width);
211                 
212         display_events();
213 }
214
215 void
216 MidiRegionView::set_y_position_and_height (double y, double h)
217 {
218         RegionView::set_y_position_and_height(y, h - 1);
219
220         display_events();
221
222         if (name_text) {
223                 name_text->raise_to_top();
224         }
225 }
226
227 void
228 MidiRegionView::show_region_editor ()
229 {
230         cerr << "No MIDI region editor." << endl;
231 }
232
233 GhostRegion*
234 MidiRegionView::add_ghost (AutomationTimeAxisView& atv)
235 {
236         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
237         assert(rtv);
238
239         double unit_position = _region->position () / samples_per_unit;
240         GhostRegion* ghost = new GhostRegion (atv, unit_position);
241
242         ghost->set_height ();
243         ghost->set_duration (_region->length() / samples_per_unit);
244         ghosts.push_back (ghost);
245
246         ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
247
248         return ghost;
249 }
250
251
252 /** Begin tracking note state for successive calls to add_event
253  */
254 void
255 MidiRegionView::begin_write()
256 {
257         _active_notes = new ArdourCanvas::SimpleRect*[128];
258         for (unsigned i=0; i < 128; ++i)
259                 _active_notes[i] = NULL;
260 }
261
262
263 /** Destroy note state for add_event
264  */
265 void
266 MidiRegionView::end_write()
267 {
268         delete[] _active_notes;
269         _active_notes = NULL;
270 }
271
272
273 /** Add a MIDI event.
274  *
275  * This is used while recording, and handles displaying still-unresolved notes.
276  * Displaying an existing model is simpler, and done with add_note.
277  */
278 void
279 MidiRegionView::add_event (const MidiEvent& ev)
280 {
281         /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
282         for (size_t i=0; i < ev.size; ++i) {
283                 printf("%X ", ev.buffer[i]);
284         }
285         printf("\n\n");*/
286
287         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
288         MidiStreamView* const view = mtv->midi_view();
289         ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
290         
291         const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
292         const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
293         const double pixel_range = (trackview.height - footer_height - 5.0) / (double)note_range;
294
295         if (mtv->note_mode() == Note) {
296                 if ((ev.buffer[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
297                         const Byte& note = ev.buffer[1];
298                         const double y1 = trackview.height - (pixel_range * (note - view->lowest_note() + 1))
299                                 - footer_height - 3.0;
300
301                         ArdourCanvas::SimpleRect * ev_rect = new Gnome::Canvas::SimpleRect(*group);
302                         ev_rect->property_x1() = trackview.editor.frame_to_pixel (
303                                         (nframes_t)ev.time);
304                         ev_rect->property_y1() = y1;
305                         ev_rect->property_x2() = trackview.editor.frame_to_pixel (
306                                         _region->length());
307                         ev_rect->property_y2() = y1 + ceil(pixel_range);
308                         ev_rect->property_outline_color_rgba() = 0xFFFFFFAA;
309                         /* outline all but right edge */
310                         ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
311                         ev_rect->property_fill_color_rgba() = 0xFFFFFF66;
312
313                         _events.push_back(ev_rect);
314                         if (_active_notes)
315                                 _active_notes[note] = ev_rect;
316
317                 } else if ((ev.buffer[0] & 0xF0) == MIDI_CMD_NOTE_OFF) {
318                         const Byte& note = ev.buffer[1];
319                         if (_active_notes && _active_notes[note]) {
320                                 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)ev.time);
321                                 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
322                                 _active_notes[note] = NULL;
323                         }
324                 }
325         
326         } else if (mtv->note_mode() == Percussion) {
327                 const Byte& note = ev.buffer[1];
328                 const double x = trackview.editor.frame_to_pixel((nframes_t)ev.time);
329                 const double y = trackview.height - (pixel_range * (note - view->lowest_note() + 1))
330                         - footer_height - 3.0;
331
332                 Diamond* ev_diamond = new Diamond(*group, std::min(pixel_range, 5.0));
333                 ev_diamond->move(x, y);
334                 ev_diamond->show();
335                 ev_diamond->property_outline_color_rgba() = 0xFFFFFFDD;
336                 ev_diamond->property_fill_color_rgba() = 0xFFFFFF66;
337                 _events.push_back(ev_diamond);
338         }
339 }
340
341
342 /** Extend active notes to rightmost edge of region (if length is changed)
343  */
344 void
345 MidiRegionView::extend_active_notes()
346 {
347         if (!_active_notes)
348                 return;
349
350         for (unsigned i=0; i < 128; ++i)
351                 if (_active_notes[i])
352                         _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
353 }
354
355
356 /** Add a MIDI note (with duration).
357  *
358  * This does no 'realtime' note resolution, notes from a MidiModel have a
359  * duration so they can be drawn in full immediately.
360  */
361 void
362 MidiRegionView::add_note (const MidiModel::Note& note)
363 {
364         assert(note.start >= 0);
365         assert(note.start < _region->length());
366         //assert(note.start + note.duration < _region->length());
367
368         /*printf("Event, time = %f, size = %zu, data = ", ev.time, ev.size);
369         for (size_t i=0; i < ev.size; ++i) {
370                 printf("%X ", ev.buffer[i]);
371         }
372         printf("\n\n");*/
373
374         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
375         MidiStreamView* const view = mtv->midi_view();
376         ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
377         
378         const uint8_t note_range = view->highest_note() - view->lowest_note() + 1;
379         const double footer_height = name_highlight->property_y2() - name_highlight->property_y1();
380         const double pixel_range = (trackview.height - footer_height - 5.0) / (double)note_range;
381
382         if (mtv->note_mode() == Note) {
383                 const double y1 = trackview.height - (pixel_range * (note.note - view->lowest_note() + 1))
384                         - footer_height - 3.0;
385
386                 ArdourCanvas::SimpleRect * ev_rect = new Gnome::Canvas::SimpleRect(*group);
387                 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note.start);
388                 ev_rect->property_y1() = y1;
389                 ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note.start + note.duration));
390                 ev_rect->property_y2() = y1 + ceil(pixel_range);
391
392                 ev_rect->property_fill_color_rgba() = 0xFFFFFF66;
393                 ev_rect->property_outline_color_rgba() = 0xFFFFFFAA;
394                 ev_rect->property_outline_what() = (guint32) 0xF; // all edges
395
396                 ev_rect->show();
397                 _events.push_back(ev_rect);
398
399         } else if (mtv->note_mode() == Percussion) {
400                 const double x = trackview.editor.frame_to_pixel((nframes_t)note.start);
401                 const double y = trackview.height - (pixel_range * (note.note - view->lowest_note() + 1))
402                         - footer_height - 3.0;
403
404                 Diamond* ev_diamond = new Diamond(*group, std::min(pixel_range, 5.0));
405                 ev_diamond->move(x, y);
406                 ev_diamond->show();
407                 ev_diamond->property_outline_color_rgba() = 0xFFFFFFDD;
408                 ev_diamond->property_fill_color_rgba() = 0xFFFFFF66;
409                 _events.push_back(ev_diamond);
410         }
411 }
412
413