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)
68 , _current_range_min(0)
69 , _current_range_max(0)
71 , _note_group(new ArdourCanvas::Group(*parent))
72 , _delta_command(NULL)
76 _note_group->raise_to_top();
79 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
80 : RegionView (parent, tv, r, spu, basic_color, false, visibility)
82 , _last_channel_selection(0xFFFF)
83 , _default_note_length(0.0)
85 , _note_group(new ArdourCanvas::Group(*parent))
86 , _delta_command(NULL)
91 _note_group->raise_to_top();
95 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
98 midi_region()->midi_source(0)->load_model();
100 const Meter& m = trackview.session().tempo_map().meter_at(_region->position());
101 const Tempo& t = trackview.session().tempo_map().tempo_at(_region->position());
102 _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
105 _model = midi_region()->midi_source(0)->model();
106 _enable_display = false;
108 RegionView::init (basic_color, false);
110 compute_colors (basic_color);
112 set_height (trackview.current_height());
115 region_sync_changed ();
116 region_resized (BoundsChanged);
119 reset_width_dependent_items (_pixel_width);
120 //reset_width_dependent_items ((double) _region->length() / samples_per_unit);
124 _enable_display = true;
129 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
132 group->raise_to_top();
133 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
135 midi_view()->signal_channel_mode_changed().connect(
136 mem_fun(this, &MidiRegionView::midi_channel_mode_changed));
140 MidiRegionView::canvas_event(GdkEvent* ev)
142 static bool delete_mod = false;
143 static Editing::MidiEditMode original_mode;
145 static double drag_start_x, drag_start_y;
146 static double last_x, last_y;
147 double event_x, event_y;
148 nframes64_t event_frame = 0;
150 static ArdourCanvas::SimpleRect* drag_rect = NULL;
152 if (trackview.editor.current_mouse_mode() != MouseNote)
159 if (ev->key.keyval == GDK_Delete && !delete_mod) {
161 original_mode = trackview.editor.current_midi_edit_mode();
162 trackview.editor.set_midi_edit_mode(MidiEditErase);
163 start_delta_command(_("erase notes"));
164 _mouse_state = EraseTouchDragging;
166 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
167 _mouse_state = SelectTouchDragging;
169 } else if (ev->key.keyval == GDK_Escape) {
175 case GDK_KEY_RELEASE:
176 if (ev->key.keyval == GDK_Delete) {
177 if (_mouse_state == EraseTouchDragging) {
182 trackview.editor.set_midi_edit_mode(original_mode);
187 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
193 case GDK_BUTTON_PRESS:
194 if (_mouse_state != SelectTouchDragging &&
195 _mouse_state != EraseTouchDragging &&
196 ev->button.button == 1) {
197 _pressed_button = ev->button.button;
198 _mouse_state = Pressed;
201 _pressed_button = ev->button.button;
204 case GDK_2BUTTON_PRESS:
207 case GDK_ENTER_NOTIFY:
208 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
209 Keyboard::magic_widget_grab_focus();
213 case GDK_MOTION_NOTIFY:
214 event_x = ev->motion.x;
215 event_y = ev->motion.y;
216 group->w2i(event_x, event_y);
218 // convert event_x to global frame
219 event_frame = trackview.editor.pixel_to_frame(event_x) + _region->position();
220 trackview.editor.snap_to(event_frame);
221 // convert event_frame back to local coordinates relative to position
222 event_frame -= _region->position();
224 switch (_mouse_state) {
225 case Pressed: // Drag start
228 if (_pressed_button == 1 && trackview.editor.current_midi_edit_mode() == MidiEditSelect) {
229 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
230 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
233 drag_start_x = event_x;
234 drag_start_y = event_y;
236 drag_rect = new ArdourCanvas::SimpleRect(*group);
237 drag_rect->property_x1() = event_x;
238 drag_rect->property_y1() = event_y;
239 drag_rect->property_x2() = event_x;
240 drag_rect->property_y2() = event_y;
241 drag_rect->property_outline_what() = 0xFF;
242 drag_rect->property_outline_color_rgba()
243 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
244 drag_rect->property_fill_color_rgba()
245 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
247 _mouse_state = SelectRectDragging;
250 // Add note drag start
251 } else if (trackview.editor.current_midi_edit_mode() == MidiEditPencil) {
252 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
253 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
256 drag_start_x = event_x;
257 drag_start_y = event_y;
259 drag_rect = new ArdourCanvas::SimpleRect(*group);
260 drag_rect->property_x1() = trackview.editor.frame_to_pixel(event_frame);
262 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
263 drag_rect->property_x2() = event_x;
264 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
265 drag_rect->property_outline_what() = 0xFF;
266 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
268 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
270 _mouse_state = AddDragging;
276 case SelectRectDragging: // Select drag motion
277 case AddDragging: // Add note drag motion
278 if (ev->motion.is_hint) {
281 GdkModifierType state;
282 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
287 if (_mouse_state == AddDragging)
288 event_x = trackview.editor.frame_to_pixel(event_frame);
291 if (event_x > drag_start_x)
292 drag_rect->property_x2() = event_x;
294 drag_rect->property_x1() = event_x;
297 if (drag_rect && _mouse_state == SelectRectDragging) {
298 if (event_y > drag_start_y)
299 drag_rect->property_y2() = event_y;
301 drag_rect->property_y1() = event_y;
303 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
309 case EraseTouchDragging:
310 case SelectTouchDragging:
318 case GDK_BUTTON_RELEASE:
319 event_x = ev->motion.x;
320 event_y = ev->motion.y;
321 group->w2i(event_x, event_y);
322 group->ungrab(ev->button.time);
323 event_frame = trackview.editor.pixel_to_frame(event_x);
325 if (_pressed_button != 1) {
329 switch (_mouse_state) {
330 case Pressed: // Clicked
331 switch (trackview.editor.current_midi_edit_mode()) {
337 create_note_at(event_x, event_y, _default_note_length);
342 case SelectRectDragging: // Select drag done
347 case AddDragging: // Add drag done
349 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
350 const double x = drag_rect->property_x1();
351 const double length = trackview.editor.pixel_to_frame(
352 drag_rect->property_x2() - drag_rect->property_x1());
354 create_note_at(x, drag_rect->property_y1(), length);
369 /** Add a note to the model, and the view, at a canvas (click) coordinate */
371 MidiRegionView::create_note_at(double x, double y, double length)
373 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
374 MidiStreamView* const view = mtv->midi_view();
376 double note = midi_stream_view()->y_to_note(y);
379 assert(note <= 127.0);
381 nframes64_t new_note_time = trackview.editor.pixel_to_frame (x);
382 assert(new_note_time >= 0);
383 new_note_time += _region->start();
386 const Meter& m = trackview.session().tempo_map().meter_at(new_note_time);
387 const Tempo& t = trackview.session().tempo_map().tempo_at(new_note_time);
388 double length = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
391 // we need to snap here again in nframes64_t in order to be sample accurate
392 // since note time is region-absolute but snap_to_frame expects position-relative
393 // time we have to coordinate transform back and forth here.
394 nframes64_t new_note_time_position_relative = new_note_time - _region->start();
395 new_note_time = snap_to_frame(new_note_time_position_relative) + _region->start();
397 // we need to snap the length too to be sample accurate
398 nframes64_t new_note_length = nframes_t(length);
399 new_note_length = snap_to_frame(new_note_time_position_relative + new_note_length) + _region->start()
402 const boost::shared_ptr<Evoral::Note> new_note(new Evoral::Note(
403 0, new_note_time, new_note_length, (uint8_t)note, 0x40));
404 view->update_note_range(new_note->note());
406 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
408 _model->apply_command(trackview.session(), cmd);
413 MidiRegionView::clear_events()
418 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
419 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
424 for (Events::iterator i = _events.begin(); i != _events.end(); ++i)
428 _pgm_changes.clear();
433 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
443 MidiRegionView::start_delta_command(string name)
446 _delta_command = _model->new_delta_command(name);
450 MidiRegionView::command_add_note(const boost::shared_ptr<Evoral::Note> note, bool selected)
453 _delta_command->add(note);
456 _marked_for_selection.insert(note);
460 MidiRegionView::command_remove_note(ArdourCanvas::CanvasNoteEvent* ev)
462 if (_delta_command && ev->note()) {
463 _delta_command->remove(ev->note());
468 MidiRegionView::apply_command()
470 if (!_delta_command) {
474 // Mark all selected notes for selection when model reloads
475 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
476 _marked_for_selection.insert((*i)->note());
479 _model->apply_command(trackview.session(), _delta_command);
480 _delta_command = NULL;
481 midi_view()->midi_track()->diskstream()->playlist_modified();
483 _marked_for_selection.clear();
488 MidiRegionView::abort_command()
490 delete _delta_command;
491 _delta_command = NULL;
497 MidiRegionView::redisplay_model()
499 // Don't redisplay the model if we're currently recording and displaying that
508 /*MidiModel::Notes notes = _model->notes();
509 cerr << endl << _model->midi_source()->name() << " : redisplaying " << notes.size() << " notes:" << endl;
510 for (MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
511 cerr << "NOTE time: " << (*i)->time()
512 << " pitch: " << int((*i)->note())
513 << " length: " << (*i)->length()
514 << " end-time: " << (*i)->end_time()
515 << " velocity: " << int((*i)->velocity())
519 for (size_t i = 0; i < _model->n_notes(); ++i)
520 add_note(_model->note_at(i));
522 // Draw program change 'flags'
523 for (Automatable::Controls::iterator control = _model->controls().begin();
524 control != _model->controls().end(); ++control) {
525 if (control->first.type() == MidiPgmChangeAutomation) {
526 Glib::Mutex::Lock list_lock (control->second->list()->lock());
528 for (AutomationList::const_iterator event = control->second->list()->begin();
529 event != control->second->list()->end(); ++event) {
530 Evoral::ControlIterator iter(control->second->list(), (*event)->when, (*event)->value);
531 boost::shared_ptr<Evoral::Event> event(new Evoral::Event());
532 _model->control_to_midi_event(event, iter);
533 add_pgm_change(event);
539 // Is this necessary?
540 /*for (Automatable::Controls::const_iterator i = _model->controls().begin();
541 i != _model->controls().end(); ++i) {
545 boost::shared_ptr<AutomationTimeAxisView> at
546 = midi_view()->automation_child(i->second->parameter());
550 Gdk::Color col = midi_stream_view()->get_region_color();
552 boost::shared_ptr<AutomationRegionView> arv;
555 Glib::Mutex::Lock list_lock (i->second->list()->lock());
557 arv = boost::shared_ptr<AutomationRegionView>(
558 new AutomationRegionView(at->canvas_display,
559 *at.get(), _region, i->second->list(),
560 midi_stream_view()->get_samples_per_unit(), col));
563 arv->set_duration(_region->length(), this);
564 arv->init(col, true);
566 _automation_children.insert(std::make_pair(i->second->parameter(), arv));
569 _model->read_unlock();
572 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
577 MidiRegionView::~MidiRegionView ()
579 in_destructor = true;
581 RegionViewGoingAway (this); /* EMIT_SIGNAL */
590 delete _delta_command;
595 MidiRegionView::region_resized (Change what_changed)
597 RegionView::region_resized(what_changed);
599 if (what_changed & ARDOUR::PositionChanged) {
606 MidiRegionView::reset_width_dependent_items (double pixel_width)
608 RegionView::reset_width_dependent_items(pixel_width);
609 assert(_pixel_width == pixel_width);
616 MidiRegionView::set_height (gdouble height)
618 static const double FUDGE = 2;
619 const double old_height = _height;
620 RegionView::set_height(height);
621 _height = height - FUDGE;
623 apply_note_range(midi_stream_view()->lowest_note(),
624 midi_stream_view()->highest_note(),
625 height != old_height + FUDGE);
628 name_text->raise_to_top();
633 /** Apply the current note range from the stream view
634 * by repositioning/hiding notes as necessary
637 MidiRegionView::apply_note_range (uint8_t min, uint8_t max, bool force)
639 if (_enable_display) {
640 if (!force && _current_range_min == min && _current_range_max == max) {
644 _current_range_min = min;
645 _current_range_max = max;
647 for (Events::const_iterator i = _events.begin(); i != _events.end(); ++i) {
648 CanvasNoteEvent* event = *i;
649 Item* item = dynamic_cast<Item*>(event);
651 if (event && event->note()) {
652 if (event->note()->note() < _current_range_min || event->note()->note() > _current_range_max) {
653 if (canvas_item_visible(item)) {
657 if (!canvas_item_visible(item)) {
661 event->hide_velocity();
662 if (CanvasNote* note = dynamic_cast<CanvasNote*>(event)) {
663 const double y1 = midi_stream_view()->note_to_y(event->note()->note());
664 const double y2 = y1 + floor(midi_stream_view()->note_height());
666 note->property_y1() = y1;
667 note->property_y2() = y2;
668 } else if (CanvasHit* hit = dynamic_cast<CanvasHit*>(event)) {
669 double x = trackview.editor.frame_to_pixel((nframes64_t)
670 event->note()->time() - _region->start());
671 const double diamond_size = midi_stream_view()->note_height() / 2.0;
672 double y = midi_stream_view()->note_to_y(event->note()->note())
673 + ((diamond_size-2.0) / 4.0);
675 hit->set_height(diamond_size);
676 hit->move(x-hit->x1(), y-hit->y1());
679 if (event->selected()) {
680 event->show_velocity();
690 MidiRegionView::add_ghost (TimeAxisView& tv)
692 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
696 double unit_position = _region->position () / samples_per_unit;
697 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
698 MidiGhostRegion* ghost;
700 if (mtv && mtv->midi_view()) {
701 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group.
702 this is because it's nice to have midi notes on top of the note lines and
703 audio waveforms under it.
705 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
708 ghost = new MidiGhostRegion (tv, trackview, unit_position);
711 ghost->set_height ();
712 ghost->set_duration (_region->length() / samples_per_unit);
713 ghosts.push_back (ghost);
715 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
716 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
717 ghost->add_note(note);
721 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
727 /** Begin tracking note state for successive calls to add_event
730 MidiRegionView::begin_write()
732 assert(!_active_notes);
733 _active_notes = new CanvasNote*[128];
734 for (unsigned i=0; i < 128; ++i) {
735 _active_notes[i] = NULL;
740 /** Destroy note state for add_event
743 MidiRegionView::end_write()
745 delete[] _active_notes;
746 _active_notes = NULL;
747 _marked_for_selection.clear();
751 /** Resolve an active MIDI note (while recording).
754 MidiRegionView::resolve_note(uint8_t note, double end_time)
756 if (midi_view()->note_mode() != Sustained)
759 if (_active_notes && _active_notes[note]) {
760 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes64_t)end_time);
761 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
762 _active_notes[note] = NULL;
767 /** Extend active notes to rightmost edge of region (if length is changed)
770 MidiRegionView::extend_active_notes()
772 if (!_active_notes) {
776 for (unsigned i=0; i < 128; ++i) {
777 if (_active_notes[i]) {
778 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
784 /** Add a MIDI note to the view (with length).
786 * If in sustained mode, notes with length 0 will be considered active
787 * notes, and resolve_note should be called when the corresponding note off
788 * event arrives, to properly display the note.
791 MidiRegionView::add_note(const boost::shared_ptr<Evoral::Note> note)
793 assert(note->time() >= 0);
794 assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
796 // dont display notes beyond the region bounds
797 if ( note->time() - _region->start() >= _region->length() ||
798 note->time() < _region->start() ||
799 note->note() < midi_stream_view()->lowest_note() ||
800 note->note() > midi_stream_view()->highest_note() ) {
804 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
806 CanvasNoteEvent* event = 0;
808 const double x = trackview.editor.frame_to_pixel((nframes64_t)note->time() - _region->start());
810 if (midi_view()->note_mode() == Sustained) {
812 const double y1 = midi_stream_view()->note_to_y(note->note());
813 const double note_endpixel =
814 trackview.editor.frame_to_pixel((nframes64_t)note->end_time() - _region->start());
816 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
817 ev_rect->property_x1() = x;
818 ev_rect->property_y1() = y1;
819 if (note->length() > 0)
820 ev_rect->property_x2() = note_endpixel;
822 ev_rect->property_x2() = trackview.editor.frame_to_pixel(_region->length());
823 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
825 if (note->length() == 0) {
828 assert(note->note() < 128);
829 // If this note is already active there's a stuck note,
830 // finish the old note rectangle
831 if (_active_notes[note->note()]) {
832 CanvasNote* const old_rect = _active_notes[note->note()];
833 boost::shared_ptr<Evoral::Note> old_note = old_rect->note();
834 cerr << "MidiModel: WARNING: Note has length 0: chan " << old_note->channel()
835 << "note " << (int)old_note->note() << " @ " << old_note->time() << endl;
836 /* FIXME: How large to make it? Make it a diamond? */
837 old_rect->property_x2() = old_rect->property_x1() + 2.0;
838 old_rect->property_outline_what() = (guint32) 0xF;
840 _active_notes[note->note()] = ev_rect;
842 /* outline all but right edge */
843 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
845 /* outline all edges */
846 ev_rect->property_outline_what() = (guint32) 0xF;
850 _events.push_back(ev_rect);
855 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
856 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
857 gr->add_note(ev_rect);
861 } else if (midi_view()->note_mode() == Percussive) {
863 //cerr << "MRV::add_note percussive " << note->note() << " @ " << note->time()
864 // << " .. " << note->end_time() << endl;
866 const double diamond_size = midi_stream_view()->note_height() / 2.0;
867 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
869 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size, note);
870 ev_diamond->move(x, y);
872 _events.push_back(ev_diamond);
879 if (_marked_for_selection.find(note) != _marked_for_selection.end()) {
880 note_selected(event, true);
882 event->on_channel_selection_change(_last_channel_selection);
887 MidiRegionView::add_pgm_change(boost::shared_ptr<Evoral::Event> event)
889 assert(event->time() >= 0);
891 // dont display notes beyond the region bounds
892 if (event->time() - _region->start() >= _region->length() || event->time() < _region->start())
895 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
896 const double x = trackview.editor.frame_to_pixel((nframes64_t)event->time() - _region->start());
898 double height = midi_stream_view()->contents_height();
899 _pgm_changes.push_back(
900 boost::shared_ptr<CanvasProgramChange>(
901 new CanvasProgramChange(*this, *group, event, height, x, 1.0)));
905 MidiRegionView::delete_selection()
907 assert(_delta_command);
909 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
910 if ((*i)->selected()) {
911 _delta_command->remove((*i)->note());
919 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev)
921 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
922 if ((*i)->selected() && (*i) != ev) {
923 (*i)->selected(false);
931 MidiRegionView::unique_select(ArdourCanvas::CanvasNoteEvent* ev)
933 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
935 (*i)->selected(false);
940 _selection.insert(ev);
942 if ( ! ev->selected()) {
948 MidiRegionView::note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
951 clear_selection_except(ev);
954 _selection.insert(ev);
956 if ( ! ev->selected()) {
963 MidiRegionView::note_deselected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
966 clear_selection_except(ev);
969 _selection.erase(ev);
971 if (ev->selected()) {
978 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
980 const double last_y = std::min(y1, y2);
981 const double y = std::max(y1, y2);
983 // TODO: Make this faster by storing the last updated selection rect, and only
984 // adjusting things that are in the area that appears/disappeared.
985 // We probably need a tree to be able to find events in O(log(n)) time.
988 double last_x1 = 0.0;
992 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
994 // Events should always be sorted by increasing x1() here
995 assert((*i)->x1() >= last_x1);
996 last_x1 = (*i)->x1();
999 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1000 if (!(*i)->selected()) {
1001 (*i)->selected(true);
1002 _selection.insert(*i);
1004 // Not inside rectangle
1005 } else if ((*i)->selected()) {
1006 (*i)->selected(false);
1007 _selection.erase(*i);
1011 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1013 // Events should always be sorted by increasing x1() here
1014 assert((*i)->x1() >= last_x1);
1015 last_x1 = (*i)->x1();
1018 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1019 if (!(*i)->selected()) {
1020 (*i)->selected(true);
1021 _selection.insert(*i);
1023 // Not inside rectangle
1024 } else if ((*i)->selected()) {
1025 (*i)->selected(false);
1026 _selection.erase(*i);
1034 MidiRegionView::move_selection(double dx, double dy)
1036 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
1037 (*i)->move_event(dx, dy);
1042 MidiRegionView::note_dropped(CanvasNoteEvent* ev, double dt, uint8_t dnote)
1044 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
1045 if (_selection.find(ev) != _selection.end()) {
1046 uint8_t lowest_note_in_selection = midi_stream_view()->lowest_note();
1047 uint8_t highest_note_in_selection = midi_stream_view()->highest_note();
1048 uint8_t highest_note_difference = 0;
1050 // find highest and lowest notes first
1051 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1052 uint8_t pitch = (*i)->note()->note();
1053 lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
1054 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
1058 cerr << "dnote: " << (int) dnote << endl;
1059 cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
1060 << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
1061 cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
1062 << int(highest_note_in_selection) << endl;
1063 cerr << "selection size: " << _selection.size() << endl;
1064 cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
1067 // Make sure the note pitch does not exceed the MIDI standard range
1068 if (dnote <= 127 && (highest_note_in_selection + dnote > 127)) {
1069 highest_note_difference = highest_note_in_selection - 127;
1072 start_delta_command(_("move notes"));
1074 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
1075 Selection::iterator next = i;
1078 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(*i)->note().get()));
1080 // we need to snap here again in nframes64_t in order to be sample accurate
1081 double new_note_time = (*i)->note()->time();
1082 new_note_time += dt;
1084 // keep notes inside region if dragged beyond left region bound
1085 if (new_note_time < _region->start()) {
1086 new_note_time = _region->start();
1089 // since note time is region-absolute but snap_to_frame expects position-relative
1090 // time we have to coordinate transform back and forth here.
1091 new_note_time = snap_to_frame(nframes64_t(new_note_time) - _region->start()) + _region->start();
1093 copy->set_time(new_note_time);
1095 uint8_t original_pitch = (*i)->note()->note();
1096 uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
1098 // keep notes in standard midi range
1099 clamp_0_to_127(new_pitch);
1101 //notes which are dragged beyond the standard midi range snap back to their original place
1102 if ((original_pitch != 0 && new_pitch == 0) || (original_pitch != 127 && new_pitch == 127)) {
1103 new_pitch = original_pitch;
1106 lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
1107 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
1109 copy->set_note(new_pitch);
1111 command_remove_note(*i);
1112 command_add_note(copy, true);
1119 // care about notes being moved beyond the upper/lower bounds on the canvas
1120 if (lowest_note_in_selection < midi_stream_view()->lowest_note() ||
1121 highest_note_in_selection > midi_stream_view()->highest_note()) {
1122 midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
1128 MidiRegionView::snap_to_frame(double x)
1130 PublicEditor &editor = trackview.editor;
1131 // x is region relative
1132 // convert x to global frame
1133 nframes64_t frame = editor.pixel_to_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_frame(nframes64_t x)
1143 PublicEditor &editor = trackview.editor;
1144 // x is region relative
1145 // convert x to global frame
1146 nframes64_t frame = x + _region->position();
1147 editor.snap_to(frame);
1148 // convert event_frame back to local coordinates relative to position
1149 frame -= _region->position();
1154 MidiRegionView::snap_to_pixel(double x)
1156 return (double) trackview.editor.frame_to_pixel(snap_to_frame(x));
1160 MidiRegionView::get_position_pixels(void)
1162 nframes64_t region_frame = get_position();
1163 return trackview.editor.frame_to_pixel(region_frame);
1167 MidiRegionView::begin_resizing(CanvasNote::NoteEnd note_end)
1169 _resize_data.clear();
1171 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1172 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
1174 // only insert CanvasNotes into the map
1176 NoteResizeData *resize_data = new NoteResizeData();
1177 resize_data->canvas_note = note;
1179 // create a new SimpleRect from the note which will be the resize preview
1180 SimpleRect *resize_rect =
1188 // calculate the colors: get the color settings
1189 uint32_t fill_color =
1191 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get(),
1194 // make the resize preview notes more transparent and bright
1195 fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
1197 // calculate color based on note velocity
1198 resize_rect->property_fill_color_rgba() =
1200 note_fill_color(note->note()->velocity()),
1204 resize_rect->property_outline_color_rgba() =
1205 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get();
1207 resize_data->resize_rect = resize_rect;
1209 if (note_end == CanvasNote::NOTE_ON) {
1210 resize_data->current_x = note->x1();
1211 } else { // NOTE_OFF
1212 resize_data->current_x = note->x2();
1215 _resize_data.push_back(resize_data);
1221 MidiRegionView::update_resizing(CanvasNote::NoteEnd note_end, double x, bool relative)
1223 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1224 SimpleRect *resize_rect = (*i)->resize_rect;
1225 CanvasNote *canvas_note = (*i)->canvas_note;
1227 const double region_start = get_position_pixels();
1230 (*i)->current_x = (*i)->current_x + x;
1232 // x is in track relative, transform it to region relative
1233 (*i)->current_x = x - region_start;
1236 double current_x = (*i)->current_x;
1238 if (note_end == CanvasNote::NOTE_ON) {
1239 resize_rect->property_x1() = snap_to_pixel(current_x);
1240 resize_rect->property_x2() = canvas_note->x2();
1242 resize_rect->property_x2() = snap_to_pixel(current_x);
1243 resize_rect->property_x1() = canvas_note->x1();
1249 MidiRegionView::commit_resizing(CanvasNote::NoteEnd note_end, double event_x, bool relative)
1251 start_delta_command(_("resize notes"));
1253 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1254 CanvasNote* canvas_note = (*i)->canvas_note;
1255 SimpleRect* resize_rect = (*i)->resize_rect;
1256 double current_x = (*i)->current_x;
1257 const double position = get_position_pixels();
1260 // event_x is in track relative, transform it to region relative
1261 current_x = event_x - position;
1264 // because snapping works on world coordinates we have to transform current_x
1265 // to world coordinates before snapping and transform it back afterwards
1266 nframes64_t current_frame = snap_to_frame(current_x);
1267 // transform to region start relative
1268 current_frame += _region->start();
1270 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(canvas_note->note().get())));
1272 // resize beginning of note
1273 if (note_end == CanvasNote::NOTE_ON && current_frame < copy->end_time()) {
1274 command_remove_note(canvas_note);
1275 copy->on_event().time() = current_frame;
1276 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1278 // resize end of note
1279 if (note_end == CanvasNote::NOTE_OFF && current_frame > copy->time()) {
1280 command_remove_note(canvas_note);
1281 copy->off_event().time() = current_frame;
1282 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1289 _resize_data.clear();
1295 MidiRegionView::change_velocity(uint8_t velocity, bool relative)
1297 start_delta_command(_("change velocity"));
1298 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1299 Selection::iterator next = i;
1302 CanvasNoteEvent *event = *i;
1303 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(event->note().get())));
1306 uint8_t new_velocity = copy->velocity() + velocity;
1307 clamp_0_to_127(new_velocity);
1309 copy->set_velocity(new_velocity);
1310 } else { // absolute
1311 copy->set_velocity(velocity);
1314 command_remove_note(event);
1315 command_add_note(copy, true);
1324 MidiRegionView::change_channel(uint8_t channel)
1326 start_delta_command(_("change channel"));
1327 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1328 Selection::iterator next = i;
1331 CanvasNoteEvent *event = *i;
1332 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(event->note().get())));
1334 copy->set_channel(channel);
1336 command_remove_note(event);
1337 command_add_note(copy, true);
1347 MidiRegionView::note_entered(ArdourCanvas::CanvasNoteEvent* ev)
1349 if (ev->note() && _mouse_state == EraseTouchDragging) {
1350 start_delta_command(_("note entered"));
1352 _delta_command->remove(ev->note());
1353 } else if (_mouse_state == SelectTouchDragging) {
1354 note_selected(ev, true);
1360 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
1362 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
1364 display_model(msrc->model());
1368 MidiRegionView::set_frame_color()
1371 if (_selected && should_show_selection) {
1372 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
1374 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
1380 MidiRegionView::midi_channel_mode_changed(ChannelMode mode, uint16_t mask)
1384 case FilterChannels:
1385 _force_channel = -1;
1388 _force_channel = mask;
1389 mask = 0xFFFF; // Show all notes as active (below)
1392 // Update notes for selection
1393 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1394 (*i)->on_channel_selection_change(mask);
1397 _last_channel_selection = mask;