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 , last_channel_selection(0xFFFF)
64 , _default_note_length(0.0)
66 , _note_group(new ArdourCanvas::Group(*parent))
67 , _delta_command(NULL)
71 group->lower_to_bottom();
72 _note_group->raise_to_top();
74 frame->property_fill_color_rgba() = 0xff000033;
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, visibility)
79 , last_channel_selection(0xFFFF)
80 , _default_note_length(0.0)
82 , _note_group(new ArdourCanvas::Group(*parent))
83 , _delta_command(NULL)
88 _note_group->raise_to_top();
92 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
95 midi_region()->midi_source(0)->load_model();
97 const Meter& m = trackview.session().tempo_map().meter_at(_region->position());
98 const Tempo& t = trackview.session().tempo_map().tempo_at(_region->position());
99 _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
102 _model = midi_region()->midi_source(0)->model();
103 _enable_display = false;
105 RegionView::init(basic_color, false);
107 compute_colors (basic_color);
109 reset_width_dependent_items ((double) _region->length() / samples_per_unit);
111 set_y_position_and_height (0, trackview.height);
114 region_resized (BoundsChanged);
117 _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
121 _enable_display = true;
127 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
130 midi_region()->midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegionView::switch_source));
132 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
134 midi_view()->signal_channel_selection_changed().connect(
135 mem_fun(this, &MidiRegionView::midi_channel_selection_changed));
139 MidiRegionView::canvas_event(GdkEvent* ev)
141 static bool delete_mod = false;
142 static Editing::MidiEditMode original_mode;
144 static double drag_start_x, drag_start_y;
145 static double last_x, last_y;
146 double event_x, event_y;
147 nframes_t event_frame = 0;
149 static ArdourCanvas::SimpleRect* drag_rect = NULL;
151 if (trackview.editor.current_mouse_mode() != MouseNote)
158 if (ev->key.keyval == GDK_Delete && !delete_mod) {
160 original_mode = trackview.editor.current_midi_edit_mode();
161 trackview.editor.set_midi_edit_mode(MidiEditErase);
162 start_delta_command(_("erase notes"));
163 _mouse_state = EraseTouchDragging;
165 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
166 _mouse_state = SelectTouchDragging;
168 } else if (ev->key.keyval == GDK_Escape) {
174 case GDK_KEY_RELEASE:
175 if (ev->key.keyval == GDK_Delete) {
176 if (_mouse_state == EraseTouchDragging) {
181 trackview.editor.set_midi_edit_mode(original_mode);
186 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
192 case GDK_BUTTON_PRESS:
193 if (_mouse_state != SelectTouchDragging &&
194 _mouse_state != EraseTouchDragging &&
195 ev->button.button == 1 ) {
196 _pressed_button = ev->button.button;
197 _mouse_state = Pressed;
200 _pressed_button = ev->button.button;
203 case GDK_ENTER_NOTIFY:
204 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
205 Keyboard::magic_widget_grab_focus();
209 case GDK_MOTION_NOTIFY:
210 event_x = ev->motion.x;
211 event_y = ev->motion.y;
212 group->w2i(event_x, event_y);
214 // convert event_x to global frame
215 event_frame = trackview.editor.pixel_to_frame(event_x) + _region->position();
216 trackview.editor.snap_to(event_frame);
217 // convert event_frame back to local coordinates relative to position
218 event_frame -= _region->position();
220 switch (_mouse_state) {
221 case Pressed: // Drag start
224 if (_pressed_button == 1 && trackview.editor.current_midi_edit_mode() == MidiEditSelect) {
225 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
226 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
229 drag_start_x = event_x;
230 drag_start_y = event_y;
232 drag_rect = new ArdourCanvas::SimpleRect(*group);
233 drag_rect->property_x1() = event_x;
234 drag_rect->property_y1() = event_y;
235 drag_rect->property_x2() = event_x;
236 drag_rect->property_y2() = event_y;
237 drag_rect->property_outline_what() = 0xFF;
238 drag_rect->property_outline_color_rgba()
239 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
240 drag_rect->property_fill_color_rgba()
241 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
243 _mouse_state = SelectRectDragging;
246 // Add note drag start
247 } else if (trackview.editor.current_midi_edit_mode() == MidiEditPencil) {
248 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
249 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
252 drag_start_x = event_x;
253 drag_start_y = event_y;
255 drag_rect = new ArdourCanvas::SimpleRect(*group);
256 drag_rect->property_x1() = trackview.editor.frame_to_pixel(event_frame);
258 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
259 drag_rect->property_x2() = event_x;
260 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
261 drag_rect->property_outline_what() = 0xFF;
262 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
264 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
266 _mouse_state = AddDragging;
272 case SelectRectDragging: // Select drag motion
273 case AddDragging: // Add note drag motion
274 if (ev->motion.is_hint) {
277 GdkModifierType state;
278 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
283 if (_mouse_state == AddDragging)
284 event_x = trackview.editor.frame_to_pixel(event_frame);
287 if (event_x > drag_start_x)
288 drag_rect->property_x2() = event_x;
290 drag_rect->property_x1() = event_x;
292 if (drag_rect && _mouse_state == SelectRectDragging) {
293 if (event_y > drag_start_y)
294 drag_rect->property_y2() = event_y;
296 drag_rect->property_y1() = event_y;
298 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
304 case EraseTouchDragging:
305 case SelectTouchDragging:
313 case GDK_BUTTON_RELEASE:
314 event_x = ev->motion.x;
315 event_y = ev->motion.y;
316 group->w2i(event_x, event_y);
317 group->ungrab(ev->button.time);
318 event_frame = trackview.editor.pixel_to_frame(event_x);
320 if(_pressed_button != 1) {
324 switch (_mouse_state) {
325 case Pressed: // Clicked
326 switch (trackview.editor.current_midi_edit_mode()) {
332 create_note_at(event_x, event_y, _default_note_length);
338 case SelectRectDragging: // Select drag done
343 case AddDragging: // Add drag done
345 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
346 const double x = drag_rect->property_x1();
347 const double length = trackview.editor.pixel_to_frame(
348 drag_rect->property_x2() - drag_rect->property_x1());
350 create_note_at(x, drag_rect->property_y1(), length);
368 /** Add a note to the model, and the view, at a canvas (click) coordinate */
370 MidiRegionView::create_note_at(double x, double y, double duration)
372 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
373 MidiStreamView* const view = mtv->midi_view();
375 double note = midi_stream_view()->y_to_note(y);
378 assert(note <= 127.0);
380 nframes_t new_note_time = trackview.editor.pixel_to_frame (x);
381 assert(new_note_time >= 0);
382 new_note_time += _region->start();
385 const Meter& m = trackview.session().tempo_map().meter_at(new_note_time);
386 const Tempo& t = trackview.session().tempo_map().tempo_at(new_note_time);
387 double duration = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
390 // we need to snap here again in nframes_t in order to be sample accurate
391 // since note time is region-absolute but snap_to_frame expects position-relative
392 // time we have to coordinate transform back and forth here.
393 nframes_t new_note_time_position_relative = new_note_time - _region->start();
394 new_note_time = snap_to_frame(new_note_time_position_relative) + _region->start();
396 // we need to snap the duration too to be sample accurate
397 nframes_t new_note_duration = nframes_t(duration);
398 new_note_duration = snap_to_frame(new_note_time_position_relative + new_note_duration) + _region->start()
401 const boost::shared_ptr<Note> new_note(new Note(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);
408 //add_note(new_note);
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 (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i)
432 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
442 MidiRegionView::redisplay_model()
444 // Don't redisplay the model if we're currently recording and displaying that
456 MidiModel::Notes notes = _model->notes();
457 cerr << endl << "Model contains " << notes.size() << " Notes:" << endl;
458 for(MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
459 Note note = *(*i).get();
460 cerr << "MODEL: Note time: " << note.time() << " duration: " << note.duration()
461 << " end-time: " << note.end_time()
462 << " velocity: " << int(note.velocity())
463 //<< " Note-on: " << note.on_event().
464 //<< " Note-off: " << note.off_event()
468 for (size_t i=0; i < _model->n_notes(); ++i) {
469 add_note(_model->note_at(i));
474 /*for (Automatable::Controls::const_iterator i = _model->controls().begin();
475 i != _model->controls().end(); ++i) {
479 boost::shared_ptr<AutomationTimeAxisView> at
480 = midi_view()->automation_child(i->second->parameter());
484 Gdk::Color col = midi_stream_view()->get_region_color();
486 boost::shared_ptr<AutomationRegionView> arv;
489 Glib::Mutex::Lock list_lock (i->second->list()->lock());
491 arv = boost::shared_ptr<AutomationRegionView>(
492 new AutomationRegionView(at->canvas_display,
493 *at.get(), _region, i->second->list(),
494 midi_stream_view()->get_samples_per_unit(), col));
497 arv->set_duration(_region->length(), this);
498 arv->init(col, true);
500 _automation_children.insert(std::make_pair(i->second->parameter(), arv));
503 _model->read_unlock();
506 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
511 MidiRegionView::~MidiRegionView ()
513 in_destructor = true;
515 RegionViewGoingAway (this); /* EMIT_SIGNAL */
523 delete _delta_command;
528 MidiRegionView::region_resized (Change what_changed)
530 RegionView::region_resized(what_changed);
532 if (what_changed & ARDOUR::PositionChanged) {
539 MidiRegionView::reset_width_dependent_items (double pixel_width)
541 RegionView::reset_width_dependent_items(pixel_width);
542 assert(_pixel_width == pixel_width);
549 MidiRegionView::set_y_position_and_height (double y, double h)
551 RegionView::set_y_position_and_height(y, h - 1);
553 if (_enable_display) {
557 for (std::vector<CanvasMidiEvent*>::const_iterator i = _events.begin(); i != _events.end(); ++i) {
558 CanvasNote* note = dynamic_cast<CanvasNote*>(*i);
559 if (note && note->note()) {
560 if (note->note()->note() < midi_stream_view()->lowest_note() ||
561 note->note()->note() > midi_stream_view()->highest_note()) {
562 if (canvas_item_visible(note)) {
567 const double y1 = midi_stream_view()->note_to_y(note->note()->note());
568 const double y2 = y1 + floor(midi_stream_view()->note_height());
570 if (!canvas_item_visible(note)) {
574 note->hide_velocity();
575 note->property_y1() = y1;
576 note->property_y2() = y2;
577 if(note->selected()) {
578 note->show_velocity();
584 _model->read_unlock();
588 name_text->raise_to_top();
593 MidiRegionView::add_ghost (TimeAxisView& tv)
595 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
599 double unit_position = _region->position () / samples_per_unit;
600 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
601 MidiGhostRegion* ghost;
603 if (mtv && mtv->midi_view()) {
604 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group.
605 this is because it's nice to have midi notes on top of the note lines and
606 audio waveforms under it.
608 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
611 ghost = new MidiGhostRegion (tv, trackview, unit_position);
614 ghost->set_height ();
615 ghost->set_duration (_region->length() / samples_per_unit);
616 ghosts.push_back (ghost);
618 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
619 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
620 ghost->add_note(note);
624 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
630 /** Begin tracking note state for successive calls to add_event
633 MidiRegionView::begin_write()
635 assert(!_active_notes);
636 _active_notes = new CanvasNote*[128];
637 for (unsigned i=0; i < 128; ++i)
638 _active_notes[i] = NULL;
642 /** Destroy note state for add_event
645 MidiRegionView::end_write()
647 delete[] _active_notes;
648 _active_notes = NULL;
649 _marked_for_selection.clear();
653 /** Resolve an active MIDI note (while recording).
656 MidiRegionView::resolve_note(uint8_t note, double end_time)
658 if (midi_view()->note_mode() != Sustained)
661 if (_active_notes && _active_notes[note]) {
662 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)end_time);
663 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
664 _active_notes[note] = NULL;
669 /** Extend active notes to rightmost edge of region (if length is changed)
672 MidiRegionView::extend_active_notes()
677 for (unsigned i=0; i < 128; ++i)
678 if (_active_notes[i])
679 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
683 /** Add a MIDI note to the view (with duration).
685 * If in sustained mode, notes with duration 0 will be considered active
686 * notes, and resolve_note should be called when the corresponding note off
687 * event arrives, to properly display the note.
690 MidiRegionView::add_note(const boost::shared_ptr<Note> note)
692 assert(note->time() >= 0);
693 assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
695 // dont display notes beyond the region bounds
697 note->time() - _region->start() >= _region->length() ||
698 note->time() < _region->start() ||
699 note->note() < midi_stream_view()->lowest_note() ||
700 note->note() > midi_stream_view()->highest_note()
705 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
707 CanvasMidiEvent *event = 0;
709 const double x = trackview.editor.frame_to_pixel((nframes_t)note->time() - _region->start());
711 if (midi_view()->note_mode() == Sustained) {
713 //cerr << "MRV::add_note sustained " << note->note() << " @ " << note->time()
714 // << " .. " << note->end_time() << endl;
716 const double y1 = midi_stream_view()->note_to_y(note->note());
717 const double note_endpixel =
718 trackview.editor.frame_to_pixel((nframes_t)note->end_time() - _region->start());
720 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
721 ev_rect->property_x1() = x;
722 ev_rect->property_y1() = y1;
723 if (note->duration() > 0)
724 ev_rect->property_x2() = note_endpixel;
726 ev_rect->property_x2() = trackview.editor.frame_to_pixel(_region->length());
727 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
729 if (note->duration() == 0) {
730 _active_notes[note->note()] = ev_rect;
731 /* outline all but right edge */
732 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
734 /* outline all edges */
735 ev_rect->property_outline_what() = (guint32) 0xF;
739 _events.push_back(ev_rect);
744 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
745 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
746 gr->add_note(ev_rect);
750 } else if (midi_view()->note_mode() == Percussive) {
752 //cerr << "MRV::add_note percussive " << note->note() << " @ " << note->time()
753 // << " .. " << note->end_time() << endl;
755 const double diamond_size = midi_stream_view()->note_height() / 2.0;
756 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
758 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size, note);
759 ev_diamond->move(x, y);
761 _events.push_back(ev_diamond);
768 if(_marked_for_selection.find(event->note()) != _marked_for_selection.end()) {
769 note_selected(event, true);
771 event->on_channel_selection_change(last_channel_selection);
776 MidiRegionView::delete_selection()
778 assert(_delta_command);
780 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
781 if ((*i)->selected())
782 _delta_command->remove((*i)->note());
788 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasMidiEvent* ev)
790 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
791 if ((*i)->selected() && (*i) != ev)
792 (*i)->selected(false);
798 MidiRegionView::unique_select(ArdourCanvas::CanvasMidiEvent* ev)
800 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
802 (*i)->selected(false);
805 _selection.insert(ev);
807 if ( ! ev->selected())
812 MidiRegionView::note_selected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
815 clear_selection_except(ev);
817 _selection.insert(ev);
819 if ( ! ev->selected())
825 MidiRegionView::note_deselected(ArdourCanvas::CanvasMidiEvent* ev, bool add)
828 clear_selection_except(ev);
830 _selection.erase(ev);
838 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
840 const double last_y = std::min(y1, y2);
841 const double y = std::max(y1, y2);
843 // FIXME: so, so, so much slower than this should be
846 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
847 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
848 (*i)->selected(true);
849 _selection.insert(*i);
851 (*i)->selected(false);
852 _selection.erase(*i);
856 for (std::vector<CanvasMidiEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
857 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
858 (*i)->selected(true);
859 _selection.insert(*i);
861 (*i)->selected(false);
862 _selection.erase(*i);
870 MidiRegionView::move_selection(double dx, double dy)
872 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
873 (*i)->move_event(dx, dy);
878 MidiRegionView::note_dropped(CanvasMidiEvent* ev, double dt, uint8_t dnote)
880 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
881 if (_selection.find(ev) != _selection.end()) {
882 uint8_t lowest_note_in_selection = midi_stream_view()->lowest_note();
883 uint8_t highest_note_in_selection = midi_stream_view()->highest_note();
884 uint8_t highest_note_difference = 0;
886 // find highest and lowest notes first
887 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
888 Selection::iterator next = i;
891 uint8_t pitch = (*i)->note()->note();
892 lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
893 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
899 cerr << "dnote: " << (int) dnote << endl;
900 cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
901 << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
902 cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
903 << int(highest_note_in_selection) << endl;
904 cerr << "selection size: " << _selection.size() << endl;
905 cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
908 // Make sure the note pitch does not exceed the MIDI standard range
909 if (dnote <= 127 && (highest_note_in_selection + dnote > 127)) {
910 highest_note_difference = highest_note_in_selection - 127;
913 start_delta_command(_("move notes"));
915 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
916 Selection::iterator next = i;
919 const boost::shared_ptr<Note> copy(new Note(*(*i)->note().get()));
921 // we need to snap here again in nframes_t in order to be sample accurate
922 double new_note_time = (*i)->note()->time();
925 // keep notes inside region if dragged beyond left region bound
926 if(new_note_time < _region->start()) {
927 new_note_time = _region->start();
930 // since note time is region-absolute but snap_to_frame expects position-relative
931 // time we have to coordinate transform back and forth here.
932 new_note_time = snap_to_frame(nframes_t(new_note_time) - _region->start()) + _region->start();
934 copy->set_time(new_note_time);
936 uint8_t original_pitch = (*i)->note()->note();
937 uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
939 // keep notes in standard midi range
940 clamp_0_to_127(new_pitch);
942 //notes which are dragged beyond the standard midi range snap back to their original place
943 if((original_pitch != 0 && new_pitch == 0) || (original_pitch != 127 && new_pitch == 127)) {
944 new_pitch = original_pitch;
947 lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
948 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
950 copy->set_note(new_pitch);
952 command_remove_note(*i);
953 command_add_note(copy);
955 _marked_for_selection.insert(copy);
960 // care about notes being moved beyond the upper/lower bounds on the canvas
961 if(lowest_note_in_selection < midi_stream_view()->lowest_note() ||
962 highest_note_in_selection > midi_stream_view()->highest_note()
964 midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
970 MidiRegionView::snap_to_frame(double x)
972 PublicEditor &editor = trackview.editor;
973 // x is region relative
974 // convert x to global frame
975 nframes_t frame = editor.pixel_to_frame(x) + _region->position();
976 editor.snap_to(frame);
977 // convert event_frame back to local coordinates relative to position
978 frame -= _region->position();
983 MidiRegionView::snap_to_frame(nframes_t x)
985 PublicEditor &editor = trackview.editor;
986 // x is region relative
987 // convert x to global frame
988 nframes_t frame = x + _region->position();
989 editor.snap_to(frame);
990 // convert event_frame back to local coordinates relative to position
991 frame -= _region->position();
996 MidiRegionView::snap_to_pixel(double x)
998 return (double) trackview.editor.frame_to_pixel(snap_to_frame(x));
1002 MidiRegionView::get_position_pixels(void)
1004 nframes_t region_frame = get_position();
1005 return trackview.editor.frame_to_pixel(region_frame);
1009 MidiRegionView::begin_resizing(CanvasNote::NoteEnd note_end)
1011 _resize_data.clear();
1013 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1014 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
1016 // only insert CanvasNotes into the map
1018 NoteResizeData *resize_data = new NoteResizeData();
1019 resize_data->canvas_note = note;
1021 // create a new SimpleRect from the note which will be the resize preview
1022 SimpleRect *resize_rect =
1030 // calculate the colors: get the color settings
1033 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get(),
1036 // make the resize preview notes more transparent and bright
1037 fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
1039 // calculate color based on note velocity
1040 resize_rect->property_fill_color_rgba() =
1042 note_fill_color(note->note()->velocity()),
1046 resize_rect->property_outline_color_rgba() =
1047 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get();
1049 resize_data->resize_rect = resize_rect;
1051 if(note_end == CanvasNote::NOTE_ON) {
1052 resize_data->current_x = note->x1();
1053 } else { // NOTE_OFF
1054 resize_data->current_x = note->x2();
1057 _resize_data.push_back(resize_data);
1063 MidiRegionView::update_resizing(CanvasNote::NoteEnd note_end, double x, bool relative)
1065 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1066 SimpleRect *resize_rect = (*i)->resize_rect;
1067 CanvasNote *canvas_note = (*i)->canvas_note;
1069 const double region_start = get_position_pixels();
1072 (*i)->current_x = (*i)->current_x + x;
1074 // x is in track relative, transform it to region relative
1075 (*i)->current_x = x - region_start;
1078 double current_x = (*i)->current_x;
1080 if(note_end == CanvasNote::NOTE_ON) {
1081 resize_rect->property_x1() = snap_to_pixel(current_x);
1082 resize_rect->property_x2() = canvas_note->x2();
1084 resize_rect->property_x2() = snap_to_pixel(current_x);
1085 resize_rect->property_x1() = canvas_note->x1();
1091 MidiRegionView::commit_resizing(CanvasNote::NoteEnd note_end, double event_x, bool relative)
1093 start_delta_command(_("resize notes"));
1095 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1096 CanvasNote *canvas_note = (*i)->canvas_note;
1097 SimpleRect *resize_rect = (*i)->resize_rect;
1098 double current_x = (*i)->current_x;
1099 const double position = get_position_pixels();
1102 // event_x is in track relative, transform it to region relative
1103 current_x = event_x - position;
1106 // because snapping works on world coordinates we have to transform current_x
1107 // to world coordinates before snapping and transform it back afterwards
1108 nframes_t current_frame = snap_to_frame(current_x);
1109 // transform to region start relative
1110 current_frame += _region->start();
1112 const boost::shared_ptr<Note> copy(new Note(*(canvas_note->note().get())));
1114 // resize beginning of note
1115 if (note_end == CanvasNote::NOTE_ON && current_frame < copy->end_time()) {
1116 command_remove_note(canvas_note);
1117 copy->on_event().time() = current_frame;
1118 command_add_note(copy);
1119 _marked_for_selection.insert(copy);
1121 // resize end of note
1122 if (note_end == CanvasNote::NOTE_OFF && current_frame > copy->time()) {
1123 command_remove_note(canvas_note);
1124 command_remove_note(canvas_note);
1125 copy->off_event().time() = current_frame;
1126 command_add_note(copy);
1127 _marked_for_selection.insert(copy);
1134 _resize_data.clear();
1140 MidiRegionView::change_velocity(uint8_t velocity, bool relative)
1142 start_delta_command(_("change velocity"));
1143 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1144 Selection::iterator next = i;
1147 CanvasMidiEvent *event = *i;
1148 const boost::shared_ptr<Note> copy(new Note(*(event->note().get())));
1151 uint8_t new_velocity = copy->velocity() + velocity;
1152 clamp_0_to_127(new_velocity);
1154 copy->set_velocity(new_velocity);
1155 } else { // absolute
1156 copy->set_velocity(velocity);
1159 command_remove_note(event);
1160 command_add_note(copy);
1162 _marked_for_selection.insert(copy);
1166 // dont keep notes selected if tweaking a single note
1167 if(_marked_for_selection.size() == 1) {
1168 _marked_for_selection.clear();
1175 MidiRegionView::change_channel(uint8_t channel)
1177 start_delta_command(_("change channel"));
1178 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1179 Selection::iterator next = i;
1182 CanvasMidiEvent *event = *i;
1183 const boost::shared_ptr<Note> copy(new Note(*(event->note().get())));
1185 copy->set_channel(channel);
1187 command_remove_note(event);
1188 command_add_note(copy);
1190 _marked_for_selection.insert(copy);
1194 // dont keep notes selected if tweaking a single note
1195 if(_marked_for_selection.size() == 1) {
1196 _marked_for_selection.clear();
1204 MidiRegionView::note_entered(ArdourCanvas::CanvasMidiEvent* ev)
1206 if (ev->note() && _mouse_state == EraseTouchDragging) {
1207 start_delta_command(_("note entered"));
1209 _delta_command->remove(ev->note());
1210 } else if (_mouse_state == SelectTouchDragging) {
1211 note_selected(ev, true);
1217 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
1219 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
1221 display_model(msrc->model());
1225 MidiRegionView::set_frame_color()
1228 if (_selected && should_show_selection) {
1229 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
1231 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
1237 MidiRegionView::midi_channel_selection_changed(uint16_t selection)
1239 for(std::vector<ArdourCanvas::CanvasMidiEvent*>::iterator i = _events.begin();
1242 (*i)->on_channel_selection_change(selection);
1244 last_channel_selection = selection;