2 Copyright (C) 2001-2007 Paul Davis
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.
27 #include <gtkmm2ext/gtk_ui.h>
29 #include <sigc++/signal.h>
31 #include "pbd/memento_command.h"
33 #include "ardour/playlist.h"
34 #include "ardour/tempo.h"
35 #include "ardour/midi_region.h"
36 #include "ardour/midi_source.h"
37 #include "ardour/midi_diskstream.h"
38 #include "ardour/midi_model.h"
39 #include "ardour/midi_patch_manager.h"
41 #include "evoral/Parameter.hpp"
42 #include "evoral/Control.hpp"
44 #include "automation_region_view.h"
45 #include "automation_time_axis.h"
46 #include "canvas-hit.h"
47 #include "canvas-note.h"
48 #include "canvas-program-change.h"
49 #include "ghostregion.h"
50 #include "gui_thread.h"
52 #include "midi_cut_buffer.h"
53 #include "midi_list_editor.h"
54 #include "midi_region_view.h"
55 #include "midi_streamview.h"
56 #include "midi_time_axis.h"
57 #include "midi_time_axis.h"
58 #include "midi_util.h"
59 #include "public_editor.h"
60 #include "selection.h"
61 #include "simpleline.h"
62 #include "streamview.h"
68 using namespace ARDOUR;
70 using namespace Editing;
71 using namespace ArdourCanvas;
73 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv,
74 boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color const & basic_color)
75 : RegionView (parent, tv, r, spu, basic_color)
77 , _last_channel_selection(0xFFFF)
78 , _default_note_length(1.0)
79 , _current_range_min(0)
80 , _current_range_max(0)
81 , _model_name(string())
82 , _custom_device_mode(string())
84 , _note_group(new ArdourCanvas::Group(*parent))
85 , _delta_command(NULL)
89 _note_group->raise_to_top();
92 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv,
93 boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color,
94 TimeAxisViewItem::Visibility visibility)
95 : RegionView (parent, tv, r, spu, basic_color, false, visibility)
97 , _last_channel_selection(0xFFFF)
98 , _default_note_length(1.0)
99 , _model_name(string())
100 , _custom_device_mode(string())
102 , _note_group(new ArdourCanvas::Group(*parent))
103 , _delta_command(NULL)
108 _note_group->raise_to_top();
112 MidiRegionView::MidiRegionView (const MidiRegionView& other)
113 : sigc::trackable(other)
116 , _last_channel_selection(0xFFFF)
117 , _default_note_length(1.0)
118 , _model_name(string())
119 , _custom_device_mode(string())
121 , _note_group(new ArdourCanvas::Group(*get_canvas_group()))
122 , _delta_command(NULL)
129 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
130 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
135 MidiRegionView::MidiRegionView (const MidiRegionView& other, boost::shared_ptr<MidiRegion> region)
136 : RegionView (other, boost::shared_ptr<Region> (region))
138 , _last_channel_selection(0xFFFF)
139 , _default_note_length(1.0)
140 , _model_name(string())
141 , _custom_device_mode(string())
143 , _note_group(new ArdourCanvas::Group(*get_canvas_group()))
144 , _delta_command(NULL)
151 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
152 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
158 MidiRegionView::init (Gdk::Color const & basic_color, bool wfd)
161 midi_region()->midi_source(0)->load_model();
164 _model = midi_region()->midi_source(0)->model();
165 _enable_display = false;
167 RegionView::init (basic_color, false);
169 compute_colors (basic_color);
171 set_height (trackview.current_height());
174 region_sync_changed ();
175 region_resized (BoundsChanged);
178 reset_width_dependent_items (_pixel_width);
182 _enable_display = true;
185 display_model (_model);
189 group->raise_to_top();
190 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
192 midi_view()->signal_channel_mode_changed().connect(
193 mem_fun(this, &MidiRegionView::midi_channel_mode_changed));
195 midi_view()->signal_midi_patch_settings_changed().connect(
196 mem_fun(this, &MidiRegionView::midi_patch_settings_changed));
200 MidiRegionView::canvas_event(GdkEvent* ev)
202 PublicEditor& editor (trackview.editor());
204 if (!editor.internal_editing()) {
208 static double drag_start_x, drag_start_y;
209 static double last_x, last_y;
210 double event_x, event_y;
211 nframes64_t event_frame = 0;
214 static ArdourCanvas::SimpleRect* drag_rect = NULL;
216 /* XXX: note that as of August 2009, the GnomeCanvas does not propagate scroll events
217 to its items, which means that ev->type == GDK_SCROLL will never be seen
222 fine = Keyboard::modifier_state_equals (ev->scroll.state, Keyboard::Level4Modifier);
224 if (ev->scroll.direction == GDK_SCROLL_UP) {
225 change_velocities (true, fine, false);
227 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
228 change_velocities (false, fine, false);
237 /* since GTK bindings are generally activated on press, and since
238 detectable auto-repeat is the name of the game and only sends
239 repeated presses, carry out key actions at key press, not release.
242 if (ev->key.keyval == GDK_Alt_L || ev->key.keyval == GDK_Alt_R){
243 _mouse_state = SelectTouchDragging;
246 } else if (ev->key.keyval == GDK_Escape) {
250 } else if (ev->key.keyval == GDK_comma || ev->key.keyval == GDK_period) {
252 bool start = (ev->key.keyval == GDK_comma);
253 bool end = (ev->key.keyval == GDK_period);
254 bool shorter = Keyboard::modifier_state_contains (ev->key.state, Keyboard::PrimaryModifier);
255 fine = Keyboard::modifier_state_contains (ev->key.state, Keyboard::SecondaryModifier);
257 change_note_lengths (fine, shorter, start, end);
261 } else if (ev->key.keyval == GDK_Delete) {
266 } else if (ev->key.keyval == GDK_Tab) {
268 if (Keyboard::modifier_state_equals (ev->key.state, Keyboard::PrimaryModifier)) {
269 goto_previous_note ();
275 } else if (ev->key.keyval == GDK_Up) {
277 bool allow_smush = Keyboard::modifier_state_contains (ev->key.state, Keyboard::SecondaryModifier);
278 bool fine = Keyboard::modifier_state_contains (ev->key.state, Keyboard::TertiaryModifier);
280 if (Keyboard::modifier_state_contains (ev->key.state, Keyboard::PrimaryModifier)) {
281 change_velocities (true, fine, allow_smush);
283 transpose (true, fine, allow_smush);
287 } else if (ev->key.keyval == GDK_Down) {
289 bool allow_smush = Keyboard::modifier_state_contains (ev->key.state, Keyboard::SecondaryModifier);
290 fine = Keyboard::modifier_state_contains (ev->key.state, Keyboard::TertiaryModifier);
292 if (Keyboard::modifier_state_contains (ev->key.state, Keyboard::PrimaryModifier)) {
293 change_velocities (false, fine, allow_smush);
295 transpose (false, fine, allow_smush);
299 } else if (ev->key.keyval == GDK_Left) {
304 } else if (ev->key.keyval == GDK_Right) {
309 } else if (ev->key.keyval == GDK_Control_L) {
315 case GDK_KEY_RELEASE:
316 if (ev->key.keyval == GDK_Alt_L || ev->key.keyval == GDK_Alt_R) {
322 case GDK_BUTTON_PRESS:
323 if (_mouse_state != SelectTouchDragging && ev->button.button == 1) {
324 _pressed_button = ev->button.button;
325 _mouse_state = Pressed;
328 _pressed_button = ev->button.button;
331 case GDK_2BUTTON_PRESS:
334 case GDK_ENTER_NOTIFY:
335 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
336 Keyboard::magic_widget_grab_focus();
340 case GDK_MOTION_NOTIFY:
341 event_x = ev->motion.x;
342 event_y = ev->motion.y;
343 group->w2i(event_x, event_y);
345 // convert event_x to global frame
346 event_frame = trackview.editor().pixel_to_frame(event_x) + _region->position();
347 trackview.editor().snap_to(event_frame);
348 // convert event_frame back to local coordinates relative to position
349 event_frame -= _region->position();
351 switch (_mouse_state) {
352 case Pressed: // Drag start
355 if (_pressed_button == 1 && editor.current_mouse_mode() == MouseObject) {
356 cerr << "MRV start select grab\n";
357 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
358 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
361 drag_start_x = event_x;
362 drag_start_y = event_y;
364 drag_rect = new ArdourCanvas::SimpleRect(*group);
365 drag_rect->property_x1() = event_x;
366 drag_rect->property_y1() = event_y;
367 drag_rect->property_x2() = event_x;
368 drag_rect->property_y2() = event_y;
369 drag_rect->property_outline_what() = 0xFF;
370 drag_rect->property_outline_color_rgba()
371 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
372 drag_rect->property_fill_color_rgba()
373 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
375 _mouse_state = SelectRectDragging;
378 // Add note drag start
379 } else if (editor.current_mouse_mode() == MouseRange) {
380 cerr << "MRV start note grab\n";
381 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
382 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
385 drag_start_x = event_x;
386 drag_start_y = event_y;
388 drag_rect = new ArdourCanvas::SimpleRect(*group);
389 drag_rect->property_x1() = trackview.editor().frame_to_pixel(event_frame);
391 drag_rect->property_y1() = midi_stream_view()->note_to_y(
392 midi_stream_view()->y_to_note(event_y));
393 drag_rect->property_x2() = event_x;
394 drag_rect->property_y2() = drag_rect->property_y1()
395 + floor(midi_stream_view()->note_height());
396 drag_rect->property_outline_what() = 0xFF;
397 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
398 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
400 _mouse_state = AddDragging;
406 case SelectRectDragging: // Select drag motion
407 case AddDragging: // Add note drag motion
408 if (ev->motion.is_hint) {
411 GdkModifierType state;
412 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
417 if (_mouse_state == AddDragging)
418 event_x = trackview.editor().frame_to_pixel(event_frame);
421 if (event_x > drag_start_x)
422 drag_rect->property_x2() = event_x;
424 drag_rect->property_x1() = event_x;
427 if (drag_rect && _mouse_state == SelectRectDragging) {
428 if (event_y > drag_start_y)
429 drag_rect->property_y2() = event_y;
431 drag_rect->property_y1() = event_y;
433 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
439 case SelectTouchDragging:
447 case GDK_BUTTON_RELEASE:
448 event_x = ev->motion.x;
449 event_y = ev->motion.y;
450 group->w2i(event_x, event_y);
451 group->ungrab(ev->button.time);
452 event_frame = trackview.editor().pixel_to_frame(event_x);
454 if (ev->button.button == 3) {
456 } else if (_pressed_button != 1) {
460 switch (_mouse_state) {
461 case Pressed: // Clicked
462 switch (editor.current_mouse_mode()) {
468 create_note_at(event_x, event_y, _default_note_length);
475 case SelectRectDragging: // Select drag done
480 case AddDragging: // Add drag done
482 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
483 const double x = drag_rect->property_x1();
484 const double length = trackview.editor().pixel_to_frame(
485 drag_rect->property_x2() - drag_rect->property_x1());
487 create_note_at(x, drag_rect->property_y1(), frames_to_beats(length));
502 MidiRegionView::show_list_editor ()
504 MidiListEditor* mle = new MidiListEditor (trackview.session(), midi_region());
508 /** Add a note to the model, and the view, at a canvas (click) coordinate.
509 * \param x horizontal position in pixels
510 * \param y vertical position in pixels
511 * \param length duration of the note in beats */
513 MidiRegionView::create_note_at(double x, double y, double length)
515 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
516 MidiStreamView* const view = mtv->midi_view();
518 double note = midi_stream_view()->y_to_note(y);
521 assert(note <= 127.0);
523 // Start of note in frames relative to region start
524 nframes64_t start_frames = snap_frame_to_frame(trackview.editor().pixel_to_frame(x));
525 assert(start_frames >= 0);
528 length = frames_to_beats(
529 snap_frame_to_frame(start_frames + beats_to_frames(length)) - start_frames);
531 const boost::shared_ptr<NoteType> new_note(new NoteType(0,
532 frames_to_beats(start_frames + _region->start()), length,
533 (uint8_t)note, 0x40));
535 view->update_note_range(new_note->note());
537 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
539 _model->apply_command(trackview.session(), cmd);
541 play_midi_note (new_note);
545 MidiRegionView::clear_events()
550 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
551 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
556 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
561 _pgm_changes.clear();
567 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
570 content_connection.disconnect ();
571 content_connection = _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
573 if (_enable_display) {
580 MidiRegionView::start_delta_command(string name)
582 if (!_delta_command) {
583 _delta_command = _model->new_delta_command(name);
588 MidiRegionView::start_diff_command(string name)
590 if (!_diff_command) {
591 _diff_command = _model->new_diff_command(name);
596 MidiRegionView::delta_add_note(const boost::shared_ptr<NoteType> note, bool selected, bool show_velocity)
598 if (_delta_command) {
599 _delta_command->add(note);
602 _marked_for_selection.insert(note);
605 _marked_for_velocity.insert(note);
610 MidiRegionView::delta_remove_note(ArdourCanvas::CanvasNoteEvent* ev)
612 if (_delta_command && ev->note()) {
613 _delta_command->remove(ev->note());
618 MidiRegionView::diff_add_change (ArdourCanvas::CanvasNoteEvent* ev,
619 MidiModel::DiffCommand::Property property,
623 _diff_command->change (ev->note(), property, val);
628 MidiRegionView::diff_add_change (ArdourCanvas::CanvasNoteEvent* ev,
629 MidiModel::DiffCommand::Property property,
630 Evoral::MusicalTime val)
633 _diff_command->change (ev->note(), property, val);
638 MidiRegionView::apply_delta()
640 if (!_delta_command) {
644 // Mark all selected notes for selection when model reloads
645 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
646 _marked_for_selection.insert((*i)->note());
649 _model->apply_command(trackview.session(), _delta_command);
650 _delta_command = NULL;
651 midi_view()->midi_track()->diskstream()->playlist_modified();
653 _marked_for_selection.clear();
654 _marked_for_velocity.clear();
658 MidiRegionView::apply_diff ()
660 if (!_diff_command) {
664 // Mark all selected notes for selection when model reloads
665 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
666 _marked_for_selection.insert((*i)->note());
669 _model->apply_command(trackview.session(), _diff_command);
670 _diff_command = NULL;
671 midi_view()->midi_track()->diskstream()->playlist_modified();
673 _marked_for_selection.clear();
674 _marked_for_velocity.clear();
678 MidiRegionView::apply_delta_as_subcommand()
680 if (!_delta_command) {
684 // Mark all selected notes for selection when model reloads
685 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
686 _marked_for_selection.insert((*i)->note());
689 _model->apply_command_as_subcommand(trackview.session(), _delta_command);
690 _delta_command = NULL;
691 midi_view()->midi_track()->diskstream()->playlist_modified();
693 _marked_for_selection.clear();
694 _marked_for_velocity.clear();
698 MidiRegionView::apply_diff_as_subcommand()
700 if (!_diff_command) {
704 // Mark all selected notes for selection when model reloads
705 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
706 _marked_for_selection.insert((*i)->note());
709 _model->apply_command_as_subcommand(trackview.session(), _diff_command);
710 _diff_command = NULL;
711 midi_view()->midi_track()->diskstream()->playlist_modified();
713 _marked_for_selection.clear();
714 _marked_for_velocity.clear();
718 MidiRegionView::abort_command()
720 delete _delta_command;
722 delete _diff_command;
728 MidiRegionView::find_canvas_note (boost::shared_ptr<NoteType> note)
730 /* XXX optimize the crap out of this SOON */
732 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
733 if ((*i)->note() == note) {
742 MidiRegionView::redisplay_model()
744 // Don't redisplay the model if we're currently recording and displaying that
751 // Mark all selected notes for selection when model reloads
752 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
753 _marked_for_selection.insert((*i)->note());
756 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
763 MidiModel::Notes notes = _model->notes();
765 for (size_t i = 0; i < _model->n_notes(); ++i) {
766 boost::shared_ptr<NoteType> note (_model->note_at (i));
768 if (note_in_visible_range (note)) {
769 CanvasNoteEvent* cne;
771 if ((cne = find_canvas_note (note)) != 0) {
778 if ((cn = dynamic_cast<CanvasNote*>(cne)) != 0) {
780 } else if ((ch = dynamic_cast<CanvasHit*>(cne)) != 0) {
791 /* remove note items that are no longer valid */
793 for (Events::iterator i = _events.begin(); i != _events.end(); ) {
794 if (!(*i)->valid ()) {
795 cerr << "Canvas note " << *i << " is invalid, deleting\n";
797 i = _events.erase (i);
804 display_program_changes();
806 _model->read_unlock();
809 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
814 MidiRegionView::display_program_changes()
816 boost::shared_ptr<Evoral::Control> control = _model->control(MidiPgmChangeAutomation);
821 Glib::Mutex::Lock lock (control->list()->lock());
823 uint8_t channel = control->parameter().channel();
825 for (AutomationList::const_iterator event = control->list()->begin();
826 event != control->list()->end(); ++event) {
827 double event_time = (*event)->when;
828 double program_number = floor((*event)->value + 0.5);
830 // Get current value of bank select MSB at time of the program change
831 Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
832 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
834 if (msb_control != 0) {
835 msb = uint8_t(floor(msb_control->get_float(true, event_time) + 0.5));
838 // Get current value of bank select LSB at time of the program change
839 Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
840 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
842 if (lsb_control != 0) {
843 lsb = uint8_t(floor(lsb_control->get_float(true, event_time) + 0.5));
846 MIDI::Name::PatchPrimaryKey patch_key(msb, lsb, program_number);
848 boost::shared_ptr<MIDI::Name::Patch> patch =
849 MIDI::Name::MidiPatchManager::instance().find_patch(
850 _model_name, _custom_device_mode, channel, patch_key);
852 PCEvent program_change(event_time, uint8_t(program_number), channel);
855 add_pgm_change(program_change, patch->name());
858 snprintf(buf, 4, "%d", int(program_number));
859 add_pgm_change(program_change, buf);
865 MidiRegionView::display_sysexes()
867 for (MidiModel::SysExes::const_iterator i = _model->sysexes().begin(); i != _model->sysexes().end(); ++i) {
868 Evoral::MusicalTime time = (*i)->time();
873 for (uint32_t b = 0; b < (*i)->size(); ++b) {
874 str << int((*i)->buffer()[b]);
875 if (b != (*i)->size() -1) {
879 string text = str.str();
881 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
883 const double x = trackview.editor().frame_to_pixel(beats_to_frames(time));
885 double height = midi_stream_view()->contents_height();
887 boost::shared_ptr<CanvasSysEx> sysex = boost::shared_ptr<CanvasSysEx>(
888 new CanvasSysEx(*this, *group, text, height, x, 1.0));
890 // Show unless program change is beyond the region bounds
891 if (time - _region->start() >= _region->length() || time < _region->start()) {
897 _sys_exes.push_back(sysex);
902 MidiRegionView::~MidiRegionView ()
904 in_destructor = true;
906 RegionViewGoingAway (this); /* EMIT_SIGNAL */
915 delete _delta_command;
919 MidiRegionView::region_resized (Change what_changed)
921 RegionView::region_resized(what_changed);
923 if (what_changed & ARDOUR::PositionChanged) {
924 set_duration(_region->length(), 0);
925 if (_enable_display) {
932 MidiRegionView::reset_width_dependent_items (double pixel_width)
934 RegionView::reset_width_dependent_items(pixel_width);
935 assert(_pixel_width == pixel_width);
937 if (_enable_display) {
943 MidiRegionView::set_height (double height)
945 static const double FUDGE = 2.0;
946 const double old_height = _height;
947 RegionView::set_height(height);
948 _height = height - FUDGE;
950 apply_note_range(midi_stream_view()->lowest_note(),
951 midi_stream_view()->highest_note(),
952 height != old_height + FUDGE);
955 name_pixbuf->raise_to_top();
960 /** Apply the current note range from the stream view
961 * by repositioning/hiding notes as necessary
964 MidiRegionView::apply_note_range (uint8_t min, uint8_t max, bool force)
966 if (!_enable_display) {
970 if (!force && _current_range_min == min && _current_range_max == max) {
974 _current_range_min = min;
975 _current_range_max = max;
977 for (Events::const_iterator i = _events.begin(); i != _events.end(); ++i) {
978 CanvasNoteEvent* event = *i;
979 Item* item = dynamic_cast<Item*>(event);
981 if (event && event->note()) {
982 if (event->note()->note() < _current_range_min
983 || event->note()->note() > _current_range_max) {
984 if (canvas_item_visible(item)) {
988 if (!canvas_item_visible(item)) {
992 if (CanvasNote* note = dynamic_cast<CanvasNote*>(event)) {
993 const double y1 = midi_stream_view()->note_to_y(event->note()->note());
994 const double y2 = y1 + floor(midi_stream_view()->note_height());
996 note->property_y1() = y1;
997 note->property_y2() = y2;
998 } else if (CanvasHit* hit = dynamic_cast<CanvasHit*>(event)) {
999 double x = trackview.editor().frame_to_pixel(
1000 beats_to_frames(event->note()->time()) - _region->start());
1001 const double diamond_size = midi_stream_view()->note_height() / 2.0;
1002 double y = midi_stream_view()->note_to_y(event->note()->note())
1003 + ((diamond_size-2.0) / 4.0);
1005 hit->set_height(diamond_size);
1006 hit->move(x-hit->x1(), y-hit->y1());
1016 MidiRegionView::add_ghost (TimeAxisView& tv)
1020 double unit_position = _region->position () / samples_per_unit;
1021 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
1022 MidiGhostRegion* ghost;
1024 if (mtv && mtv->midi_view()) {
1025 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group
1026 to allow having midi notes on top of note lines and waveforms.
1028 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
1030 ghost = new MidiGhostRegion (tv, trackview, unit_position);
1033 ghost->set_height ();
1034 ghost->set_duration (_region->length() / samples_per_unit);
1035 ghosts.push_back (ghost);
1037 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1038 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
1039 ghost->add_note(note);
1043 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
1049 /** Begin tracking note state for successive calls to add_event
1052 MidiRegionView::begin_write()
1054 assert(!_active_notes);
1055 _active_notes = new CanvasNote*[128];
1056 for (unsigned i=0; i < 128; ++i) {
1057 _active_notes[i] = NULL;
1062 /** Destroy note state for add_event
1065 MidiRegionView::end_write()
1067 delete[] _active_notes;
1068 _active_notes = NULL;
1069 _marked_for_selection.clear();
1070 _marked_for_velocity.clear();
1074 /** Resolve an active MIDI note (while recording).
1077 MidiRegionView::resolve_note(uint8_t note, double end_time)
1079 if (midi_view()->note_mode() != Sustained) {
1083 if (_active_notes && _active_notes[note]) {
1084 const nframes64_t end_time_frames = beats_to_frames(end_time);
1085 _active_notes[note]->property_x2() = trackview.editor().frame_to_pixel(end_time_frames);
1086 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
1087 _active_notes[note] = NULL;
1092 /** Extend active notes to rightmost edge of region (if length is changed)
1095 MidiRegionView::extend_active_notes()
1097 if (!_active_notes) {
1101 for (unsigned i=0; i < 128; ++i) {
1102 if (_active_notes[i]) {
1103 _active_notes[i]->property_x2() = trackview.editor().frame_to_pixel(_region->length());
1109 MidiRegionView::play_midi_note(boost::shared_ptr<NoteType> note)
1111 if (!trackview.editor().sound_notes()) {
1115 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
1118 route_ui->midi_track()->write_immediate_event(
1119 note->on_event().size(), note->on_event().buffer());
1121 const double note_length_beats = (note->off_event().time() - note->on_event().time());
1122 nframes_t note_length_ms = beats_to_frames(note_length_beats)
1123 * (1000 / (double)route_ui->session().nominal_frame_rate());
1124 Glib::signal_timeout().connect(bind(mem_fun(this, &MidiRegionView::play_midi_note_off), note),
1125 note_length_ms, G_PRIORITY_DEFAULT);
1129 MidiRegionView::play_midi_note_off(boost::shared_ptr<NoteType> note)
1131 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
1134 route_ui->midi_track()->write_immediate_event(
1135 note->off_event().size(), note->off_event().buffer());
1141 MidiRegionView::note_in_visible_range(const boost::shared_ptr<NoteType> note) const
1143 const nframes64_t note_start_frames = beats_to_frames(note->time());
1144 bool outside = (note_start_frames - _region->start() >= _region->length())
1145 || (note_start_frames < _region->start())
1146 || (note->note() < midi_stream_view()->lowest_note())
1147 || (note->note() > midi_stream_view()->highest_note());
1152 MidiRegionView::update_note (CanvasNote* ev)
1154 boost::shared_ptr<NoteType> note = ev->note();
1156 const nframes64_t note_start_frames = beats_to_frames(note->time());
1157 const nframes64_t note_end_frames = beats_to_frames(note->end_time());
1159 const double x = trackview.editor().frame_to_pixel(note_start_frames - _region->start());
1162 const double y1 = midi_stream_view()->note_to_y(note->note());
1163 const double note_endpixel =
1164 trackview.editor().frame_to_pixel(note_end_frames - _region->start());
1166 ev->property_x1() = x;
1167 ev->property_y1() = y1;
1168 if (note->length() > 0) {
1169 ev->property_x2() = note_endpixel;
1171 ev->property_x2() = trackview.editor().frame_to_pixel(_region->length());
1173 ev->property_y2() = y1 + floor(midi_stream_view()->note_height());
1175 if (note->length() == 0) {
1176 if (_active_notes) {
1177 assert(note->note() < 128);
1178 // If this note is already active there's a stuck note,
1179 // finish the old note rectangle
1180 if (_active_notes[note->note()]) {
1181 CanvasNote* const old_rect = _active_notes[note->note()];
1182 boost::shared_ptr<NoteType> old_note = old_rect->note();
1183 old_rect->property_x2() = x;
1184 old_rect->property_outline_what() = (guint32) 0xF;
1186 _active_notes[note->note()] = ev;
1188 /* outline all but right edge */
1189 ev->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
1191 /* outline all edges */
1192 ev->property_outline_what() = (guint32) 0xF;
1197 MidiRegionView::update_hit (CanvasHit* ev)
1199 boost::shared_ptr<NoteType> note = ev->note();
1201 const nframes64_t note_start_frames = beats_to_frames(note->time());
1202 const double x = trackview.editor().frame_to_pixel(note_start_frames - _region->start());
1203 const double diamond_size = midi_stream_view()->note_height() / 2.0;
1204 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
1209 /** Add a MIDI note to the view (with length).
1211 * If in sustained mode, notes with length 0 will be considered active
1212 * notes, and resolve_note should be called when the corresponding note off
1213 * event arrives, to properly display the note.
1216 MidiRegionView::add_note(const boost::shared_ptr<NoteType> note)
1218 CanvasNoteEvent* event = 0;
1220 assert(note->time() >= 0);
1221 assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
1223 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
1225 if (midi_view()->note_mode() == Sustained) {
1227 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
1229 update_note (ev_rect);
1233 MidiGhostRegion* gr;
1235 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
1236 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
1237 gr->add_note(ev_rect);
1241 } else if (midi_view()->note_mode() == Percussive) {
1243 const double diamond_size = midi_stream_view()->note_height() / 2.0;
1245 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size, note);
1247 update_hit (ev_diamond);
1256 if (_marked_for_selection.find(note) != _marked_for_selection.end()) {
1257 note_selected(event, true);
1260 if (_marked_for_velocity.find(note) != _marked_for_velocity.end()) {
1261 event->show_velocity();
1263 event->on_channel_selection_change(_last_channel_selection);
1264 _events.push_back(event);
1266 if (note_in_visible_range(note)) {
1275 MidiRegionView::add_pgm_change(PCEvent& program, const string& displaytext)
1277 assert(program.time >= 0);
1279 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
1280 const double x = trackview.editor().frame_to_pixel(beats_to_frames(program.time));
1282 double height = midi_stream_view()->contents_height();
1284 boost::shared_ptr<CanvasProgramChange> pgm_change = boost::shared_ptr<CanvasProgramChange>(
1285 new CanvasProgramChange(*this, *group,
1290 _custom_device_mode,
1291 program.time, program.channel, program.value));
1293 // Show unless program change is beyond the region bounds
1294 if (program.time - _region->start() >= _region->length() || program.time < _region->start()) {
1300 _pgm_changes.push_back(pgm_change);
1304 MidiRegionView::get_patch_key_at(double time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key)
1306 cerr << "getting patch key at " << time << " for channel " << channel << endl;
1307 Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
1308 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
1310 if (msb_control != 0) {
1311 msb = int(msb_control->get_float(true, time));
1312 cerr << "got msb " << msb;
1315 Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
1316 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
1318 if (lsb_control != 0) {
1319 lsb = lsb_control->get_float(true, time);
1320 cerr << " got lsb " << lsb;
1323 Evoral::Parameter program_change(MidiPgmChangeAutomation, channel, 0);
1324 boost::shared_ptr<Evoral::Control> program_control = _model->control(program_change);
1325 float program_number = -1.0;
1326 if (program_control != 0) {
1327 program_number = program_control->get_float(true, time);
1328 cerr << " got program " << program_number << endl;
1331 key.msb = (int) floor(msb + 0.5);
1332 key.lsb = (int) floor(lsb + 0.5);
1333 key.program_number = (int) floor(program_number + 0.5);
1334 assert(key.is_sane());
1339 MidiRegionView::alter_program_change(PCEvent& old_program, const MIDI::Name::PatchPrimaryKey& new_patch)
1341 // TODO: Get the real event here and alter them at the original times
1342 Evoral::Parameter bank_select_msb(MidiCCAutomation, old_program.channel, MIDI_CTL_MSB_BANK);
1343 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
1344 if (msb_control != 0) {
1345 msb_control->set_float(float(new_patch.msb), true, old_program.time);
1348 // TODO: Get the real event here and alter them at the original times
1349 Evoral::Parameter bank_select_lsb(MidiCCAutomation, old_program.channel, MIDI_CTL_LSB_BANK);
1350 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
1351 if (lsb_control != 0) {
1352 lsb_control->set_float(float(new_patch.lsb), true, old_program.time);
1355 Evoral::Parameter program_change(MidiPgmChangeAutomation, old_program.channel, 0);
1356 boost::shared_ptr<Evoral::Control> program_control = _model->control(program_change);
1358 assert(program_control != 0);
1359 program_control->set_float(float(new_patch.program_number), true, old_program.time);
1365 MidiRegionView::program_selected(CanvasProgramChange& program, const MIDI::Name::PatchPrimaryKey& new_patch)
1367 PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1368 alter_program_change(program_change_event, new_patch);
1372 MidiRegionView::previous_program(CanvasProgramChange& program)
1374 MIDI::Name::PatchPrimaryKey key;
1375 get_patch_key_at(program.event_time(), program.channel(), key);
1377 boost::shared_ptr<MIDI::Name::Patch> patch =
1378 MIDI::Name::MidiPatchManager::instance().previous_patch(
1380 _custom_device_mode,
1384 PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1386 alter_program_change(program_change_event, patch->patch_primary_key());
1391 MidiRegionView::next_program(CanvasProgramChange& program)
1393 MIDI::Name::PatchPrimaryKey key;
1394 get_patch_key_at(program.event_time(), program.channel(), key);
1396 boost::shared_ptr<MIDI::Name::Patch> patch =
1397 MIDI::Name::MidiPatchManager::instance().next_patch(
1399 _custom_device_mode,
1403 PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1405 alter_program_change(program_change_event, patch->patch_primary_key());
1410 MidiRegionView::delete_selection()
1412 if (_selection.empty()) {
1416 start_delta_command (_("delete selection"));
1418 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1419 if ((*i)->selected()) {
1420 _delta_command->remove((*i)->note());
1430 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev)
1432 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1433 if ((*i)->selected() && (*i) != ev) {
1434 (*i)->selected(false);
1435 (*i)->hide_velocity();
1443 MidiRegionView::unique_select(ArdourCanvas::CanvasNoteEvent* ev)
1445 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
1447 Selection::iterator tmp = i;
1451 remove_from_selection (*i);
1457 if (!ev->selected()) {
1458 add_to_selection (ev);
1463 MidiRegionView::note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add, bool extend)
1466 clear_selection_except(ev);
1471 if (!ev->selected()) {
1472 add_to_selection (ev);
1476 /* find end of latest note selected, select all between that and the start of "ev" */
1478 Evoral::MusicalTime earliest = DBL_MAX;
1479 Evoral::MusicalTime latest = 0;
1481 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1482 if ((*i)->note()->end_time() > latest) {
1483 latest = (*i)->note()->end_time();
1485 if ((*i)->note()->time() < earliest) {
1486 earliest = (*i)->note()->time();
1490 if (ev->note()->end_time() > latest) {
1491 latest = ev->note()->end_time();
1494 if (ev->note()->time() < earliest) {
1495 earliest = ev->note()->time();
1499 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1501 /* find notes entirely within OR spanning the earliest..latest range */
1503 if (((*i)->note()->time() >= earliest && (*i)->note()->end_time() <= latest) ||
1504 ((*i)->note()->time() <= earliest && (*i)->note()->end_time() >= latest)) {
1505 add_to_selection (*i);
1508 if ((*i)->note()->time() > latest) {
1517 MidiRegionView::note_deselected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
1520 clear_selection_except(ev);
1523 remove_from_selection (ev);
1528 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
1530 const double last_y = std::min(y1, y2);
1531 const double y = std::max(y1, y2);
1533 // TODO: Make this faster by storing the last updated selection rect, and only
1534 // adjusting things that are in the area that appears/disappeared.
1535 // We probably need a tree to be able to find events in O(log(n)) time.
1538 double last_x1 = 0.0;
1542 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1544 // Events should always be sorted by increasing x1() here
1545 assert((*i)->x1() >= last_x1);
1546 last_x1 = (*i)->x1();
1548 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1550 add_to_selection (*i);
1551 } else if ((*i)->selected()) {
1552 // Not inside rectangle
1553 remove_from_selection (*i);
1557 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1559 // Events should always be sorted by increasing x1() here
1560 assert((*i)->x1() >= last_x1);
1561 last_x1 = (*i)->x1();
1563 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1565 add_to_selection (*i);
1566 } else if ((*i)->selected()) {
1567 // Not inside rectangle
1568 remove_from_selection (*i);
1575 MidiRegionView::remove_from_selection (CanvasNoteEvent* ev)
1577 Selection::iterator i = _selection.find (ev);
1579 if (i != _selection.end()) {
1580 _selection.erase (i);
1583 ev->selected (false);
1584 ev->hide_velocity ();
1586 if (_selection.empty()) {
1587 PublicEditor& editor (trackview.editor());
1588 editor.get_selection().remove (this);
1593 MidiRegionView::add_to_selection (CanvasNoteEvent* ev)
1595 bool add_mrv_selection = false;
1597 if (_selection.empty()) {
1598 add_mrv_selection = true;
1601 if (_selection.insert (ev).second) {
1602 ev->selected (true);
1603 play_midi_note ((ev)->note());
1606 if (add_mrv_selection) {
1607 PublicEditor& editor (trackview.editor());
1608 editor.get_selection().add (this);
1613 MidiRegionView::move_selection(double dx, double dy)
1615 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1616 (*i)->move_event(dx, dy);
1621 MidiRegionView::note_dropped(CanvasNoteEvent* ev, double dt, uint8_t dnote)
1623 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
1624 if (_selection.find(ev) == _selection.end()) {
1628 uint8_t lowest_note_in_selection = midi_stream_view()->lowest_note();
1629 uint8_t highest_note_in_selection = midi_stream_view()->highest_note();
1630 uint8_t highest_note_difference = 0;
1632 // find highest and lowest notes first
1633 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1634 uint8_t pitch = (*i)->note()->note();
1635 lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
1636 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
1640 cerr << "dnote: " << (int) dnote << endl;
1641 cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
1642 << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
1643 cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
1644 << int(highest_note_in_selection) << endl;
1645 cerr << "selection size: " << _selection.size() << endl;
1646 cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
1649 // Make sure the note pitch does not exceed the MIDI standard range
1650 if (dnote <= 127 && (highest_note_in_selection + dnote > 127)) {
1651 highest_note_difference = highest_note_in_selection - 127;
1654 start_diff_command(_("move notes"));
1656 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ++i) {
1658 nframes64_t start_frames = beats_to_frames((*i)->note()->time());
1660 cerr << "starting at " << (*i)->note()->time()
1661 << " (" << start_frames << ") delta on drag = " << dt << endl;
1664 /* XXX THERE IS SOMETHING WRONG HERE THAT IS RELATED TO USING DT AND NOT
1665 SOMETHING RELATED TO REGION START + DT ... XXXX
1669 cerr << "Motion was " << snap_frame_to_frame(trackview.editor().pixel_to_frame(dt)) << endl;
1670 start_frames += snap_frame_to_frame(trackview.editor().pixel_to_frame(dt));
1672 cerr << "rev Motion was " << snap_frame_to_frame(trackview.editor().pixel_to_frame(dt)) << endl;
1673 start_frames -= snap_frame_to_frame(trackview.editor().pixel_to_frame(-dt));
1676 cerr << "start frame will be " << start_frames << " vs. region "
1677 << _region->position ()
1680 Evoral::MusicalTime new_time = frames_to_beats(start_frames);
1686 diff_add_change (*i, MidiModel::DiffCommand::StartTime, new_time);
1688 uint8_t original_pitch = (*i)->note()->note();
1689 uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
1691 // keep notes in standard midi range
1692 clamp_to_0_127(new_pitch);
1694 // keep original pitch if note is dragged outside valid midi range
1695 if ((original_pitch != 0 && new_pitch == 0)
1696 || (original_pitch != 127 && new_pitch == 127)) {
1697 new_pitch = original_pitch;
1700 lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
1701 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
1703 diff_add_change (*i, MidiModel::DiffCommand::NoteNumber, new_pitch);
1708 // care about notes being moved beyond the upper/lower bounds on the canvas
1709 if (lowest_note_in_selection < midi_stream_view()->lowest_note() ||
1710 highest_note_in_selection > midi_stream_view()->highest_note()) {
1711 midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
1716 MidiRegionView::snap_pixel_to_frame(double x)
1718 PublicEditor& editor = trackview.editor();
1719 // x is region relative, convert it to global absolute frames
1720 nframes64_t frame = editor.pixel_to_frame(x) + _region->position();
1721 editor.snap_to(frame);
1722 return frame - _region->position(); // convert back to region relative
1726 MidiRegionView::snap_frame_to_frame(nframes64_t x)
1728 PublicEditor& editor = trackview.editor();
1729 // x is region relative, convert it to global absolute frames
1730 nframes64_t frame = x + _region->position();
1731 editor.snap_to(frame);
1732 return frame - _region->position(); // convert back to region relative
1736 MidiRegionView::snap_to_pixel(double x)
1738 return (double) trackview.editor().frame_to_pixel(snap_pixel_to_frame(x));
1742 MidiRegionView::get_position_pixels()
1744 nframes64_t region_frame = get_position();
1745 return trackview.editor().frame_to_pixel(region_frame);
1749 MidiRegionView::get_end_position_pixels()
1751 nframes64_t frame = get_position() + get_duration ();
1752 return trackview.editor().frame_to_pixel(frame);
1756 MidiRegionView::beats_to_frames(double beats) const
1758 return _time_converter.to(beats);
1762 MidiRegionView::frames_to_beats(nframes64_t frames) const
1764 return _time_converter.from(frames);
1768 MidiRegionView::begin_resizing(bool at_front)
1770 _resize_data.clear();
1772 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1773 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
1775 // only insert CanvasNotes into the map
1777 NoteResizeData *resize_data = new NoteResizeData();
1778 resize_data->canvas_note = note;
1780 // create a new SimpleRect from the note which will be the resize preview
1781 SimpleRect *resize_rect = new SimpleRect(
1782 *group, note->x1(), note->y1(), note->x2(), note->y2());
1784 // calculate the colors: get the color settings
1785 uint32_t fill_color = UINT_RGBA_CHANGE_A(
1786 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(),
1789 // make the resize preview notes more transparent and bright
1790 fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
1792 // calculate color based on note velocity
1793 resize_rect->property_fill_color_rgba() = UINT_INTERPOLATE(
1794 CanvasNoteEvent::meter_style_fill_color(note->note()->velocity()),
1798 resize_rect->property_outline_color_rgba() = CanvasNoteEvent::calculate_outline(
1799 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get());
1801 resize_data->resize_rect = resize_rect;
1802 _resize_data.push_back(resize_data);
1808 MidiRegionView::update_resizing (bool at_front, double delta_x, bool relative)
1810 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1811 SimpleRect* resize_rect = (*i)->resize_rect;
1812 CanvasNote* canvas_note = (*i)->canvas_note;
1817 current_x = canvas_note->x1() + delta_x;
1819 // x is in track relative, transform it to region relative
1820 current_x = delta_x - get_position_pixels();
1824 current_x = canvas_note->x2() + delta_x;
1826 // x is in track relative, transform it to region relative
1827 current_x = delta_x - get_end_position_pixels ();
1832 resize_rect->property_x1() = snap_to_pixel(current_x);
1833 resize_rect->property_x2() = canvas_note->x2();
1835 resize_rect->property_x2() = snap_to_pixel(current_x);
1836 resize_rect->property_x1() = canvas_note->x1();
1842 MidiRegionView::commit_resizing (bool at_front, double delta_x, bool relative)
1844 start_diff_command(_("resize notes"));
1846 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1847 CanvasNote* canvas_note = (*i)->canvas_note;
1848 SimpleRect* resize_rect = (*i)->resize_rect;
1849 const double region_start = get_position_pixels();
1854 current_x = canvas_note->x1() + delta_x;
1856 // x is in track relative, transform it to region relative
1857 current_x = region_start + delta_x;
1861 current_x = canvas_note->x2() + delta_x;
1863 // x is in track relative, transform it to region relative
1864 current_x = region_start + delta_x;
1868 current_x = snap_pixel_to_frame (current_x);
1869 current_x = frames_to_beats (current_x);
1871 if (at_front && current_x < canvas_note->note()->end_time()) {
1872 diff_add_change (canvas_note, MidiModel::DiffCommand::StartTime, current_x);
1876 double len = current_x - canvas_note->note()->time();
1879 /* XXX convert to beats */
1880 diff_add_change (canvas_note, MidiModel::DiffCommand::Length, len);
1888 _resize_data.clear();
1893 MidiRegionView::change_note_velocity(CanvasNoteEvent* event, int8_t velocity, bool relative)
1895 uint8_t new_velocity;
1898 new_velocity = event->note()->velocity() + velocity;
1899 clamp_to_0_127(new_velocity);
1901 new_velocity = velocity;
1904 diff_add_change (event, MidiModel::DiffCommand::Velocity, new_velocity);
1908 MidiRegionView::change_note_note (CanvasNoteEvent* event, int8_t note, bool relative)
1913 new_note = event->note()->note() + note;
1918 clamp_to_0_127 (new_note);
1919 diff_add_change (event, MidiModel::DiffCommand::NoteNumber, new_note);
1923 MidiRegionView::trim_note (CanvasNoteEvent* event, Evoral::MusicalTime front_delta, Evoral::MusicalTime end_delta)
1925 bool change_start = false;
1926 bool change_length = false;
1927 Evoral::MusicalTime new_start;
1928 Evoral::MusicalTime new_length;
1930 /* NOTE: the semantics of the two delta arguments are slightly subtle:
1932 front_delta: if positive - move the start of the note later in time (shortening it)
1933 if negative - move the start of the note earlier in time (lengthening it)
1935 end_delta: if positive - move the end of the note later in time (lengthening it)
1936 if negative - move the end of the note earlier in time (shortening it)
1940 if (front_delta < 0) {
1942 if (event->note()->time() < -front_delta) {
1945 new_start = event->note()->time() + front_delta; // moves earlier
1948 /* start moved toward zero, so move the end point out to where it used to be.
1949 Note that front_delta is negative, so this increases the length.
1952 new_length = event->note()->length() - front_delta;
1953 change_start = true;
1954 change_length = true;
1958 Evoral::MusicalTime new_pos = event->note()->time() + front_delta;
1960 if (new_pos < event->note()->end_time()) {
1961 new_start = event->note()->time() + front_delta;
1962 /* start moved toward the end, so move the end point back to where it used to be */
1963 new_length = event->note()->length() - front_delta;
1964 change_start = true;
1965 change_length = true;
1972 bool can_change = true;
1973 if (end_delta < 0) {
1974 if (event->note()->length() < -end_delta) {
1980 new_length = event->note()->length() + end_delta;
1981 change_length = true;
1986 diff_add_change (event, MidiModel::DiffCommand::StartTime, new_start);
1989 if (change_length) {
1990 diff_add_change (event, MidiModel::DiffCommand::Length, new_length);
1995 MidiRegionView::change_note_time (CanvasNoteEvent* event, Evoral::MusicalTime delta, bool relative)
1997 Evoral::MusicalTime new_time;
2001 if (event->note()->time() < -delta) {
2004 new_time = event->note()->time() + delta;
2007 new_time = event->note()->time() + delta;
2013 diff_add_change (event, MidiModel::DiffCommand::StartTime, new_time);
2017 MidiRegionView::change_velocities (bool up, bool fine, bool allow_smush)
2021 if (_selection.empty()) {
2036 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2037 if ((*i)->note()->velocity() + delta == 0 || (*i)->note()->velocity() + delta == 127) {
2043 start_diff_command(_("change velocities"));
2045 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
2046 Selection::iterator next = i;
2048 change_note_velocity (*i, delta, true);
2057 MidiRegionView::transpose (bool up, bool fine, bool allow_smush)
2059 if (_selection.empty()) {
2076 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2078 if ((int8_t) (*i)->note()->note() + delta <= 0) {
2082 if ((int8_t) (*i)->note()->note() + delta > 127) {
2089 start_diff_command (_("transpose"));
2091 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
2092 Selection::iterator next = i;
2094 change_note_note (*i, delta, true);
2102 MidiRegionView::change_note_lengths (bool fine, bool shorter, bool start, bool end)
2104 Evoral::MusicalTime delta;
2109 /* grab the current grid distance */
2111 delta = trackview.editor().get_grid_type_as_beats (success, _region->position());
2113 /* XXX cannot get grid type as beats ... should always be possible ... FIX ME */
2114 cerr << "Grid type not available as beats - TO BE FIXED\n";
2123 start_diff_command (_("change note lengths"));
2125 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
2126 Selection::iterator next = i;
2129 /* note the negation of the delta for start */
2131 trim_note (*i, (start ? -delta : 0), (end ? delta : 0));
2140 MidiRegionView::nudge_notes (bool forward)
2142 if (_selection.empty()) {
2146 /* pick a note as the point along the timeline to get the nudge distance.
2147 its not necessarily the earliest note, so we may want to pull the notes out
2148 into a vector and sort before using the first one.
2151 nframes64_t ref_point = _region->position() + beats_to_frames ((*(_selection.begin()))->note()->time());
2153 nframes64_t distance;
2155 if ((distance = trackview.editor().get_nudge_distance (ref_point, unused)) == 0) {
2157 /* no nudge distance set - use grid */
2159 nframes64_t next_pos = ref_point;
2162 /* XXX need check on max_frames, but that needs max_frames64 or something */
2165 if (next_pos == 0) {
2171 cerr << "ref point was " << ref_point << " next was " << next_pos;
2172 trackview.editor().snap_to (next_pos, (forward ? 1 : -1), false);
2173 distance = ref_point - next_pos;
2174 cerr << " final is " << next_pos << " distance = " << distance << endl;
2177 if (distance == 0) {
2181 Evoral::MusicalTime delta = frames_to_beats (fabs (distance));
2187 start_diff_command (_("nudge"));
2189 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
2190 Selection::iterator next = i;
2192 change_note_time (*i, delta, true);
2200 MidiRegionView::change_channel(uint8_t channel)
2202 start_diff_command(_("change channel"));
2203 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2204 diff_add_change (*i, MidiModel::DiffCommand::Channel, channel);
2211 MidiRegionView::note_entered(ArdourCanvas::CanvasNoteEvent* ev)
2213 if (_mouse_state == SelectTouchDragging) {
2214 note_selected(ev, true);
2217 PublicEditor& editor (trackview.editor());
2218 editor.show_verbose_canvas_cursor_with (Evoral::midi_note_name (ev->note()->note()));
2222 MidiRegionView::note_left (ArdourCanvas::CanvasNoteEvent* ev)
2224 PublicEditor& editor (trackview.editor());
2225 editor.hide_verbose_canvas_cursor ();
2230 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
2232 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
2234 display_model(msrc->model());
2238 MidiRegionView::set_frame_color()
2241 if (_selected && should_show_selection) {
2242 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
2244 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
2250 MidiRegionView::midi_channel_mode_changed(ChannelMode mode, uint16_t mask)
2254 case FilterChannels:
2255 _force_channel = -1;
2258 _force_channel = mask;
2259 mask = 0xFFFF; // Show all notes as active (below)
2262 // Update notes for selection
2263 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2264 (*i)->on_channel_selection_change(mask);
2267 _last_channel_selection = mask;
2271 MidiRegionView::midi_patch_settings_changed(std::string model, std::string custom_device_mode)
2273 _model_name = model;
2274 _custom_device_mode = custom_device_mode;
2279 MidiRegionView::cut_copy_clear (Editing::CutCopyOp op)
2281 if (_selection.empty()) {
2285 PublicEditor& editor (trackview.editor());
2290 editor.get_cut_buffer().add (selection_as_cut_buffer());
2296 start_delta_command();
2298 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2303 delta_remove_note (*i);
2314 MidiRegionView::selection_as_cut_buffer () const
2318 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
2319 notes.push_back (boost::shared_ptr<NoteType> (new NoteType (*((*i)->note().get()))));
2322 /* sort them into time order */
2324 Evoral::Sequence<Evoral::MusicalTime>::LaterNoteComparator cmp;
2325 sort (notes.begin(), notes.end(), cmp);
2327 MidiCutBuffer* cb = new MidiCutBuffer (trackview.session());
2334 MidiRegionView::paste (nframes64_t pos, float times, const MidiCutBuffer& mcb)
2340 start_delta_command (_("paste"));
2342 Evoral::MusicalTime beat_delta;
2343 Evoral::MusicalTime paste_pos_beats;
2344 Evoral::MusicalTime duration;
2345 Evoral::MusicalTime end_point;
2347 duration = mcb.notes().back()->end_time() - mcb.notes().front()->time();
2348 paste_pos_beats = frames_to_beats (pos - _region->position());
2349 beat_delta = mcb.notes().front()->time() - paste_pos_beats;
2350 paste_pos_beats = 0;
2352 _selection.clear ();
2354 for (int n = 0; n < (int) times; ++n) {
2356 for (NoteList::const_iterator i = mcb.notes().begin(); i != mcb.notes().end(); ++i) {
2358 boost::shared_ptr<NoteType> copied_note (new NoteType (*((*i).get())));
2359 copied_note->set_time (paste_pos_beats + copied_note->time() - beat_delta);
2361 /* make all newly added notes selected */
2363 delta_add_note (copied_note, true);
2364 end_point = copied_note->end_time();
2367 paste_pos_beats += duration;
2370 /* if we pasted past the current end of the region, extend the region */
2372 nframes64_t end_frame = _region->position() + beats_to_frames (end_point);
2373 nframes64_t region_end = _region->position() + _region->length() - 1;
2375 if (end_frame > region_end) {
2377 trackview.session().begin_reversible_command (_("paste"));
2379 XMLNode& before (_region->get_state());
2380 _region->set_length (end_frame, this);
2381 trackview.session().add_command (new MementoCommand<Region>(*_region, &before, &_region->get_state()));
2388 MidiRegionView::add_note (uint8_t channel, uint8_t number, uint8_t velocity,
2389 Evoral::MusicalTime pos, Evoral::MusicalTime len)
2391 boost::shared_ptr<NoteType> new_note (new NoteType (channel, pos, len, number, velocity));
2393 start_delta_command (_("step add"));
2394 delta_add_note (new_note, true, false);
2397 /* potentially extend region to hold new note */
2399 nframes64_t end_frame = _region->position() + beats_to_frames (new_note->end_time());
2400 nframes64_t region_end = _region->position() + _region->length() - 1;
2402 if (end_frame > region_end) {
2403 _region->set_length (end_frame, this);
2410 MidiRegionView::goto_next_note ()
2412 // nframes64_t pos = -1;
2413 bool use_next = false;
2415 if (_events.back()->selected()) {
2419 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2420 if ((*i)->selected()) {
2423 } else if (use_next) {
2425 // pos = _region->position() + beats_to_frames ((*i)->note()->time());
2430 /* use the first one */
2432 unique_select (_events.front());
2437 MidiRegionView::goto_previous_note ()
2439 // nframes64_t pos = -1;
2440 bool use_next = false;
2442 if (_events.front()->selected()) {
2446 for (Events::reverse_iterator i = _events.rbegin(); i != _events.rend(); ++i) {
2447 if ((*i)->selected()) {
2450 } else if (use_next) {
2452 // pos = _region->position() + beats_to_frames ((*i)->note()->time());
2457 /* use the last one */
2459 unique_select (*(_events.rbegin()));
2463 MidiRegionView::selection_as_notelist (NoteList& selected)
2465 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
2466 if ((*i)->selected()) {
2467 selected.push_back ((*i)->note());