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 "public_editor.h"
44 #include "ghostregion.h"
45 #include "midi_time_axis.h"
46 #include "automation_time_axis.h"
47 #include "automation_region_view.h"
49 #include "midi_util.h"
50 #include "gui_thread.h"
56 using namespace ARDOUR;
58 using namespace Editing;
59 using namespace ArdourCanvas;
61 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color)
62 : RegionView (parent, tv, r, spu, basic_color)
63 , _default_note_length(0.0)
65 , _note_group(new ArdourCanvas::Group(*parent))
66 , _delta_command(NULL)
70 group->lower_to_bottom();
71 _note_group->raise_to_top();
73 frame->property_fill_color_rgba() = 0xff000033;
76 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
77 : RegionView (parent, tv, r, spu, basic_color, visibility)
78 , _default_note_length(0.0)
80 , _note_group(new ArdourCanvas::Group(*parent))
81 , _delta_command(NULL)
85 _note_group->raise_to_top();
89 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
92 midi_region()->midi_source(0)->load_model();
94 const Meter& m = trackview.session().tempo_map().meter_at(_region->position());
95 const Tempo& t = trackview.session().tempo_map().tempo_at(_region->position());
96 _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
99 _model = midi_region()->midi_source(0)->model();
100 _enable_display = false;
102 RegionView::init(basic_color, false);
104 compute_colors (basic_color);
106 reset_width_dependent_items ((double) _region->length() / samples_per_unit);
108 set_y_position_and_height (0, trackview.height);
111 region_resized (BoundsChanged);
114 _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
118 _enable_display = true;
124 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
127 midi_region()->midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegionView::switch_source));
129 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
134 MidiRegionView::canvas_event(GdkEvent* ev)
136 static bool delete_mod = false;
137 static Editing::MidiEditMode original_mode;
139 static double drag_start_x, drag_start_y;
140 static double last_x, last_y;
141 double event_x, event_y;
142 nframes_t event_frame = 0;
144 static ArdourCanvas::SimpleRect* drag_rect = NULL;
146 if (trackview.editor.current_mouse_mode() != MouseNote)
153 if (ev->key.keyval == GDK_Delete && !delete_mod) {
155 original_mode = trackview.editor.current_midi_edit_mode();
156 trackview.editor.set_midi_edit_mode(MidiEditErase);
157 start_delta_command();
158 _mouse_state = EraseTouchDragging;
160 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
161 _mouse_state = SelectTouchDragging;
163 } else if (ev->key.keyval == GDK_Escape) {
169 case GDK_KEY_RELEASE:
170 if (ev->key.keyval == GDK_Delete) {
171 if (_mouse_state == EraseTouchDragging) {
176 trackview.editor.set_midi_edit_mode(original_mode);
180 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
186 case GDK_BUTTON_PRESS:
187 if (_mouse_state != SelectTouchDragging && _mouse_state != EraseTouchDragging)
188 _mouse_state = Pressed;
189 _pressed_button = ev->button.button;
192 case GDK_ENTER_NOTIFY:
193 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
194 Keyboard::magic_widget_grab_focus();
198 case GDK_MOTION_NOTIFY:
199 event_x = ev->motion.x;
200 event_y = ev->motion.y;
201 group->w2i(event_x, event_y);
203 event_frame = trackview.editor.pixel_to_frame(event_x);
204 trackview.editor.snap_to(event_frame);
206 switch (_mouse_state) {
207 case Pressed: // Drag start
210 if (_pressed_button == 1 && trackview.editor.current_midi_edit_mode() == MidiEditSelect) {
211 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
212 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
215 drag_start_x = event_x;
216 drag_start_y = event_y;
218 drag_rect = new ArdourCanvas::SimpleRect(*group);
219 drag_rect->property_x1() = event_x;
220 drag_rect->property_y1() = event_y;
221 drag_rect->property_x2() = event_x;
222 drag_rect->property_y2() = event_y;
223 drag_rect->property_outline_what() = 0xFF;
224 drag_rect->property_outline_color_rgba()
225 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
226 drag_rect->property_fill_color_rgba()
227 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
229 _mouse_state = SelectRectDragging;
232 // Add note drag start
233 } else if (trackview.editor.current_midi_edit_mode() == MidiEditPencil) {
234 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
235 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
238 drag_start_x = event_x;
239 drag_start_y = event_y;
241 drag_rect = new ArdourCanvas::SimpleRect(*group);
242 drag_rect->property_x1() = trackview.editor.frame_to_pixel(event_frame);
244 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
245 drag_rect->property_x2() = event_x;
246 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
247 drag_rect->property_outline_what() = 0xFF;
248 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
250 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
252 _mouse_state = AddDragging;
258 case SelectRectDragging: // Select drag motion
259 case AddDragging: // Add note drag motion
260 if (ev->motion.is_hint) {
263 GdkModifierType state;
264 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
269 if (_mouse_state == AddDragging)
270 event_x = trackview.editor.frame_to_pixel(event_frame);
273 if (event_x > drag_start_x)
274 drag_rect->property_x2() = event_x;
276 drag_rect->property_x1() = event_x;
278 if (drag_rect && _mouse_state == SelectRectDragging) {
279 if (event_y > drag_start_y)
280 drag_rect->property_y2() = event_y;
282 drag_rect->property_y1() = event_y;
284 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
290 case EraseTouchDragging:
291 case SelectTouchDragging:
299 case GDK_BUTTON_RELEASE:
300 event_x = ev->motion.x;
301 event_y = ev->motion.y;
302 group->w2i(event_x, event_y);
303 group->ungrab(ev->button.time);
304 event_frame = trackview.editor.pixel_to_frame(event_x);
306 switch (_mouse_state) {
307 case Pressed: // Clicked
308 switch (trackview.editor.current_midi_edit_mode()) {
314 trackview.editor.snap_to(event_frame, -1);
315 event_x = trackview.editor.frame_to_pixel(event_frame);
316 create_note_at(event_x, event_y, _default_note_length);
322 case SelectRectDragging: // Select drag done
327 case AddDragging: // Add drag done
329 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
330 create_note_at(drag_rect->property_x1(), drag_rect->property_y1(),
331 trackview.editor.pixel_to_frame(
332 drag_rect->property_x2() - drag_rect->property_x1()));
350 /** Add a note to the model, and the view, at a canvas (click) coordinate */
352 MidiRegionView::create_note_at(double x, double y, double dur)
354 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
355 MidiStreamView* const view = mtv->midi_view();
357 double note = midi_stream_view()->y_to_note(y);
360 assert(note <= 127.0);
362 const nframes_t stamp = trackview.editor.pixel_to_frame (x);
364 //assert(stamp <= _region->length());
366 //const Meter& m = trackview.session().tempo_map().meter_at(stamp);
367 //const Tempo& t = trackview.session().tempo_map().tempo_at(stamp);
368 //double dur = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
370 // Add a 1 beat long note (for now)
371 const boost::shared_ptr<Note> new_note(new Note(0, stamp, dur, (uint8_t)note, 0x40));
373 view->update_bounds(new_note->note());
375 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
377 _model->apply_command(cmd);
379 //add_note(new_note);
384 MidiRegionView::clear_events()
389 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
390 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
395 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i)
403 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
413 MidiRegionView::redisplay_model()
415 // Don't redisplay the model if we're currently recording and displaying that
427 MidiModel::Notes notes = _model->notes();
428 cerr << endl << "Model contains " << notes.size() << " Notes:" << endl;
429 for(MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
430 Note note = *(*i).get();
431 cerr << "MODEL: Note time: " << note.time() << " duration: " << note.duration()
432 << " end-time: " << note.end_time()
433 << " velocity: " << int(note.velocity())
434 //<< " Note-on: " << note.on_event().
435 //<< " Note-off: " << note.off_event()
439 for (size_t i=0; i < _model->n_notes(); ++i) {
440 add_note(_model->note_at(i));
445 /*for (Automatable::Controls::const_iterator i = _model->controls().begin();
446 i != _model->controls().end(); ++i) {
450 boost::shared_ptr<AutomationTimeAxisView> at
451 = midi_view()->automation_child(i->second->parameter());
455 Gdk::Color col = midi_stream_view()->get_region_color();
457 boost::shared_ptr<AutomationRegionView> arv;
460 Glib::Mutex::Lock list_lock (i->second->list()->lock());
462 arv = boost::shared_ptr<AutomationRegionView>(
463 new AutomationRegionView(at->canvas_display,
464 *at.get(), _region, i->second->list(),
465 midi_stream_view()->get_samples_per_unit(), col));
468 arv->set_duration(_region->length(), this);
469 arv->init(col, true);
471 _automation_children.insert(std::make_pair(i->second->parameter(), arv));
474 _model->read_unlock();
477 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
482 MidiRegionView::~MidiRegionView ()
484 in_destructor = true;
486 RegionViewGoingAway (this); /* EMIT_SIGNAL */
494 delete _delta_command;
499 MidiRegionView::region_resized (Change what_changed)
501 RegionView::region_resized(what_changed);
503 if (what_changed & ARDOUR::PositionChanged) {
508 } else if (what_changed & Change (StartChanged)) {
510 //cerr << "MIDI RV START CHANGED" << endl;
512 } else if (what_changed & Change (LengthChanged)) {
514 //cerr << "MIDI RV LENGTH CHANGED" << endl;
520 MidiRegionView::reset_width_dependent_items (double pixel_width)
522 RegionView::reset_width_dependent_items(pixel_width);
523 assert(_pixel_width == pixel_width);
530 MidiRegionView::set_y_position_and_height (double y, double h)
532 RegionView::set_y_position_and_height(y, h - 1);
534 if (_enable_display) {
538 for (std::vector<CanvasMidiEvent*>::const_iterator i = _events.begin(); i != _events.end(); ++i) {
539 CanvasNote* note = dynamic_cast<CanvasNote*>(*i);
540 if (note && note->note()) {
541 if (note->note()->note() < midi_stream_view()->lowest_note() ||
542 note->note()->note() > midi_stream_view()->highest_note()) {
543 if (canvas_item_visible(note)) {
548 const double y1 = midi_stream_view()->note_to_y(note->note()->note());
549 const double y2 = y1 + floor(midi_stream_view()->note_height());
551 if (!canvas_item_visible(note)) {
555 note->hide_velocity();
556 note->property_y1() = y1;
557 note->property_y2() = y2;
558 if(note->selected()) {
559 note->show_velocity();
565 _model->read_unlock();
569 name_text->raise_to_top();
574 MidiRegionView::add_ghost (TimeAxisView& tv)
576 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
580 double unit_position = _region->position () / samples_per_unit;
581 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
582 MidiGhostRegion* ghost;
584 if (mtv && mtv->midi_view()) {
585 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group.
586 this is because it's nice to have midi notes on top of the note lines and
587 audio waveforms under it.
589 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
592 ghost = new MidiGhostRegion (tv, trackview, unit_position);
595 ghost->set_height ();
596 ghost->set_duration (_region->length() / samples_per_unit);
597 ghosts.push_back (ghost);
599 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
600 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
601 ghost->add_note(note);
605 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
611 /** Begin tracking note state for successive calls to add_event
614 MidiRegionView::begin_write()
616 assert(!_active_notes);
617 _active_notes = new CanvasNote*[128];
618 for (unsigned i=0; i < 128; ++i)
619 _active_notes[i] = NULL;
623 /** Destroy note state for add_event
626 MidiRegionView::end_write()
628 delete[] _active_notes;
629 _active_notes = NULL;
630 _marked_for_selection.clear();
634 /** Resolve an active MIDI note (while recording).
637 MidiRegionView::resolve_note(uint8_t note, double end_time)
639 if (midi_view()->note_mode() != Sustained)
642 if (_active_notes && _active_notes[note]) {
643 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)end_time);
644 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
645 _active_notes[note] = NULL;
650 /** Extend active notes to rightmost edge of region (if length is changed)
653 MidiRegionView::extend_active_notes()
658 for (unsigned i=0; i < 128; ++i)
659 if (_active_notes[i])
660 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
664 /** Add a MIDI note to the view (with duration).
666 * If in sustained mode, notes with duration 0 will be considered active
667 * notes, and resolve_note should be called when the corresponding note off
668 * event arrives, to properly display the note.
671 MidiRegionView::add_note(const boost::shared_ptr<Note> note)
673 assert(note->time() >= 0);
674 //assert(note->time() < _region->length());
675 assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
677 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
679 CanvasMidiEvent *event = 0;
681 if (midi_view()->note_mode() == Sustained) {
683 //cerr << "MRV::add_note sustained " << note->note() << " @ " << note->time()
684 // << " .. " << note->end_time() << endl;
686 const double y1 = midi_stream_view()->note_to_y(note->note());
688 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
689 ev_rect->property_x1() = trackview.editor.frame_to_pixel((nframes_t)note->time());
690 ev_rect->property_y1() = y1;
691 if (note->duration() > 0)
692 ev_rect->property_x2() = trackview.editor.frame_to_pixel((nframes_t)(note->end_time()));
694 ev_rect->property_x2() = trackview.editor.frame_to_pixel(_region->length());
695 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
697 ev_rect->property_fill_color_rgba() = note_fill_color(note->velocity());
698 ev_rect->property_outline_color_rgba() = note_outline_color(note->velocity());
700 if (note->duration() == 0) {
701 _active_notes[note->note()] = ev_rect;
702 /* outline all but right edge */
703 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
705 /* outline all edges */
706 ev_rect->property_outline_what() = (guint32) 0xF;
710 _events.push_back(ev_rect);
715 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
716 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
717 gr->add_note(ev_rect);
721 } else if (midi_view()->note_mode() == Percussive) {
723 //cerr << "MRV::add_note percussive " << note->note() << " @ " << note->time()
724 // << " .. " << note->end_time() << endl;
726 const double diamond_size = midi_stream_view()->note_height() / 2.0;
727 const double x = trackview.editor.frame_to_pixel((nframes_t)note->time());
728 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
730 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size);
731 ev_diamond->move(x, y);
733 ev_diamond->property_fill_color_rgba() = note_fill_color(note->velocity());
734 ev_diamond->property_outline_color_rgba() = note_outline_color(note->velocity());
735 _events.push_back(ev_diamond);
742 Note *note = event->note().get();
744 if(_marked_for_selection.find(note) != _marked_for_selection.end()) {
745 note_selected(event, true);
751 MidiRegionView::delete_selection()
753 assert(_delta_command);
755 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
756 if ((*i)->selected())
757 _delta_command->remove((*i)->note());
763 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasMidiEvent* ev)
765 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
766 if ((*i)->selected() && (*i) != ev)
767 (*i)->selected(false);
773 MidiRegionView::unique_select(ArdourCanvas::CanvasMidiEvent* ev)
775 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
777 (*i)->selected(false);
780 _selection.insert(ev);
782 if ( ! ev->selected())
787 MidiRegionView::note_selected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
790 clear_selection_except(ev);
792 _selection.insert(ev);
794 if ( ! ev->selected())
800 MidiRegionView::note_deselected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
803 clear_selection_except(ev);
805 _selection.erase(ev);
813 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
815 const double last_y = std::min(y1, y2);
816 const double y = std::max(y1, y2);
818 // FIXME: so, so, so much slower than this should be
821 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
822 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
823 (*i)->selected(true);
824 _selection.insert(*i);
826 (*i)->selected(false);
827 _selection.erase(*i);
831 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
832 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
833 (*i)->selected(true);
834 _selection.insert(*i);
836 (*i)->selected(false);
837 _selection.erase(*i);
845 MidiRegionView::move_selection(double dx, double dy)
847 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
848 (*i)->move_event(dx, dy);
853 MidiRegionView::note_dropped(CanvasMidiEvent* ev, double dt, uint8_t dnote)
855 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
856 if (_selection.find(ev) != _selection.end()) {
857 uint8_t lowest_note_in_selection = midi_stream_view()->lowest_note();
858 uint8_t highest_note_in_selection = midi_stream_view()->highest_note();
859 uint8_t highest_note_difference = 0;
861 // find highest and lowest notes first
862 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
863 Selection::iterator next = i;
866 uint8_t pitch = (*i)->note()->note();
867 lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
868 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
874 cerr << "dnote: " << (int) dnote << endl;
875 cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
876 << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
877 cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
878 << int(highest_note_in_selection) << endl;
879 cerr << "selection size: " << _selection.size() << endl;
880 cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
883 // Make sure the note pitch does not exceed the MIDI standard range
884 if (dnote <= 127 && (highest_note_in_selection + dnote > 127)) {
885 highest_note_difference = highest_note_in_selection - 127;
888 start_delta_command();
890 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
891 Selection::iterator next = i;
894 command_remove_note(*i);
895 const boost::shared_ptr<Note> copy(new Note(*(*i)->note().get()));
897 copy->set_time((*i)->note()->time() + dt);
898 if(copy->time() < 0) {
902 uint8_t original_pitch = (*i)->note()->note();
903 uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
905 // keep notes in standard midi range
906 clamp_0_to_127(new_pitch);
908 //notes which are dragged beyond the standard midi range snap back to their original place
909 if((original_pitch != 0 && new_pitch == 0) || (original_pitch != 127 && new_pitch == 127)) {
910 new_pitch = original_pitch;
913 lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
914 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
916 copy->set_note(new_pitch);
918 command_add_note(copy);
920 _marked_for_selection.insert(copy.get());
926 // care about notes being moved beyond the upper/lower bounds on the canvas
927 if(lowest_note_in_selection < midi_stream_view()->lowest_note() ||
928 highest_note_in_selection > midi_stream_view()->highest_note()
930 midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
937 MidiRegionView::snap_to(double x)
939 PublicEditor &editor = trackview.editor;
941 nframes_t frame = editor.pixel_to_frame(x);
942 editor.snap_to(frame);
943 return (double) editor.frame_to_pixel(frame);
947 MidiRegionView::get_position_pixels(void)
949 nframes_t region_frame = get_position();
950 return trackview.editor.frame_to_pixel(region_frame);
954 MidiRegionView::begin_resizing(CanvasNote::NoteEnd note_end)
956 _resize_data.clear();
958 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
959 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
961 // only insert CanvasNotes into the map
963 NoteResizeData *resize_data = new NoteResizeData();
964 resize_data->canvas_note = note;
966 // create a new SimpleRect from the note which will be the resize preview
967 SimpleRect *resize_rect =
975 // calculate the colors: get the color settings
978 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get(),
981 // make the resize preview notes more transparent and bright
982 fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
984 // calculate color based on note velocity
985 resize_rect->property_fill_color_rgba() =
987 note_fill_color(note->note()->velocity()),
991 resize_rect->property_outline_color_rgba() =
992 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get();
994 resize_data->resize_rect = resize_rect;
996 if(note_end == CanvasNote::NOTE_ON) {
997 resize_data->current_x = note->x1();
999 resize_data->current_x = note->x2();
1002 _resize_data.push_back(resize_data);
1008 MidiRegionView::update_resizing(CanvasNote::NoteEnd note_end, double x, bool relative)
1010 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1011 SimpleRect *resize_rect = (*i)->resize_rect;
1012 CanvasNote *canvas_note = (*i)->canvas_note;
1014 const double region_start = get_position_pixels();
1017 (*i)->current_x = (*i)->current_x + x;
1019 // x is in track relative, transform it to region relative
1020 (*i)->current_x = x - region_start;
1023 double current_x = (*i)->current_x;
1025 if(note_end == CanvasNote::NOTE_ON) {
1026 // because snapping works on world coordinates we have to transform current_x
1027 // to world coordinates before snapping and transform it back afterwards
1028 resize_rect->property_x1() = snap_to(region_start + current_x) - region_start;
1029 resize_rect->property_x2() = canvas_note->x2();
1031 // because snapping works on world coordinates we have to transform current_x
1032 // to world coordinates before snapping and transform it back afterwards
1033 resize_rect->property_x2() = snap_to(region_start + current_x) - region_start;
1034 resize_rect->property_x1() = canvas_note->x1();
1040 MidiRegionView::commit_resizing(CanvasNote::NoteEnd note_end, double event_x, bool relative)
1042 start_delta_command();
1044 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1045 CanvasNote *canvas_note = (*i)->canvas_note;
1046 SimpleRect *resize_rect = (*i)->resize_rect;
1047 double current_x = (*i)->current_x;
1048 const double region_start = get_position_pixels();
1051 // event_x is in track relative, transform it to region relative
1052 current_x = event_x - region_start;
1055 // because snapping works on world coordinates we have to transform current_x
1056 // to world coordinates before snapping and transform it back afterwards
1057 nframes_t current_frame = trackview.editor.pixel_to_frame(current_x + region_start);
1058 trackview.editor.snap_to(current_frame);
1059 current_frame -= get_position();
1061 const boost::shared_ptr<Note> copy(new Note(*(canvas_note->note().get())));
1063 // resize beginning of note
1064 if (note_end == CanvasNote::NOTE_ON && current_frame < copy->end_time()) {
1065 command_remove_note(canvas_note);
1066 copy->on_event().time() = current_frame;
1067 command_add_note(copy);
1068 _marked_for_selection.insert(copy.get());
1070 // resize end of note
1071 if (note_end == CanvasNote::NOTE_OFF && current_frame > copy->time()) {
1072 command_remove_note(canvas_note);
1073 command_remove_note(canvas_note);
1074 copy->off_event().time() = current_frame;
1075 command_add_note(copy);
1076 _marked_for_selection.insert(copy.get());
1083 _resize_data.clear();
1089 MidiRegionView::change_velocity(uint8_t velocity, bool relative)
1091 start_delta_command();
1092 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1093 Selection::iterator next = i;
1096 CanvasMidiEvent *event = *i;
1097 const boost::shared_ptr<Note> copy(new Note(*(event->note().get())));
1100 uint8_t new_velocity = copy->velocity() + velocity;
1101 clamp_0_to_127(new_velocity);
1103 copy->set_velocity(new_velocity);
1104 } else { // absolute
1105 copy->set_velocity(velocity);
1108 command_remove_note(event);
1109 command_add_note(copy);
1111 _marked_for_selection.insert(copy.get());
1115 // dont keep notes selected if tweaking a single note
1116 if(_marked_for_selection.size() == 1) {
1117 _marked_for_selection.clear();
1125 MidiRegionView::note_entered(ArdourCanvas::CanvasMidiEvent* ev)
1127 if (ev->note() && _mouse_state == EraseTouchDragging) {
1128 start_delta_command();
1130 _delta_command->remove(ev->note());
1131 } else if (_mouse_state == SelectTouchDragging) {
1132 note_selected(ev, true);
1138 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
1140 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
1142 display_model(msrc->model());
1146 MidiRegionView::set_frame_color()
1149 if (_selected && should_show_selection) {
1150 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
1152 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();