2 Copyright (C) 2001-2007 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <gtkmm2ext/gtk_ui.h>
28 #include <sigc++/signal.h>
30 #include <ardour/playlist.h>
31 #include <ardour/tempo.h>
32 #include <ardour/midi_region.h>
33 #include <ardour/midi_source.h>
34 #include <ardour/midi_diskstream.h>
35 #include <ardour/midi_model.h>
37 #include "streamview.h"
38 #include "midi_region_view.h"
39 #include "midi_streamview.h"
40 #include "midi_time_axis.h"
41 #include "simpleline.h"
42 #include "canvas-hit.h"
43 #include "canvas-note.h"
44 #include "canvas-program-change.h"
45 #include "public_editor.h"
46 #include "ghostregion.h"
47 #include "midi_time_axis.h"
48 #include "automation_time_axis.h"
49 #include "automation_region_view.h"
51 #include "midi_util.h"
52 #include "gui_thread.h"
58 using namespace ARDOUR;
60 using namespace Editing;
61 using namespace ArdourCanvas;
63 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color)
64 : RegionView (parent, tv, r, spu, basic_color)
66 , last_channel_selection(0xFFFF)
67 , _default_note_length(0.0)
69 , _note_group(new ArdourCanvas::Group(*parent))
70 , _delta_command(NULL)
74 group->lower_to_bottom();
75 _note_group->raise_to_top();
77 frame->property_fill_color_rgba() = 0xff000033;
80 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
81 : RegionView (parent, tv, r, spu, basic_color, visibility)
83 , last_channel_selection(0xFFFF)
84 , _default_note_length(0.0)
86 , _note_group(new ArdourCanvas::Group(*parent))
87 , _delta_command(NULL)
92 _note_group->raise_to_top();
96 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
99 midi_region()->midi_source(0)->load_model();
101 const Meter& m = trackview.session().tempo_map().meter_at(_region->position());
102 const Tempo& t = trackview.session().tempo_map().tempo_at(_region->position());
103 _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
106 _model = midi_region()->midi_source(0)->model();
107 _enable_display = false;
109 RegionView::init(basic_color, false);
111 compute_colors (basic_color);
113 reset_width_dependent_items ((double) _region->length() / samples_per_unit);
115 set_y_position_and_height (0, trackview.height);
118 region_resized (BoundsChanged);
121 _region->StateChanged.connect (mem_fun(*this, &MidiRegionView::region_changed));
125 _enable_display = true;
131 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
134 midi_region()->midi_source(0)->Switched.connect(sigc::mem_fun(this, &MidiRegionView::switch_source));
136 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
138 midi_view()->signal_force_channel_changed().connect(
139 mem_fun(this, &MidiRegionView::midi_force_channel_changed));
140 midi_view()->signal_channel_selection_changed().connect(
141 mem_fun(this, &MidiRegionView::midi_channel_selection_changed));
145 MidiRegionView::canvas_event(GdkEvent* ev)
147 static bool delete_mod = false;
148 static Editing::MidiEditMode original_mode;
150 static double drag_start_x, drag_start_y;
151 static double last_x, last_y;
152 double event_x, event_y;
153 nframes_t event_frame = 0;
155 static ArdourCanvas::SimpleRect* drag_rect = NULL;
157 if (trackview.editor.current_mouse_mode() != MouseNote)
164 if (ev->key.keyval == GDK_Delete && !delete_mod) {
166 original_mode = trackview.editor.current_midi_edit_mode();
167 trackview.editor.set_midi_edit_mode(MidiEditErase);
168 start_delta_command(_("erase notes"));
169 _mouse_state = EraseTouchDragging;
171 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
172 _mouse_state = SelectTouchDragging;
174 } else if (ev->key.keyval == GDK_Escape) {
180 case GDK_KEY_RELEASE:
181 if (ev->key.keyval == GDK_Delete) {
182 if (_mouse_state == EraseTouchDragging) {
187 trackview.editor.set_midi_edit_mode(original_mode);
192 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
198 case GDK_BUTTON_PRESS:
199 if (_mouse_state != SelectTouchDragging &&
200 _mouse_state != EraseTouchDragging &&
201 ev->button.button == 1 ) {
202 _pressed_button = ev->button.button;
203 _mouse_state = Pressed;
206 _pressed_button = ev->button.button;
209 case GDK_ENTER_NOTIFY:
210 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
211 Keyboard::magic_widget_grab_focus();
215 case GDK_MOTION_NOTIFY:
216 event_x = ev->motion.x;
217 event_y = ev->motion.y;
218 group->w2i(event_x, event_y);
220 // convert event_x to global frame
221 event_frame = trackview.editor.pixel_to_frame(event_x) + _region->position();
222 trackview.editor.snap_to(event_frame);
223 // convert event_frame back to local coordinates relative to position
224 event_frame -= _region->position();
226 switch (_mouse_state) {
227 case Pressed: // Drag start
230 if (_pressed_button == 1 && trackview.editor.current_midi_edit_mode() == MidiEditSelect) {
231 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
232 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
235 drag_start_x = event_x;
236 drag_start_y = event_y;
238 drag_rect = new ArdourCanvas::SimpleRect(*group);
239 drag_rect->property_x1() = event_x;
240 drag_rect->property_y1() = event_y;
241 drag_rect->property_x2() = event_x;
242 drag_rect->property_y2() = event_y;
243 drag_rect->property_outline_what() = 0xFF;
244 drag_rect->property_outline_color_rgba()
245 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
246 drag_rect->property_fill_color_rgba()
247 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
249 _mouse_state = SelectRectDragging;
252 // Add note drag start
253 } else if (trackview.editor.current_midi_edit_mode() == MidiEditPencil) {
254 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
255 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
258 drag_start_x = event_x;
259 drag_start_y = event_y;
261 drag_rect = new ArdourCanvas::SimpleRect(*group);
262 drag_rect->property_x1() = trackview.editor.frame_to_pixel(event_frame);
264 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
265 drag_rect->property_x2() = event_x;
266 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
267 drag_rect->property_outline_what() = 0xFF;
268 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
270 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
272 _mouse_state = AddDragging;
278 case SelectRectDragging: // Select drag motion
279 case AddDragging: // Add note drag motion
280 if (ev->motion.is_hint) {
283 GdkModifierType state;
284 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
289 if (_mouse_state == AddDragging)
290 event_x = trackview.editor.frame_to_pixel(event_frame);
293 if (event_x > drag_start_x)
294 drag_rect->property_x2() = event_x;
296 drag_rect->property_x1() = event_x;
298 if (drag_rect && _mouse_state == SelectRectDragging) {
299 if (event_y > drag_start_y)
300 drag_rect->property_y2() = event_y;
302 drag_rect->property_y1() = event_y;
304 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
310 case EraseTouchDragging:
311 case SelectTouchDragging:
319 case GDK_BUTTON_RELEASE:
320 event_x = ev->motion.x;
321 event_y = ev->motion.y;
322 group->w2i(event_x, event_y);
323 group->ungrab(ev->button.time);
324 event_frame = trackview.editor.pixel_to_frame(event_x);
326 if(_pressed_button != 1) {
330 switch (_mouse_state) {
331 case Pressed: // Clicked
332 switch (trackview.editor.current_midi_edit_mode()) {
338 create_note_at(event_x, event_y, _default_note_length);
344 case SelectRectDragging: // Select drag done
349 case AddDragging: // Add drag done
351 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
352 const double x = drag_rect->property_x1();
353 const double length = trackview.editor.pixel_to_frame(
354 drag_rect->property_x2() - drag_rect->property_x1());
356 create_note_at(x, drag_rect->property_y1(), length);
374 /** Add a note to the model, and the view, at a canvas (click) coordinate */
376 MidiRegionView::create_note_at(double x, double y, double duration)
378 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
379 MidiStreamView* const view = mtv->midi_view();
381 double note = midi_stream_view()->y_to_note(y);
384 assert(note <= 127.0);
386 nframes_t new_note_time = trackview.editor.pixel_to_frame (x);
387 assert(new_note_time >= 0);
388 new_note_time += _region->start();
391 const Meter& m = trackview.session().tempo_map().meter_at(new_note_time);
392 const Tempo& t = trackview.session().tempo_map().tempo_at(new_note_time);
393 double duration = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
396 // we need to snap here again in nframes_t in order to be sample accurate
397 // since note time is region-absolute but snap_to_frame expects position-relative
398 // time we have to coordinate transform back and forth here.
399 nframes_t new_note_time_position_relative = new_note_time - _region->start();
400 new_note_time = snap_to_frame(new_note_time_position_relative) + _region->start();
402 // we need to snap the duration too to be sample accurate
403 nframes_t new_note_duration = nframes_t(duration);
404 new_note_duration = snap_to_frame(new_note_time_position_relative + new_note_duration) + _region->start()
407 const boost::shared_ptr<Note> new_note(new Note(0, new_note_time, new_note_duration, (uint8_t)note, 0x40));
408 view->update_bounds(new_note->note());
410 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
412 _model->apply_command(cmd);
414 //add_note(new_note);
419 MidiRegionView::clear_events()
424 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
425 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
430 for (std::vector<CanvasNoteEvent*>::iterator i = _events.begin(); i != _events.end(); ++i)
434 _pgm_changes.clear();
439 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
449 MidiRegionView::redisplay_model()
451 // Don't redisplay the model if we're currently recording and displaying that
463 MidiModel::Notes notes = _model->notes();
464 cerr << endl << "Model contains " << notes.size() << " Notes:" << endl;
465 for(MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
466 Note note = *(*i).get();
467 cerr << "MODEL: Note time: " << note.time() << " duration: " << note.duration()
468 << " end-time: " << note.end_time()
469 << " velocity: " << int(note.velocity())
470 //<< " Note-on: " << note.on_event().
471 //<< " Note-off: " << note.off_event()
475 for (size_t i=0; i < _model->n_notes(); ++i) {
476 add_note(_model->note_at(i));
479 MidiModel::PgmChanges& pgm_changes = _model->pgm_changes();
480 for_each(pgm_changes.begin(), pgm_changes.end(),
481 sigc::mem_fun(this, &MidiRegionView::add_pgm_change));
485 /*for (Automatable::Controls::const_iterator i = _model->controls().begin();
486 i != _model->controls().end(); ++i) {
490 boost::shared_ptr<AutomationTimeAxisView> at
491 = midi_view()->automation_child(i->second->parameter());
495 Gdk::Color col = midi_stream_view()->get_region_color();
497 boost::shared_ptr<AutomationRegionView> arv;
500 Glib::Mutex::Lock list_lock (i->second->list()->lock());
502 arv = boost::shared_ptr<AutomationRegionView>(
503 new AutomationRegionView(at->canvas_display,
504 *at.get(), _region, i->second->list(),
505 midi_stream_view()->get_samples_per_unit(), col));
508 arv->set_duration(_region->length(), this);
509 arv->init(col, true);
511 _automation_children.insert(std::make_pair(i->second->parameter(), arv));
514 _model->read_unlock();
517 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
522 MidiRegionView::~MidiRegionView ()
524 in_destructor = true;
526 RegionViewGoingAway (this); /* EMIT_SIGNAL */
534 delete _delta_command;
539 MidiRegionView::region_resized (Change what_changed)
541 RegionView::region_resized(what_changed);
543 if (what_changed & ARDOUR::PositionChanged) {
550 MidiRegionView::reset_width_dependent_items (double pixel_width)
552 RegionView::reset_width_dependent_items(pixel_width);
553 assert(_pixel_width == pixel_width);
560 MidiRegionView::set_y_position_and_height (double y, double h)
562 RegionView::set_y_position_and_height(y, h - 1);
564 if (_enable_display) {
568 for (std::vector<CanvasNoteEvent*>::const_iterator i = _events.begin(); i != _events.end(); ++i) {
569 CanvasNoteEvent* event = *i;
570 Item* item = dynamic_cast<Item*>(event);
572 if (event && event->note()) {
573 if (event->note()->note() < midi_stream_view()->lowest_note() ||
574 event->note()->note() > midi_stream_view()->highest_note()) {
576 if (canvas_item_visible(item)) {
580 if (!canvas_item_visible(item)) {
584 event->hide_velocity();
585 if(CanvasNote* note = dynamic_cast<CanvasNote*>(event)) {
586 const double y1 = midi_stream_view()->note_to_y(event->note()->note());
587 const double y2 = y1 + floor(midi_stream_view()->note_height());
589 note->property_y1() = y1;
590 note->property_y2() = y2;
592 if(CanvasHit* hit = dynamic_cast<CanvasHit*>(event)) {
593 double x = trackview.editor.frame_to_pixel((nframes_t)
594 event->note()->time() - _region->start());
595 const double diamond_size = midi_stream_view()->note_height() / 2.0;
596 double y = midi_stream_view()->note_to_y(event->note()->note())
597 + ((diamond_size-2.0) / 4.0);
599 hit->set_height(diamond_size);
600 hit->move(x-hit->x1(), y-hit->y1());
603 if(event->selected()) {
604 event->show_velocity();
610 _model->read_unlock();
614 name_text->raise_to_top();
619 MidiRegionView::add_ghost (TimeAxisView& tv)
621 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
625 double unit_position = _region->position () / samples_per_unit;
626 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
627 MidiGhostRegion* ghost;
629 if (mtv && mtv->midi_view()) {
630 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group.
631 this is because it's nice to have midi notes on top of the note lines and
632 audio waveforms under it.
634 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
637 ghost = new MidiGhostRegion (tv, trackview, unit_position);
640 ghost->set_height ();
641 ghost->set_duration (_region->length() / samples_per_unit);
642 ghosts.push_back (ghost);
644 for (std::vector<CanvasNoteEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
645 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
646 ghost->add_note(note);
650 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
656 /** Begin tracking note state for successive calls to add_event
659 MidiRegionView::begin_write()
661 assert(!_active_notes);
662 _active_notes = new CanvasNote*[128];
663 for (unsigned i=0; i < 128; ++i)
664 _active_notes[i] = NULL;
668 /** Destroy note state for add_event
671 MidiRegionView::end_write()
673 delete[] _active_notes;
674 _active_notes = NULL;
675 _marked_for_selection.clear();
679 /** Resolve an active MIDI note (while recording).
682 MidiRegionView::resolve_note(uint8_t note, double end_time)
684 if (midi_view()->note_mode() != Sustained)
687 if (_active_notes && _active_notes[note]) {
688 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes_t)end_time);
689 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
690 _active_notes[note] = NULL;
695 /** Extend active notes to rightmost edge of region (if length is changed)
698 MidiRegionView::extend_active_notes()
703 for (unsigned i=0; i < 128; ++i)
704 if (_active_notes[i])
705 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
709 /** Add a MIDI note to the view (with duration).
711 * If in sustained mode, notes with duration 0 will be considered active
712 * notes, and resolve_note should be called when the corresponding note off
713 * event arrives, to properly display the note.
716 MidiRegionView::add_note(const boost::shared_ptr<Note> note)
718 assert(note->time() >= 0);
719 assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
721 // dont display notes beyond the region bounds
723 note->time() - _region->start() >= _region->length() ||
724 note->time() < _region->start() ||
725 note->note() < midi_stream_view()->lowest_note() ||
726 note->note() > midi_stream_view()->highest_note()
731 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
733 CanvasNoteEvent *event = 0;
735 const double x = trackview.editor.frame_to_pixel((nframes_t)note->time() - _region->start());
737 if (midi_view()->note_mode() == Sustained) {
739 //cerr << "MRV::add_note sustained " << note->note() << " @ " << note->time()
740 // << " .. " << note->end_time() << endl;
742 const double y1 = midi_stream_view()->note_to_y(note->note());
743 const double note_endpixel =
744 trackview.editor.frame_to_pixel((nframes_t)note->end_time() - _region->start());
746 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
747 ev_rect->property_x1() = x;
748 ev_rect->property_y1() = y1;
749 if (note->duration() > 0)
750 ev_rect->property_x2() = note_endpixel;
752 ev_rect->property_x2() = trackview.editor.frame_to_pixel(_region->length());
753 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
755 if (note->duration() == 0) {
756 _active_notes[note->note()] = ev_rect;
757 /* outline all but right edge */
758 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
760 /* outline all edges */
761 ev_rect->property_outline_what() = (guint32) 0xF;
765 _events.push_back(ev_rect);
770 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
771 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
772 gr->add_note(ev_rect);
776 } else if (midi_view()->note_mode() == Percussive) {
778 //cerr << "MRV::add_note percussive " << note->note() << " @ " << note->time()
779 // << " .. " << note->end_time() << endl;
781 const double diamond_size = midi_stream_view()->note_height() / 2.0;
782 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
784 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size, note);
785 ev_diamond->move(x, y);
787 _events.push_back(ev_diamond);
794 if(_marked_for_selection.find(event->note()) != _marked_for_selection.end()) {
795 note_selected(event, true);
797 event->on_channel_selection_change(last_channel_selection);
802 MidiRegionView::add_pgm_change(boost::shared_ptr<MIDI::Event> event)
804 assert(event->time() >= 0);
806 // dont display notes beyond the region bounds
808 event->time() - _region->start() >= _region->length() ||
809 event->time() < _region->start()
814 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
815 const double x = trackview.editor.frame_to_pixel((nframes_t)event->time() - _region->start());
817 double height = midi_stream_view()->contents_height();
818 _pgm_changes.push_back(
819 boost::shared_ptr<CanvasProgramChange>(
820 new CanvasProgramChange(*this, *group, event, height, x, 1.0)));
824 MidiRegionView::delete_selection()
826 assert(_delta_command);
828 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
829 if ((*i)->selected())
830 _delta_command->remove((*i)->note());
836 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev)
838 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
839 if ((*i)->selected() && (*i) != ev)
840 (*i)->selected(false);
846 MidiRegionView::unique_select(ArdourCanvas::CanvasNoteEvent* ev)
848 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
850 (*i)->selected(false);
853 _selection.insert(ev);
855 if ( ! ev->selected())
860 MidiRegionView::note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
863 clear_selection_except(ev);
865 _selection.insert(ev);
867 if ( ! ev->selected())
873 MidiRegionView::note_deselected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
876 clear_selection_except(ev);
878 _selection.erase(ev);
886 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
888 const double last_y = std::min(y1, y2);
889 const double y = std::max(y1, y2);
891 // FIXME: so, so, so much slower than this should be
894 for (std::vector<CanvasNoteEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
895 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
896 (*i)->selected(true);
897 _selection.insert(*i);
899 (*i)->selected(false);
900 _selection.erase(*i);
904 for (std::vector<CanvasNoteEvent*>::iterator i = _events.begin(); i != _events.end(); ++i) {
905 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
906 (*i)->selected(true);
907 _selection.insert(*i);
909 (*i)->selected(false);
910 _selection.erase(*i);
918 MidiRegionView::move_selection(double dx, double dy)
920 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
921 (*i)->move_event(dx, dy);
926 MidiRegionView::note_dropped(CanvasNoteEvent* ev, double dt, uint8_t dnote)
928 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
929 if (_selection.find(ev) != _selection.end()) {
930 uint8_t lowest_note_in_selection = midi_stream_view()->lowest_note();
931 uint8_t highest_note_in_selection = midi_stream_view()->highest_note();
932 uint8_t highest_note_difference = 0;
934 // find highest and lowest notes first
935 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
936 Selection::iterator next = i;
939 uint8_t pitch = (*i)->note()->note();
940 lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
941 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
947 cerr << "dnote: " << (int) dnote << endl;
948 cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
949 << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
950 cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
951 << int(highest_note_in_selection) << endl;
952 cerr << "selection size: " << _selection.size() << endl;
953 cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
956 // Make sure the note pitch does not exceed the MIDI standard range
957 if (dnote <= 127 && (highest_note_in_selection + dnote > 127)) {
958 highest_note_difference = highest_note_in_selection - 127;
961 start_delta_command(_("move notes"));
963 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
964 Selection::iterator next = i;
967 const boost::shared_ptr<Note> copy(new Note(*(*i)->note().get()));
969 // we need to snap here again in nframes_t in order to be sample accurate
970 double new_note_time = (*i)->note()->time();
973 // keep notes inside region if dragged beyond left region bound
974 if(new_note_time < _region->start()) {
975 new_note_time = _region->start();
978 // since note time is region-absolute but snap_to_frame expects position-relative
979 // time we have to coordinate transform back and forth here.
980 new_note_time = snap_to_frame(nframes_t(new_note_time) - _region->start()) + _region->start();
982 copy->set_time(new_note_time);
984 uint8_t original_pitch = (*i)->note()->note();
985 uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
987 // keep notes in standard midi range
988 clamp_0_to_127(new_pitch);
990 //notes which are dragged beyond the standard midi range snap back to their original place
991 if((original_pitch != 0 && new_pitch == 0) || (original_pitch != 127 && new_pitch == 127)) {
992 new_pitch = original_pitch;
995 lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
996 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
998 copy->set_note(new_pitch);
1000 command_remove_note(*i);
1001 command_add_note(copy);
1003 _marked_for_selection.insert(copy);
1008 // care about notes being moved beyond the upper/lower bounds on the canvas
1009 if(lowest_note_in_selection < midi_stream_view()->lowest_note() ||
1010 highest_note_in_selection > midi_stream_view()->highest_note()
1012 midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
1018 MidiRegionView::snap_to_frame(double x)
1020 PublicEditor &editor = trackview.editor;
1021 // x is region relative
1022 // convert x to global frame
1023 nframes_t frame = editor.pixel_to_frame(x) + _region->position();
1024 editor.snap_to(frame);
1025 // convert event_frame back to local coordinates relative to position
1026 frame -= _region->position();
1031 MidiRegionView::snap_to_frame(nframes_t x)
1033 PublicEditor &editor = trackview.editor;
1034 // x is region relative
1035 // convert x to global frame
1036 nframes_t frame = x + _region->position();
1037 editor.snap_to(frame);
1038 // convert event_frame back to local coordinates relative to position
1039 frame -= _region->position();
1044 MidiRegionView::snap_to_pixel(double x)
1046 return (double) trackview.editor.frame_to_pixel(snap_to_frame(x));
1050 MidiRegionView::get_position_pixels(void)
1052 nframes_t region_frame = get_position();
1053 return trackview.editor.frame_to_pixel(region_frame);
1057 MidiRegionView::begin_resizing(CanvasNote::NoteEnd note_end)
1059 _resize_data.clear();
1061 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1062 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
1064 // only insert CanvasNotes into the map
1066 NoteResizeData *resize_data = new NoteResizeData();
1067 resize_data->canvas_note = note;
1069 // create a new SimpleRect from the note which will be the resize preview
1070 SimpleRect *resize_rect =
1078 // calculate the colors: get the color settings
1081 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get(),
1084 // make the resize preview notes more transparent and bright
1085 fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
1087 // calculate color based on note velocity
1088 resize_rect->property_fill_color_rgba() =
1090 note_fill_color(note->note()->velocity()),
1094 resize_rect->property_outline_color_rgba() =
1095 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get();
1097 resize_data->resize_rect = resize_rect;
1099 if(note_end == CanvasNote::NOTE_ON) {
1100 resize_data->current_x = note->x1();
1101 } else { // NOTE_OFF
1102 resize_data->current_x = note->x2();
1105 _resize_data.push_back(resize_data);
1111 MidiRegionView::update_resizing(CanvasNote::NoteEnd note_end, double x, bool relative)
1113 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1114 SimpleRect *resize_rect = (*i)->resize_rect;
1115 CanvasNote *canvas_note = (*i)->canvas_note;
1117 const double region_start = get_position_pixels();
1120 (*i)->current_x = (*i)->current_x + x;
1122 // x is in track relative, transform it to region relative
1123 (*i)->current_x = x - region_start;
1126 double current_x = (*i)->current_x;
1128 if(note_end == CanvasNote::NOTE_ON) {
1129 resize_rect->property_x1() = snap_to_pixel(current_x);
1130 resize_rect->property_x2() = canvas_note->x2();
1132 resize_rect->property_x2() = snap_to_pixel(current_x);
1133 resize_rect->property_x1() = canvas_note->x1();
1139 MidiRegionView::commit_resizing(CanvasNote::NoteEnd note_end, double event_x, bool relative)
1141 start_delta_command(_("resize notes"));
1143 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1144 CanvasNote *canvas_note = (*i)->canvas_note;
1145 SimpleRect *resize_rect = (*i)->resize_rect;
1146 double current_x = (*i)->current_x;
1147 const double position = get_position_pixels();
1150 // event_x is in track relative, transform it to region relative
1151 current_x = event_x - position;
1154 // because snapping works on world coordinates we have to transform current_x
1155 // to world coordinates before snapping and transform it back afterwards
1156 nframes_t current_frame = snap_to_frame(current_x);
1157 // transform to region start relative
1158 current_frame += _region->start();
1160 const boost::shared_ptr<Note> copy(new Note(*(canvas_note->note().get())));
1162 // resize beginning of note
1163 if (note_end == CanvasNote::NOTE_ON && current_frame < copy->end_time()) {
1164 command_remove_note(canvas_note);
1165 copy->on_event().time() = current_frame;
1166 command_add_note(copy);
1167 _marked_for_selection.insert(copy);
1169 // resize end of note
1170 if (note_end == CanvasNote::NOTE_OFF && current_frame > copy->time()) {
1171 command_remove_note(canvas_note);
1172 command_remove_note(canvas_note);
1173 copy->off_event().time() = current_frame;
1174 command_add_note(copy);
1175 _marked_for_selection.insert(copy);
1182 _resize_data.clear();
1188 MidiRegionView::change_velocity(uint8_t velocity, bool relative)
1190 start_delta_command(_("change velocity"));
1191 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1192 Selection::iterator next = i;
1195 CanvasNoteEvent *event = *i;
1196 const boost::shared_ptr<Note> copy(new Note(*(event->note().get())));
1199 uint8_t new_velocity = copy->velocity() + velocity;
1200 clamp_0_to_127(new_velocity);
1202 copy->set_velocity(new_velocity);
1203 } else { // absolute
1204 copy->set_velocity(velocity);
1207 command_remove_note(event);
1208 command_add_note(copy);
1210 _marked_for_selection.insert(copy);
1214 // dont keep notes selected if tweaking a single note
1215 if(_marked_for_selection.size() == 1) {
1216 _marked_for_selection.clear();
1223 MidiRegionView::change_channel(uint8_t channel)
1225 start_delta_command(_("change channel"));
1226 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1227 Selection::iterator next = i;
1230 CanvasNoteEvent *event = *i;
1231 const boost::shared_ptr<Note> copy(new Note(*(event->note().get())));
1233 copy->set_channel(channel);
1235 command_remove_note(event);
1236 command_add_note(copy);
1238 _marked_for_selection.insert(copy);
1242 // dont keep notes selected if tweaking a single note
1243 if(_marked_for_selection.size() == 1) {
1244 _marked_for_selection.clear();
1252 MidiRegionView::note_entered(ArdourCanvas::CanvasNoteEvent* ev)
1254 if (ev->note() && _mouse_state == EraseTouchDragging) {
1255 start_delta_command(_("note entered"));
1257 _delta_command->remove(ev->note());
1258 } else if (_mouse_state == SelectTouchDragging) {
1259 note_selected(ev, true);
1265 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
1267 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
1269 display_model(msrc->model());
1273 MidiRegionView::set_frame_color()
1276 if (_selected && should_show_selection) {
1277 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
1279 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
1285 MidiRegionView::midi_force_channel_changed(int8_t channel)
1287 force_channel = channel;
1291 MidiRegionView::midi_channel_selection_changed(uint16_t selection)
1293 if(force_channel >= 0) {
1297 for(std::vector<ArdourCanvas::CanvasNoteEvent*>::iterator i = _events.begin();
1300 (*i)->on_channel_selection_change(selection);
1302 last_channel_selection = selection;