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>
36 #include <ardour/midi_patch_manager.h>
38 #include <evoral/Parameter.hpp>
39 #include <evoral/Control.hpp>
41 #include "streamview.h"
42 #include "midi_region_view.h"
43 #include "midi_streamview.h"
44 #include "midi_time_axis.h"
45 #include "simpleline.h"
46 #include "canvas-hit.h"
47 #include "canvas-note.h"
48 #include "canvas-program-change.h"
49 #include "public_editor.h"
50 #include "ghostregion.h"
51 #include "midi_time_axis.h"
52 #include "automation_time_axis.h"
53 #include "automation_region_view.h"
55 #include "midi_util.h"
56 #include "gui_thread.h"
62 using namespace ARDOUR;
64 using namespace Editing;
65 using namespace ArdourCanvas;
67 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color)
68 : RegionView (parent, tv, r, spu, basic_color)
70 , _last_channel_selection(0xFFFF)
71 , _default_note_length(0.0)
72 , _current_range_min(0)
73 , _current_range_max(0)
74 , _model_name(string())
75 , _custom_device_mode(string())
77 , _note_group(new ArdourCanvas::Group(*parent))
78 , _delta_command(NULL)
82 _note_group->raise_to_top();
85 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
86 : RegionView (parent, tv, r, spu, basic_color, false, visibility)
88 , _last_channel_selection(0xFFFF)
89 , _default_note_length(0.0)
90 , _model_name(string())
91 , _custom_device_mode(string())
93 , _note_group(new ArdourCanvas::Group(*parent))
94 , _delta_command(NULL)
99 _note_group->raise_to_top();
103 MidiRegionView::MidiRegionView (const MidiRegionView& other)
106 , _last_channel_selection(0xFFFF)
107 , _default_note_length(0.0)
108 , _model_name(string())
109 , _custom_device_mode(string())
111 , _note_group(new ArdourCanvas::Group(*get_canvas_group()))
112 , _delta_command(NULL)
119 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
120 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
125 MidiRegionView::MidiRegionView (const MidiRegionView& other, boost::shared_ptr<MidiRegion> other_region)
126 : RegionView (other, boost::shared_ptr<Region> (other_region))
128 , _last_channel_selection(0xFFFF)
129 , _default_note_length(0.0)
130 , _model_name(string())
131 , _custom_device_mode(string())
133 , _note_group(new ArdourCanvas::Group(*get_canvas_group()))
134 , _delta_command(NULL)
141 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
142 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
148 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
151 midi_region()->midi_source(0)->load_model();
153 const Meter& m = trackview.session().tempo_map().meter_at(_region->position());
154 const Tempo& t = trackview.session().tempo_map().tempo_at(_region->position());
155 _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
158 _model = midi_region()->midi_source(0)->model();
159 _enable_display = false;
161 RegionView::init (basic_color, false);
163 compute_colors (basic_color);
165 set_height (trackview.current_height());
168 region_sync_changed ();
169 region_resized (BoundsChanged);
172 reset_width_dependent_items (_pixel_width);
173 //reset_width_dependent_items ((double) _region->length() / samples_per_unit);
177 _enable_display = true;
182 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
185 group->raise_to_top();
186 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
188 midi_view()->signal_channel_mode_changed().connect(
189 mem_fun(this, &MidiRegionView::midi_channel_mode_changed));
191 midi_view()->signal_midi_patch_settings_changed().connect(
192 mem_fun(this, &MidiRegionView::midi_patch_settings_changed));
196 MidiRegionView::canvas_event(GdkEvent* ev)
198 static bool delete_mod = false;
199 static Editing::MidiEditMode original_mode;
201 static double drag_start_x, drag_start_y;
202 static double last_x, last_y;
203 double event_x, event_y;
204 nframes64_t event_frame = 0;
206 static ArdourCanvas::SimpleRect* drag_rect = NULL;
208 if (trackview.editor.current_mouse_mode() != MouseNote)
215 if (ev->key.keyval == GDK_Delete && !delete_mod) {
217 original_mode = trackview.editor.current_midi_edit_mode();
218 trackview.editor.set_midi_edit_mode(MidiEditErase);
219 start_delta_command(_("erase notes"));
220 _mouse_state = EraseTouchDragging;
222 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
223 _mouse_state = SelectTouchDragging;
225 } else if (ev->key.keyval == GDK_Escape) {
231 case GDK_KEY_RELEASE:
232 if (ev->key.keyval == GDK_Delete) {
233 if (_mouse_state == EraseTouchDragging) {
238 trackview.editor.set_midi_edit_mode(original_mode);
243 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
249 case GDK_BUTTON_PRESS:
250 if (_mouse_state != SelectTouchDragging &&
251 _mouse_state != EraseTouchDragging &&
252 ev->button.button == 1) {
253 _pressed_button = ev->button.button;
254 _mouse_state = Pressed;
257 _pressed_button = ev->button.button;
260 case GDK_2BUTTON_PRESS:
263 case GDK_ENTER_NOTIFY:
264 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
265 Keyboard::magic_widget_grab_focus();
269 case GDK_MOTION_NOTIFY:
270 event_x = ev->motion.x;
271 event_y = ev->motion.y;
272 group->w2i(event_x, event_y);
274 // convert event_x to global frame
275 event_frame = trackview.editor.pixel_to_frame(event_x) + _region->position();
276 trackview.editor.snap_to(event_frame);
277 // convert event_frame back to local coordinates relative to position
278 event_frame -= _region->position();
280 switch (_mouse_state) {
281 case Pressed: // Drag start
284 if (_pressed_button == 1 && trackview.editor.current_midi_edit_mode() == MidiEditSelect) {
285 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
286 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
289 drag_start_x = event_x;
290 drag_start_y = event_y;
292 drag_rect = new ArdourCanvas::SimpleRect(*group);
293 drag_rect->property_x1() = event_x;
294 drag_rect->property_y1() = event_y;
295 drag_rect->property_x2() = event_x;
296 drag_rect->property_y2() = event_y;
297 drag_rect->property_outline_what() = 0xFF;
298 drag_rect->property_outline_color_rgba()
299 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
300 drag_rect->property_fill_color_rgba()
301 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
303 _mouse_state = SelectRectDragging;
306 // Add note drag start
307 } else if (trackview.editor.current_midi_edit_mode() == MidiEditPencil) {
308 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
309 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
312 drag_start_x = event_x;
313 drag_start_y = event_y;
315 drag_rect = new ArdourCanvas::SimpleRect(*group);
316 drag_rect->property_x1() = trackview.editor.frame_to_pixel(event_frame);
318 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
319 drag_rect->property_x2() = event_x;
320 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
321 drag_rect->property_outline_what() = 0xFF;
322 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
324 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
326 _mouse_state = AddDragging;
332 case SelectRectDragging: // Select drag motion
333 case AddDragging: // Add note drag motion
334 if (ev->motion.is_hint) {
337 GdkModifierType state;
338 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
343 if (_mouse_state == AddDragging)
344 event_x = trackview.editor.frame_to_pixel(event_frame);
347 if (event_x > drag_start_x)
348 drag_rect->property_x2() = event_x;
350 drag_rect->property_x1() = event_x;
353 if (drag_rect && _mouse_state == SelectRectDragging) {
354 if (event_y > drag_start_y)
355 drag_rect->property_y2() = event_y;
357 drag_rect->property_y1() = event_y;
359 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
365 case EraseTouchDragging:
366 case SelectTouchDragging:
374 case GDK_BUTTON_RELEASE:
375 event_x = ev->motion.x;
376 event_y = ev->motion.y;
377 group->w2i(event_x, event_y);
378 group->ungrab(ev->button.time);
379 event_frame = trackview.editor.pixel_to_frame(event_x);
381 if (_pressed_button != 1) {
385 switch (_mouse_state) {
386 case Pressed: // Clicked
387 switch (trackview.editor.current_midi_edit_mode()) {
393 create_note_at(event_x, event_y, _default_note_length);
398 case SelectRectDragging: // Select drag done
403 case AddDragging: // Add drag done
405 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
406 const double x = drag_rect->property_x1();
407 const double length = trackview.editor.pixel_to_frame(
408 drag_rect->property_x2() - drag_rect->property_x1());
410 create_note_at(x, drag_rect->property_y1(), length);
425 /** Add a note to the model, and the view, at a canvas (click) coordinate */
427 MidiRegionView::create_note_at(double x, double y, double length)
429 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
430 MidiStreamView* const view = mtv->midi_view();
432 double note = midi_stream_view()->y_to_note(y);
435 assert(note <= 127.0);
437 nframes64_t new_note_time = trackview.editor.pixel_to_frame (x);
438 assert(new_note_time >= 0);
439 new_note_time += _region->start();
442 const Meter& m = trackview.session().tempo_map().meter_at(new_note_time);
443 const Tempo& t = trackview.session().tempo_map().tempo_at(new_note_time);
444 double length = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
447 // we need to snap here again in nframes64_t in order to be sample accurate
448 // since note time is region-absolute but snap_to_frame expects position-relative
449 // time we have to coordinate transform back and forth here.
450 nframes64_t new_note_time_position_relative = new_note_time - _region->start();
451 new_note_time = snap_to_frame(new_note_time_position_relative) + _region->start();
453 // we need to snap the length too to be sample accurate
454 nframes64_t new_note_length = nframes_t(length);
455 new_note_length = snap_to_frame(new_note_time_position_relative + new_note_length) + _region->start()
458 const boost::shared_ptr<Evoral::Note> new_note(new Evoral::Note(
459 0, new_note_time, new_note_length, (uint8_t)note, 0x40));
460 view->update_note_range(new_note->note());
462 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
464 _model->apply_command(trackview.session(), cmd);
469 MidiRegionView::clear_events()
474 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
475 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
480 for (Events::iterator i = _events.begin(); i != _events.end(); ++i)
484 _pgm_changes.clear();
489 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
499 MidiRegionView::start_delta_command(string name)
502 _delta_command = _model->new_delta_command(name);
506 MidiRegionView::command_add_note(const boost::shared_ptr<Evoral::Note> note, bool selected)
509 _delta_command->add(note);
512 _marked_for_selection.insert(note);
516 MidiRegionView::command_remove_note(ArdourCanvas::CanvasNoteEvent* ev)
518 if (_delta_command && ev->note()) {
519 _delta_command->remove(ev->note());
524 MidiRegionView::apply_command()
526 if (!_delta_command) {
530 // Mark all selected notes for selection when model reloads
531 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
532 _marked_for_selection.insert((*i)->note());
535 _model->apply_command(trackview.session(), _delta_command);
536 _delta_command = NULL;
537 midi_view()->midi_track()->diskstream()->playlist_modified();
539 _marked_for_selection.clear();
544 MidiRegionView::abort_command()
546 delete _delta_command;
547 _delta_command = NULL;
553 MidiRegionView::redisplay_model()
555 // Don't redisplay the model if we're currently recording and displaying that
565 MidiModel::Notes notes = _model->notes();
567 cerr << endl << _model->midi_source()->name() << " : redisplaying " << notes.size() << " notes:" << endl;
568 for (MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
569 cerr << "NOTE time: " << (*i)->time()
570 << " pitch: " << int((*i)->note())
571 << " length: " << (*i)->length()
572 << " end-time: " << (*i)->end_time()
573 << " velocity: " << int((*i)->velocity())
578 for (size_t i = 0; i < _model->n_notes(); ++i) {
579 add_note(_model->note_at(i));
582 find_and_insert_program_change_flags();
584 // Is this necessary?
585 /*for (Automatable::Controls::const_iterator i = _model->controls().begin();
586 i != _model->controls().end(); ++i) {
590 boost::shared_ptr<AutomationTimeAxisView> at
591 = midi_view()->automation_child(i->second->parameter());
595 Gdk::Color col = midi_stream_view()->get_region_color();
597 boost::shared_ptr<AutomationRegionView> arv;
600 Glib::Mutex::Lock list_lock (i->second->list()->lock());
602 arv = boost::shared_ptr<AutomationRegionView>(
603 new AutomationRegionView(at->canvas_display,
604 *at.get(), _region, i->second->list(),
605 midi_stream_view()->get_samples_per_unit(), col));
608 arv->set_duration(_region->length(), this);
609 arv->init(col, true);
611 _automation_children.insert(std::make_pair(i->second->parameter(), arv));
613 _model->read_unlock();
616 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
621 MidiRegionView::find_and_insert_program_change_flags()
623 // Draw program change 'flags'
624 for (Automatable::Controls::iterator control = _model->controls().begin();
625 control != _model->controls().end(); ++control) {
626 if (control->first.type() == MidiPgmChangeAutomation) {
627 Glib::Mutex::Lock list_lock (control->second->list()->lock());
629 uint8_t channel = control->first.channel();
631 for (AutomationList::const_iterator event = control->second->list()->begin();
632 event != control->second->list()->end(); ++event) {
633 double event_time = (*event)->when;
634 double program_number = floor((*event)->value + 0.5);
636 //cerr << " got program change on channel " << int(channel) << " time: " << event_time << " number: " << program_number << endl;
638 // find bank select msb and lsb for the program change
639 Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
640 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
642 if (msb_control != 0) {
643 msb = uint8_t(floor(msb_control->get_float(true, event_time) + 0.5));
646 Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
647 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
649 if (lsb_control != 0) {
650 lsb = uint8_t(floor(lsb_control->get_float(true, event_time) + 0.5));
653 //cerr << " got msb " << int(msb) << " and lsb " << int(lsb) << " thread_id: " << pthread_self() << endl;
655 MIDI::Name::PatchPrimaryKey patch_key(msb, lsb, program_number);
657 boost::shared_ptr<MIDI::Name::Patch> patch =
658 MIDI::Name::MidiPatchManager::instance().find_patch(
665 ControlEvent program_change(nframes_t(event_time), uint8_t(program_number), channel);
668 //cerr << " got patch with name " << patch->name() << " number " << patch->number() << endl;
669 add_pgm_change(program_change, patch->name());
672 snprintf(buf, 4, "%d", int(program_number));
673 add_pgm_change(program_change, buf);
677 } else if (control->first.type() == MidiCCAutomation) {
678 //cerr << " found CC Automation of channel " << int(control->first.channel()) << " and id " << control->first.id() << endl;
684 MidiRegionView::~MidiRegionView ()
686 in_destructor = true;
688 RegionViewGoingAway (this); /* EMIT_SIGNAL */
697 delete _delta_command;
702 MidiRegionView::region_resized (Change what_changed)
704 RegionView::region_resized(what_changed);
706 if (what_changed & ARDOUR::PositionChanged) {
713 MidiRegionView::reset_width_dependent_items (double pixel_width)
715 RegionView::reset_width_dependent_items(pixel_width);
716 assert(_pixel_width == pixel_width);
723 MidiRegionView::set_height (gdouble height)
725 static const double FUDGE = 2;
726 const double old_height = _height;
727 RegionView::set_height(height);
728 _height = height - FUDGE;
730 apply_note_range(midi_stream_view()->lowest_note(),
731 midi_stream_view()->highest_note(),
732 height != old_height + FUDGE);
735 name_text->raise_to_top();
740 /** Apply the current note range from the stream view
741 * by repositioning/hiding notes as necessary
744 MidiRegionView::apply_note_range (uint8_t min, uint8_t max, bool force)
746 if (_enable_display) {
747 if (!force && _current_range_min == min && _current_range_max == max) {
751 _current_range_min = min;
752 _current_range_max = max;
754 for (Events::const_iterator i = _events.begin(); i != _events.end(); ++i) {
755 CanvasNoteEvent* event = *i;
756 Item* item = dynamic_cast<Item*>(event);
758 if (event && event->note()) {
759 if (event->note()->note() < _current_range_min || event->note()->note() > _current_range_max) {
760 if (canvas_item_visible(item)) {
764 if (!canvas_item_visible(item)) {
768 event->hide_velocity();
769 if (CanvasNote* note = dynamic_cast<CanvasNote*>(event)) {
770 const double y1 = midi_stream_view()->note_to_y(event->note()->note());
771 const double y2 = y1 + floor(midi_stream_view()->note_height());
773 note->property_y1() = y1;
774 note->property_y2() = y2;
775 } else if (CanvasHit* hit = dynamic_cast<CanvasHit*>(event)) {
776 double x = trackview.editor.frame_to_pixel((nframes64_t)
777 event->note()->time() - _region->start());
778 const double diamond_size = midi_stream_view()->note_height() / 2.0;
779 double y = midi_stream_view()->note_to_y(event->note()->note())
780 + ((diamond_size-2.0) / 4.0);
782 hit->set_height(diamond_size);
783 hit->move(x-hit->x1(), y-hit->y1());
786 if (event->selected()) {
787 event->show_velocity();
797 MidiRegionView::add_ghost (TimeAxisView& tv)
799 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
803 double unit_position = _region->position () / samples_per_unit;
804 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
805 MidiGhostRegion* ghost;
807 if (mtv && mtv->midi_view()) {
808 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group.
809 this is because it's nice to have midi notes on top of the note lines and
810 audio waveforms under it.
812 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
815 ghost = new MidiGhostRegion (tv, trackview, unit_position);
818 ghost->set_height ();
819 ghost->set_duration (_region->length() / samples_per_unit);
820 ghosts.push_back (ghost);
822 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
823 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
824 ghost->add_note(note);
828 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
834 /** Begin tracking note state for successive calls to add_event
837 MidiRegionView::begin_write()
839 assert(!_active_notes);
840 _active_notes = new CanvasNote*[128];
841 for (unsigned i=0; i < 128; ++i) {
842 _active_notes[i] = NULL;
847 /** Destroy note state for add_event
850 MidiRegionView::end_write()
852 delete[] _active_notes;
853 _active_notes = NULL;
854 _marked_for_selection.clear();
858 /** Resolve an active MIDI note (while recording).
861 MidiRegionView::resolve_note(uint8_t note, double end_time)
863 if (midi_view()->note_mode() != Sustained)
866 if (_active_notes && _active_notes[note]) {
867 _active_notes[note]->property_x2() = trackview.editor.frame_to_pixel((nframes64_t)end_time);
868 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
869 _active_notes[note] = NULL;
874 /** Extend active notes to rightmost edge of region (if length is changed)
877 MidiRegionView::extend_active_notes()
879 if (!_active_notes) {
883 for (unsigned i=0; i < 128; ++i) {
884 if (_active_notes[i]) {
885 _active_notes[i]->property_x2() = trackview.editor.frame_to_pixel(_region->length());
891 /** Add a MIDI note to the view (with length).
893 * If in sustained mode, notes with length 0 will be considered active
894 * notes, and resolve_note should be called when the corresponding note off
895 * event arrives, to properly display the note.
898 MidiRegionView::add_note(const boost::shared_ptr<Evoral::Note> note)
900 assert(note->time() >= 0);
901 assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
903 // dont display notes beyond the region bounds
904 if ( note->time() - _region->start() >= _region->length() ||
905 note->time() < _region->start() ||
906 note->note() < midi_stream_view()->lowest_note() ||
907 note->note() > midi_stream_view()->highest_note() ) {
911 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
913 CanvasNoteEvent* event = 0;
915 const double x = trackview.editor.frame_to_pixel((nframes64_t)note->time() - _region->start());
917 if (midi_view()->note_mode() == Sustained) {
919 const double y1 = midi_stream_view()->note_to_y(note->note());
920 const double note_endpixel =
921 trackview.editor.frame_to_pixel((nframes64_t)note->end_time() - _region->start());
923 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
924 ev_rect->property_x1() = x;
925 ev_rect->property_y1() = y1;
926 if (note->length() > 0)
927 ev_rect->property_x2() = note_endpixel;
929 ev_rect->property_x2() = trackview.editor.frame_to_pixel(_region->length());
930 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
932 if (note->length() == 0) {
935 assert(note->note() < 128);
936 // If this note is already active there's a stuck note,
937 // finish the old note rectangle
938 if (_active_notes[note->note()]) {
939 CanvasNote* const old_rect = _active_notes[note->note()];
940 boost::shared_ptr<Evoral::Note> old_note = old_rect->note();
941 cerr << "MidiModel: WARNING: Note has length 0: chan " << old_note->channel()
942 << "note " << (int)old_note->note() << " @ " << old_note->time() << endl;
943 /* FIXME: How large to make it? Make it a diamond? */
944 old_rect->property_x2() = old_rect->property_x1() + 2.0;
945 old_rect->property_outline_what() = (guint32) 0xF;
947 _active_notes[note->note()] = ev_rect;
949 /* outline all but right edge */
950 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
952 /* outline all edges */
953 ev_rect->property_outline_what() = (guint32) 0xF;
957 _events.push_back(ev_rect);
962 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
963 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
964 gr->add_note(ev_rect);
968 } else if (midi_view()->note_mode() == Percussive) {
970 //cerr << "MRV::add_note percussive " << note->note() << " @ " << note->time()
971 // << " .. " << note->end_time() << endl;
973 const double diamond_size = midi_stream_view()->note_height() / 2.0;
974 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
976 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size, note);
977 ev_diamond->move(x, y);
979 _events.push_back(ev_diamond);
986 if (_marked_for_selection.find(note) != _marked_for_selection.end()) {
987 note_selected(event, true);
989 event->on_channel_selection_change(_last_channel_selection);
994 MidiRegionView::add_pgm_change(ControlEvent& program, string displaytext)
996 assert(program.time >= 0);
998 // dont display program changes beyond the region bounds
999 if (program.time - _region->start() >= _region->length() || program.time < _region->start())
1002 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
1003 const double x = trackview.editor.frame_to_pixel((nframes64_t)program.time - _region->start());
1005 double height = midi_stream_view()->contents_height();
1007 boost::shared_ptr<CanvasProgramChange> pgm_change = boost::shared_ptr<CanvasProgramChange>(
1008 new CanvasProgramChange(
1016 _custom_device_mode,
1021 _pgm_changes.push_back(pgm_change);
1025 MidiRegionView::get_patch_key_at(double time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key)
1028 cerr << "getting patch key at " << time << " for channel " << channel << endl;
1029 Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
1030 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
1032 if (msb_control != 0) {
1033 msb = int(msb_control->get_float(true, time));
1034 cerr << "got msb " << msb;
1037 Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
1038 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
1040 if (lsb_control != 0) {
1041 lsb = lsb_control->get_float(true, time);
1042 cerr << " got lsb " << lsb;
1045 Evoral::Parameter program_change(MidiPgmChangeAutomation, channel, 0);
1046 boost::shared_ptr<Evoral::Control> program_control = _model->control(program_change);
1047 float program_number = -1.0;
1048 if (program_control != 0) {
1049 program_number = program_control->get_float(true, time);
1050 cerr << " got program " << program_number << endl;
1053 key.msb = (int) floor(msb + 0.5);
1054 key.lsb = (int) floor(lsb + 0.5);
1055 key.program_number = (int) floor(program_number + 0.5);
1056 assert(key.is_sane());
1061 MidiRegionView::alter_program_change(ControlEvent& old_program, const MIDI::Name::PatchPrimaryKey& new_patch)
1064 // TODO: Get the real event here and alter them at the original times
1065 Evoral::Parameter bank_select_msb(MidiCCAutomation, old_program.channel, MIDI_CTL_MSB_BANK);
1066 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
1067 if (msb_control != 0) {
1068 msb_control->set_float(float(new_patch.msb), true, old_program.time);
1071 // TODO: Get the real event here and alter them at the original times
1072 Evoral::Parameter bank_select_lsb(MidiCCAutomation, old_program.channel, MIDI_CTL_LSB_BANK);
1073 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
1074 if (lsb_control != 0) {
1075 lsb_control->set_float(float(new_patch.lsb), true, old_program.time);
1078 Evoral::Parameter program_change(MidiPgmChangeAutomation, old_program.channel, 0);
1079 boost::shared_ptr<Evoral::Control> program_control = _model->control(program_change);
1081 assert(program_control != 0);
1082 program_control->set_float(float(new_patch.program_number), true, old_program.time);
1088 MidiRegionView::program_selected(CanvasProgramChange& program, const MIDI::Name::PatchPrimaryKey& new_patch)
1090 ControlEvent program_change_event(program.event_time(), program.program(), program.channel());
1091 alter_program_change(program_change_event, new_patch);
1095 MidiRegionView::previous_program(CanvasProgramChange& program)
1097 MIDI::Name::PatchPrimaryKey key;
1098 get_patch_key_at(program.event_time(), program.channel(), key);
1100 boost::shared_ptr<MIDI::Name::Patch> patch =
1101 MIDI::Name::MidiPatchManager::instance().previous_patch(
1103 _custom_device_mode,
1108 ControlEvent program_change_event(program.event_time(), program.program(), program.channel());
1110 alter_program_change(program_change_event, patch->patch_primary_key());
1115 MidiRegionView::next_program(CanvasProgramChange& program)
1117 MIDI::Name::PatchPrimaryKey key;
1118 get_patch_key_at(program.event_time(), program.channel(), key);
1120 boost::shared_ptr<MIDI::Name::Patch> patch =
1121 MIDI::Name::MidiPatchManager::instance().next_patch(
1123 _custom_device_mode,
1127 ControlEvent program_change_event(program.event_time(), program.program(), program.channel());
1129 alter_program_change(program_change_event, patch->patch_primary_key());
1134 MidiRegionView::delete_selection()
1136 assert(_delta_command);
1138 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1139 if ((*i)->selected()) {
1140 _delta_command->remove((*i)->note());
1148 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev)
1150 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1151 if ((*i)->selected() && (*i) != ev) {
1152 (*i)->selected(false);
1160 MidiRegionView::unique_select(ArdourCanvas::CanvasNoteEvent* ev)
1162 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1164 (*i)->selected(false);
1169 _selection.insert(ev);
1171 if ( ! ev->selected()) {
1177 MidiRegionView::note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
1180 clear_selection_except(ev);
1183 _selection.insert(ev);
1185 if ( ! ev->selected()) {
1192 MidiRegionView::note_deselected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
1195 clear_selection_except(ev);
1198 _selection.erase(ev);
1200 if (ev->selected()) {
1201 ev->selected(false);
1207 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
1209 const double last_y = std::min(y1, y2);
1210 const double y = std::max(y1, y2);
1212 // TODO: Make this faster by storing the last updated selection rect, and only
1213 // adjusting things that are in the area that appears/disappeared.
1214 // We probably need a tree to be able to find events in O(log(n)) time.
1217 double last_x1 = 0.0;
1221 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1223 // Events should always be sorted by increasing x1() here
1224 assert((*i)->x1() >= last_x1);
1225 last_x1 = (*i)->x1();
1228 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1229 if (!(*i)->selected()) {
1230 (*i)->selected(true);
1231 _selection.insert(*i);
1233 // Not inside rectangle
1234 } else if ((*i)->selected()) {
1235 (*i)->selected(false);
1236 _selection.erase(*i);
1240 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1242 // Events should always be sorted by increasing x1() here
1243 assert((*i)->x1() >= last_x1);
1244 last_x1 = (*i)->x1();
1247 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1248 if (!(*i)->selected()) {
1249 (*i)->selected(true);
1250 _selection.insert(*i);
1252 // Not inside rectangle
1253 } else if ((*i)->selected()) {
1254 (*i)->selected(false);
1255 _selection.erase(*i);
1263 MidiRegionView::move_selection(double dx, double dy)
1265 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
1266 (*i)->move_event(dx, dy);
1271 MidiRegionView::note_dropped(CanvasNoteEvent* ev, double dt, uint8_t dnote)
1273 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
1274 if (_selection.find(ev) != _selection.end()) {
1275 uint8_t lowest_note_in_selection = midi_stream_view()->lowest_note();
1276 uint8_t highest_note_in_selection = midi_stream_view()->highest_note();
1277 uint8_t highest_note_difference = 0;
1279 // find highest and lowest notes first
1280 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1281 uint8_t pitch = (*i)->note()->note();
1282 lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
1283 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
1287 cerr << "dnote: " << (int) dnote << endl;
1288 cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
1289 << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
1290 cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
1291 << int(highest_note_in_selection) << endl;
1292 cerr << "selection size: " << _selection.size() << endl;
1293 cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
1296 // Make sure the note pitch does not exceed the MIDI standard range
1297 if (dnote <= 127 && (highest_note_in_selection + dnote > 127)) {
1298 highest_note_difference = highest_note_in_selection - 127;
1301 start_delta_command(_("move notes"));
1303 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
1304 Selection::iterator next = i;
1307 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(*i)->note().get()));
1309 // we need to snap here again in nframes64_t in order to be sample accurate
1310 double new_note_time = (*i)->note()->time();
1311 new_note_time += dt;
1313 // keep notes inside region if dragged beyond left region bound
1314 if (new_note_time < _region->start()) {
1315 new_note_time = _region->start();
1318 // since note time is region-absolute but snap_to_frame expects position-relative
1319 // time we have to coordinate transform back and forth here.
1320 new_note_time = snap_to_frame(nframes64_t(new_note_time) - _region->start()) + _region->start();
1322 copy->set_time(new_note_time);
1324 uint8_t original_pitch = (*i)->note()->note();
1325 uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
1327 // keep notes in standard midi range
1328 clamp_0_to_127(new_pitch);
1330 //notes which are dragged beyond the standard midi range snap back to their original place
1331 if ((original_pitch != 0 && new_pitch == 0) || (original_pitch != 127 && new_pitch == 127)) {
1332 new_pitch = original_pitch;
1335 lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
1336 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
1338 copy->set_note(new_pitch);
1340 command_remove_note(*i);
1341 command_add_note(copy, true);
1348 // care about notes being moved beyond the upper/lower bounds on the canvas
1349 if (lowest_note_in_selection < midi_stream_view()->lowest_note() ||
1350 highest_note_in_selection > midi_stream_view()->highest_note()) {
1351 midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
1357 MidiRegionView::snap_to_frame(double x)
1359 PublicEditor &editor = trackview.editor;
1360 // x is region relative
1361 // convert x to global frame
1362 nframes64_t frame = editor.pixel_to_frame(x) + _region->position();
1363 editor.snap_to(frame);
1364 // convert event_frame back to local coordinates relative to position
1365 frame -= _region->position();
1370 MidiRegionView::snap_to_frame(nframes64_t x)
1372 PublicEditor &editor = trackview.editor;
1373 // x is region relative
1374 // convert x to global frame
1375 nframes64_t frame = x + _region->position();
1376 editor.snap_to(frame);
1377 // convert event_frame back to local coordinates relative to position
1378 frame -= _region->position();
1383 MidiRegionView::snap_to_pixel(double x)
1385 return (double) trackview.editor.frame_to_pixel(snap_to_frame(x));
1389 MidiRegionView::get_position_pixels(void)
1391 nframes64_t region_frame = get_position();
1392 return trackview.editor.frame_to_pixel(region_frame);
1396 MidiRegionView::begin_resizing(CanvasNote::NoteEnd note_end)
1398 _resize_data.clear();
1400 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1401 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
1403 // only insert CanvasNotes into the map
1405 NoteResizeData *resize_data = new NoteResizeData();
1406 resize_data->canvas_note = note;
1408 // create a new SimpleRect from the note which will be the resize preview
1409 SimpleRect *resize_rect =
1417 // calculate the colors: get the color settings
1418 uint32_t fill_color =
1420 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get(),
1423 // make the resize preview notes more transparent and bright
1424 fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
1426 // calculate color based on note velocity
1427 resize_rect->property_fill_color_rgba() =
1429 note_fill_color(note->note()->velocity()),
1433 resize_rect->property_outline_color_rgba() =
1434 ARDOUR_UI::config()->canvasvar_MidiNoteSelectedOutline.get();
1436 resize_data->resize_rect = resize_rect;
1438 if (note_end == CanvasNote::NOTE_ON) {
1439 resize_data->current_x = note->x1();
1440 } else { // NOTE_OFF
1441 resize_data->current_x = note->x2();
1444 _resize_data.push_back(resize_data);
1450 MidiRegionView::update_resizing(CanvasNote::NoteEnd note_end, double x, bool relative)
1452 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1453 SimpleRect *resize_rect = (*i)->resize_rect;
1454 CanvasNote *canvas_note = (*i)->canvas_note;
1456 const double region_start = get_position_pixels();
1459 (*i)->current_x = (*i)->current_x + x;
1461 // x is in track relative, transform it to region relative
1462 (*i)->current_x = x - region_start;
1465 double current_x = (*i)->current_x;
1467 if (note_end == CanvasNote::NOTE_ON) {
1468 resize_rect->property_x1() = snap_to_pixel(current_x);
1469 resize_rect->property_x2() = canvas_note->x2();
1471 resize_rect->property_x2() = snap_to_pixel(current_x);
1472 resize_rect->property_x1() = canvas_note->x1();
1478 MidiRegionView::commit_resizing(CanvasNote::NoteEnd note_end, double event_x, bool relative)
1480 start_delta_command(_("resize notes"));
1482 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1483 CanvasNote* canvas_note = (*i)->canvas_note;
1484 SimpleRect* resize_rect = (*i)->resize_rect;
1485 double current_x = (*i)->current_x;
1486 const double position = get_position_pixels();
1489 // event_x is in track relative, transform it to region relative
1490 current_x = event_x - position;
1493 // because snapping works on world coordinates we have to transform current_x
1494 // to world coordinates before snapping and transform it back afterwards
1495 nframes64_t current_frame = snap_to_frame(current_x);
1496 // transform to region start relative
1497 current_frame += _region->start();
1499 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(canvas_note->note().get())));
1501 // resize beginning of note
1502 if (note_end == CanvasNote::NOTE_ON && current_frame < copy->end_time()) {
1503 command_remove_note(canvas_note);
1504 copy->on_event().time() = current_frame;
1505 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1507 // resize end of note
1508 if (note_end == CanvasNote::NOTE_OFF && current_frame > copy->time()) {
1509 command_remove_note(canvas_note);
1510 copy->off_event().time() = current_frame;
1511 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1518 _resize_data.clear();
1524 MidiRegionView::change_velocity(uint8_t velocity, bool relative)
1526 start_delta_command(_("change velocity"));
1527 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1528 Selection::iterator next = i;
1531 CanvasNoteEvent *event = *i;
1532 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(event->note().get())));
1535 uint8_t new_velocity = copy->velocity() + velocity;
1536 clamp_0_to_127(new_velocity);
1538 copy->set_velocity(new_velocity);
1539 } else { // absolute
1540 copy->set_velocity(velocity);
1543 command_remove_note(event);
1544 command_add_note(copy, true);
1553 MidiRegionView::change_channel(uint8_t channel)
1555 start_delta_command(_("change channel"));
1556 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1557 Selection::iterator next = i;
1560 CanvasNoteEvent *event = *i;
1561 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(event->note().get())));
1563 copy->set_channel(channel);
1565 command_remove_note(event);
1566 command_add_note(copy, true);
1576 MidiRegionView::note_entered(ArdourCanvas::CanvasNoteEvent* ev)
1578 if (ev->note() && _mouse_state == EraseTouchDragging) {
1579 start_delta_command(_("note entered"));
1581 _delta_command->remove(ev->note());
1582 } else if (_mouse_state == SelectTouchDragging) {
1583 note_selected(ev, true);
1589 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
1591 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
1593 display_model(msrc->model());
1597 MidiRegionView::set_frame_color()
1600 if (_selected && should_show_selection) {
1601 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
1603 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
1609 MidiRegionView::midi_channel_mode_changed(ChannelMode mode, uint16_t mask)
1613 case FilterChannels:
1614 _force_channel = -1;
1617 _force_channel = mask;
1618 mask = 0xFFFF; // Show all notes as active (below)
1621 // Update notes for selection
1622 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1623 (*i)->on_channel_selection_change(mask);
1626 _last_channel_selection = mask;
1630 MidiRegionView::midi_patch_settings_changed(std::string model, std::string custom_device_mode)
1632 _model_name = model;
1633 _custom_device_mode = custom_device_mode;