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.
26 #include <gtkmm2ext/gtk_ui.h>
28 #include <sigc++/signal.h>
30 #include <ardour/playlist.h>
31 #include <ardour/tempo.h>
32 #include <ardour/midi_region.h>
33 #include <ardour/midi_source.h>
34 #include <ardour/midi_diskstream.h>
35 #include <ardour/midi_model.h>
37 #include "streamview.h"
38 #include "midi_region_view.h"
39 #include "midi_streamview.h"
40 #include "midi_time_axis.h"
41 #include "simpleline.h"
42 #include "canvas-hit.h"
43 #include "canvas-note.h"
44 #include "canvas-program-change.h"
45 #include "public_editor.h"
46 #include "ghostregion.h"
47 #include "midi_time_axis.h"
48 #include "automation_time_axis.h"
49 #include "automation_region_view.h"
51 #include "midi_util.h"
52 #include "gui_thread.h"
58 using namespace ARDOUR;
60 using namespace Editing;
61 using namespace ArdourCanvas;
63 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color)
64 : RegionView (parent, tv, r, spu, basic_color)
66 , _last_channel_selection(0xFFFF)
67 , _default_note_length(0.0)
69 , _note_group(new ArdourCanvas::Group(*parent))
70 , _delta_command(NULL)
74 _note_group->raise_to_top();
77 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
78 : RegionView (parent, tv, r, spu, basic_color, false, visibility)
80 , _last_channel_selection(0xFFFF)
81 , _default_note_length(0.0)
83 , _note_group(new ArdourCanvas::Group(*parent))
84 , _delta_command(NULL)
89 _note_group->raise_to_top();
93 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
96 midi_region()->midi_source(0)->load_model();
98 const Meter& m = trackview.session().tempo_map().meter_at(_region->position());
99 const Tempo& t = trackview.session().tempo_map().tempo_at(_region->position());
100 _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
103 _model = midi_region()->midi_source(0)->model();
104 _enable_display = false;
106 RegionView::init (basic_color, false);
108 compute_colors (basic_color);
110 set_y_position_and_height (0, trackview.current_height());
113 region_sync_changed ();
114 region_resized (BoundsChanged);
117 reset_width_dependent_items (_pixel_width);
118 //reset_width_dependent_items ((double) _region->length() / samples_per_unit);
122 _enable_display = true;
127 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
130 group->raise_to_top();
131 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
133 midi_view()->signal_channel_mode_changed().connect(
134 mem_fun(this, &MidiRegionView::midi_channel_mode_changed));
138 MidiRegionView::canvas_event(GdkEvent* ev)
140 static bool delete_mod = false;
141 static Editing::MidiEditMode original_mode;
143 static double drag_start_x, drag_start_y;
144 static double last_x, last_y;
145 double event_x, event_y;
146 nframes64_t event_frame = 0;
148 static ArdourCanvas::SimpleRect* drag_rect = NULL;
150 if (trackview.editor.current_mouse_mode() != MouseNote)
157 if (ev->key.keyval == GDK_Delete && !delete_mod) {
159 original_mode = trackview.editor.current_midi_edit_mode();
160 trackview.editor.set_midi_edit_mode(MidiEditErase);
161 start_delta_command(_("erase notes"));
162 _mouse_state = EraseTouchDragging;
164 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
165 _mouse_state = SelectTouchDragging;
167 } else if (ev->key.keyval == GDK_Escape) {
173 case GDK_KEY_RELEASE:
174 if (ev->key.keyval == GDK_Delete) {
175 if (_mouse_state == EraseTouchDragging) {
180 trackview.editor.set_midi_edit_mode(original_mode);
185 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
191 case GDK_BUTTON_PRESS:
192 if (_mouse_state != SelectTouchDragging &&
193 _mouse_state != EraseTouchDragging &&
194 ev->button.button == 1) {
195 _pressed_button = ev->button.button;
196 _mouse_state = Pressed;
199 _pressed_button = ev->button.button;
202 case GDK_2BUTTON_PRESS:
205 case GDK_ENTER_NOTIFY:
206 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
207 Keyboard::magic_widget_grab_focus();
211 case GDK_MOTION_NOTIFY:
212 event_x = ev->motion.x;
213 event_y = ev->motion.y;
214 group->w2i(event_x, event_y);
216 // convert event_x to global frame
217 event_frame = trackview.editor.pixel_to_frame(event_x) + _region->position();
218 trackview.editor.snap_to(event_frame);
219 // convert event_frame back to local coordinates relative to position
220 event_frame -= _region->position();
222 switch (_mouse_state) {
223 case Pressed: // Drag start
226 if (_pressed_button == 1 && trackview.editor.current_midi_edit_mode() == MidiEditSelect) {
227 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
228 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
231 drag_start_x = event_x;
232 drag_start_y = event_y;
234 drag_rect = new ArdourCanvas::SimpleRect(*group);
235 drag_rect->property_x1() = event_x;
236 drag_rect->property_y1() = event_y;
237 drag_rect->property_x2() = event_x;
238 drag_rect->property_y2() = event_y;
239 drag_rect->property_outline_what() = 0xFF;
240 drag_rect->property_outline_color_rgba()
241 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
242 drag_rect->property_fill_color_rgba()
243 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
245 _mouse_state = SelectRectDragging;
248 // Add note drag start
249 } else if (trackview.editor.current_midi_edit_mode() == MidiEditPencil) {
250 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
251 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
254 drag_start_x = event_x;
255 drag_start_y = event_y;
257 drag_rect = new ArdourCanvas::SimpleRect(*group);
258 drag_rect->property_x1() = trackview.editor.frame_to_pixel(event_frame);
260 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
261 drag_rect->property_x2() = event_x;
262 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
263 drag_rect->property_outline_what() = 0xFF;
264 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
266 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
268 _mouse_state = AddDragging;
274 case SelectRectDragging: // Select drag motion
275 case AddDragging: // Add note drag motion
276 if (ev->motion.is_hint) {
279 GdkModifierType state;
280 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
285 if (_mouse_state == AddDragging)
286 event_x = trackview.editor.frame_to_pixel(event_frame);
289 if (event_x > drag_start_x)
290 drag_rect->property_x2() = event_x;
292 drag_rect->property_x1() = event_x;
295 if (drag_rect && _mouse_state == SelectRectDragging) {
296 if (event_y > drag_start_y)
297 drag_rect->property_y2() = event_y;
299 drag_rect->property_y1() = event_y;
301 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
307 case EraseTouchDragging:
308 case SelectTouchDragging:
316 case GDK_BUTTON_RELEASE:
317 event_x = ev->motion.x;
318 event_y = ev->motion.y;
319 group->w2i(event_x, event_y);
320 group->ungrab(ev->button.time);
321 event_frame = trackview.editor.pixel_to_frame(event_x);
323 if (_pressed_button != 1) {
327 switch (_mouse_state) {
328 case Pressed: // Clicked
329 switch (trackview.editor.current_midi_edit_mode()) {
335 create_note_at(event_x, event_y, _default_note_length);
340 case SelectRectDragging: // Select drag done
345 case AddDragging: // Add drag done
347 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
348 const double x = drag_rect->property_x1();
349 const double length = trackview.editor.pixel_to_frame(
350 drag_rect->property_x2() - drag_rect->property_x1());
352 create_note_at(x, drag_rect->property_y1(), length);
367 /** Add a note to the model, and the view, at a canvas (click) coordinate */
369 MidiRegionView::create_note_at(double x, double y, double duration)
371 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
372 MidiStreamView* const view = mtv->midi_view();
374 double note = midi_stream_view()->y_to_note(y);
377 assert(note <= 127.0);
379 nframes64_t new_note_time = trackview.editor.pixel_to_frame (x);
380 assert(new_note_time >= 0);
381 new_note_time += _region->start();
384 const Meter& m = trackview.session().tempo_map().meter_at(new_note_time);
385 const Tempo& t = trackview.session().tempo_map().tempo_at(new_note_time);
386 double duration = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
389 // we need to snap here again in nframes64_t in order to be sample accurate
390 // since note time is region-absolute but snap_to_frame expects position-relative
391 // time we have to coordinate transform back and forth here.
392 nframes64_t new_note_time_position_relative = new_note_time - _region->start();
393 new_note_time = snap_to_frame(new_note_time_position_relative) + _region->start();
395 // we need to snap the duration too to be sample accurate
396 nframes64_t new_note_duration = nframes_t(duration);
397 new_note_duration = snap_to_frame(new_note_time_position_relative + new_note_duration) + _region->start()
400 const boost::shared_ptr<Evoral::Note> new_note(new Evoral::Note(
401 0, new_note_time, new_note_duration, (uint8_t)note, 0x40));
402 view->update_bounds(new_note->note());
404 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
406 _model->apply_command(cmd);
411 MidiRegionView::clear_events()
416 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
417 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
422 for (std::vector<CanvasNoteEvent*>::iterator i = _events.begin(); i != _events.end(); ++i)
426 _pgm_changes.clear();
431 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
441 MidiRegionView::start_delta_command(string name)
444 _delta_command = _model->new_delta_command(name);
448 MidiRegionView::command_add_note(const boost::shared_ptr<Evoral::Note> note, bool selected)
451 _delta_command->add(note);
454 _marked_for_selection.insert(note);
458 MidiRegionView::command_remove_note(ArdourCanvas::CanvasNoteEvent* ev)
460 if (_delta_command && ev->note()) {
461 _delta_command->remove(ev->note());
466 MidiRegionView::apply_command()
468 if (!_delta_command) {
472 // Mark all selected notes for selection when model reloads
473 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
474 _marked_for_selection.insert((*i)->note());
477 _model->apply_command(_delta_command);
478 _delta_command = NULL;
479 midi_view()->midi_track()->diskstream()->playlist_modified();
481 _marked_for_selection.clear();
486 MidiRegionView::abort_command()
488 delete _delta_command;
489 _delta_command = NULL;
495 MidiRegionView::redisplay_model()
497 // Don't redisplay the model if we're currently recording and displaying that
506 /*MidiModel::Notes notes = _model->notes();
507 cerr << endl << _model->midi_source()->name() << " : redisplaying " << notes.size() << " notes:" << endl;
508 for (MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
509 cerr << "NOTE time: " << (*i)->time()
510 << " pitch: " << int((*i)->note())
511 << " duration: " << (*i)->duration()
512 << " end-time: " << (*i)->end_time()
513 << " velocity: " << int((*i)->velocity())
517 for (size_t i = 0; i < _model->n_notes(); ++i)
518 add_note(_model->note_at(i));
520 // Draw program change 'flags'
521 for (Automatable::Controls::iterator control = _model->controls().begin();
522 control != _model->controls().end(); ++control) {
523 if (control->first.type() == MidiPgmChangeAutomation) {
524 Glib::Mutex::Lock list_lock (control->second->list()->lock());
526 for (AutomationList::const_iterator event = control->second->list()->begin();
527 event != control->second->list()->end(); ++event) {
528 Evoral::ControlIterator iter(control->second->list(), (*event)->when, (*event)->value);
529 boost::shared_ptr<Evoral::Event> event(new Evoral::Event());
530 _model->control_to_midi_event(event, iter);
531 add_pgm_change(event);
537 // Is this necessary?
538 /*for (Automatable::Controls::const_iterator i = _model->controls().begin();
539 i != _model->controls().end(); ++i) {
543 boost::shared_ptr<AutomationTimeAxisView> at
544 = midi_view()->automation_child(i->second->parameter());
548 Gdk::Color col = midi_stream_view()->get_region_color();
550 boost::shared_ptr<AutomationRegionView> arv;
553 Glib::Mutex::Lock list_lock (i->second->list()->lock());
555 arv = boost::shared_ptr<AutomationRegionView>(
556 new AutomationRegionView(at->canvas_display,
557 *at.get(), _region, i->second->list(),
558 midi_stream_view()->get_samples_per_unit(), col));
561 arv->set_duration(_region->length(), this);
562 arv->init(col, true);
564 _automation_children.insert(std::make_pair(i->second->parameter(), arv));
567 _model->read_unlock();
570 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
575 MidiRegionView::~MidiRegionView ()
577 in_destructor = true;
579 RegionViewGoingAway (this); /* EMIT_SIGNAL */
588 delete _delta_command;
593 MidiRegionView::region_resized (Change what_changed)
595 RegionView::region_resized(what_changed);
597 if (what_changed & ARDOUR::PositionChanged) {
604 MidiRegionView::reset_width_dependent_items (double pixel_width)
606 RegionView::reset_width_dependent_items(pixel_width);
607 assert(_pixel_width == pixel_width);
614 MidiRegionView::set_y_position_and_height (double y, double h)
616 RegionView::set_y_position_and_height(y, h - 1);
618 /* XXX why is this code here */
622 if (_enable_display) {
626 for (std::vector<CanvasNoteEvent*>::const_iterator i = _events.begin(); i != _events.end(); ++i) {
627 CanvasNoteEvent* event = *i;
628 Item* item = dynamic_cast<Item*>(event);
630 if (event && event->note()) {
631 if (event->note()->note() < midi_stream_view()->lowest_note() ||
632 event->note()->note() > midi_stream_view()->highest_note()) {
634 if (canvas_item_visible(item)) {
638 if (!canvas_item_visible(item)) {
642 event->hide_velocity();
643 if (CanvasNote* note = dynamic_cast<CanvasNote*>(event)) {
644 const double y1 = midi_stream_view()->note_to_y(event->note()->note());
645 const double y2 = y1 + floor(midi_stream_view()->note_height());
647 note->property_y1() = y1;
648 note->property_y2() = y2;
650 if (CanvasHit* hit = dynamic_cast<CanvasHit*>(event)) {
651 double x = trackview.editor.frame_to_pixel((nframes64_t)
652 event->note()->time() - _region->start());
653 const double diamond_size = midi_stream_view()->note_height() / 2.0;
654 double y = midi_stream_view()->note_to_y(event->note()->note())
655 + ((diamond_size-2.0) / 4.0);
657 hit->set_height(diamond_size);
658 hit->move(x-hit->x1(), y-hit->y1());
661 if (event->selected()) {
662 event->show_velocity();
668 _model->read_unlock();
672 name_text->raise_to_top();
677 MidiRegionView::add_ghost (TimeAxisView& tv)
679 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
683 double unit_position = _region->position () / samples_per_unit;
684 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
685 MidiGhostRegion* ghost;
687 if (mtv && mtv->midi_view()) {
688 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group.
689 this is because it's nice to have midi notes on top of the note lines and
690 audio waveforms under it.
692 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
695 ghost = new MidiGhostRegion (tv, trackview, unit_position);
698 ghost->set_height ();
699 ghost->set_duration (_region->length() / samples_per_unit);
700 ghosts.push_back (ghost);
702 for (std::vector<CanvasNoteEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
703 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
704 ghost->add_note(note);
708 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
714 /** Begin tracking note state for successive calls to add_event
717 MidiRegionView::begin_write()
719 assert(!_active_notes);
720 _active_notes = new CanvasNote*[128];
721 for (unsigned i=0; i < 128; ++i) {
722 _active_notes[i] = NULL;
727 /** Destroy note state for add_event
730 MidiRegionView::end_write()
732 delete[] _active_notes;
733 _active_notes = NULL;
734 _marked_for_selection.clear();
738 /** Resolve an active MIDI note (while recording).
741 MidiRegionView::resolve_note(uint8_t note, double end_time)
743 if (midi_view()->note_mode() != Sustained)
746 if (_active_notes && _active_notes[note]) {
747 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes64_t)end_time);
748 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
749 _active_notes[note] = NULL;
754 /** Extend active notes to rightmost edge of region (if length is changed)
757 MidiRegionView::extend_active_notes()
759 if (!_active_notes) {
763 for (unsigned i=0; i < 128; ++i) {
764 if (_active_notes[i]) {
765 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
771 /** Add a MIDI note to the view (with duration).
773 * If in sustained mode, notes with duration 0 will be considered active
774 * notes, and resolve_note should be called when the corresponding note off
775 * event arrives, to properly display the note.
778 MidiRegionView::add_note(const boost::shared_ptr<Evoral::Note> note)
780 assert(note->time() >= 0);
781 assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
783 // dont display notes beyond the region bounds
784 if ( note->time() - _region->start() >= _region->length() ||
785 note->time() < _region->start() ||
786 note->note() < midi_stream_view()->lowest_note() ||
787 note->note() > midi_stream_view()->highest_note() ) {
791 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
793 CanvasNoteEvent* event = 0;
795 const double x = trackview.editor.frame_to_pixel((nframes64_t)note->time() - _region->start());
797 if (midi_view()->note_mode() == Sustained) {
799 const double y1 = midi_stream_view()->note_to_y(note->note());
800 const double note_endpixel =
801 trackview.editor.frame_to_pixel((nframes64_t)note->end_time() - _region->start());
803 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
804 ev_rect->property_x1() = x;
805 ev_rect->property_y1() = y1;
806 if (note->duration() > 0)
807 ev_rect->property_x2() = note_endpixel;
809 ev_rect->property_x2() = trackview.editor.frame_to_pixel(_region->length());
810 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
812 if (note->duration() == 0) {
815 assert(note->note() < 128);
816 // If this note is already active there's a stuck note,
817 // finish the old note rectangle
818 if (_active_notes[note->note()]) {
819 CanvasNote* const old_rect = _active_notes[note->note()];
820 boost::shared_ptr<Evoral::Note> old_note = old_rect->note();
821 cerr << "MidiModel: WARNING: Note has duration 0: chan " << old_note->channel()
822 << "note " << (int)old_note->note() << " @ " << old_note->time() << endl;
823 /* FIXME: How large to make it? Make it a diamond? */
824 old_rect->property_x2() = old_rect->property_x1() + 2.0;
825 old_rect->property_outline_what() = (guint32) 0xF;
827 _active_notes[note->note()] = ev_rect;
829 /* outline all but right edge */
830 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
832 /* outline all edges */
833 ev_rect->property_outline_what() = (guint32) 0xF;
837 _events.push_back(ev_rect);
842 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
843 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
844 gr->add_note(ev_rect);
848 } else if (midi_view()->note_mode() == Percussive) {
850 //cerr << "MRV::add_note percussive " << note->note() << " @ " << note->time()
851 // << " .. " << note->end_time() << endl;
853 const double diamond_size = midi_stream_view()->note_height() / 2.0;
854 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
856 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size, note);
857 ev_diamond->move(x, y);
859 _events.push_back(ev_diamond);
866 if (_marked_for_selection.find(note) != _marked_for_selection.end()) {
867 note_selected(event, true);
869 event->on_channel_selection_change(_last_channel_selection);
874 MidiRegionView::add_pgm_change(boost::shared_ptr<Evoral::Event> event)
876 assert(event->time() >= 0);
878 // dont display notes beyond the region bounds
879 if (event->time() - _region->start() >= _region->length() || event->time() < _region->start())
882 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
883 const double x = trackview.editor.frame_to_pixel((nframes64_t)event->time() - _region->start());
885 double height = midi_stream_view()->contents_height();
886 _pgm_changes.push_back(
887 boost::shared_ptr<CanvasProgramChange>(
888 new CanvasProgramChange(*this, *group, event, height, x, 1.0)));
892 MidiRegionView::delete_selection()
894 assert(_delta_command);
896 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
897 if ((*i)->selected()) {
898 _delta_command->remove((*i)->note());
906 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev)
908 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
909 if ((*i)->selected() && (*i) != ev) {
910 (*i)->selected(false);
918 MidiRegionView::unique_select(ArdourCanvas::CanvasNoteEvent* ev)
920 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
922 (*i)->selected(false);
927 _selection.insert(ev);
929 if ( ! ev->selected()) {
935 MidiRegionView::note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
938 clear_selection_except(ev);
941 _selection.insert(ev);
943 if ( ! ev->selected()) {
950 MidiRegionView::note_deselected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
953 clear_selection_except(ev);
956 _selection.erase(ev);
958 if (ev->selected()) {
965 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
967 const double last_y = std::min(y1, y2);
968 const double y = std::max(y1, y2);
970 // TODO: Make this faster by storing the last updated selection rect, and only
971 // adjusting things that are in the area that appears/disappeared.
972 // We probably need a tree to be able to find events in O(log(n)) time.
975 double last_x1 = 0.0;
979 for (std::vector<CanvasNoteEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
981 // Events should always be sorted by increasing x1() here
982 assert((*i)->x1() >= last_x1);
983 last_x1 = (*i)->x1();
986 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
987 if (!(*i)->selected()) {
988 (*i)->selected(true);
989 _selection.insert(*i);
991 // Not inside rectangle
992 } else if ((*i)->selected()) {
993 (*i)->selected(false);
994 _selection.erase(*i);
998 for (std::vector<CanvasNoteEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
1000 // Events should always be sorted by increasing x1() here
1001 assert((*i)->x1() >= last_x1);
1002 last_x1 = (*i)->x1();
1005 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1006 if (!(*i)->selected()) {
1007 (*i)->selected(true);
1008 _selection.insert(*i);
1010 // Not inside rectangle
1011 } else if ((*i)->selected()) {
1012 (*i)->selected(false);
1013 _selection.erase(*i);
1021 MidiRegionView::move_selection(double dx, double dy)
1023 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
1024 (*i)->move_event(dx, dy);
1029 MidiRegionView::note_dropped(CanvasNoteEvent* ev, double dt, uint8_t dnote)
1031 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
1032 if (_selection.find(ev) != _selection.end()) {
1033 uint8_t lowest_note_in_selection = midi_stream_view()->lowest_note();
1034 uint8_t highest_note_in_selection = midi_stream_view()->highest_note();
1035 uint8_t highest_note_difference = 0;
1037 // find highest and lowest notes first
1038 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1039 uint8_t pitch = (*i)->note()->note();
1040 lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
1041 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
1045 cerr << "dnote: " << (int) dnote << endl;
1046 cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
1047 << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
1048 cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
1049 << int(highest_note_in_selection) << endl;
1050 cerr << "selection size: " << _selection.size() << endl;
1051 cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
1054 // Make sure the note pitch does not exceed the MIDI standard range
1055 if (dnote <= 127 && (highest_note_in_selection + dnote > 127)) {
1056 highest_note_difference = highest_note_in_selection - 127;
1059 start_delta_command(_("move notes"));
1061 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
1062 Selection::iterator next = i;
1065 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(*i)->note().get()));
1067 // we need to snap here again in nframes64_t in order to be sample accurate
1068 double new_note_time = (*i)->note()->time();
1069 new_note_time += dt;
1071 // keep notes inside region if dragged beyond left region bound
1072 if (new_note_time < _region->start()) {
1073 new_note_time = _region->start();
1076 // since note time is region-absolute but snap_to_frame expects position-relative
1077 // time we have to coordinate transform back and forth here.
1078 new_note_time = snap_to_frame(nframes64_t(new_note_time) - _region->start()) + _region->start();
1080 copy->set_time(new_note_time);
1082 uint8_t original_pitch = (*i)->note()->note();
1083 uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
1085 // keep notes in standard midi range
1086 clamp_0_to_127(new_pitch);
1088 //notes which are dragged beyond the standard midi range snap back to their original place
1089 if ((original_pitch != 0 && new_pitch == 0) || (original_pitch != 127 && new_pitch == 127)) {
1090 new_pitch = original_pitch;
1093 lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
1094 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
1096 copy->set_note(new_pitch);
1098 command_remove_note(*i);
1099 command_add_note(copy, true);
1106 // care about notes being moved beyond the upper/lower bounds on the canvas
1107 if (lowest_note_in_selection < midi_stream_view()->lowest_note() ||
1108 highest_note_in_selection > midi_stream_view()->highest_note()) {
1109 midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
1115 MidiRegionView::snap_to_frame(double x)
1117 PublicEditor &editor = trackview.editor;
1118 // x is region relative
1119 // convert x to global frame
1120 nframes64_t frame = editor.pixel_to_frame(x) + _region->position();
1121 editor.snap_to(frame);
1122 // convert event_frame back to local coordinates relative to position
1123 frame -= _region->position();
1128 MidiRegionView::snap_to_frame(nframes64_t x)
1130 PublicEditor &editor = trackview.editor;
1131 // x is region relative
1132 // convert x to global frame
1133 nframes64_t frame = x + _region->position();
1134 editor.snap_to(frame);
1135 // convert event_frame back to local coordinates relative to position
1136 frame -= _region->position();
1141 MidiRegionView::snap_to_pixel(double x)
1143 return (double) trackview.editor.frame_to_pixel(snap_to_frame(x));
1147 MidiRegionView::get_position_pixels(void)
1149 nframes64_t region_frame = get_position();
1150 return trackview.editor.frame_to_pixel(region_frame);
1154 MidiRegionView::begin_resizing(CanvasNote::NoteEnd note_end)
1156 _resize_data.clear();
1158 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1159 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
1161 // only insert CanvasNotes into the map
1163 NoteResizeData *resize_data = new NoteResizeData();
1164 resize_data->canvas_note = note;
1166 // create a new SimpleRect from the note which will be the resize preview
1167 SimpleRect *resize_rect =
1175 // calculate the colors: get the color settings
1176 uint32_t fill_color =
1178 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get(),
1181 // make the resize preview notes more transparent and bright
1182 fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
1184 // calculate color based on note velocity
1185 resize_rect->property_fill_color_rgba() =
1187 note_fill_color(note->note()->velocity()),
1191 resize_rect->property_outline_color_rgba() =
1192 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get();
1194 resize_data->resize_rect = resize_rect;
1196 if (note_end == CanvasNote::NOTE_ON) {
1197 resize_data->current_x = note->x1();
1198 } else { // NOTE_OFF
1199 resize_data->current_x = note->x2();
1202 _resize_data.push_back(resize_data);
1208 MidiRegionView::update_resizing(CanvasNote::NoteEnd note_end, double x, bool relative)
1210 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1211 SimpleRect *resize_rect = (*i)->resize_rect;
1212 CanvasNote *canvas_note = (*i)->canvas_note;
1214 const double region_start = get_position_pixels();
1217 (*i)->current_x = (*i)->current_x + x;
1219 // x is in track relative, transform it to region relative
1220 (*i)->current_x = x - region_start;
1223 double current_x = (*i)->current_x;
1225 if (note_end == CanvasNote::NOTE_ON) {
1226 resize_rect->property_x1() = snap_to_pixel(current_x);
1227 resize_rect->property_x2() = canvas_note->x2();
1229 resize_rect->property_x2() = snap_to_pixel(current_x);
1230 resize_rect->property_x1() = canvas_note->x1();
1236 MidiRegionView::commit_resizing(CanvasNote::NoteEnd note_end, double event_x, bool relative)
1238 start_delta_command(_("resize notes"));
1240 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1241 CanvasNote* canvas_note = (*i)->canvas_note;
1242 SimpleRect* resize_rect = (*i)->resize_rect;
1243 double current_x = (*i)->current_x;
1244 const double position = get_position_pixels();
1247 // event_x is in track relative, transform it to region relative
1248 current_x = event_x - position;
1251 // because snapping works on world coordinates we have to transform current_x
1252 // to world coordinates before snapping and transform it back afterwards
1253 nframes64_t current_frame = snap_to_frame(current_x);
1254 // transform to region start relative
1255 current_frame += _region->start();
1257 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(canvas_note->note().get())));
1259 // resize beginning of note
1260 if (note_end == CanvasNote::NOTE_ON && current_frame < copy->end_time()) {
1261 command_remove_note(canvas_note);
1262 copy->on_event().time() = current_frame;
1263 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1265 // resize end of note
1266 if (note_end == CanvasNote::NOTE_OFF && current_frame > copy->time()) {
1267 command_remove_note(canvas_note);
1268 copy->off_event().time() = current_frame;
1269 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1276 _resize_data.clear();
1282 MidiRegionView::change_velocity(uint8_t velocity, bool relative)
1284 start_delta_command(_("change velocity"));
1285 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1286 Selection::iterator next = i;
1289 CanvasNoteEvent *event = *i;
1290 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(event->note().get())));
1293 uint8_t new_velocity = copy->velocity() + velocity;
1294 clamp_0_to_127(new_velocity);
1296 copy->set_velocity(new_velocity);
1297 } else { // absolute
1298 copy->set_velocity(velocity);
1301 command_remove_note(event);
1302 command_add_note(copy, true);
1311 MidiRegionView::change_channel(uint8_t channel)
1313 start_delta_command(_("change channel"));
1314 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1315 Selection::iterator next = i;
1318 CanvasNoteEvent *event = *i;
1319 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(event->note().get())));
1321 copy->set_channel(channel);
1323 command_remove_note(event);
1324 command_add_note(copy, true);
1334 MidiRegionView::note_entered(ArdourCanvas::CanvasNoteEvent* ev)
1336 if (ev->note() && _mouse_state == EraseTouchDragging) {
1337 start_delta_command(_("note entered"));
1339 _delta_command->remove(ev->note());
1340 } else if (_mouse_state == SelectTouchDragging) {
1341 note_selected(ev, true);
1347 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
1349 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
1351 display_model(msrc->model());
1355 MidiRegionView::set_frame_color()
1358 if (_selected && should_show_selection) {
1359 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
1361 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
1367 MidiRegionView::midi_channel_mode_changed(ChannelMode mode, uint16_t mask)
1371 case FilterChannels:
1372 _force_channel = -1;
1375 _force_channel = mask;
1376 mask = 0xFFFF; // Show all notes as active (below)
1379 // Update notes for selection
1380 for (std::vector<ArdourCanvas::CanvasNoteEvent*>::iterator i = _events.begin();
1381 i != _events.end(); ++i) {
1382 (*i)->on_channel_selection_change(mask);
1385 _last_channel_selection = mask;