2 Copyright (C) 2001-2011 Paul Davis
3 Author: David Robillard
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.
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.
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.
26 #include "gtkmm2ext/gtk_ui.h"
28 #include <sigc++/signal.h>
30 #include "midi++/midnam_patch.h"
32 #include "pbd/memento_command.h"
33 #include "pbd/stateful_diff_command.h"
35 #include "ardour/midi_model.h"
36 #include "ardour/midi_playlist.h"
37 #include "ardour/midi_region.h"
38 #include "ardour/midi_source.h"
39 #include "ardour/midi_track.h"
40 #include "ardour/operations.h"
41 #include "ardour/session.h"
43 #include "evoral/Parameter.hpp"
44 #include "evoral/Event.hpp"
45 #include "evoral/Control.hpp"
46 #include "evoral/midi_util.h"
48 #include "canvas/debug.h"
49 #include "canvas/text.h"
51 #include "automation_region_view.h"
52 #include "automation_time_axis.h"
53 #include "control_point.h"
56 #include "editor_drag.h"
57 #include "ghostregion.h"
58 #include "gui_thread.h"
59 #include "item_counts.h"
61 #include "midi_channel_dialog.h"
62 #include "midi_cut_buffer.h"
63 #include "midi_list_editor.h"
64 #include "midi_region_view.h"
65 #include "midi_streamview.h"
66 #include "midi_time_axis.h"
67 #include "midi_util.h"
68 #include "midi_velocity_dialog.h"
69 #include "mouse_cursors.h"
70 #include "note_player.h"
71 #include "paste_context.h"
72 #include "public_editor.h"
73 #include "route_time_axis.h"
74 #include "rgb_macros.h"
75 #include "selection.h"
76 #include "streamview.h"
77 #include "patch_change_dialog.h"
78 #include "verbose_cursor.h"
81 #include "patch_change.h"
83 #include "ui_config.h"
87 using namespace ARDOUR;
89 using namespace Editing;
91 using Gtkmm2ext::Keyboard;
93 #define MIDI_BP_ZERO ((Config->get_first_midi_bank_is_zero())?0:1)
95 MidiRegionView::MidiRegionView (ArdourCanvas::Container* parent,
96 RouteTimeAxisView& tv,
97 boost::shared_ptr<MidiRegion> r,
100 : RegionView (parent, tv, r, spu, basic_color)
101 , _current_range_min(0)
102 , _current_range_max(0)
103 , _region_relative_time_converter(r->session().tempo_map(), r->position())
104 , _source_relative_time_converter(r->session().tempo_map(), r->position() - r->start())
105 , _region_relative_time_converter_double(r->session().tempo_map(), r->position())
107 , _note_group (new ArdourCanvas::Container (group))
108 , _note_diff_command (0)
110 , _step_edit_cursor (0)
111 , _step_edit_cursor_width (1.0)
112 , _step_edit_cursor_position (0.0)
113 , _channel_selection_scoped_note (0)
116 , _optimization_iterator (_events.end())
118 , _no_sound_notes (false)
119 , _last_display_zoom (0)
122 , _grabbed_keyboard (false)
125 , _mouse_changed_selection (false)
127 CANVAS_DEBUG_NAME (_note_group, string_compose ("note group for %1", get_item_name()));
129 _patch_change_outline = UIConfiguration::instance().color ("midi patch change outline");
130 _patch_change_fill = UIConfiguration::instance().color_mod ("midi patch change fill", "midi patch change fill");
132 _note_group->raise_to_top();
133 PublicEditor::DropDownKeys.connect (sigc::mem_fun (*this, &MidiRegionView::drop_down_keys));
135 Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&MidiRegionView::parameter_changed, this, _1), gui_context());
136 UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &MidiRegionView::parameter_changed));
138 connect_to_diskstream ();
141 MidiRegionView::MidiRegionView (ArdourCanvas::Container* parent,
142 RouteTimeAxisView& tv,
143 boost::shared_ptr<MidiRegion> r,
145 uint32_t basic_color,
147 TimeAxisViewItem::Visibility visibility)
148 : RegionView (parent, tv, r, spu, basic_color, recording, visibility)
149 , _current_range_min(0)
150 , _current_range_max(0)
151 , _region_relative_time_converter(r->session().tempo_map(), r->position())
152 , _source_relative_time_converter(r->session().tempo_map(), r->position() - r->start())
153 , _region_relative_time_converter_double(r->session().tempo_map(), r->position())
155 , _note_group (new ArdourCanvas::Container (group))
156 , _note_diff_command (0)
158 , _step_edit_cursor (0)
159 , _step_edit_cursor_width (1.0)
160 , _step_edit_cursor_position (0.0)
161 , _channel_selection_scoped_note (0)
164 , _optimization_iterator (_events.end())
166 , _no_sound_notes (false)
167 , _last_display_zoom (0)
170 , _grabbed_keyboard (false)
173 , _mouse_changed_selection (false)
175 CANVAS_DEBUG_NAME (_note_group, string_compose ("note group for %1", get_item_name()));
177 _patch_change_outline = UIConfiguration::instance().color ("midi patch change outline");
178 _patch_change_fill = UIConfiguration::instance().color_mod ("midi patch change fill", "midi patch change fill");
180 _note_group->raise_to_top();
182 PublicEditor::DropDownKeys.connect (sigc::mem_fun (*this, &MidiRegionView::drop_down_keys));
184 connect_to_diskstream ();
188 MidiRegionView::parameter_changed (std::string const & p)
190 if (p == "display-first-midi-bank-as-zero") {
191 if (_enable_display) {
194 } else if (p == "color-regions-using-track-color") {
196 } else if (p == "use-note-color-for-velocity") {
201 MidiRegionView::MidiRegionView (const MidiRegionView& other)
202 : sigc::trackable(other)
204 , _current_range_min(0)
205 , _current_range_max(0)
206 , _region_relative_time_converter(other.region_relative_time_converter())
207 , _source_relative_time_converter(other.source_relative_time_converter())
208 , _region_relative_time_converter_double(other.region_relative_time_converter_double())
210 , _note_group (new ArdourCanvas::Container (get_canvas_group()))
211 , _note_diff_command (0)
213 , _step_edit_cursor (0)
214 , _step_edit_cursor_width (1.0)
215 , _step_edit_cursor_position (0.0)
216 , _channel_selection_scoped_note (0)
219 , _optimization_iterator (_events.end())
221 , _no_sound_notes (false)
222 , _last_display_zoom (0)
225 , _grabbed_keyboard (false)
228 , _mouse_changed_selection (false)
233 MidiRegionView::MidiRegionView (const MidiRegionView& other, boost::shared_ptr<MidiRegion> region)
234 : RegionView (other, boost::shared_ptr<Region> (region))
235 , _current_range_min(0)
236 , _current_range_max(0)
237 , _region_relative_time_converter(other.region_relative_time_converter())
238 , _source_relative_time_converter(other.source_relative_time_converter())
239 , _region_relative_time_converter_double(other.region_relative_time_converter_double())
241 , _note_group (new ArdourCanvas::Container (get_canvas_group()))
242 , _note_diff_command (0)
244 , _step_edit_cursor (0)
245 , _step_edit_cursor_width (1.0)
246 , _step_edit_cursor_position (0.0)
247 , _channel_selection_scoped_note (0)
250 , _optimization_iterator (_events.end())
252 , _no_sound_notes (false)
253 , _last_display_zoom (0)
256 , _grabbed_keyboard (false)
259 , _mouse_changed_selection (false)
265 MidiRegionView::init (bool wfd)
267 PublicEditor::DropDownKeys.connect (sigc::mem_fun (*this, &MidiRegionView::drop_down_keys));
270 Glib::Threads::Mutex::Lock lm(midi_region()->midi_source(0)->mutex());
271 midi_region()->midi_source(0)->load_model(lm);
274 _model = midi_region()->midi_source(0)->model();
275 _enable_display = false;
276 fill_color_name = "midi frame base";
278 RegionView::init (false);
280 //set_height (trackview.current_height());
283 region_sync_changed ();
284 region_resized (ARDOUR::bounds_change);
289 _enable_display = true;
292 display_model (_model);
296 reset_width_dependent_items (_pixel_width);
298 group->raise_to_top();
300 midi_view()->midi_track()->playback_filter().ChannelModeChanged.connect (_channel_mode_changed_connection, invalidator (*this),
301 boost::bind (&MidiRegionView::midi_channel_mode_changed, this),
304 instrument_info().Changed.connect (_instrument_changed_connection, invalidator (*this),
305 boost::bind (&MidiRegionView::instrument_settings_changed, this), gui_context());
307 trackview.editor().SnapChanged.connect(snap_changed_connection, invalidator(*this),
308 boost::bind (&MidiRegionView::snap_changed, this),
311 trackview.editor().MouseModeChanged.connect(_mouse_mode_connection, invalidator (*this),
312 boost::bind (&MidiRegionView::mouse_mode_changed, this),
315 Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&MidiRegionView::parameter_changed, this, _1), gui_context());
316 UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &MidiRegionView::parameter_changed));
317 connect_to_diskstream ();
321 MidiRegionView::instrument_info () const
323 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
324 return route_ui->route()->instrument_info();
327 const boost::shared_ptr<ARDOUR::MidiRegion>
328 MidiRegionView::midi_region() const
330 return boost::dynamic_pointer_cast<ARDOUR::MidiRegion>(_region);
334 MidiRegionView::connect_to_diskstream ()
336 midi_view()->midi_track()->DataRecorded.connect(
337 *this, invalidator(*this),
338 boost::bind (&MidiRegionView::data_recorded, this, _1),
343 MidiRegionView::canvas_group_event(GdkEvent* ev)
345 if (in_destructor || _recregion) {
349 if (!trackview.editor().internal_editing()) {
350 // not in internal edit mode, so just act like a normal region
351 return RegionView::canvas_group_event (ev);
354 //For now, move the snapped cursor aside so it doesn't bother you during internal editing
355 //trackview.editor().set_snapped_cursor_position(_region->position());
360 case GDK_ENTER_NOTIFY:
361 _last_event_x = ev->crossing.x;
362 _last_event_y = ev->crossing.y;
363 enter_notify(&ev->crossing);
364 // set entered_regionview (among other things)
365 return RegionView::canvas_group_event (ev);
367 case GDK_LEAVE_NOTIFY:
368 _last_event_x = ev->crossing.x;
369 _last_event_y = ev->crossing.y;
370 leave_notify(&ev->crossing);
371 // reset entered_regionview (among other things)
372 return RegionView::canvas_group_event (ev);
375 if (scroll (&ev->scroll)) {
381 return key_press (&ev->key);
383 case GDK_KEY_RELEASE:
384 return key_release (&ev->key);
386 case GDK_BUTTON_PRESS:
387 return button_press (&ev->button);
389 case GDK_BUTTON_RELEASE:
390 r = button_release (&ev->button);
393 case GDK_MOTION_NOTIFY:
394 _last_event_x = ev->motion.x;
395 _last_event_y = ev->motion.y;
396 return motion (&ev->motion);
402 return RegionView::canvas_group_event (ev);
406 MidiRegionView::enter_notify (GdkEventCrossing* ev)
408 enter_internal (ev->state);
415 MidiRegionView::leave_notify (GdkEventCrossing*)
424 MidiRegionView::mouse_mode_changed ()
426 // Adjust frame colour (become more transparent for internal tools)
430 if (!trackview.editor().internal_editing()) {
431 /* Switched out of internal editing mode while entered.
432 Only necessary for leave as a mouse_mode_change over a region
433 automatically triggers an enter event. */
436 else if (trackview.editor().current_mouse_mode() == MouseContent) {
437 // hide cursor and ghost note after changing to internal edit mode
438 remove_ghost_note ();
440 /* XXX This is problematic as the function is executed for every region
441 and only for one region _entered_note can be true. Still it's
442 necessary as to hide the verbose cursor when we're changing from
443 draw mode to internal edit mode. These lines are the reason why
444 in some situations no verbose cursor is shown when we enter internal
445 edit mode over a note. */
446 if (!_entered_note) {
447 hide_verbose_cursor ();
454 MidiRegionView::enter_internal (uint32_t state)
456 if (trackview.editor().current_mouse_mode() == MouseDraw && _mouse_state != AddDragging) {
457 // Show ghost note under pencil
458 create_ghost_note(_last_event_x, _last_event_y, state);
461 if (!_selection.empty()) {
462 // Grab keyboard for moving selected notes with arrow keys
463 Keyboard::magic_widget_grab_focus();
464 _grabbed_keyboard = true;
467 // Lower frame handles below notes so they don't steal events
468 if (frame_handle_start) {
469 frame_handle_start->lower_to_bottom();
471 if (frame_handle_end) {
472 frame_handle_end->lower_to_bottom();
477 MidiRegionView::leave_internal()
479 hide_verbose_cursor ();
480 remove_ghost_note ();
483 if (_grabbed_keyboard) {
484 Keyboard::magic_widget_drop_focus();
485 _grabbed_keyboard = false;
488 // Raise sample handles above notes so they catch events
489 if (frame_handle_start) {
490 frame_handle_start->raise_to_top();
492 if (frame_handle_end) {
493 frame_handle_end->raise_to_top();
498 MidiRegionView::button_press (GdkEventButton* ev)
500 if (ev->button != 1) {
504 Editor* editor = dynamic_cast<Editor *> (&trackview.editor());
505 MouseMode m = editor->current_mouse_mode();
507 if (m == MouseContent && Keyboard::modifier_state_contains (ev->state, Keyboard::insert_note_modifier())) {
508 _press_cursor_ctx = CursorContext::create(*editor, editor->cursors()->midi_pencil);
511 if (_mouse_state != SelectTouchDragging) {
513 _pressed_button = ev->button;
515 if (m == MouseDraw || (m == MouseContent && Keyboard::modifier_state_contains (ev->state, Keyboard::insert_note_modifier()))) {
517 if (midi_view()->note_mode() == Percussive) {
518 editor->drags()->set (new HitCreateDrag (dynamic_cast<Editor *> (editor), group, this), (GdkEvent *) ev);
520 editor->drags()->set (new NoteCreateDrag (dynamic_cast<Editor *> (editor), group, this), (GdkEvent *) ev);
523 _mouse_state = AddDragging;
524 remove_ghost_note ();
525 hide_verbose_cursor ();
527 _mouse_state = Pressed;
533 _pressed_button = ev->button;
534 _mouse_changed_selection = false;
540 MidiRegionView::button_release (GdkEventButton* ev)
542 double event_x, event_y;
544 if (ev->button != 1) {
551 group->canvas_to_item (event_x, event_y);
554 PublicEditor& editor = trackview.editor ();
556 _press_cursor_ctx.reset();
558 switch (_mouse_state) {
559 case Pressed: // Clicked
561 switch (editor.current_mouse_mode()) {
563 /* no motion occurred - simple click */
564 clear_editor_note_selection ();
565 _mouse_changed_selection = true;
571 _mouse_changed_selection = true;
572 clear_editor_note_selection ();
587 /* Don't a ghost note when we added a note - wait until motion to avoid visual confusion.
588 we don't want one when we were drag-selecting either. */
589 case SelectRectDragging:
590 editor.drags()->end_grab ((GdkEvent *) ev);
599 if (_mouse_changed_selection) {
600 trackview.editor().begin_reversible_selection_op (X_("Mouse Selection Change"));
601 trackview.editor().commit_reversible_selection_op ();
608 MidiRegionView::motion (GdkEventMotion* ev)
610 PublicEditor& editor = trackview.editor ();
612 if (!_entered_note) {
614 if (_mouse_state == AddDragging) {
616 remove_ghost_note ();
619 } else if (!_ghost_note && editor.current_mouse_mode() == MouseContent &&
620 Keyboard::modifier_state_contains (ev->state, Keyboard::insert_note_modifier()) &&
621 _mouse_state != AddDragging) {
623 create_ghost_note (ev->x, ev->y, ev->state);
625 } else if (_ghost_note && editor.current_mouse_mode() == MouseContent &&
626 Keyboard::modifier_state_contains (ev->state, Keyboard::insert_note_modifier())) {
628 update_ghost_note (ev->x, ev->y, ev->state);
630 } else if (_ghost_note && editor.current_mouse_mode() == MouseContent) {
632 remove_ghost_note ();
633 hide_verbose_cursor ();
635 } else if (editor.current_mouse_mode() == MouseDraw) {
638 update_ghost_note (ev->x, ev->y, ev->state);
641 create_ghost_note (ev->x, ev->y, ev->state);
646 /* any motion immediately hides velocity text that may have been visible */
648 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
649 (*i)->hide_velocity ();
652 switch (_mouse_state) {
655 if (_pressed_button == 1) {
657 MouseMode m = editor.current_mouse_mode();
659 if (m == MouseContent && !Keyboard::modifier_state_contains (ev->state, Keyboard::insert_note_modifier())) {
660 editor.drags()->set (new MidiRubberbandSelectDrag (dynamic_cast<Editor *> (&editor), this), (GdkEvent *) ev);
661 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
662 clear_editor_note_selection ();
663 _mouse_changed_selection = true;
665 _mouse_state = SelectRectDragging;
667 } else if (m == MouseRange) {
668 editor.drags()->set (new MidiVerticalSelectDrag (dynamic_cast<Editor *> (&editor), this), (GdkEvent *) ev);
669 _mouse_state = SelectVerticalDragging;
676 case SelectRectDragging:
677 case SelectVerticalDragging:
679 editor.drags()->motion_handler ((GdkEvent *) ev, false);
682 case SelectTouchDragging:
690 //let RegionView do it's thing. drags are handled in here
691 return RegionView::canvas_group_event ((GdkEvent *) ev);
696 MidiRegionView::scroll (GdkEventScroll* ev)
698 if (trackview.editor().drags()->active()) {
701 if (_selection.empty()) {
705 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier) ||
706 Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
707 /* XXX: bit of a hack; allow PrimaryModifier and TertiaryModifier scroll
708 * through so that it still works for navigation.
713 hide_verbose_cursor ();
715 bool fine = !Keyboard::modifier_state_contains (ev->state, Keyboard::SecondaryModifier);
716 Keyboard::ModifierMask mask_together(Keyboard::PrimaryModifier|Keyboard::TertiaryModifier);
717 bool together = Keyboard::modifier_state_contains (ev->state, mask_together);
719 if (ev->direction == GDK_SCROLL_UP) {
720 change_velocities (true, fine, false, together);
721 } else if (ev->direction == GDK_SCROLL_DOWN) {
722 change_velocities (false, fine, false, together);
724 /* left, right: we don't use them */
732 MidiRegionView::key_press (GdkEventKey* ev)
734 /* since GTK bindings are generally activated on press, and since
735 detectable auto-repeat is the name of the game and only sends
736 repeated presses, carry out key actions at key press, not release.
738 bool unmodified = Keyboard::no_modifier_keys_pressed (ev);
740 if (unmodified && (ev->keyval == GDK_Alt_L || ev->keyval == GDK_Alt_R)) {
742 if (_mouse_state != AddDragging) {
743 _mouse_state = SelectTouchDragging;
748 } else if (ev->keyval == GDK_Escape && unmodified) {
749 clear_editor_note_selection ();
752 } else if (ev->keyval == GDK_comma || ev->keyval == GDK_period) {
754 bool start = (ev->keyval == GDK_comma);
755 bool end = (ev->keyval == GDK_period);
756 bool shorter = Keyboard::modifier_state_contains (ev->state, Keyboard::PrimaryModifier);
757 bool fine = Keyboard::modifier_state_contains (ev->state, Keyboard::SecondaryModifier);
759 change_note_lengths (fine, shorter, Temporal::Beats(), start, end);
763 } else if ((ev->keyval == GDK_BackSpace || ev->keyval == GDK_Delete) && unmodified) {
765 if (_selection.empty()) {
772 } else if (ev->keyval == GDK_Tab || ev->keyval == GDK_ISO_Left_Tab) {
774 trackview.editor().begin_reversible_selection_op (X_("Select Adjacent Note"));
776 if (Keyboard::modifier_state_contains (ev->state, Keyboard::PrimaryModifier)) {
777 goto_previous_note (Keyboard::modifier_state_contains (ev->state, Keyboard::TertiaryModifier));
779 goto_next_note (Keyboard::modifier_state_contains (ev->state, Keyboard::TertiaryModifier));
782 trackview.editor().commit_reversible_selection_op();
786 } else if (ev->keyval == GDK_Up) {
788 bool allow_smush = Keyboard::modifier_state_contains (ev->state, Keyboard::TertiaryModifier);
789 bool fine = !Keyboard::modifier_state_contains (ev->state, Keyboard::SecondaryModifier);
790 bool together = Keyboard::modifier_state_contains (ev->state, Keyboard::Level4Modifier);
792 if (Keyboard::modifier_state_contains (ev->state, Keyboard::PrimaryModifier)) {
793 change_velocities (true, fine, allow_smush, together);
795 transpose (true, fine, allow_smush);
799 } else if (ev->keyval == GDK_Down) {
801 bool allow_smush = Keyboard::modifier_state_contains (ev->state, Keyboard::TertiaryModifier);
802 bool fine = !Keyboard::modifier_state_contains (ev->state, Keyboard::SecondaryModifier);
803 bool together = Keyboard::modifier_state_contains (ev->state, Keyboard::Level4Modifier);
805 if (Keyboard::modifier_state_contains (ev->state, Keyboard::PrimaryModifier)) {
806 change_velocities (false, fine, allow_smush, together);
808 transpose (false, fine, allow_smush);
812 } else if (ev->keyval == GDK_Left) {
814 bool fine = !Keyboard::modifier_state_contains (ev->state, Keyboard::SecondaryModifier);
815 nudge_notes (false, fine);
818 } else if (ev->keyval == GDK_Right) {
820 bool fine = !Keyboard::modifier_state_contains (ev->state, Keyboard::SecondaryModifier);
821 nudge_notes (true, fine);
824 } else if (ev->keyval == GDK_c && unmodified) {
828 } else if (ev->keyval == GDK_v && unmodified) {
837 MidiRegionView::key_release (GdkEventKey* ev)
839 if ((_mouse_state == SelectTouchDragging) && (ev->keyval == GDK_Alt_L || ev->keyval == GDK_Alt_R)) {
847 MidiRegionView::channel_edit ()
849 if (_selection.empty()) {
853 /* pick a note somewhat at random (since Selection is a set<>) to
854 * provide the "current" channel for the dialog.
857 uint8_t current_channel = (*_selection.begin())->note()->channel ();
858 MidiChannelDialog channel_dialog (current_channel);
859 int ret = channel_dialog.run ();
862 case Gtk::RESPONSE_OK:
868 uint8_t new_channel = channel_dialog.active_channel ();
870 start_note_diff_command (_("channel edit"));
872 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
873 Selection::iterator next = i;
875 change_note_channel (*i, new_channel);
883 MidiRegionView::velocity_edit ()
885 if (_selection.empty()) {
889 /* pick a note somewhat at random (since Selection is a set<>) to
890 * provide the "current" velocity for the dialog.
893 uint8_t current_velocity = (*_selection.begin())->note()->velocity ();
894 MidiVelocityDialog velocity_dialog (current_velocity);
895 int ret = velocity_dialog.run ();
898 case Gtk::RESPONSE_OK:
904 uint8_t new_velocity = velocity_dialog.velocity ();
906 start_note_diff_command (_("velocity edit"));
908 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
909 Selection::iterator next = i;
911 change_note_velocity (*i, new_velocity, false);
919 MidiRegionView::show_list_editor ()
922 _list_editor = new MidiListEditor (trackview.session(), midi_region(), midi_view()->midi_track());
924 _list_editor->present ();
927 /** Add a note to the model, and the view, at a canvas (click) coordinate.
928 * \param t time in samples relative to the position of the region
929 * \param y vertical position in pixels
930 * \param length duration of the note in beats
931 * \param snap_t true to snap t to the grid, otherwise false.
934 MidiRegionView::create_note_at (samplepos_t t, double y, Temporal::Beats length, uint32_t state, bool shift_snap)
936 if (length < 2 * DBL_EPSILON) {
940 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
941 MidiStreamView* const view = mtv->midi_view();
942 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion> (_region);
948 // Start of note in samples relative to region start
949 const int32_t divisions = trackview.editor().get_grid_music_divisions (state);
950 Temporal::Beats beat_time = snap_sample_to_grid_underneath (t, divisions, shift_snap);
952 const double note = view->y_to_note(y);
953 const uint8_t chan = mtv->get_channel_for_add();
954 const uint8_t velocity = get_velocity_for_add(beat_time);
956 const boost::shared_ptr<NoteType> new_note(
957 new NoteType (chan, beat_time, length, (uint8_t)note, velocity));
959 if (_model->contains (new_note)) {
963 view->update_note_range(new_note->note());
965 start_note_diff_command(_("add note"));
967 note_diff_add_note (new_note, true, false);
971 play_midi_note (new_note);
975 MidiRegionView::clear_events ()
977 // clear selection without signaling
978 clear_selection_internal ();
981 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
982 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
988 _note_group->clear (true);
990 _patch_changes.clear();
992 _optimization_iterator = _events.end();
996 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
1000 content_connection.disconnect ();
1001 _model->ContentsChanged.connect (content_connection, invalidator (*this), boost::bind (&MidiRegionView::redisplay_model, this), gui_context());
1002 /* Don't signal as nobody else needs to know until selection has been altered. */
1005 if (_enable_display) {
1011 MidiRegionView::start_note_diff_command (string name)
1013 if (!_note_diff_command) {
1014 trackview.editor().begin_reversible_command (name);
1015 _note_diff_command = _model->new_note_diff_command (name);
1020 MidiRegionView::note_diff_add_note (const boost::shared_ptr<NoteType> note, bool selected, bool show_velocity)
1022 if (_note_diff_command) {
1023 _note_diff_command->add (note);
1026 _marked_for_selection.insert(note);
1028 if (show_velocity) {
1029 _marked_for_velocity.insert(note);
1034 MidiRegionView::note_diff_remove_note (NoteBase* ev)
1036 if (_note_diff_command && ev->note()) {
1037 _note_diff_command->remove(ev->note());
1042 MidiRegionView::note_diff_add_change (NoteBase* ev,
1043 MidiModel::NoteDiffCommand::Property property,
1046 if (_note_diff_command) {
1047 _note_diff_command->change (ev->note(), property, val);
1052 MidiRegionView::note_diff_add_change (NoteBase* ev,
1053 MidiModel::NoteDiffCommand::Property property,
1054 Temporal::Beats val)
1056 if (_note_diff_command) {
1057 _note_diff_command->change (ev->note(), property, val);
1062 MidiRegionView::apply_diff (bool as_subcommand, bool was_copy)
1064 bool commit = false;
1066 if (!_note_diff_command) {
1070 bool add_or_remove = _note_diff_command->adds_or_removes();
1072 if (!was_copy && add_or_remove) {
1073 // Mark all selected notes for selection when model reloads
1074 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1075 _marked_for_selection.insert((*i)->note());
1079 midi_view()->midi_track()->midi_playlist()->region_edited (_region, _note_diff_command);
1081 if (as_subcommand) {
1082 _model->apply_command_as_subcommand (*trackview.session(), _note_diff_command);
1084 _model->apply_command (*trackview.session(), _note_diff_command);
1088 _note_diff_command = 0;
1090 if (add_or_remove) {
1091 _marked_for_selection.clear();
1094 _marked_for_velocity.clear();
1096 trackview.editor().commit_reversible_command ();
1101 MidiRegionView::abort_command()
1103 delete _note_diff_command;
1104 _note_diff_command = 0;
1105 trackview.editor().abort_reversible_command();
1106 clear_editor_note_selection();
1110 MidiRegionView::find_canvas_note (boost::shared_ptr<NoteType> note)
1113 if (_optimization_iterator != _events.end()) {
1114 ++_optimization_iterator;
1117 if (_optimization_iterator != _events.end() && _optimization_iterator->first == note) {
1118 return _optimization_iterator->second;
1121 _optimization_iterator = _events.find (note);
1122 if (_optimization_iterator != _events.end()) {
1123 return _optimization_iterator->second;
1129 /** This version finds any canvas note matching the supplied note. */
1131 MidiRegionView::find_canvas_note (Evoral::event_id_t id)
1133 Events::iterator it;
1135 for (it = _events.begin(); it != _events.end(); ++it) {
1136 if (it->first->id() == id) {
1144 boost::shared_ptr<PatchChange>
1145 MidiRegionView::find_canvas_patch_change (MidiModel::PatchChangePtr p)
1147 PatchChanges::const_iterator f = _patch_changes.find (p);
1149 if (f != _patch_changes.end()) {
1153 return boost::shared_ptr<PatchChange>();
1156 boost::shared_ptr<SysEx>
1157 MidiRegionView::find_canvas_sys_ex (MidiModel::SysExPtr s)
1159 SysExes::const_iterator f = _sys_exes.find (s);
1161 if (f != _sys_exes.end()) {
1165 return boost::shared_ptr<SysEx>();
1169 MidiRegionView::get_events (Events& e, Evoral::Sequence<Temporal::Beats>::NoteOperator op, uint8_t val, int chan_mask)
1171 MidiModel::Notes notes;
1172 _model->get_notes (notes, op, val, chan_mask);
1174 for (MidiModel::Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
1175 NoteBase* cne = find_canvas_note (*n);
1177 e.insert (make_pair (*n, cne));
1183 MidiRegionView::redisplay_model()
1185 if (_active_notes) {
1186 // Currently recording
1187 const samplecnt_t zoom = trackview.editor().get_current_zoom();
1188 if (zoom != _last_display_zoom) {
1189 /* Update resolved canvas notes to reflect changes in zoom without
1190 touching model. Leave active notes (with length 0) alone since
1191 they are being extended. */
1192 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1193 if (i->second->note()->length() > 0) {
1194 update_note(i->second);
1197 _last_display_zoom = zoom;
1206 for (_optimization_iterator = _events.begin(); _optimization_iterator != _events.end(); ++_optimization_iterator) {
1207 _optimization_iterator->second->invalidate();
1210 bool empty_when_starting = _events.empty();
1211 _optimization_iterator = _events.begin();
1212 MidiModel::Notes missing_notes;
1216 MidiModel::ReadLock lock(_model->read_lock());
1217 MidiModel::Notes& notes (_model->notes());
1220 for (MidiModel::Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
1222 boost::shared_ptr<NoteType> note (*n);
1225 if (note_in_region_range (note, visible)) {
1226 if (!empty_when_starting && (cne = find_canvas_note (note)) != 0) {
1234 missing_notes.insert (note);
1239 if (!empty_when_starting) {
1240 MidiModel::Notes::iterator f;
1241 for (Events::iterator i = _events.begin(); i != _events.end(); ) {
1243 NoteBase* cne = i->second;
1245 /* remove note items that are no longer valid */
1246 if (!cne->valid()) {
1248 for (vector<GhostRegion*>::iterator j = ghosts.begin(); j != ghosts.end(); ++j) {
1249 MidiGhostRegion* gr = dynamic_cast<MidiGhostRegion*> (*j);
1251 gr->remove_note (cne);
1256 i = _events.erase (i);
1259 bool visible = cne->item()->visible();
1261 if ((sus = dynamic_cast<Note*>(cne))) {
1264 update_sustained (sus);
1267 } else if ((hit = dynamic_cast<Hit*>(cne))) {
1279 for (MidiModel::Notes::iterator n = missing_notes.begin(); n != missing_notes.end(); ++n) {
1280 boost::shared_ptr<NoteType> note (*n);
1284 if (note_in_region_range (note, visible)) {
1286 cne = add_note (note, true);
1288 cne = add_note (note, false);
1291 cne = add_note (note, false);
1294 for (set<Evoral::event_id_t>::iterator it = _pending_note_selection.begin(); it != _pending_note_selection.end(); ++it) {
1295 if ((*it) == note->id()) {
1296 add_to_selection (cne);
1301 for (vector<GhostRegion*>::iterator j = ghosts.begin(); j != ghosts.end(); ++j) {
1302 MidiGhostRegion* gr = dynamic_cast<MidiGhostRegion*> (*j);
1303 if (gr && !gr->trackview.hidden()) {
1304 gr->redisplay_model ();
1309 display_patch_changes ();
1311 _marked_for_selection.clear ();
1312 _marked_for_velocity.clear ();
1313 _pending_note_selection.clear ();
1318 MidiRegionView::display_patch_changes ()
1320 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
1321 uint16_t chn_mask = mtv->midi_track()->get_playback_channel_mask();
1323 for (uint8_t i = 0; i < 16; ++i) {
1324 display_patch_changes_on_channel (i, chn_mask & (1 << i));
1328 /** @param active_channel true to display patch changes fully, false to display
1329 * them `greyed-out' (as on an inactive channel)
1332 MidiRegionView::display_patch_changes_on_channel (uint8_t channel, bool active_channel)
1334 for (MidiModel::PatchChanges::const_iterator i = _model->patch_changes().begin(); i != _model->patch_changes().end(); ++i) {
1335 boost::shared_ptr<PatchChange> p;
1337 if ((*i)->channel() != channel) {
1341 if ((p = find_canvas_patch_change (*i)) != 0) {
1343 const samplecnt_t region_samples = source_beats_to_region_samples ((*i)->time());
1345 if (region_samples < 0 || region_samples >= _region->length()) {
1348 const double x = trackview.editor().sample_to_pixel (region_samples);
1349 const string patch_name = instrument_info().get_patch_name ((*i)->bank(), (*i)->program(), channel);
1350 p->canvas_item()->set_position (ArdourCanvas::Duple (x, 1.0));
1351 p->set_text (patch_name);
1357 const string patch_name = instrument_info().get_patch_name ((*i)->bank(), (*i)->program(), channel);
1358 add_canvas_patch_change (*i, patch_name, active_channel);
1364 MidiRegionView::display_sysexes()
1366 bool have_periodic_system_messages = false;
1367 bool display_periodic_messages = true;
1369 if (!UIConfiguration::instance().get_never_display_periodic_midi()) {
1371 for (MidiModel::SysExes::const_iterator i = _model->sysexes().begin(); i != _model->sysexes().end(); ++i) {
1372 if ((*i)->is_spp() || (*i)->is_mtc_quarter() || (*i)->is_mtc_full()) {
1373 have_periodic_system_messages = true;
1378 if (have_periodic_system_messages) {
1379 double zoom = trackview.editor().get_current_zoom (); // samples per pixel
1381 /* get an approximate value for the number of samples per video frame */
1383 double video_frame = trackview.session()->sample_rate() * (1.0/30);
1385 /* if we are zoomed out beyond than the cutoff (i.e. more
1386 * samples per pixel than samples per 4 video frames), don't
1387 * show periodic sysex messages.
1390 if (zoom > (video_frame*4)) {
1391 display_periodic_messages = false;
1395 display_periodic_messages = false;
1398 for (MidiModel::SysExes::const_iterator i = _model->sysexes().begin(); i != _model->sysexes().end(); ++i) {
1399 MidiModel::SysExPtr sysex_ptr = *i;
1400 Temporal::Beats time = sysex_ptr->time();
1402 if ((*i)->is_spp() || (*i)->is_mtc_quarter() || (*i)->is_mtc_full()) {
1403 if (!display_periodic_messages) {
1410 for (uint32_t b = 0; b < (*i)->size(); ++b) {
1411 str << int((*i)->buffer()[b]);
1412 if (b != (*i)->size() -1) {
1416 string text = str.str();
1418 const double x = trackview.editor().sample_to_pixel(source_beats_to_region_samples(time));
1420 double height = midi_stream_view()->contents_height();
1422 // CAIROCANVAS: no longer passing *i (the sysex event) to the
1423 // SysEx canvas object!!!
1424 boost::shared_ptr<SysEx> sysex = find_canvas_sys_ex (sysex_ptr);
1427 sysex = boost::shared_ptr<SysEx>(
1428 new SysEx (*this, _note_group, text, height, x, 1.0, sysex_ptr));
1429 _sys_exes.insert (make_pair (sysex_ptr, sysex));
1431 sysex->set_height (height);
1432 sysex->item().set_position (ArdourCanvas::Duple (x, 1.0));
1435 // Show unless message is beyond the region bounds
1436 // XXX REQUIRES APPROPRIATE OPERATORS FOR Temporal::Beats and samplepos? say what?
1437 #warning paul fix this
1438 // if (time - _region->start() >= _region->length() || time < _region->start()) {
1446 MidiRegionView::~MidiRegionView ()
1448 in_destructor = true;
1450 hide_verbose_cursor ();
1452 delete _list_editor;
1454 RegionViewGoingAway (this); /* EMIT_SIGNAL */
1456 if (_active_notes) {
1463 delete _note_diff_command;
1464 delete _step_edit_cursor;
1468 MidiRegionView::region_resized (const PropertyChange& what_changed)
1470 RegionView::region_resized(what_changed); // calls RegionView::set_duration()
1472 if (what_changed.contains (ARDOUR::Properties::position)) {
1473 _region_relative_time_converter.set_origin_b(_region->position());
1474 _region_relative_time_converter_double.set_origin_b(_region->position());
1475 /* reset_width dependent_items() redisplays model */
1479 if (what_changed.contains (ARDOUR::Properties::start) ||
1480 what_changed.contains (ARDOUR::Properties::position)) {
1481 _source_relative_time_converter.set_origin_b (_region->position() - _region->start());
1483 /* catch end and start trim so we can update the view*/
1484 if (!what_changed.contains (ARDOUR::Properties::start) &&
1485 what_changed.contains (ARDOUR::Properties::length)) {
1486 enable_display (true);
1487 } else if (what_changed.contains (ARDOUR::Properties::start) &&
1488 what_changed.contains (ARDOUR::Properties::length)) {
1489 enable_display (true);
1494 MidiRegionView::reset_width_dependent_items (double pixel_width)
1496 RegionView::reset_width_dependent_items(pixel_width);
1498 if (_enable_display) {
1502 bool hide_all = false;
1503 PatchChanges::iterator x = _patch_changes.begin();
1504 if (x != _patch_changes.end()) {
1505 hide_all = x->second->width() >= _pixel_width;
1509 for (; x != _patch_changes.end(); ++x) {
1514 move_step_edit_cursor (_step_edit_cursor_position);
1515 set_step_edit_cursor_width (_step_edit_cursor_width);
1519 MidiRegionView::set_height (double height)
1521 double old_height = _height;
1522 RegionView::set_height(height);
1524 apply_note_range (midi_stream_view()->lowest_note(),
1525 midi_stream_view()->highest_note(),
1526 height != old_height);
1529 name_text->raise_to_top();
1532 for (PatchChanges::iterator x = _patch_changes.begin(); x != _patch_changes.end(); ++x) {
1533 (*x).second->set_height (midi_stream_view()->contents_height());
1536 if (_step_edit_cursor) {
1537 _step_edit_cursor->set_y1 (midi_stream_view()->contents_height());
1542 /** Apply the current note range from the stream view
1543 * by repositioning/hiding notes as necessary
1546 MidiRegionView::apply_note_range (uint8_t min, uint8_t max, bool force)
1548 if (!_enable_display) {
1552 if (!force && _current_range_min == min && _current_range_max == max) {
1556 _current_range_min = min;
1557 _current_range_max = max;
1563 MidiRegionView::add_ghost (TimeAxisView& tv)
1565 double unit_position = _region->position () / samples_per_pixel;
1566 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
1567 MidiGhostRegion* ghost;
1569 if (mtv && mtv->midi_view()) {
1570 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group
1571 to allow having midi notes on top of note lines and waveforms.
1573 ghost = new MidiGhostRegion (*this, *mtv->midi_view(), trackview, unit_position);
1575 ghost = new MidiGhostRegion (*this, tv, trackview, unit_position);
1578 ghost->set_colors ();
1579 ghost->set_height ();
1580 ghost->set_duration (_region->length() / samples_per_pixel);
1582 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1583 ghost->add_note(i->second);
1586 ghosts.push_back (ghost);
1587 enable_display (true);
1592 /** Begin tracking note state for successive calls to add_event
1595 MidiRegionView::begin_write()
1597 if (_active_notes) {
1598 delete[] _active_notes;
1600 _active_notes = new Note*[128];
1601 for (unsigned i = 0; i < 128; ++i) {
1602 _active_notes[i] = 0;
1607 /** Destroy note state for add_event
1610 MidiRegionView::end_write()
1612 delete[] _active_notes;
1614 _marked_for_selection.clear();
1615 _marked_for_velocity.clear();
1619 /** Resolve an active MIDI note (while recording).
1622 MidiRegionView::resolve_note(uint8_t note, Temporal::Beats end_time)
1624 if (midi_view()->note_mode() != Sustained) {
1628 if (_active_notes && _active_notes[note]) {
1629 /* Set note length so update_note() works. Note this is a local note
1630 for recording, not from a model, so we can safely mess with it. */
1631 _active_notes[note]->note()->set_length(
1632 end_time - _active_notes[note]->note()->time());
1634 /* End time is relative to the region being recorded. */
1635 const samplepos_t end_time_samples = region_beats_to_region_samples(end_time);
1637 _active_notes[note]->set_x1 (trackview.editor().sample_to_pixel(end_time_samples));
1638 _active_notes[note]->set_outline_all ();
1639 _active_notes[note] = 0;
1644 /** Extend active notes to rightmost edge of region (if length is changed)
1647 MidiRegionView::extend_active_notes()
1649 if (!_active_notes) {
1653 for (unsigned i = 0; i < 128; ++i) {
1654 if (_active_notes[i]) {
1655 _active_notes[i]->set_x1(
1656 trackview.editor().sample_to_pixel(_region->length()));
1662 MidiRegionView::play_midi_note(boost::shared_ptr<NoteType> note)
1664 if (_no_sound_notes || !UIConfiguration::instance().get_sound_midi_notes()) {
1668 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
1670 if (!route_ui || !route_ui->midi_track()) {
1674 NotePlayer* np = new NotePlayer (route_ui->midi_track ());
1678 /* NotePlayer deletes itself */
1682 MidiRegionView::start_playing_midi_note(boost::shared_ptr<NoteType> note)
1684 const std::vector< boost::shared_ptr<NoteType> > notes(1, note);
1685 start_playing_midi_chord(notes);
1689 MidiRegionView::start_playing_midi_chord (vector<boost::shared_ptr<NoteType> > notes)
1691 if (_no_sound_notes || !UIConfiguration::instance().get_sound_midi_notes()) {
1695 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
1697 if (!route_ui || !route_ui->midi_track()) {
1701 NotePlayer* player = new NotePlayer (route_ui->midi_track());
1703 for (vector<boost::shared_ptr<NoteType> >::iterator n = notes.begin(); n != notes.end(); ++n) {
1712 MidiRegionView::note_in_region_range (const boost::shared_ptr<NoteType> note, bool& visible) const
1714 const boost::shared_ptr<ARDOUR::MidiRegion> midi_reg = midi_region();
1716 /* must compare double explicitly as Beats::operator< rounds to ppqn */
1717 const bool outside = (note->time().to_double() < midi_reg->start_beats() ||
1718 note->time().to_double() >= midi_reg->start_beats() + midi_reg->length_beats());
1720 visible = (note->note() >= _current_range_min) &&
1721 (note->note() <= _current_range_max);
1727 MidiRegionView::update_note (NoteBase* note, bool update_ghost_regions)
1731 if ((sus = dynamic_cast<Note*>(note))) {
1732 update_sustained(sus, update_ghost_regions);
1733 } else if ((hit = dynamic_cast<Hit*>(note))) {
1734 update_hit(hit, update_ghost_regions);
1738 /** Update a canvas note's size from its model note.
1739 * @param ev Canvas note to update.
1740 * @param update_ghost_regions true to update the note in any ghost regions that we have, otherwise false.
1743 MidiRegionView::update_sustained (Note* ev, bool update_ghost_regions)
1745 TempoMap& map (trackview.session()->tempo_map());
1746 const boost::shared_ptr<ARDOUR::MidiRegion> mr = midi_region();
1747 boost::shared_ptr<NoteType> note = ev->note();
1749 const double session_source_start = _region->quarter_note() - mr->start_beats();
1750 const samplepos_t note_start_samples = map.sample_at_quarter_note (note->time().to_double() + session_source_start) - _region->position();
1752 const double x0 = trackview.editor().sample_to_pixel (note_start_samples);
1754 const double y0 = 1 + floor(note_to_y(note->note()));
1757 /* trim note display to not overlap the end of its region */
1758 if (note->length().to_double() > 0.0) {
1759 double note_end_time = note->end_time().to_double();
1761 if (note_end_time > mr->start_beats() + mr->length_beats()) {
1762 note_end_time = mr->start_beats() + mr->length_beats();
1765 const samplepos_t note_end_samples = map.sample_at_quarter_note (session_source_start + note_end_time) - _region->position();
1767 x1 = std::max(1., trackview.editor().sample_to_pixel (note_end_samples)) - 1;
1769 x1 = std::max(1., trackview.editor().sample_to_pixel (_region->length())) - 1;
1772 y1 = y0 + std::max(1., floor(note_height()) - 1);
1774 ev->set (ArdourCanvas::Rect (x0, y0, x1, y1));
1775 ev->set_velocity (note->velocity()/127.0);
1777 if (!note->length()) {
1778 if (_active_notes && note->note() < 128) {
1779 Note* const old_rect = _active_notes[note->note()];
1781 /* There is an active note on this key, so we have a stuck
1782 note. Finish the old rectangle here. */
1783 old_rect->set_x1 (x1);
1784 old_rect->set_outline_all ();
1786 _active_notes[note->note()] = ev;
1788 /* outline all but right edge */
1789 ev->set_outline_what (ArdourCanvas::Rectangle::What (
1790 ArdourCanvas::Rectangle::TOP|
1791 ArdourCanvas::Rectangle::LEFT|
1792 ArdourCanvas::Rectangle::BOTTOM));
1794 /* outline all edges */
1795 ev->set_outline_all ();
1798 // Update color in case velocity has changed
1799 const uint32_t base_col = ev->base_color();
1800 ev->set_fill_color(base_col);
1801 ev->set_outline_color(ev->calculate_outline(base_col, ev->selected()));
1806 MidiRegionView::update_hit (Hit* ev, bool update_ghost_regions)
1808 boost::shared_ptr<NoteType> note = ev->note();
1810 const double note_time_qn = note->time().to_double() + (_region->quarter_note() - midi_region()->start_beats());
1811 const samplepos_t note_start_samples = trackview.session()->tempo_map().sample_at_quarter_note (note_time_qn) - _region->position();
1813 const double x = trackview.editor().sample_to_pixel(note_start_samples);
1814 const double diamond_size = std::max(1., floor(note_height()) - 2.);
1815 const double y = 1.5 + floor(note_to_y(note->note())) + diamond_size * .5;
1817 // see DnD note in MidiRegionView::apply_note_range() above
1818 if (y <= 0 || y >= _height) {
1824 ev->set_position (ArdourCanvas::Duple (x, y));
1825 ev->set_height (diamond_size);
1827 // Update color in case velocity has changed
1828 const uint32_t base_col = ev->base_color();
1829 ev->set_fill_color(base_col);
1830 ev->set_outline_color(ev->calculate_outline(base_col, ev->selected()));
1834 /** Add a MIDI note to the view (with length).
1836 * If in sustained mode, notes with length 0 will be considered active
1837 * notes, and resolve_note should be called when the corresponding note off
1838 * event arrives, to properly display the note.
1841 MidiRegionView::add_note(const boost::shared_ptr<NoteType> note, bool visible)
1843 NoteBase* event = 0;
1845 if (midi_view()->note_mode() == Sustained) {
1847 Note* ev_rect = new Note (*this, _note_group, note); // XXX may leak
1849 update_sustained (ev_rect);
1853 } else if (midi_view()->note_mode() == Percussive) {
1855 const double diamond_size = std::max(1., floor(note_height()) - 2.);
1857 Hit* ev_diamond = new Hit (*this, _note_group, diamond_size, note); // XXX may leak
1859 update_hit (ev_diamond);
1868 MidiGhostRegion* gr;
1870 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
1871 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
1872 gr->add_note(event);
1876 if (_marked_for_selection.find(note) != _marked_for_selection.end()) {
1877 note_selected(event, true);
1880 if (_marked_for_velocity.find(note) != _marked_for_velocity.end()) {
1881 event->show_velocity();
1884 event->on_channel_selection_change (get_selected_channels());
1885 _events.insert (make_pair (event->note(), event));
1894 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
1895 MidiStreamView* const view = mtv->midi_view();
1897 view->update_note_range (note->note());
1902 MidiRegionView::step_add_note (uint8_t channel, uint8_t number, uint8_t velocity,
1903 Temporal::Beats pos, Temporal::Beats len)
1905 boost::shared_ptr<NoteType> new_note (new NoteType (channel, pos, len, number, velocity));
1907 /* potentially extend region to hold new note */
1909 samplepos_t end_sample = source_beats_to_absolute_samples (new_note->end_time());
1910 samplepos_t region_end = _region->last_sample();
1912 if (end_sample > region_end) {
1913 /* XX sets length in beats from audio space. make musical */
1914 _region->set_length (end_sample - _region->position(), 0);
1917 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
1918 MidiStreamView* const view = mtv->midi_view();
1920 view->update_note_range(new_note->note());
1922 _marked_for_selection.clear ();
1924 start_note_diff_command (_("step add"));
1926 clear_editor_note_selection ();
1927 note_diff_add_note (new_note, true, false);
1931 // last_step_edit_note = new_note;
1935 MidiRegionView::step_sustain (Temporal::Beats beats)
1937 change_note_lengths (false, false, beats, false, true);
1940 /** Add a new patch change flag to the canvas.
1941 * @param patch the patch change to add
1942 * @param the text to display in the flag
1943 * @param active_channel true to display the flag as on an active channel, false to grey it out for an inactive channel.
1946 MidiRegionView::add_canvas_patch_change (MidiModel::PatchChangePtr patch, const string& displaytext, bool /*active_channel*/)
1948 samplecnt_t region_samples = source_beats_to_region_samples (patch->time());
1949 const double x = trackview.editor().sample_to_pixel (region_samples);
1951 double const height = midi_stream_view()->contents_height();
1953 // CAIROCANVAS: active_channel info removed from PatcChange constructor
1954 // so we need to do something more sophisticated to keep its color
1955 // appearance (MidiPatchChangeFill/MidiPatchChangeInactiveChannelFill)
1957 boost::shared_ptr<PatchChange> patch_change = boost::shared_ptr<PatchChange>(
1958 new PatchChange(*this, group,
1964 _patch_change_outline,
1968 if (patch_change->item().width() < _pixel_width) {
1969 // Show unless patch change is beyond the region bounds
1970 if (region_samples < 0 || region_samples >= _region->length()) {
1971 patch_change->hide();
1973 patch_change->show();
1976 patch_change->hide ();
1979 _patch_changes.insert (make_pair (patch, patch_change));
1983 MidiRegionView::remove_canvas_patch_change (PatchChange* pc)
1985 /* remove the canvas item */
1986 for (PatchChanges::iterator x = _patch_changes.begin(); x != _patch_changes.end(); ++x) {
1987 if (x->second->patch() == pc->patch()) {
1988 _patch_changes.erase (x);
1994 MIDI::Name::PatchPrimaryKey
1995 MidiRegionView::patch_change_to_patch_key (MidiModel::PatchChangePtr p)
1997 return MIDI::Name::PatchPrimaryKey (p->program(), p->bank());
2000 /// Return true iff @p pc applies to the given time on the given channel.
2002 patch_applies (const ARDOUR::MidiModel::constPatchChangePtr pc, Temporal::Beats time, uint8_t channel)
2004 return pc->time() <= time && pc->channel() == channel;
2008 MidiRegionView::get_patch_key_at (Temporal::Beats time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key) const
2010 // The earliest event not before time
2011 MidiModel::PatchChanges::iterator i = _model->patch_change_lower_bound (time);
2013 // Go backwards until we find the latest PC for this channel, or the start
2014 while (i != _model->patch_changes().begin() &&
2015 (i == _model->patch_changes().end() ||
2016 !patch_applies(*i, time, channel))) {
2020 if (i != _model->patch_changes().end() && patch_applies(*i, time, channel)) {
2021 key.set_bank((*i)->bank());
2022 key.set_program((*i)->program ());
2030 MidiRegionView::change_patch_change (PatchChange& pc, const MIDI::Name::PatchPrimaryKey& new_patch)
2032 string name = _("alter patch change");
2033 trackview.editor().begin_reversible_command (name);
2035 MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (name);
2037 if (pc.patch()->program() != new_patch.program()) {
2038 c->change_program (pc.patch (), new_patch.program());
2041 int const new_bank = new_patch.bank();
2042 if (pc.patch()->bank() != new_bank) {
2043 c->change_bank (pc.patch (), new_bank);
2046 _model->apply_command (*trackview.session(), c);
2047 trackview.editor().commit_reversible_command ();
2049 remove_canvas_patch_change (&pc);
2050 display_patch_changes ();
2054 MidiRegionView::change_patch_change (MidiModel::PatchChangePtr old_change, const Evoral::PatchChange<Temporal::Beats> & new_change)
2056 string name = _("alter patch change");
2057 trackview.editor().begin_reversible_command (name);
2058 MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (name);
2060 if (old_change->time() != new_change.time()) {
2061 c->change_time (old_change, new_change.time());
2064 if (old_change->channel() != new_change.channel()) {
2065 c->change_channel (old_change, new_change.channel());
2068 if (old_change->program() != new_change.program()) {
2069 c->change_program (old_change, new_change.program());
2072 if (old_change->bank() != new_change.bank()) {
2073 c->change_bank (old_change, new_change.bank());
2076 _model->apply_command (*trackview.session(), c);
2077 trackview.editor().commit_reversible_command ();
2079 for (PatchChanges::iterator x = _patch_changes.begin(); x != _patch_changes.end(); ++x) {
2080 if (x->second->patch() == old_change) {
2081 _patch_changes.erase (x);
2086 display_patch_changes ();
2089 /** Add a patch change to the region.
2090 * @param t Time in samples relative to region position
2091 * @param patch Patch to add; time and channel are ignored (time is converted from t, and channel comes from
2092 * MidiTimeAxisView::get_channel_for_add())
2095 MidiRegionView::add_patch_change (samplecnt_t t, Evoral::PatchChange<Temporal::Beats> const & patch)
2097 string name = _("add patch change");
2099 trackview.editor().begin_reversible_command (name);
2100 MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (name);
2101 c->add (MidiModel::PatchChangePtr (
2102 new Evoral::PatchChange<Temporal::Beats> (
2103 absolute_samples_to_source_beats (_region->position() + t),
2104 patch.channel(), patch.program(), patch.bank()
2109 _model->apply_command (*trackview.session(), c);
2110 trackview.editor().commit_reversible_command ();
2112 display_patch_changes ();
2116 MidiRegionView::move_patch_change (PatchChange& pc, Temporal::Beats t)
2118 trackview.editor().begin_reversible_command (_("move patch change"));
2119 MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (_("move patch change"));
2120 c->change_time (pc.patch (), t);
2121 _model->apply_command (*trackview.session(), c);
2122 trackview.editor().commit_reversible_command ();
2124 display_patch_changes ();
2128 MidiRegionView::delete_patch_change (PatchChange* pc)
2130 trackview.editor().begin_reversible_command (_("delete patch change"));
2132 MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (_("delete patch change"));
2133 c->remove (pc->patch ());
2134 _model->apply_command (*trackview.session(), c);
2135 trackview.editor().commit_reversible_command ();
2137 remove_canvas_patch_change (pc);
2138 display_patch_changes ();
2142 MidiRegionView::step_patch (PatchChange& patch, bool bank, int delta)
2144 MIDI::Name::PatchPrimaryKey key = patch_change_to_patch_key(patch.patch());
2146 key.set_bank(key.bank() + delta);
2148 key.set_program(key.program() + delta);
2150 change_patch_change(patch, key);
2154 MidiRegionView::note_deleted (NoteBase* cne)
2156 if (_entered_note && cne == _entered_note) {
2160 if (_selection.empty()) {
2164 _selection.erase (cne);
2168 MidiRegionView::delete_selection()
2170 if (_selection.empty()) {
2174 if (trackview.editor().drags()->active()) {
2178 start_note_diff_command (_("delete selection"));
2180 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2181 if ((*i)->selected()) {
2182 _note_diff_command->remove((*i)->note());
2190 hide_verbose_cursor ();
2194 MidiRegionView::delete_note (boost::shared_ptr<NoteType> n)
2196 start_note_diff_command (_("delete note"));
2197 _note_diff_command->remove (n);
2200 hide_verbose_cursor ();
2204 MidiRegionView::clear_editor_note_selection ()
2206 DEBUG_TRACE(DEBUG::Selection, "MRV::clear_editor_note_selection\n");
2207 PublicEditor& editor(trackview.editor());
2208 editor.get_selection().clear_midi_notes();
2212 MidiRegionView::clear_selection ()
2214 clear_selection_internal();
2215 PublicEditor& editor(trackview.editor());
2216 editor.get_selection().remove(this);
2220 MidiRegionView::clear_selection_internal ()
2222 DEBUG_TRACE(DEBUG::Selection, "MRV::clear_selection_internal\n");
2224 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2225 (*i)->set_selected(false);
2226 (*i)->hide_velocity();
2231 // Clearing selection entirely, ungrab keyboard
2232 Keyboard::magic_widget_drop_focus();
2233 _grabbed_keyboard = false;
2238 MidiRegionView::unique_select(NoteBase* ev)
2240 clear_editor_note_selection();
2241 add_to_selection(ev);
2245 MidiRegionView::select_all_notes ()
2247 clear_editor_note_selection ();
2249 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2250 add_to_selection (i->second);
2255 MidiRegionView::select_range (samplepos_t start, samplepos_t end)
2257 clear_editor_note_selection ();
2259 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2260 samplepos_t t = source_beats_to_absolute_samples(i->first->time());
2261 if (t >= start && t <= end) {
2262 add_to_selection (i->second);
2268 MidiRegionView::invert_selection ()
2270 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2271 if (i->second->selected()) {
2272 remove_from_selection(i->second);
2274 add_to_selection (i->second);
2279 /** Used for selection undo/redo.
2280 The requested notes most likely won't exist in the view until the next model redisplay.
2283 MidiRegionView::select_notes (list<Evoral::event_id_t> notes)
2286 list<Evoral::event_id_t>::iterator n;
2288 for (n = notes.begin(); n != notes.end(); ++n) {
2289 if ((cne = find_canvas_note(*n)) != 0) {
2290 add_to_selection (cne);
2292 _pending_note_selection.insert(*n);
2298 MidiRegionView::select_matching_notes (uint8_t notenum, uint16_t channel_mask, bool add, bool extend)
2300 bool have_selection = !_selection.empty();
2301 uint8_t low_note = 127;
2302 uint8_t high_note = 0;
2303 MidiModel::Notes& notes (_model->notes());
2304 _optimization_iterator = _events.begin();
2306 if (extend && !have_selection) {
2310 /* scan existing selection to get note range */
2312 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2313 if ((*i)->note()->note() < low_note) {
2314 low_note = (*i)->note()->note();
2316 if ((*i)->note()->note() > high_note) {
2317 high_note = (*i)->note()->note();
2322 clear_editor_note_selection ();
2324 if (!extend && (low_note == high_note) && (high_note == notenum)) {
2325 /* only note previously selected is the one we are
2326 * reselecting. treat this as cancelling the selection.
2333 low_note = min (low_note, notenum);
2334 high_note = max (high_note, notenum);
2337 _no_sound_notes = true;
2339 for (MidiModel::Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
2341 boost::shared_ptr<NoteType> note (*n);
2343 bool select = false;
2345 if (((1 << note->channel()) & channel_mask) != 0) {
2347 if ((note->note() >= low_note && note->note() <= high_note)) {
2350 } else if (note->note() == notenum) {
2356 if ((cne = find_canvas_note (note)) != 0) {
2357 // extend is false because we've taken care of it,
2358 // since it extends by time range, not pitch.
2359 note_selected (cne, add, false);
2363 add = true; // we need to add all remaining matching notes, even if the passed in value was false (for "set")
2367 _no_sound_notes = false;
2371 MidiRegionView::toggle_matching_notes (uint8_t notenum, uint16_t channel_mask)
2373 MidiModel::Notes& notes (_model->notes());
2374 _optimization_iterator = _events.begin();
2376 for (MidiModel::Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
2378 boost::shared_ptr<NoteType> note (*n);
2381 if (note->note() == notenum && (((0x0001 << note->channel()) & channel_mask) != 0)) {
2382 if ((cne = find_canvas_note (note)) != 0) {
2383 if (cne->selected()) {
2384 note_deselected (cne);
2386 note_selected (cne, true, false);
2394 MidiRegionView::note_selected (NoteBase* ev, bool add, bool extend)
2397 clear_editor_note_selection();
2398 add_to_selection (ev);
2403 if (!ev->selected()) {
2404 add_to_selection (ev);
2408 /* find end of latest note selected, select all between that and the start of "ev" */
2410 Temporal::Beats earliest = std::numeric_limits<Temporal::Beats>::max();
2411 Temporal::Beats latest = Temporal::Beats();
2413 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2414 if ((*i)->note()->end_time() > latest) {
2415 latest = (*i)->note()->end_time();
2417 if ((*i)->note()->time() < earliest) {
2418 earliest = (*i)->note()->time();
2422 if (ev->note()->end_time() > latest) {
2423 latest = ev->note()->end_time();
2426 if (ev->note()->time() < earliest) {
2427 earliest = ev->note()->time();
2430 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2432 /* find notes entirely within OR spanning the earliest..latest range */
2434 if ((i->first->time() >= earliest && i->first->end_time() <= latest) ||
2435 (i->first->time() <= earliest && i->first->end_time() >= latest)) {
2436 add_to_selection (i->second);
2443 MidiRegionView::note_deselected(NoteBase* ev)
2445 remove_from_selection (ev);
2449 MidiRegionView::update_drag_selection(samplepos_t start, samplepos_t end, double gy0, double gy1, bool extend)
2451 PublicEditor& editor = trackview.editor();
2453 // Convert to local coordinates
2454 const samplepos_t p = _region->position();
2455 const double y = midi_view()->y_position();
2456 const double x0 = editor.sample_to_pixel(max((samplepos_t)0, start - p));
2457 const double x1 = editor.sample_to_pixel(max((samplepos_t)0, end - p));
2458 const double y0 = max(0.0, gy0 - y);
2459 const double y1 = max(0.0, gy1 - y);
2461 // TODO: Make this faster by storing the last updated selection rect, and only
2462 // adjusting things that are in the area that appears/disappeared.
2463 // We probably need a tree to be able to find events in O(log(n)) time.
2465 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2466 if (i->second->x0() < x1 && i->second->x1() > x0 && i->second->y0() < y1 && i->second->y1() > y0) {
2467 // Rectangles intersect
2468 if (!i->second->selected()) {
2469 add_to_selection (i->second);
2471 } else if (i->second->selected() && !extend) {
2472 // Rectangles do not intersect
2473 remove_from_selection (i->second);
2477 typedef RouteTimeAxisView::AutomationTracks ATracks;
2478 typedef std::list<Selectable*> Selectables;
2480 /* Add control points to selection. */
2481 const ATracks& atracks = midi_view()->automation_tracks();
2482 Selectables selectables;
2483 editor.get_selection().clear_points();
2484 for (ATracks::const_iterator a = atracks.begin(); a != atracks.end(); ++a) {
2485 a->second->get_selectables(start, end, gy0, gy1, selectables);
2486 for (Selectables::const_iterator s = selectables.begin(); s != selectables.end(); ++s) {
2487 ControlPoint* cp = dynamic_cast<ControlPoint*>(*s);
2489 editor.get_selection().add(cp);
2492 a->second->set_selected_points(editor.get_selection().points);
2497 MidiRegionView::update_vertical_drag_selection (double y1, double y2, bool extend)
2503 // TODO: Make this faster by storing the last updated selection rect, and only
2504 // adjusting things that are in the area that appears/disappeared.
2505 // We probably need a tree to be able to find events in O(log(n)) time.
2507 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2508 if ((i->second->y1() >= y1 && i->second->y1() <= y2)) {
2509 // within y- (note-) range
2510 if (!i->second->selected()) {
2511 add_to_selection (i->second);
2513 } else if (i->second->selected() && !extend) {
2514 remove_from_selection (i->second);
2520 MidiRegionView::remove_from_selection (NoteBase* ev)
2522 Selection::iterator i = _selection.find (ev);
2524 if (i != _selection.end()) {
2525 _selection.erase (i);
2526 if (_selection.empty() && _grabbed_keyboard) {
2528 Keyboard::magic_widget_drop_focus();
2529 _grabbed_keyboard = false;
2533 ev->set_selected (false);
2534 ev->hide_velocity ();
2536 if (_selection.empty()) {
2537 PublicEditor& editor (trackview.editor());
2538 editor.get_selection().remove (this);
2543 MidiRegionView::add_to_selection (NoteBase* ev)
2545 const bool selection_was_empty = _selection.empty();
2547 if (_selection.insert (ev).second) {
2548 ev->set_selected (true);
2549 start_playing_midi_note ((ev)->note());
2550 if (selection_was_empty && _entered) {
2551 // Grab keyboard for moving notes with arrow keys
2552 Keyboard::magic_widget_grab_focus();
2553 _grabbed_keyboard = true;
2557 if (selection_was_empty) {
2558 PublicEditor& editor (trackview.editor());
2559 editor.get_selection().add (this);
2564 MidiRegionView::earliest_in_selection ()
2566 Temporal::Beats earliest = std::numeric_limits<Temporal::Beats>::max();
2568 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2569 if ((*i)->note()->time() < earliest) {
2570 earliest = (*i)->note()->time();
2578 MidiRegionView::move_selection(double dx_qn, double dy, double cumulative_dy)
2580 typedef vector<boost::shared_ptr<NoteType> > PossibleChord;
2581 Editor* editor = dynamic_cast<Editor*> (&trackview.editor());
2582 TempoMap& tmap (editor->session()->tempo_map());
2583 PossibleChord to_play;
2584 Temporal::Beats earliest = earliest_in_selection();
2586 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2588 if (n->note()->time() == earliest) {
2589 to_play.push_back (n->note());
2591 double const note_time_qn = session_relative_qn (n->note()->time().to_double());
2593 if (midi_view()->note_mode() == Sustained) {
2594 dx = editor->sample_to_pixel_unrounded (tmap.sample_at_quarter_note (note_time_qn + dx_qn))
2595 - n->item()->item_to_canvas (ArdourCanvas::Duple (n->x0(), 0)).x;
2597 /* Hit::x0() is offset by _position.x, unlike Note::x0() */
2598 Hit* hit = dynamic_cast<Hit*>(n);
2600 dx = editor->sample_to_pixel_unrounded (tmap.sample_at_quarter_note (note_time_qn + dx_qn))
2601 - n->item()->item_to_canvas (ArdourCanvas::Duple (((hit->x0() + hit->x1()) / 2.0) - hit->position().x, 0)).x;
2605 (*i)->move_event(dx, dy);
2608 if (midi_view()->note_mode() == Sustained) {
2609 Note* sus = dynamic_cast<Note*> (*i);
2610 double const len_dx = editor->sample_to_pixel_unrounded (
2611 tmap.sample_at_quarter_note (note_time_qn + dx_qn + n->note()->length().to_double()));
2613 sus->set_x1 (n->item()->canvas_to_item (ArdourCanvas::Duple (len_dx, 0)).x);
2617 if (dy && !_selection.empty() && !_no_sound_notes && UIConfiguration::instance().get_sound_midi_notes()) {
2619 if (to_play.size() > 1) {
2621 PossibleChord shifted;
2623 for (PossibleChord::iterator n = to_play.begin(); n != to_play.end(); ++n) {
2624 boost::shared_ptr<NoteType> moved_note (new NoteType (**n));
2625 moved_note->set_note (moved_note->note() + cumulative_dy);
2626 shifted.push_back (moved_note);
2629 start_playing_midi_chord (shifted);
2631 } else if (!to_play.empty()) {
2633 boost::shared_ptr<NoteType> moved_note (new NoteType (*to_play.front()));
2634 moved_note->set_note (moved_note->note() + cumulative_dy);
2635 start_playing_midi_note (moved_note);
2641 MidiRegionView::copy_selection (NoteBase* primary)
2643 _copy_drag_events.clear ();
2645 if (_selection.empty()) {
2652 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2653 boost::shared_ptr<NoteType> g (new NoteType (*((*i)->note())));
2654 if (midi_view()->note_mode() == Sustained) {
2655 Note* n = new Note (*this, _note_group, g);
2656 update_sustained (n, false);
2659 Hit* h = new Hit (*this, _note_group, 10, g);
2660 update_hit (h, false);
2664 if ((*i) == primary) {
2668 _copy_drag_events.push_back (note);
2675 MidiRegionView::move_copies (double dx_qn, double dy, double cumulative_dy)
2677 typedef vector<boost::shared_ptr<NoteType> > PossibleChord;
2678 Editor* editor = dynamic_cast<Editor*> (&trackview.editor());
2679 TempoMap& tmap (editor->session()->tempo_map());
2680 PossibleChord to_play;
2681 Temporal::Beats earliest = earliest_in_selection();
2683 for (CopyDragEvents::iterator i = _copy_drag_events.begin(); i != _copy_drag_events.end(); ++i) {
2685 if (n->note()->time() == earliest) {
2686 to_play.push_back (n->note());
2688 double const note_time_qn = session_relative_qn (n->note()->time().to_double());
2690 if (midi_view()->note_mode() == Sustained) {
2691 dx = editor->sample_to_pixel_unrounded (tmap.sample_at_quarter_note (note_time_qn + dx_qn))
2692 - n->item()->item_to_canvas (ArdourCanvas::Duple (n->x0(), 0)).x;
2694 Hit* hit = dynamic_cast<Hit*>(n);
2696 dx = editor->sample_to_pixel_unrounded (tmap.sample_at_quarter_note (note_time_qn + dx_qn))
2697 - n->item()->item_to_canvas (ArdourCanvas::Duple (((hit->x0() + hit->x1()) / 2.0) - hit->position().x, 0)).x;
2701 (*i)->move_event(dx, dy);
2703 if (midi_view()->note_mode() == Sustained) {
2704 Note* sus = dynamic_cast<Note*> (*i);
2705 double const len_dx = editor->sample_to_pixel_unrounded (
2706 tmap.sample_at_quarter_note (note_time_qn + dx_qn + n->note()->length().to_double()));
2708 sus->set_x1 (n->item()->canvas_to_item (ArdourCanvas::Duple (len_dx, 0)).x);
2712 if (dy && !_copy_drag_events.empty() && !_no_sound_notes && UIConfiguration::instance().get_sound_midi_notes()) {
2714 if (to_play.size() > 1) {
2716 PossibleChord shifted;
2718 for (PossibleChord::iterator n = to_play.begin(); n != to_play.end(); ++n) {
2719 boost::shared_ptr<NoteType> moved_note (new NoteType (**n));
2720 moved_note->set_note (moved_note->note() + cumulative_dy);
2721 shifted.push_back (moved_note);
2724 start_playing_midi_chord (shifted);
2726 } else if (!to_play.empty()) {
2728 boost::shared_ptr<NoteType> moved_note (new NoteType (*to_play.front()));
2729 moved_note->set_note (moved_note->note() + cumulative_dy);
2730 start_playing_midi_note (moved_note);
2736 MidiRegionView::note_dropped(NoteBase *, double d_qn, int8_t dnote, bool copy)
2738 uint8_t lowest_note_in_selection = 127;
2739 uint8_t highest_note_in_selection = 0;
2740 uint8_t highest_note_difference = 0;
2743 // find highest and lowest notes first
2745 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2746 uint8_t pitch = (*i)->note()->note();
2747 lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
2748 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
2752 cerr << "dnote: " << (int) dnote << endl;
2753 cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
2754 << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
2755 cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
2756 << int(highest_note_in_selection) << endl;
2757 cerr << "selection size: " << _selection.size() << endl;
2758 cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
2761 // Make sure the note pitch does not exceed the MIDI standard range
2762 if (highest_note_in_selection + dnote > 127) {
2763 highest_note_difference = highest_note_in_selection - 127;
2766 start_note_diff_command (_("move notes"));
2768 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ++i) {
2770 Temporal::Beats new_time = Temporal::Beats ((*i)->note()->time().to_double() + d_qn);
2776 note_diff_add_change (*i, MidiModel::NoteDiffCommand::StartTime, new_time);
2778 uint8_t original_pitch = (*i)->note()->note();
2779 uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
2781 // keep notes in standard midi range
2782 clamp_to_0_127(new_pitch);
2784 lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
2785 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
2787 note_diff_add_change (*i, MidiModel::NoteDiffCommand::NoteNumber, new_pitch);
2791 clear_editor_note_selection ();
2793 for (CopyDragEvents::iterator i = _copy_drag_events.begin(); i != _copy_drag_events.end(); ++i) {
2794 uint8_t pitch = (*i)->note()->note();
2795 lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
2796 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
2799 // Make sure the note pitch does not exceed the MIDI standard range
2800 if (highest_note_in_selection + dnote > 127) {
2801 highest_note_difference = highest_note_in_selection - 127;
2804 start_note_diff_command (_("copy notes"));
2806 for (CopyDragEvents::iterator i = _copy_drag_events.begin(); i != _copy_drag_events.end() ; ++i) {
2809 Temporal::Beats new_time = Temporal::Beats ((*i)->note()->time().to_double() + d_qn);
2815 (*i)->note()->set_time (new_time);
2819 uint8_t original_pitch = (*i)->note()->note();
2820 uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
2822 (*i)->note()->set_note (new_pitch);
2824 // keep notes in standard midi range
2825 clamp_to_0_127(new_pitch);
2827 lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
2828 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
2830 note_diff_add_note ((*i)->note(), true);
2835 _copy_drag_events.clear ();
2838 apply_diff (false, copy);
2840 // care about notes being moved beyond the upper/lower bounds on the canvas
2841 if (lowest_note_in_selection < midi_stream_view()->lowest_note() ||
2842 highest_note_in_selection > midi_stream_view()->highest_note()) {
2843 midi_stream_view()->set_note_range (MidiStreamView::ContentsRange);
2847 /** @param x Pixel relative to the region position.
2848 * @param ensure_snap defaults to false. true = snap always, ignoring snap mode and magnetic snap.
2849 * Used for inverting the snap logic with key modifiers and snap delta calculation.
2850 * @return Snapped sample relative to the region position.
2853 MidiRegionView::snap_pixel_to_sample(double x, bool ensure_snap)
2855 PublicEditor& editor (trackview.editor());
2856 return snap_sample_to_sample (editor.pixel_to_sample (x), ensure_snap).sample;
2859 /** @param x Pixel relative to the region position.
2860 * @param ensure_snap defaults to false. true = ignore magnetic snap and snap mode (used for snap delta calculation).
2861 * @return Snapped pixel relative to the region position.
2864 MidiRegionView::snap_to_pixel(double x, bool ensure_snap)
2866 return (double) trackview.editor().sample_to_pixel(snap_pixel_to_sample(x, ensure_snap));
2870 MidiRegionView::get_position_pixels()
2872 samplepos_t region_sample = get_position();
2873 return trackview.editor().sample_to_pixel(region_sample);
2877 MidiRegionView::get_end_position_pixels()
2879 samplepos_t sample = get_position() + get_duration ();
2880 return trackview.editor().sample_to_pixel(sample);
2884 MidiRegionView::source_beats_to_absolute_samples(Temporal::Beats beats) const
2886 /* the time converter will return the sample corresponding to `beats'
2887 relative to the start of the source. The start of the source
2888 is an implied position given by region->position - region->start
2890 const samplepos_t source_start = _region->position() - _region->start();
2891 return source_start + _source_relative_time_converter.to (beats);
2895 MidiRegionView::absolute_samples_to_source_beats(samplepos_t samples) const
2897 /* the `samples' argument needs to be converted into a sample count
2898 relative to the start of the source before being passed in to the
2901 const samplepos_t source_start = _region->position() - _region->start();
2902 return _source_relative_time_converter.from (samples - source_start);
2906 MidiRegionView::region_beats_to_region_samples(Temporal::Beats beats) const
2908 return _region_relative_time_converter.to(beats);
2912 MidiRegionView::region_samples_to_region_beats(samplepos_t samples) const
2914 return _region_relative_time_converter.from(samples);
2918 MidiRegionView::region_samples_to_region_beats_double (samplepos_t samples) const
2920 return _region_relative_time_converter_double.from(samples);
2924 MidiRegionView::begin_resizing (bool /*at_front*/)
2926 _resize_data.clear();
2928 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2929 Note *note = dynamic_cast<Note*> (*i);
2931 // only insert CanvasNotes into the map
2933 NoteResizeData *resize_data = new NoteResizeData();
2934 resize_data->note = note;
2936 // create a new SimpleRect from the note which will be the resize preview
2937 ArdourCanvas::Rectangle *resize_rect = new ArdourCanvas::Rectangle (_note_group,
2938 ArdourCanvas::Rect (note->x0(), note->y0(), note->x0(), note->y1()));
2940 // calculate the colors: get the color settings
2941 uint32_t fill_color = NoteBase::meter_style_fill_color (note->note()->velocity(), true);
2943 // make the resize preview notes more transparent and bright
2944 fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
2946 // calculate color based on note velocity
2947 resize_rect->set_fill_color (UINT_INTERPOLATE(
2948 NoteBase::meter_style_fill_color(note->note()->velocity(), note->selected()),
2952 resize_rect->set_outline_color (NoteBase::calculate_outline (
2953 UIConfiguration::instance().color ("midi note selected outline")));
2955 resize_data->resize_rect = resize_rect;
2956 _resize_data.push_back(resize_data);
2961 /** Update resizing notes while user drags.
2962 * @param primary `primary' note for the drag; ie the one that is used as the reference in non-relative mode.
2963 * @param at_front which end of the note (true == note on, false == note off)
2964 * @param delta_x change in mouse position since the start of the drag
2965 * @param relative true if relative resizing is taking place, false if absolute resizing. This only makes
2966 * a difference when multiple notes are being resized; in relative mode, each note's length is changed by the
2967 * amount of the drag. In non-relative mode, all selected notes are set to have the same start or end point
2968 * as the \a primary note.
2969 * @param snap_delta snap offset of the primary note in pixels. used in SnapRelative SnapDelta mode.
2970 * @param with_snap true if snap is to be used to determine the position, false if no snap is to be used.
2973 MidiRegionView::update_resizing (NoteBase* primary, bool at_front, double delta_x, bool relative, double snap_delta, bool with_snap)
2975 TempoMap& tmap (trackview.session()->tempo_map());
2976 bool cursor_set = false;
2977 bool const ensure_snap = trackview.editor().snap_mode () != SnapMagnetic;
2979 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
2980 ArdourCanvas::Rectangle* resize_rect = (*i)->resize_rect;
2981 Note* canvas_note = (*i)->note;
2986 current_x = canvas_note->x0() + delta_x + snap_delta;
2988 current_x = primary->x0() + delta_x + snap_delta;
2992 current_x = canvas_note->x1() + delta_x + snap_delta;
2994 current_x = primary->x1() + delta_x + snap_delta;
2998 if (current_x < 0) {
2999 /* This works even with snapping because RegionView::snap_sample_to_sample()
3000 * snaps forward if the snapped sample is before the beginning of the region
3004 if (current_x > trackview.editor().sample_to_pixel(_region->length())) {
3005 current_x = trackview.editor().sample_to_pixel(_region->length());
3010 resize_rect->set_x0 (snap_to_pixel (current_x, ensure_snap) - snap_delta);
3012 resize_rect->set_x0 (current_x - snap_delta);
3014 resize_rect->set_x1 (canvas_note->x1());
3017 resize_rect->set_x1 (snap_to_pixel (current_x, ensure_snap) - snap_delta);
3019 resize_rect->set_x1 (current_x - snap_delta);
3021 resize_rect->set_x0 (canvas_note->x0());
3025 /* Convert snap delta from pixels to beats. */
3026 samplepos_t snap_delta_samps = trackview.editor().pixel_to_sample (snap_delta);
3027 double snap_delta_beats = 0.0;
3030 /* negative beat offsets aren't allowed */
3031 if (snap_delta_samps > 0) {
3032 snap_delta_beats = region_samples_to_region_beats_double (snap_delta_samps);
3033 } else if (snap_delta_samps < 0) {
3034 snap_delta_beats = region_samples_to_region_beats_double (- snap_delta_samps);
3039 int32_t divisions = 0;
3042 snapped_x = snap_pixel_to_sample (current_x, ensure_snap);
3043 divisions = trackview.editor().get_grid_music_divisions (0);
3045 snapped_x = trackview.editor ().pixel_to_sample (current_x);
3048 const Temporal::Beats beats = Temporal::Beats (tmap.exact_beat_at_sample (snapped_x + midi_region()->position(), divisions)
3049 - midi_region()->beat())
3050 + midi_region()->start_beats();
3052 Temporal::Beats len = Temporal::Beats();
3055 if (beats < canvas_note->note()->end_time()) {
3056 len = canvas_note->note()->time() - beats + (sign * snap_delta_beats);
3057 len += canvas_note->note()->length();
3060 if (beats >= canvas_note->note()->time()) {
3061 len = beats - canvas_note->note()->time() - (sign * snap_delta_beats);
3065 len = std::max(Temporal::Beats(1 / 512.0), len);
3068 snprintf (buf, sizeof (buf), "%.3g beats", len.to_double());
3069 show_verbose_cursor (buf, 0, 0);
3073 trackview.editor().set_snapped_cursor_position ( snapped_x + midi_region()->position() );
3080 /** Finish resizing notes when the user releases the mouse button.
3081 * Parameters the same as for \a update_resizing().
3084 MidiRegionView::commit_resizing (NoteBase* primary, bool at_front, double delta_x, bool relative, double snap_delta, bool with_snap)
3086 _note_diff_command = _model->new_note_diff_command (_("resize notes"));
3087 TempoMap& tmap (trackview.session()->tempo_map());
3089 /* XX why doesn't snap_pixel_to_sample() handle this properly? */
3090 bool const ensure_snap = trackview.editor().snap_mode () != SnapMagnetic;
3092 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
3093 Note* canvas_note = (*i)->note;
3094 ArdourCanvas::Rectangle* resize_rect = (*i)->resize_rect;
3096 /* Get the new x position for this resize, which is in pixels relative
3097 * to the region position.
3104 current_x = canvas_note->x0() + delta_x + snap_delta;
3106 current_x = primary->x0() + delta_x + snap_delta;
3110 current_x = canvas_note->x1() + delta_x + snap_delta;
3112 current_x = primary->x1() + delta_x + snap_delta;
3116 if (current_x < 0) {
3119 if (current_x > trackview.editor().sample_to_pixel(_region->length())) {
3120 current_x = trackview.editor().sample_to_pixel(_region->length());
3123 /* Convert snap delta from pixels to beats with sign. */
3124 samplepos_t snap_delta_samps = trackview.editor().pixel_to_sample (snap_delta);
3125 double snap_delta_beats = 0.0;
3128 if (snap_delta_samps > 0) {
3129 snap_delta_beats = region_samples_to_region_beats_double (snap_delta_samps);
3130 } else if (snap_delta_samps < 0) {
3131 snap_delta_beats = region_samples_to_region_beats_double ( - snap_delta_samps);
3135 uint32_t divisions = 0;
3136 /* Convert the new x position to a sample within the source */
3137 samplepos_t current_fr;
3139 current_fr = snap_pixel_to_sample (current_x, ensure_snap);
3140 divisions = trackview.editor().get_grid_music_divisions (0);
3142 current_fr = trackview.editor().pixel_to_sample (current_x);
3145 /* and then to beats */
3146 const double e_qaf = tmap.exact_qn_at_sample (current_fr + midi_region()->position(), divisions);
3147 const double quarter_note_start = _region->quarter_note() - midi_region()->start_beats();
3148 const Temporal::Beats x_beats = Temporal::Beats (e_qaf - quarter_note_start);
3150 if (at_front && x_beats < canvas_note->note()->end_time()) {
3151 note_diff_add_change (canvas_note, MidiModel::NoteDiffCommand::StartTime, x_beats - (sign * snap_delta_beats));
3152 Temporal::Beats len = canvas_note->note()->time() - x_beats + (sign * snap_delta_beats);
3153 len += canvas_note->note()->length();
3156 note_diff_add_change (canvas_note, MidiModel::NoteDiffCommand::Length, len);
3161 Temporal::Beats len = std::max(Temporal::Beats(1 / 512.0),
3162 x_beats - canvas_note->note()->time() - (sign * snap_delta_beats));
3163 note_diff_add_change (canvas_note, MidiModel::NoteDiffCommand::Length, len);
3170 _resize_data.clear();
3175 MidiRegionView::abort_resizing ()
3177 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
3178 delete (*i)->resize_rect;
3182 _resize_data.clear ();
3186 MidiRegionView::change_note_velocity(NoteBase* event, int8_t velocity, bool relative)
3188 uint8_t new_velocity;
3191 new_velocity = event->note()->velocity() + velocity;
3192 clamp_to_0_127(new_velocity);
3194 new_velocity = velocity;
3197 event->set_selected (event->selected()); // change color
3199 note_diff_add_change (event, MidiModel::NoteDiffCommand::Velocity, new_velocity);
3203 MidiRegionView::change_note_note (NoteBase* event, int8_t note, bool relative)
3208 new_note = event->note()->note() + note;
3213 clamp_to_0_127 (new_note);
3214 note_diff_add_change (event, MidiModel::NoteDiffCommand::NoteNumber, new_note);
3218 MidiRegionView::trim_note (NoteBase* event, Temporal::Beats front_delta, Temporal::Beats end_delta)
3220 bool change_start = false;
3221 bool change_length = false;
3222 Temporal::Beats new_start;
3223 Temporal::Beats new_length;
3225 /* NOTE: the semantics of the two delta arguments are slightly subtle:
3227 front_delta: if positive - move the start of the note later in time (shortening it)
3228 if negative - move the start of the note earlier in time (lengthening it)
3230 end_delta: if positive - move the end of the note later in time (lengthening it)
3231 if negative - move the end of the note earlier in time (shortening it)
3234 if (!!front_delta) {
3235 if (front_delta < 0) {
3237 if (event->note()->time() < -front_delta) {
3238 new_start = Temporal::Beats();
3240 new_start = event->note()->time() + front_delta; // moves earlier
3243 /* start moved toward zero, so move the end point out to where it used to be.
3244 Note that front_delta is negative, so this increases the length.
3247 new_length = event->note()->length() - front_delta;
3248 change_start = true;
3249 change_length = true;
3253 Temporal::Beats new_pos = event->note()->time() + front_delta;
3255 if (new_pos < event->note()->end_time()) {
3256 new_start = event->note()->time() + front_delta;
3257 /* start moved toward the end, so move the end point back to where it used to be */
3258 new_length = event->note()->length() - front_delta;
3259 change_start = true;
3260 change_length = true;
3267 bool can_change = true;
3268 if (end_delta < 0) {
3269 if (event->note()->length() < -end_delta) {
3275 new_length = event->note()->length() + end_delta;
3276 change_length = true;
3281 note_diff_add_change (event, MidiModel::NoteDiffCommand::StartTime, new_start);
3284 if (change_length) {
3285 note_diff_add_change (event, MidiModel::NoteDiffCommand::Length, new_length);
3290 MidiRegionView::change_note_channel (NoteBase* event, int8_t chn, bool relative)
3292 uint8_t new_channel;
3296 if (event->note()->channel() < -chn) {
3299 new_channel = event->note()->channel() + chn;
3302 new_channel = event->note()->channel() + chn;
3305 new_channel = (uint8_t) chn;
3308 note_diff_add_change (event, MidiModel::NoteDiffCommand::Channel, new_channel);
3312 MidiRegionView::change_note_time (NoteBase* event, Temporal::Beats delta, bool relative)
3314 Temporal::Beats new_time;
3318 if (event->note()->time() < -delta) {
3319 new_time = Temporal::Beats();
3321 new_time = event->note()->time() + delta;
3324 new_time = event->note()->time() + delta;
3330 note_diff_add_change (event, MidiModel::NoteDiffCommand::StartTime, new_time);
3334 MidiRegionView::change_note_length (NoteBase* event, Temporal::Beats t)
3336 note_diff_add_change (event, MidiModel::NoteDiffCommand::Length, t);
3340 MidiRegionView::change_velocities (bool up, bool fine, bool allow_smush, bool all_together)
3345 if (_selection.empty()) {
3360 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
3361 if ((*i)->note()->velocity() < -delta || (*i)->note()->velocity() + delta > 127) {
3367 start_note_diff_command (_("change velocities"));
3369 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
3370 Selection::iterator next = i;
3374 if (i == _selection.begin()) {
3375 change_note_velocity (*i, delta, true);
3376 value = (*i)->note()->velocity() + delta;
3378 change_note_velocity (*i, value, false);
3382 change_note_velocity (*i, delta, true);
3391 if (!_selection.empty()) {
3393 snprintf (buf, sizeof (buf), "Vel %d",
3394 (int) (*_selection.begin())->note()->velocity());
3395 show_verbose_cursor (buf, 10, 10);
3401 MidiRegionView::transpose (bool up, bool fine, bool allow_smush)
3403 if (_selection.empty()) {
3420 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
3422 if ((int8_t) (*i)->note()->note() + delta <= 0) {
3426 if ((int8_t) (*i)->note()->note() + delta > 127) {
3433 start_note_diff_command (_("transpose"));
3435 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
3436 Selection::iterator next = i;
3438 change_note_note (*i, delta, true);
3446 MidiRegionView::change_note_lengths (bool fine, bool shorter, Temporal::Beats delta, bool start, bool end)
3450 delta = Temporal::Beats(1.0/128.0);
3452 /* grab the current grid distance */
3453 delta = get_grid_beats(_region->position());
3461 start_note_diff_command (_("change note lengths"));
3463 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
3464 Selection::iterator next = i;
3467 /* note the negation of the delta for start */
3470 (start ? -delta : Temporal::Beats()),
3471 (end ? delta : Temporal::Beats()));
3480 MidiRegionView::nudge_notes (bool forward, bool fine)
3482 if (_selection.empty()) {
3486 /* pick a note as the point along the timeline to get the nudge distance.
3487 its not necessarily the earliest note, so we may want to pull the notes out
3488 into a vector and sort before using the first one.
3491 const samplepos_t ref_point = source_beats_to_absolute_samples ((*(_selection.begin()))->note()->time());
3492 Temporal::Beats delta;
3496 /* non-fine, move by 1 bar regardless of snap */
3497 delta = Temporal::Beats(trackview.session()->tempo_map().meter_at_sample (ref_point).divisions_per_bar());
3499 } else if (trackview.editor().snap_mode() == Editing::SnapOff) {
3501 /* grid is off - use nudge distance */
3504 const samplecnt_t distance = trackview.editor().get_nudge_distance (ref_point, unused);
3505 delta = region_samples_to_region_beats (fabs ((double)distance));
3511 MusicSample next_pos (ref_point, 0);
3513 if (max_samplepos - 1 < next_pos.sample) {
3514 next_pos.sample += 1;
3517 if (next_pos.sample == 0) {
3520 next_pos.sample -= 1;
3523 trackview.editor().snap_to (next_pos, (forward ? RoundUpAlways : RoundDownAlways), SnapToGrid_Unscaled, false);
3524 const samplecnt_t distance = ref_point - next_pos.sample;
3525 delta = region_samples_to_region_beats (fabs ((double)distance));
3536 start_note_diff_command (_("nudge"));
3538 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
3539 Selection::iterator next = i;
3541 change_note_time (*i, delta, true);
3549 MidiRegionView::change_channel(uint8_t channel)
3551 start_note_diff_command(_("change channel"));
3552 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
3553 note_diff_add_change (*i, MidiModel::NoteDiffCommand::Channel, channel);
3561 MidiRegionView::note_entered(NoteBase* ev)
3565 Editor* editor = dynamic_cast<Editor*>(&trackview.editor());
3567 if (_mouse_state == SelectTouchDragging) {
3569 note_selected (ev, true);
3571 } else if (editor->current_mouse_mode() == MouseContent) {
3573 remove_ghost_note ();
3574 show_verbose_cursor (ev->note ());
3576 } else if (editor->current_mouse_mode() == MouseDraw) {
3578 remove_ghost_note ();
3579 show_verbose_cursor (ev->note ());
3584 MidiRegionView::note_left (NoteBase*)
3588 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
3589 (*i)->hide_velocity ();
3592 hide_verbose_cursor ();
3596 MidiRegionView::patch_entered (PatchChange* p)
3599 s << _("Bank ") << (p->patch()->bank() + MIDI_BP_ZERO) << '\n'
3600 << instrument_info().get_patch_name_without (p->patch()->bank(), p->patch()->program(), p->patch()->channel()) << '\n'
3601 << _("Channel ") << ((int) p->patch()->channel() + 1);
3602 show_verbose_cursor (s.str(), 10, 20);
3603 p->item().grab_focus();
3607 MidiRegionView::patch_left (PatchChange *)
3609 hide_verbose_cursor ();
3610 /* focus will transfer back via the enter-notify event sent to this
3616 MidiRegionView::sysex_entered (SysEx* p)
3620 // need a way to extract text from p->_flag->_text
3622 // show_verbose_cursor (s.str(), 10, 20);
3623 p->item().grab_focus();
3627 MidiRegionView::sysex_left (SysEx *)
3629 hide_verbose_cursor ();
3630 /* focus will transfer back via the enter-notify event sent to this
3636 MidiRegionView::note_mouse_position (float x_fraction, float /*y_fraction*/, bool can_set_cursor)
3638 Editor* editor = dynamic_cast<Editor*>(&trackview.editor());
3639 Editing::MouseMode mm = editor->current_mouse_mode();
3640 bool trimmable = (mm == MouseContent || mm == MouseTimeFX || mm == MouseDraw);
3642 Editor::EnterContext* ctx = editor->get_enter_context(NoteItem);
3643 if (can_set_cursor && ctx) {
3644 if (trimmable && x_fraction > 0.0 && x_fraction < 0.2) {
3645 ctx->cursor_ctx->change(editor->cursors()->left_side_trim);
3646 } else if (trimmable && x_fraction >= 0.8 && x_fraction < 1.0) {
3647 ctx->cursor_ctx->change(editor->cursors()->right_side_trim);
3649 ctx->cursor_ctx->change(editor->cursors()->grabber_note);
3655 MidiRegionView::get_fill_color() const
3657 const std::string mod_name = (_dragging ? "dragging region" :
3658 trackview.editor().internal_editing() ? "editable region" :
3661 return UIConfiguration::instance().color_mod ("selected region base", mod_name);
3662 } else if ((!UIConfiguration::instance().get_show_name_highlight() || high_enough_for_name) &&
3663 !UIConfiguration::instance().get_color_regions_using_track_color()) {
3664 return UIConfiguration::instance().color_mod ("midi frame base", mod_name);
3666 return UIConfiguration::instance().color_mod (fill_color, mod_name);
3670 MidiRegionView::midi_channel_mode_changed ()
3672 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
3673 uint16_t mask = mtv->midi_track()->get_playback_channel_mask();
3674 ChannelMode mode = mtv->midi_track()->get_playback_channel_mode ();
3676 if (mode == ForceChannel) {
3677 mask = 0xFFFF; // Show all notes as active (below)
3680 // Update notes for selection
3681 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
3682 i->second->on_channel_selection_change (mask);
3685 _patch_changes.clear ();
3686 display_patch_changes ();
3690 MidiRegionView::instrument_settings_changed ()
3696 MidiRegionView::cut_copy_clear (Editing::CutCopyOp op)
3698 if (_selection.empty()) {
3702 PublicEditor& editor (trackview.editor());
3706 /* XXX what to do ? */
3710 editor.get_cut_buffer().add (selection_as_cut_buffer());
3718 start_note_diff_command();
3720 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
3727 note_diff_remove_note (*i);
3737 MidiRegionView::selection_as_cut_buffer () const
3741 for (Selection::const_iterator i = _selection.begin(); i != _selection.end(); ++i) {
3742 NoteType* n = (*i)->note().get();
3743 notes.insert (boost::shared_ptr<NoteType> (new NoteType (*n)));
3746 MidiCutBuffer* cb = new MidiCutBuffer (trackview.session());
3752 /** This method handles undo */
3754 MidiRegionView::paste (samplepos_t pos, const ::Selection& selection, PasteContext& ctx, const int32_t sub_num)
3756 bool commit = false;
3757 // Paste notes, if available
3758 MidiNoteSelection::const_iterator m = selection.midi_notes.get_nth(ctx.counts.n_notes());
3759 if (m != selection.midi_notes.end()) {
3760 ctx.counts.increase_n_notes();
3761 if (!(*m)->empty()) {
3764 paste_internal(pos, ctx.count, ctx.times, **m);
3767 // Paste control points to automation children, if available
3768 typedef RouteTimeAxisView::AutomationTracks ATracks;
3769 const ATracks& atracks = midi_view()->automation_tracks();
3770 for (ATracks::const_iterator a = atracks.begin(); a != atracks.end(); ++a) {
3771 if (a->second->paste(pos, selection, ctx, sub_num)) {
3773 trackview.editor().begin_reversible_command (Operations::paste);
3780 trackview.editor().commit_reversible_command ();
3785 /** This method handles undo */
3787 MidiRegionView::paste_internal (samplepos_t pos, unsigned paste_count, float times, const MidiCutBuffer& mcb)
3793 start_note_diff_command (_("paste"));
3795 const Temporal::Beats snap_beats = get_grid_beats(pos);
3796 const Temporal::Beats first_time = (*mcb.notes().begin())->time();
3797 const Temporal::Beats last_time = (*mcb.notes().rbegin())->end_time();
3798 const Temporal::Beats duration = last_time - first_time;
3799 const Temporal::Beats snap_duration = duration.snap_to(snap_beats);
3800 const Temporal::Beats paste_offset = snap_duration * paste_count;
3801 const Temporal::Beats quarter_note = absolute_samples_to_source_beats(pos) + paste_offset;
3802 Temporal::Beats end_point = Temporal::Beats();
3804 DEBUG_TRACE (DEBUG::CutNPaste, string_compose ("Paste data spans from %1 to %2 (%3) ; paste pos beats = %4 (based on %5 - %6)\n",
3807 duration, pos, _region->position(),
3810 clear_editor_note_selection ();
3812 for (int n = 0; n < (int) times; ++n) {
3814 for (Notes::const_iterator i = mcb.notes().begin(); i != mcb.notes().end(); ++i) {
3816 boost::shared_ptr<NoteType> copied_note (new NoteType (*((*i).get())));
3817 copied_note->set_time (quarter_note + copied_note->time() - first_time);
3818 copied_note->set_id (Evoral::next_event_id());
3820 /* make all newly added notes selected */
3822 note_diff_add_note (copied_note, true);
3823 end_point = copied_note->end_time();
3827 /* if we pasted past the current end of the region, extend the region */
3829 samplepos_t end_sample = source_beats_to_absolute_samples (end_point);
3830 samplepos_t region_end = _region->position() + _region->length() - 1;
3832 if (end_sample > region_end) {
3834 DEBUG_TRACE (DEBUG::CutNPaste, string_compose ("Paste extended region from %1 to %2\n", region_end, end_sample));
3836 _region->clear_changes ();
3837 /* we probably need to get the snap modifier somehow to make this correct for non-musical use */
3838 _region->set_length (end_sample - _region->position(), trackview.editor().get_grid_music_divisions (0));
3839 trackview.session()->add_command (new StatefulDiffCommand (_region));
3845 struct EventNoteTimeEarlyFirstComparator {
3846 bool operator() (NoteBase* a, NoteBase* b) {
3847 return a->note()->time() < b->note()->time();
3852 MidiRegionView::goto_next_note (bool add_to_selection)
3854 bool use_next = false;
3856 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
3857 uint16_t const channel_mask = mtv->midi_track()->get_playback_channel_mask();
3858 NoteBase* first_note = 0;
3860 MidiModel::ReadLock lock(_model->read_lock());
3861 MidiModel::Notes& notes (_model->notes());
3863 for (MidiModel::Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
3865 if ((cne = find_canvas_note (*n))) {
3867 if (!first_note && (channel_mask & (1 << (*n)->channel()))) {
3871 if (cne->selected()) {
3874 } else if (use_next) {
3875 if (channel_mask & (1 << (*n)->channel())) {
3876 if (!add_to_selection) {
3877 unique_select (cne);
3879 note_selected (cne, true, false);
3888 /* use the first one */
3890 if (!_events.empty() && first_note) {
3891 unique_select (first_note);
3896 MidiRegionView::goto_previous_note (bool add_to_selection)
3898 bool use_next = false;
3900 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
3901 uint16_t const channel_mask = mtv->midi_track()->get_playback_channel_mask ();
3902 NoteBase* last_note = 0;
3904 MidiModel::ReadLock lock(_model->read_lock());
3905 MidiModel::Notes& notes (_model->notes());
3907 for (MidiModel::Notes::reverse_iterator n = notes.rbegin(); n != notes.rend(); ++n) {
3909 if ((cne = find_canvas_note (*n))) {
3911 if (!last_note && (channel_mask & (1 << (*n)->channel()))) {
3915 if (cne->selected()) {
3919 } else if (use_next) {
3920 if (channel_mask & (1 << (*n)->channel())) {
3921 if (!add_to_selection) {
3922 unique_select (cne);
3924 note_selected (cne, true, false);
3933 /* use the last one */
3935 if (!_events.empty() && last_note) {
3936 unique_select (last_note);
3941 MidiRegionView::selection_as_notelist (Notes& selected, bool allow_all_if_none_selected)
3943 bool had_selected = false;
3945 /* we previously time sorted events here, but Notes is a multiset sorted by time */
3947 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
3948 if (i->second->selected()) {
3949 selected.insert (i->first);
3950 had_selected = true;
3954 if (allow_all_if_none_selected && !had_selected) {
3955 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
3956 selected.insert (i->first);
3962 MidiRegionView::update_ghost_note (double x, double y, uint32_t state)
3964 x = std::max(0.0, x);
3966 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
3971 _note_group->canvas_to_item (x, y);
3973 PublicEditor& editor = trackview.editor ();
3975 samplepos_t const unsnapped_sample = editor.pixel_to_sample (x);
3977 const int32_t divisions = editor.get_grid_music_divisions (state);
3978 const bool shift_snap = midi_view()->note_mode() != Percussive;
3979 const Temporal::Beats snapped_beats = snap_sample_to_grid_underneath (unsnapped_sample, divisions, shift_snap);
3981 /* prevent Percussive mode from displaying a ghost hit at region end */
3982 if (!shift_snap && snapped_beats >= midi_region()->start_beats() + midi_region()->length_beats()) {
3983 _ghost_note->hide();
3984 hide_verbose_cursor ();
3988 /* ghost note may have been snapped before region */
3989 if (_ghost_note && snapped_beats.to_double() < 0.0) {
3990 _ghost_note->hide();
3993 } else if (_ghost_note) {
3994 _ghost_note->show();
3997 /* calculate time in beats relative to start of source */
3998 const Temporal::Beats length = get_grid_beats(unsnapped_sample + _region->position());
4000 _ghost_note->note()->set_time (snapped_beats);
4001 _ghost_note->note()->set_length (length);
4002 _ghost_note->note()->set_note (y_to_note (y));
4003 _ghost_note->note()->set_channel (mtv->get_channel_for_add ());
4004 _ghost_note->note()->set_velocity (get_velocity_for_add (snapped_beats));
4005 /* the ghost note does not appear in ghost regions, so pass false in here */
4006 update_note (_ghost_note, false);
4008 show_verbose_cursor (_ghost_note->note ());
4012 MidiRegionView::create_ghost_note (double x, double y, uint32_t state)
4014 remove_ghost_note ();
4016 boost::shared_ptr<NoteType> g (new NoteType);
4017 if (midi_view()->note_mode() == Sustained) {
4018 _ghost_note = new Note (*this, _note_group, g);
4020 _ghost_note = new Hit (*this, _note_group, 10, g);
4022 _ghost_note->set_ignore_events (true);
4023 _ghost_note->set_outline_color (0x000000aa);
4024 update_ghost_note (x, y, state);
4025 _ghost_note->show ();
4027 show_verbose_cursor (_ghost_note->note ());
4031 MidiRegionView::remove_ghost_note ()
4038 MidiRegionView::hide_verbose_cursor ()
4040 trackview.editor().verbose_cursor()->hide ();
4041 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
4043 mtv->set_note_highlight (NO_MIDI_NOTE);
4048 MidiRegionView::snap_changed ()
4054 create_ghost_note (_last_ghost_x, _last_ghost_y, 0);
4058 MidiRegionView::drop_down_keys ()
4060 _mouse_state = None;
4064 MidiRegionView::maybe_select_by_position (GdkEventButton* ev, double /*x*/, double y)
4066 /* XXX: This is dead code. What was it for? */
4068 double note = y_to_note(y);
4070 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
4072 uint16_t chn_mask = mtv->midi_track()->get_playback_channel_mask();
4074 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
4075 get_events (e, Evoral::Sequence<Temporal::Beats>::PitchGreaterThanOrEqual, (uint8_t) floor (note), chn_mask);
4076 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
4077 get_events (e, Evoral::Sequence<Temporal::Beats>::PitchLessThanOrEqual, (uint8_t) floor (note), chn_mask);
4082 bool add_mrv_selection = false;
4084 if (_selection.empty()) {
4085 add_mrv_selection = true;
4088 for (Events::iterator i = e.begin(); i != e.end(); ++i) {
4089 if (_selection.insert (i->second).second) {
4090 i->second->set_selected (true);
4094 if (add_mrv_selection) {
4095 PublicEditor& editor (trackview.editor());
4096 editor.get_selection().add (this);
4101 MidiRegionView::color_handler ()
4103 RegionView::color_handler ();
4105 _patch_change_outline = UIConfiguration::instance().color ("midi patch change outline");
4106 _patch_change_fill = UIConfiguration::instance().color_mod ("midi patch change fill", "midi patch change fill");
4108 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
4109 i->second->set_selected (i->second->selected()); // will change color
4112 /* XXX probably more to do here */
4116 MidiRegionView::enable_display (bool yn)
4118 RegionView::enable_display (yn);
4122 MidiRegionView::show_step_edit_cursor (Temporal::Beats pos)
4124 if (_step_edit_cursor == 0) {
4125 ArdourCanvas::Item* const group = get_canvas_group();
4127 _step_edit_cursor = new ArdourCanvas::Rectangle (group);
4128 _step_edit_cursor->set_y0 (0);
4129 _step_edit_cursor->set_y1 (midi_stream_view()->contents_height());
4130 _step_edit_cursor->set_fill_color (RGBA_TO_UINT (45,0,0,90));
4131 _step_edit_cursor->set_outline_color (RGBA_TO_UINT (85,0,0,90));
4134 move_step_edit_cursor (pos);
4135 _step_edit_cursor->show ();
4139 MidiRegionView::move_step_edit_cursor (Temporal::Beats pos)
4141 _step_edit_cursor_position = pos;
4143 if (_step_edit_cursor) {
4144 double pixel = trackview.editor().sample_to_pixel (region_beats_to_region_samples (pos));
4145 _step_edit_cursor->set_x0 (pixel);
4146 set_step_edit_cursor_width (_step_edit_cursor_width);
4151 MidiRegionView::hide_step_edit_cursor ()
4153 if (_step_edit_cursor) {
4154 _step_edit_cursor->hide ();
4159 MidiRegionView::set_step_edit_cursor_width (Temporal::Beats beats)
4161 _step_edit_cursor_width = beats;
4163 if (_step_edit_cursor) {
4164 _step_edit_cursor->set_x1 (_step_edit_cursor->x0()
4165 + trackview.editor().sample_to_pixel (
4166 region_beats_to_region_samples (_step_edit_cursor_position + beats)
4167 - region_beats_to_region_samples (_step_edit_cursor_position)));
4171 /** Called when a diskstream on our track has received some data. Update the view, if applicable.
4172 * @param w Source that the data will end up in.
4175 MidiRegionView::data_recorded (boost::weak_ptr<MidiSource> w)
4177 if (!_active_notes) {
4178 /* we aren't actively being recorded to */
4182 boost::shared_ptr<MidiSource> src = w.lock ();
4183 if (!src || src != midi_region()->midi_source()) {
4184 /* recorded data was not destined for our source */
4188 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*> (&trackview);
4190 boost::shared_ptr<MidiBuffer> buf = mtv->midi_track()->get_gui_feed_buffer ();
4192 samplepos_t back = max_samplepos;
4194 for (MidiBuffer::iterator i = buf->begin(); i != buf->end(); ++i) {
4195 const Evoral::Event<MidiBuffer::TimeType>& ev = *i;
4197 if (ev.is_channel_event()) {
4198 if (get_channel_mode() == FilterChannels) {
4199 if (((uint16_t(1) << ev.channel()) & get_selected_channels()) == 0) {
4205 /* convert from session samples to source beats */
4206 Temporal::Beats const time_beats = _source_relative_time_converter.from(
4207 ev.time() - src->timeline_position() + _region->start());
4209 if (ev.type() == MIDI_CMD_NOTE_ON) {
4210 boost::shared_ptr<NoteType> note (
4211 new NoteType (ev.channel(), time_beats, Temporal::Beats(), ev.note(), ev.velocity()));
4213 add_note (note, true);
4215 /* fix up our note range */
4216 if (ev.note() < _current_range_min) {
4217 midi_stream_view()->apply_note_range (ev.note(), _current_range_max, true);
4218 } else if (ev.note() > _current_range_max) {
4219 midi_stream_view()->apply_note_range (_current_range_min, ev.note(), true);
4222 } else if (ev.type() == MIDI_CMD_NOTE_OFF) {
4223 resolve_note (ev.note (), time_beats);
4229 midi_stream_view()->check_record_layers (region(), back);
4233 MidiRegionView::trim_front_starting ()
4235 /* We used to eparent the note group to the region view's parent, so that it didn't change.
4241 MidiRegionView::trim_front_ending ()
4243 if (_region->start() < 0) {
4244 /* Trim drag made start time -ve; fix this */
4245 midi_region()->fix_negative_start ();
4250 MidiRegionView::edit_patch_change (PatchChange* pc)
4252 PatchChangeDialog d (&_source_relative_time_converter, trackview.session(), *pc->patch (), instrument_info(), Gtk::Stock::APPLY, true);
4254 int response = d.run();
4257 case Gtk::RESPONSE_ACCEPT:
4259 case Gtk::RESPONSE_REJECT:
4260 delete_patch_change (pc);
4266 change_patch_change (pc->patch(), d.patch ());
4270 MidiRegionView::delete_sysex (SysEx* /*sysex*/)
4273 // sysyex object doesn't have a pointer to a sysex event
4274 // MidiModel::SysExDiffCommand* c = _model->new_sysex_diff_command (_("delete sysex"));
4275 // c->remove (sysex->sysex());
4276 // _model->apply_command (*trackview.session(), c);
4278 //_sys_exes.clear ();
4279 // display_sysexes();
4283 MidiRegionView::get_note_name (boost::shared_ptr<NoteType> n, uint8_t note_value) const
4285 using namespace MIDI::Name;
4288 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
4290 boost::shared_ptr<MasterDeviceNames> device_names(mtv->get_device_names());
4292 MIDI::Name::PatchPrimaryKey patch_key;
4293 get_patch_key_at(n->time(), n->channel(), patch_key);
4294 name = device_names->note_name(mtv->gui_property(X_("midnam-custom-device-mode")),
4297 patch_key.program(),
4303 snprintf (buf, sizeof (buf), "%d %s\nCh %d Vel %d",
4305 name.empty() ? ParameterDescriptor::midi_note_name (note_value).c_str() : name.c_str(),
4306 (int) n->channel() + 1,
4307 (int) n->velocity());
4313 MidiRegionView::show_verbose_cursor_for_new_note_value(boost::shared_ptr<NoteType> current_note,
4314 uint8_t new_value) const
4316 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
4318 mtv->set_note_highlight (new_value);
4321 show_verbose_cursor(get_note_name(current_note, new_value), 10, 20);
4325 MidiRegionView::show_verbose_cursor (boost::shared_ptr<NoteType> n) const
4327 show_verbose_cursor_for_new_note_value(n, n->note());
4331 MidiRegionView::show_verbose_cursor (string const & text, double xoffset, double yoffset) const
4333 trackview.editor().verbose_cursor()->set (text);
4334 trackview.editor().verbose_cursor()->show ();
4335 trackview.editor().verbose_cursor()->set_offset (ArdourCanvas::Duple (xoffset, yoffset));
4339 MidiRegionView::get_velocity_for_add (MidiModel::TimeType time) const
4341 if (_model->notes().empty()) {
4342 return 0x40; // No notes, use default
4345 MidiModel::Notes::const_iterator m = _model->note_lower_bound(time);
4346 if (m == _model->notes().begin()) {
4347 // Before the start, use the velocity of the first note
4348 return (*m)->velocity();
4349 } else if (m == _model->notes().end()) {
4350 // Past the end, use the velocity of the last note
4352 return (*m)->velocity();
4355 // Interpolate velocity of surrounding notes
4356 MidiModel::Notes::const_iterator n = m;
4359 const double frac = ((time - (*n)->time()).to_double() /
4360 ((*m)->time() - (*n)->time()).to_double());
4362 return (*n)->velocity() + (frac * ((*m)->velocity() - (*n)->velocity()));
4365 /** @param p A session samplepos.
4366 * @param divisions beat division to snap given by Editor::get_grid_music_divisions() where
4367 * bar is -1, 0 is audio samples and a positive integer is beat subdivisions.
4368 * @return beat duration of p snapped to the grid subdivision underneath it.
4371 MidiRegionView::snap_sample_to_grid_underneath (samplepos_t p, int32_t divisions, bool shift_snap) const
4373 TempoMap& map (trackview.session()->tempo_map());
4374 double eqaf = map.exact_qn_at_sample (p + _region->position(), divisions);
4376 if (divisions != 0 && shift_snap) {
4377 const double qaf = map.quarter_note_at_sample (p + _region->position());
4378 /* Hack so that we always snap to the note that we are over, instead of snapping
4379 to the next one if we're more than halfway through the one we're over.
4381 const Temporal::Beats grid_beats = get_grid_beats (p + _region->position());
4382 const double rem = eqaf - qaf;
4384 eqaf -= grid_beats.to_double();
4387 const double session_start_off = _region->quarter_note() - midi_region()->start_beats();
4389 return Temporal::Beats (eqaf - session_start_off);
4393 MidiRegionView::get_channel_mode () const
4395 RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (&trackview);
4396 return rtav->midi_track()->get_playback_channel_mode();
4400 MidiRegionView::get_selected_channels () const
4402 RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (&trackview);
4403 return rtav->midi_track()->get_playback_channel_mask();
4408 MidiRegionView::get_grid_beats(samplepos_t pos) const
4410 PublicEditor& editor = trackview.editor();
4411 bool success = false;
4412 Temporal::Beats beats = editor.get_grid_type_as_beats (success, pos);
4414 beats = Temporal::Beats(1);
4419 MidiRegionView::y_to_note (double y) const
4421 int const n = ((contents_height() - y) / contents_height() * (double)(_current_range_max - _current_range_min + 1))
4422 + _current_range_min;
4426 } else if (n > 127) {
4430 /* min due to rounding and/or off-by-one errors */
4431 return min ((uint8_t) n, _current_range_max);
4435 MidiRegionView::note_to_y(uint8_t note) const
4437 return contents_height() - (note + 1 - _current_range_min) * note_height() + 1;
4441 MidiRegionView::session_relative_qn (double qn) const
4443 return qn + (region()->quarter_note() - midi_region()->start_beats());