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,
68 boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color)
69 : RegionView (parent, tv, r, spu, basic_color)
71 , _last_channel_selection(0xFFFF)
72 , _default_note_length(1.0)
73 , _current_range_min(0)
74 , _current_range_max(0)
75 , _model_name(string())
76 , _custom_device_mode(string())
78 , _note_group(new ArdourCanvas::Group(*parent))
79 , _delta_command(NULL)
83 _note_group->raise_to_top();
86 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv,
87 boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color,
88 TimeAxisViewItem::Visibility visibility)
89 : RegionView (parent, tv, r, spu, basic_color, false, visibility)
91 , _last_channel_selection(0xFFFF)
92 , _default_note_length(1.0)
93 , _model_name(string())
94 , _custom_device_mode(string())
96 , _note_group(new ArdourCanvas::Group(*parent))
97 , _delta_command(NULL)
102 _note_group->raise_to_top();
106 MidiRegionView::MidiRegionView (const MidiRegionView& other)
109 , _last_channel_selection(0xFFFF)
110 , _default_note_length(1.0)
111 , _model_name(string())
112 , _custom_device_mode(string())
114 , _note_group(new ArdourCanvas::Group(*get_canvas_group()))
115 , _delta_command(NULL)
122 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
123 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
128 MidiRegionView::MidiRegionView (const MidiRegionView& other, boost::shared_ptr<MidiRegion> region)
129 : RegionView (other, boost::shared_ptr<Region> (region))
131 , _last_channel_selection(0xFFFF)
132 , _default_note_length(1.0)
133 , _model_name(string())
134 , _custom_device_mode(string())
136 , _note_group(new ArdourCanvas::Group(*get_canvas_group()))
137 , _delta_command(NULL)
144 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
145 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
151 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
154 midi_region()->midi_source(0)->load_model();
157 _model = midi_region()->midi_source(0)->model();
158 _enable_display = false;
160 RegionView::init (basic_color, false);
162 compute_colors (basic_color);
164 set_height (trackview.current_height());
167 region_sync_changed ();
168 region_resized (BoundsChanged);
171 reset_width_dependent_items (_pixel_width);
175 _enable_display = true;
180 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
183 group->raise_to_top();
184 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
186 midi_view()->signal_channel_mode_changed().connect(
187 mem_fun(this, &MidiRegionView::midi_channel_mode_changed));
189 midi_view()->signal_midi_patch_settings_changed().connect(
190 mem_fun(this, &MidiRegionView::midi_patch_settings_changed));
194 MidiRegionView::canvas_event(GdkEvent* ev)
196 static bool delete_mod = false;
197 static Editing::MidiEditMode original_mode;
199 static double drag_start_x, drag_start_y;
200 static double last_x, last_y;
201 double event_x, event_y;
202 nframes64_t event_frame = 0;
204 static ArdourCanvas::SimpleRect* drag_rect = NULL;
206 if (trackview.editor().current_mouse_mode() != MouseNote)
209 const Editing::MidiEditMode midi_edit_mode = trackview.editor().current_midi_edit_mode();
213 if (ev->key.keyval == GDK_Delete && !delete_mod) {
215 original_mode = midi_edit_mode;
216 trackview.editor().set_midi_edit_mode(MidiEditErase);
217 start_delta_command(_("erase notes"));
218 _mouse_state = EraseTouchDragging;
220 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
221 _mouse_state = SelectTouchDragging;
223 } else if (ev->key.keyval == GDK_Escape) {
229 case GDK_KEY_RELEASE:
230 if (ev->key.keyval == GDK_Delete) {
231 if (_mouse_state == EraseTouchDragging) {
236 trackview.editor().set_midi_edit_mode(original_mode);
241 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
247 case GDK_BUTTON_PRESS:
248 if (_mouse_state != SelectTouchDragging &&
249 _mouse_state != EraseTouchDragging &&
250 ev->button.button == 1) {
251 _pressed_button = ev->button.button;
252 _mouse_state = Pressed;
255 _pressed_button = ev->button.button;
258 case GDK_2BUTTON_PRESS:
261 case GDK_ENTER_NOTIFY:
262 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
263 Keyboard::magic_widget_grab_focus();
267 case GDK_MOTION_NOTIFY:
268 event_x = ev->motion.x;
269 event_y = ev->motion.y;
270 group->w2i(event_x, event_y);
272 // convert event_x to global frame
273 event_frame = trackview.editor().pixel_to_frame(event_x) + _region->position();
274 trackview.editor().snap_to(event_frame);
275 // convert event_frame back to local coordinates relative to position
276 event_frame -= _region->position();
278 switch (_mouse_state) {
279 case Pressed: // Drag start
282 if (_pressed_button == 1 && midi_edit_mode == MidiEditSelect) {
283 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
284 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
287 drag_start_x = event_x;
288 drag_start_y = event_y;
290 drag_rect = new ArdourCanvas::SimpleRect(*group);
291 drag_rect->property_x1() = event_x;
292 drag_rect->property_y1() = event_y;
293 drag_rect->property_x2() = event_x;
294 drag_rect->property_y2() = event_y;
295 drag_rect->property_outline_what() = 0xFF;
296 drag_rect->property_outline_color_rgba()
297 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
298 drag_rect->property_fill_color_rgba()
299 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
301 _mouse_state = SelectRectDragging;
304 // Add note drag start
305 } else if (midi_edit_mode == MidiEditPencil) {
306 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
307 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
310 drag_start_x = event_x;
311 drag_start_y = event_y;
313 drag_rect = new ArdourCanvas::SimpleRect(*group);
314 drag_rect->property_x1() = trackview.editor().frame_to_pixel(event_frame);
316 drag_rect->property_y1() = midi_stream_view()->note_to_y(
317 midi_stream_view()->y_to_note(event_y));
318 drag_rect->property_x2() = event_x;
319 drag_rect->property_y2() = drag_rect->property_y1()
320 + floor(midi_stream_view()->note_height());
321 drag_rect->property_outline_what() = 0xFF;
322 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
323 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
325 _mouse_state = AddDragging;
331 case SelectRectDragging: // Select drag motion
332 case AddDragging: // Add note drag motion
333 if (ev->motion.is_hint) {
336 GdkModifierType state;
337 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
342 if (_mouse_state == AddDragging)
343 event_x = trackview.editor().frame_to_pixel(event_frame);
346 if (event_x > drag_start_x)
347 drag_rect->property_x2() = event_x;
349 drag_rect->property_x1() = event_x;
352 if (drag_rect && _mouse_state == SelectRectDragging) {
353 if (event_y > drag_start_y)
354 drag_rect->property_y2() = event_y;
356 drag_rect->property_y1() = event_y;
358 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
364 case EraseTouchDragging:
365 case SelectTouchDragging:
373 case GDK_BUTTON_RELEASE:
374 event_x = ev->motion.x;
375 event_y = ev->motion.y;
376 group->w2i(event_x, event_y);
377 group->ungrab(ev->button.time);
378 event_frame = trackview.editor().pixel_to_frame(event_x);
380 if (_pressed_button != 1) {
384 switch (_mouse_state) {
385 case Pressed: // Clicked
386 switch (midi_edit_mode) {
392 create_note_at(event_x, event_y, _default_note_length);
397 case SelectRectDragging: // Select drag done
402 case AddDragging: // Add drag done
404 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
405 const double x = drag_rect->property_x1();
406 const double length = trackview.editor().pixel_to_frame(
407 drag_rect->property_x2() - drag_rect->property_x1());
409 create_note_at(x, drag_rect->property_y1(), frames_to_beats(length));
424 /** Add a note to the model, and the view, at a canvas (click) coordinate.
425 * \param x horizontal position in pixels
426 * \param y vertical position in pixels
427 * \param length duration of the note in beats */
429 MidiRegionView::create_note_at(double x, double y, double length)
431 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
432 MidiStreamView* const view = mtv->midi_view();
434 double note = midi_stream_view()->y_to_note(y);
437 assert(note <= 127.0);
439 // Start of note in frames relative to region start
440 nframes64_t start_frames = snap_to_frame(trackview.editor().pixel_to_frame(x));
441 assert(start_frames >= 0);
444 length = frames_to_beats(
445 snap_to_frame(start_frames + beats_to_frames(length)) - start_frames);
447 const boost::shared_ptr<NoteType> new_note(new NoteType(0,
448 frames_to_beats(start_frames + _region->start()), length,
449 (uint8_t)note, 0x40));
451 view->update_note_range(new_note->note());
453 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
455 _model->apply_command(trackview.session(), cmd);
460 MidiRegionView::clear_events()
465 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
466 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
471 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
476 _pgm_changes.clear();
481 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
484 if (_enable_display) {
491 MidiRegionView::start_delta_command(string name)
493 if (!_delta_command) {
494 _delta_command = _model->new_delta_command(name);
499 MidiRegionView::command_add_note(const boost::shared_ptr<NoteType> note, bool selected)
501 if (_delta_command) {
502 _delta_command->add(note);
505 _marked_for_selection.insert(note);
510 MidiRegionView::command_remove_note(ArdourCanvas::CanvasNoteEvent* ev)
512 if (_delta_command && ev->note()) {
513 _delta_command->remove(ev->note());
518 MidiRegionView::apply_command()
520 if (!_delta_command) {
524 // Mark all selected notes for selection when model reloads
525 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
526 _marked_for_selection.insert((*i)->note());
529 _model->apply_command(trackview.session(), _delta_command);
530 _delta_command = NULL;
531 midi_view()->midi_track()->diskstream()->playlist_modified();
533 _marked_for_selection.clear();
538 MidiRegionView::abort_command()
540 delete _delta_command;
541 _delta_command = NULL;
547 MidiRegionView::redisplay_model()
549 // Don't redisplay the model if we're currently recording and displaying that
558 MidiModel::Notes notes = _model->notes();
560 for (size_t i = 0; i < _model->n_notes(); ++i) {
561 add_note(_model->note_at(i));
564 display_program_change_flags();
566 _model->read_unlock();
569 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
574 MidiRegionView::display_program_change_flags()
576 boost::shared_ptr<Evoral::Control> control = _model->control(MidiPgmChangeAutomation);
581 Glib::Mutex::Lock lock (control->list()->lock());
583 uint8_t channel = control->parameter().channel();
585 for (AutomationList::const_iterator event = control->list()->begin();
586 event != control->list()->end(); ++event) {
587 double event_time = (*event)->when;
588 double program_number = floor((*event)->value + 0.5);
590 // Get current value of bank select MSB at time of the program change
591 Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
592 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
594 if (msb_control != 0) {
595 msb = uint8_t(floor(msb_control->get_float(true, event_time) + 0.5));
598 // Get current value of bank select LSB at time of the program change
599 Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
600 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
602 if (lsb_control != 0) {
603 lsb = uint8_t(floor(lsb_control->get_float(true, event_time) + 0.5));
606 MIDI::Name::PatchPrimaryKey patch_key(msb, lsb, program_number);
608 boost::shared_ptr<MIDI::Name::Patch> patch =
609 MIDI::Name::MidiPatchManager::instance().find_patch(
610 _model_name, _custom_device_mode, channel, patch_key);
612 PCEvent program_change(event_time, uint8_t(program_number), channel);
615 add_pgm_change(program_change, patch->name());
618 snprintf(buf, 4, "%d", int(program_number));
619 add_pgm_change(program_change, buf);
625 MidiRegionView::~MidiRegionView ()
627 in_destructor = true;
629 RegionViewGoingAway (this); /* EMIT_SIGNAL */
638 delete _delta_command;
643 MidiRegionView::region_resized (Change what_changed)
645 RegionView::region_resized(what_changed);
647 if (what_changed & ARDOUR::PositionChanged) {
648 if (_enable_display) {
655 MidiRegionView::reset_width_dependent_items (double pixel_width)
657 RegionView::reset_width_dependent_items(pixel_width);
658 assert(_pixel_width == pixel_width);
660 if (_enable_display) {
666 MidiRegionView::set_height (double height)
668 static const double FUDGE = 2.0;
669 const double old_height = _height;
670 RegionView::set_height(height);
671 _height = height - FUDGE;
673 apply_note_range(midi_stream_view()->lowest_note(),
674 midi_stream_view()->highest_note(),
675 height != old_height + FUDGE);
678 name_text->raise_to_top();
683 /** Apply the current note range from the stream view
684 * by repositioning/hiding notes as necessary
687 MidiRegionView::apply_note_range (uint8_t min, uint8_t max, bool force)
689 if (!_enable_display) {
693 if (!force && _current_range_min == min && _current_range_max == max) {
697 _current_range_min = min;
698 _current_range_max = max;
700 for (Events::const_iterator i = _events.begin(); i != _events.end(); ++i) {
701 CanvasNoteEvent* event = *i;
702 Item* item = dynamic_cast<Item*>(event);
704 if (event && event->note()) {
705 if (event->note()->note() < _current_range_min
706 || event->note()->note() > _current_range_max) {
707 if (canvas_item_visible(item)) {
711 if (!canvas_item_visible(item)) {
715 event->hide_velocity();
716 if (CanvasNote* note = dynamic_cast<CanvasNote*>(event)) {
717 const double y1 = midi_stream_view()->note_to_y(event->note()->note());
718 const double y2 = y1 + floor(midi_stream_view()->note_height());
720 note->property_y1() = y1;
721 note->property_y2() = y2;
722 } else if (CanvasHit* hit = dynamic_cast<CanvasHit*>(event)) {
723 double x = trackview.editor().frame_to_pixel(
724 beats_to_frames(event->note()->time()) - _region->start());
725 const double diamond_size = midi_stream_view()->note_height() / 2.0;
726 double y = midi_stream_view()->note_to_y(event->note()->note())
727 + ((diamond_size-2.0) / 4.0);
729 hit->set_height(diamond_size);
730 hit->move(x-hit->x1(), y-hit->y1());
733 if (event->selected()) {
734 event->show_velocity();
743 MidiRegionView::add_ghost (TimeAxisView& tv)
745 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
749 double unit_position = _region->position () / samples_per_unit;
750 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
751 MidiGhostRegion* ghost;
753 if (mtv && mtv->midi_view()) {
754 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group
755 to allow having midi notes on top of note lines and waveforms.
757 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
759 ghost = new MidiGhostRegion (tv, trackview, unit_position);
762 ghost->set_height ();
763 ghost->set_duration (_region->length() / samples_per_unit);
764 ghosts.push_back (ghost);
766 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
767 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
768 ghost->add_note(note);
772 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
778 /** Begin tracking note state for successive calls to add_event
781 MidiRegionView::begin_write()
783 assert(!_active_notes);
784 _active_notes = new CanvasNote*[128];
785 for (unsigned i=0; i < 128; ++i) {
786 _active_notes[i] = NULL;
791 /** Destroy note state for add_event
794 MidiRegionView::end_write()
796 delete[] _active_notes;
797 _active_notes = NULL;
798 _marked_for_selection.clear();
802 /** Resolve an active MIDI note (while recording).
805 MidiRegionView::resolve_note(uint8_t note, double end_time)
807 if (midi_view()->note_mode() != Sustained) {
811 if (_active_notes && _active_notes[note]) {
812 const nframes64_t end_time_frames = beats_to_frames(end_time);
813 _active_notes[note]->property_x2() = trackview.editor().frame_to_pixel(end_time_frames);
814 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
815 _active_notes[note] = NULL;
820 /** Extend active notes to rightmost edge of region (if length is changed)
823 MidiRegionView::extend_active_notes()
825 if (!_active_notes) {
829 for (unsigned i=0; i < 128; ++i) {
830 if (_active_notes[i]) {
831 _active_notes[i]->property_x2() = trackview.editor().frame_to_pixel(_region->length());
837 MidiRegionView::play_midi_note(boost::shared_ptr<NoteType> note)
839 if (!trackview.editor().sound_notes()) {
843 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
846 route_ui->midi_track()->write_immediate_event(
847 note->on_event().size(), note->on_event().buffer());
849 const double note_length_beats = (note->off_event().time() - note->on_event().time());
850 nframes_t note_length_ms = beats_to_frames(note_length_beats)
851 * (1000 / (double)route_ui->session().nominal_frame_rate());
852 Glib::signal_timeout().connect(bind(mem_fun(this, &MidiRegionView::play_midi_note_off), note),
853 note_length_ms, G_PRIORITY_DEFAULT);
857 MidiRegionView::play_midi_note_off(boost::shared_ptr<NoteType> note)
859 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
862 route_ui->midi_track()->write_immediate_event(
863 note->off_event().size(), note->off_event().buffer());
869 MidiRegionView::note_in_visible_range(const boost::shared_ptr<NoteType> note) const
871 const nframes64_t note_start_frames = beats_to_frames(note->time());
872 bool outside = (note_start_frames - _region->start() >= _region->length())
873 || (note_start_frames < _region->start())
874 || (note->note() < midi_stream_view()->lowest_note())
875 || (note->note() > midi_stream_view()->highest_note());
879 /** Add a MIDI note to the view (with length).
881 * If in sustained mode, notes with length 0 will be considered active
882 * notes, and resolve_note should be called when the corresponding note off
883 * event arrives, to properly display the note.
886 MidiRegionView::add_note(const boost::shared_ptr<NoteType> note)
888 assert(note->time() >= 0);
889 assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
891 const nframes64_t note_start_frames = beats_to_frames(note->time());
892 const nframes64_t note_end_frames = beats_to_frames(note->end_time());
894 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
896 CanvasNoteEvent* event = 0;
898 const double x = trackview.editor().frame_to_pixel(note_start_frames - _region->start());
900 if (midi_view()->note_mode() == Sustained) {
901 const double y1 = midi_stream_view()->note_to_y(note->note());
902 const double note_endpixel =
903 trackview.editor().frame_to_pixel(note_end_frames - _region->start());
905 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
906 ev_rect->property_x1() = x;
907 ev_rect->property_y1() = y1;
908 if (note->length() > 0) {
909 ev_rect->property_x2() = note_endpixel;
911 ev_rect->property_x2() = trackview.editor().frame_to_pixel(_region->length());
913 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
915 if (note->length() == 0) {
917 assert(note->note() < 128);
918 // If this note is already active there's a stuck note,
919 // finish the old note rectangle
920 if (_active_notes[note->note()]) {
921 CanvasNote* const old_rect = _active_notes[note->note()];
922 boost::shared_ptr<NoteType> old_note = old_rect->note();
923 old_rect->property_x2() = x;
924 old_rect->property_outline_what() = (guint32) 0xF;
926 _active_notes[note->note()] = ev_rect;
928 /* outline all but right edge */
929 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
931 /* outline all edges */
932 ev_rect->property_outline_what() = (guint32) 0xF;
938 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
939 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
940 gr->add_note(ev_rect);
944 } else if (midi_view()->note_mode() == Percussive) {
945 const double diamond_size = midi_stream_view()->note_height() / 2.0;
946 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
948 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size, note);
949 ev_diamond->move(x, y);
956 if (_marked_for_selection.find(note) != _marked_for_selection.end()) {
957 note_selected(event, true);
959 event->on_channel_selection_change(_last_channel_selection);
960 _events.push_back(event);
961 if (note_in_visible_range(note)) {
970 MidiRegionView::add_pgm_change(PCEvent& program, const string& displaytext)
972 assert(program.time >= 0);
974 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
975 const double x = trackview.editor().frame_to_pixel(beats_to_frames(program.time));
977 double height = midi_stream_view()->contents_height();
979 boost::shared_ptr<CanvasProgramChange> pgm_change = boost::shared_ptr<CanvasProgramChange>(
980 new CanvasProgramChange(*this, *group,
986 program.time, program.channel, program.value));
988 // Show unless program change is beyond the region bounds
989 if (program.time - _region->start() >= _region->length() || program.time < _region->start()) {
995 _pgm_changes.push_back(pgm_change);
999 MidiRegionView::get_patch_key_at(double time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key)
1001 cerr << "getting patch key at " << time << " for channel " << channel << endl;
1002 Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
1003 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
1005 if (msb_control != 0) {
1006 msb = int(msb_control->get_float(true, time));
1007 cerr << "got msb " << msb;
1010 Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
1011 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
1013 if (lsb_control != 0) {
1014 lsb = lsb_control->get_float(true, time);
1015 cerr << " got lsb " << lsb;
1018 Evoral::Parameter program_change(MidiPgmChangeAutomation, channel, 0);
1019 boost::shared_ptr<Evoral::Control> program_control = _model->control(program_change);
1020 float program_number = -1.0;
1021 if (program_control != 0) {
1022 program_number = program_control->get_float(true, time);
1023 cerr << " got program " << program_number << endl;
1026 key.msb = (int) floor(msb + 0.5);
1027 key.lsb = (int) floor(lsb + 0.5);
1028 key.program_number = (int) floor(program_number + 0.5);
1029 assert(key.is_sane());
1034 MidiRegionView::alter_program_change(PCEvent& old_program, const MIDI::Name::PatchPrimaryKey& new_patch)
1036 // TODO: Get the real event here and alter them at the original times
1037 Evoral::Parameter bank_select_msb(MidiCCAutomation, old_program.channel, MIDI_CTL_MSB_BANK);
1038 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
1039 if (msb_control != 0) {
1040 msb_control->set_float(float(new_patch.msb), true, old_program.time);
1043 // TODO: Get the real event here and alter them at the original times
1044 Evoral::Parameter bank_select_lsb(MidiCCAutomation, old_program.channel, MIDI_CTL_LSB_BANK);
1045 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
1046 if (lsb_control != 0) {
1047 lsb_control->set_float(float(new_patch.lsb), true, old_program.time);
1050 Evoral::Parameter program_change(MidiPgmChangeAutomation, old_program.channel, 0);
1051 boost::shared_ptr<Evoral::Control> program_control = _model->control(program_change);
1053 assert(program_control != 0);
1054 program_control->set_float(float(new_patch.program_number), true, old_program.time);
1060 MidiRegionView::program_selected(CanvasProgramChange& program, const MIDI::Name::PatchPrimaryKey& new_patch)
1062 PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1063 alter_program_change(program_change_event, new_patch);
1067 MidiRegionView::previous_program(CanvasProgramChange& program)
1069 MIDI::Name::PatchPrimaryKey key;
1070 get_patch_key_at(program.event_time(), program.channel(), key);
1072 boost::shared_ptr<MIDI::Name::Patch> patch =
1073 MIDI::Name::MidiPatchManager::instance().previous_patch(
1075 _custom_device_mode,
1079 PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1081 alter_program_change(program_change_event, patch->patch_primary_key());
1086 MidiRegionView::next_program(CanvasProgramChange& program)
1088 MIDI::Name::PatchPrimaryKey key;
1089 get_patch_key_at(program.event_time(), program.channel(), key);
1091 boost::shared_ptr<MIDI::Name::Patch> patch =
1092 MIDI::Name::MidiPatchManager::instance().next_patch(
1094 _custom_device_mode,
1098 PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1100 alter_program_change(program_change_event, patch->patch_primary_key());
1105 MidiRegionView::delete_selection()
1107 assert(_delta_command);
1109 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1110 if ((*i)->selected()) {
1111 _delta_command->remove((*i)->note());
1119 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev)
1121 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1122 if ((*i)->selected() && (*i) != ev) {
1123 (*i)->selected(false);
1131 MidiRegionView::unique_select(ArdourCanvas::CanvasNoteEvent* ev)
1133 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1135 (*i)->selected(false);
1140 _selection.insert(ev);
1142 if ( ! ev->selected()) {
1148 MidiRegionView::note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
1151 clear_selection_except(ev);
1154 if (_selection.insert(ev).second) {
1155 play_midi_note(ev->note());
1158 if ( ! ev->selected()) {
1165 MidiRegionView::note_deselected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
1168 clear_selection_except(ev);
1171 _selection.erase(ev);
1173 if (ev->selected()) {
1174 ev->selected(false);
1180 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
1182 const double last_y = std::min(y1, y2);
1183 const double y = std::max(y1, y2);
1185 // TODO: Make this faster by storing the last updated selection rect, and only
1186 // adjusting things that are in the area that appears/disappeared.
1187 // We probably need a tree to be able to find events in O(log(n)) time.
1190 double last_x1 = 0.0;
1194 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1196 // Events should always be sorted by increasing x1() here
1197 assert((*i)->x1() >= last_x1);
1198 last_x1 = (*i)->x1();
1201 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1202 if (!(*i)->selected()) {
1203 (*i)->selected(true);
1204 _selection.insert(*i);
1205 play_midi_note((*i)->note());
1207 // Not inside rectangle
1208 } else if ((*i)->selected()) {
1209 (*i)->selected(false);
1210 _selection.erase(*i);
1214 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1216 // Events should always be sorted by increasing x1() here
1217 assert((*i)->x1() >= last_x1);
1218 last_x1 = (*i)->x1();
1221 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1222 if (!(*i)->selected()) {
1223 (*i)->selected(true);
1224 _selection.insert(*i);
1225 play_midi_note((*i)->note());
1227 // Not inside rectangle
1228 } else if ((*i)->selected()) {
1229 (*i)->selected(false);
1230 _selection.erase(*i);
1238 MidiRegionView::move_selection(double dx, double dy)
1240 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
1241 (*i)->move_event(dx, dy);
1246 MidiRegionView::note_dropped(CanvasNoteEvent* ev, double dt, uint8_t dnote)
1248 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
1249 if (_selection.find(ev) == _selection.end()) {
1253 uint8_t lowest_note_in_selection = midi_stream_view()->lowest_note();
1254 uint8_t highest_note_in_selection = midi_stream_view()->highest_note();
1255 uint8_t highest_note_difference = 0;
1257 // find highest and lowest notes first
1258 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1259 uint8_t pitch = (*i)->note()->note();
1260 lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
1261 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
1265 cerr << "dnote: " << (int) dnote << endl;
1266 cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
1267 << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
1268 cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
1269 << int(highest_note_in_selection) << endl;
1270 cerr << "selection size: " << _selection.size() << endl;
1271 cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
1274 // Make sure the note pitch does not exceed the MIDI standard range
1275 if (dnote <= 127 && (highest_note_in_selection + dnote > 127)) {
1276 highest_note_difference = highest_note_in_selection - 127;
1279 start_delta_command(_("move notes"));
1281 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
1282 Selection::iterator next = i;
1285 const boost::shared_ptr<NoteType> copy(new NoteType(*(*i)->note().get()));
1287 // we need to snap here again in nframes64_t in order to be sample accurate
1288 double start_frames = snap_to_frame(beats_to_frames((*i)->note()->time()) + dt);
1290 // keep notes inside region if dragged beyond left region bound
1291 if (start_frames < _region->start()) {
1292 start_frames = _region->start();
1295 copy->set_time(frames_to_beats(start_frames));
1297 uint8_t original_pitch = (*i)->note()->note();
1298 uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
1300 // keep notes in standard midi range
1301 clamp_to_0_127(new_pitch);
1303 // keep original pitch if note is dragged outside valid midi range
1304 if ((original_pitch != 0 && new_pitch == 0)
1305 || (original_pitch != 127 && new_pitch == 127)) {
1306 new_pitch = original_pitch;
1309 lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
1310 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
1312 copy->set_note(new_pitch);
1314 command_remove_note(*i);
1315 command_add_note(copy, (*i)->selected());
1322 // care about notes being moved beyond the upper/lower bounds on the canvas
1323 if (lowest_note_in_selection < midi_stream_view()->lowest_note() ||
1324 highest_note_in_selection > midi_stream_view()->highest_note()) {
1325 midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
1330 MidiRegionView::snap_to_frame(double x)
1332 PublicEditor &editor = trackview.editor();
1333 // x is region relative, convert it to global absolute frames
1334 nframes64_t frame = editor.pixel_to_frame(x) + _region->position();
1335 editor.snap_to(frame);
1336 return frame - _region->position(); // convert back to region relative
1340 MidiRegionView::snap_to_frame(nframes64_t x)
1342 PublicEditor &editor = trackview.editor();
1343 // x is region relative
1344 // convert x to global frame
1345 nframes64_t frame = x + _region->position();
1346 editor.snap_to(frame);
1347 // convert event_frame back to local coordinates relative to position
1348 frame -= _region->position();
1353 MidiRegionView::snap_to_pixel(double x)
1355 return (double) trackview.editor().frame_to_pixel(snap_to_frame(x));
1359 MidiRegionView::get_position_pixels()
1361 nframes64_t region_frame = get_position();
1362 return trackview.editor().frame_to_pixel(region_frame);
1366 MidiRegionView::beats_to_frames(double beats) const
1368 return midi_region()->midi_source()->time_converter().to(beats);
1372 MidiRegionView::frames_to_beats(nframes64_t frames) const
1374 return midi_region()->midi_source()->time_converter().from(frames);
1378 MidiRegionView::begin_resizing(CanvasNote::NoteEnd note_end)
1380 _resize_data.clear();
1382 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1383 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
1385 // only insert CanvasNotes into the map
1387 NoteResizeData *resize_data = new NoteResizeData();
1388 resize_data->canvas_note = note;
1390 // create a new SimpleRect from the note which will be the resize preview
1391 SimpleRect *resize_rect = new SimpleRect(
1392 *group, note->x1(), note->y1(), note->x2(), note->y2());
1394 // calculate the colors: get the color settings
1395 uint32_t fill_color = UINT_RGBA_CHANGE_A(
1396 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(),
1399 // make the resize preview notes more transparent and bright
1400 fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
1402 // calculate color based on note velocity
1403 resize_rect->property_fill_color_rgba() = UINT_INTERPOLATE(
1404 CanvasNoteEvent::meter_style_fill_color(note->note()->velocity()),
1408 resize_rect->property_outline_color_rgba() = CanvasNoteEvent::calculate_outline(
1409 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get());
1411 resize_data->resize_rect = resize_rect;
1413 if (note_end == CanvasNote::NOTE_ON) {
1414 resize_data->current_x = note->x1();
1415 } else { // NOTE_OFF
1416 resize_data->current_x = note->x2();
1419 _resize_data.push_back(resize_data);
1425 MidiRegionView::update_resizing(CanvasNote::NoteEnd note_end, double x, bool relative)
1427 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1428 SimpleRect* resize_rect = (*i)->resize_rect;
1429 CanvasNote* canvas_note = (*i)->canvas_note;
1431 const double region_start = get_position_pixels();
1434 (*i)->current_x = (*i)->current_x + x;
1436 // x is in track relative, transform it to region relative
1437 (*i)->current_x = x - region_start;
1440 double current_x = (*i)->current_x;
1442 if (note_end == CanvasNote::NOTE_ON) {
1443 resize_rect->property_x1() = snap_to_pixel(current_x);
1444 resize_rect->property_x2() = canvas_note->x2();
1446 resize_rect->property_x2() = snap_to_pixel(current_x);
1447 resize_rect->property_x1() = canvas_note->x1();
1453 MidiRegionView::commit_resizing(CanvasNote::NoteEnd note_end, double event_x, bool relative)
1455 start_delta_command(_("resize notes"));
1457 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1458 CanvasNote* canvas_note = (*i)->canvas_note;
1459 SimpleRect* resize_rect = (*i)->resize_rect;
1460 double current_x = (*i)->current_x;
1461 const double position = get_position_pixels();
1464 // event_x is in track relative, transform it to region relative
1465 current_x = event_x - position;
1468 // because snapping works on world coordinates we have to transform current_x
1469 // to world coordinates before snapping and transform it back afterwards
1470 nframes64_t current_frame = snap_to_frame(current_x);
1471 // transform to region start relative
1472 current_frame += _region->start();
1474 const boost::shared_ptr<NoteType> copy(new NoteType(*(canvas_note->note().get())));
1476 // resize beginning of note
1477 if (note_end == CanvasNote::NOTE_ON && current_frame < copy->end_time()) {
1478 command_remove_note(canvas_note);
1479 copy->on_event().time() = current_frame;
1480 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1482 // resize end of note
1483 if (note_end == CanvasNote::NOTE_OFF && current_frame > copy->time()) {
1484 command_remove_note(canvas_note);
1485 copy->off_event().time() = current_frame;
1486 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1493 _resize_data.clear();
1498 MidiRegionView::change_note_velocity(CanvasNoteEvent* event, int8_t velocity, bool relative)
1500 const boost::shared_ptr<NoteType> copy(new NoteType(*(event->note().get())));
1503 uint8_t new_velocity = copy->velocity() + velocity;
1504 clamp_to_0_127(new_velocity);
1505 copy->set_velocity(new_velocity);
1507 copy->set_velocity(velocity);
1510 command_remove_note(event);
1511 command_add_note(copy, event->selected());
1515 MidiRegionView::change_velocity(CanvasNoteEvent* ev, int8_t velocity, bool relative)
1517 start_delta_command(_("change velocity"));
1519 change_note_velocity(ev, velocity, relative);
1521 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1522 Selection::iterator next = i;
1524 if ( !(*((*i)->note()) == *(ev->note())) ) {
1525 change_note_velocity(*i, velocity, relative);
1534 MidiRegionView::change_channel(uint8_t channel)
1536 start_delta_command(_("change channel"));
1537 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1538 Selection::iterator next = i;
1541 CanvasNoteEvent* event = *i;
1542 const boost::shared_ptr<NoteType> copy(new NoteType(*(event->note().get())));
1544 copy->set_channel(channel);
1546 command_remove_note(event);
1547 command_add_note(copy, event->selected());
1557 MidiRegionView::note_entered(ArdourCanvas::CanvasNoteEvent* ev)
1559 if (ev->note() && _mouse_state == EraseTouchDragging) {
1560 start_delta_command(_("note entered"));
1562 _delta_command->remove(ev->note());
1563 } else if (_mouse_state == SelectTouchDragging) {
1564 note_selected(ev, true);
1570 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
1572 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
1574 display_model(msrc->model());
1578 MidiRegionView::set_frame_color()
1581 if (_selected && should_show_selection) {
1582 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
1584 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
1590 MidiRegionView::midi_channel_mode_changed(ChannelMode mode, uint16_t mask)
1594 case FilterChannels:
1595 _force_channel = -1;
1598 _force_channel = mask;
1599 mask = 0xFFFF; // Show all notes as active (below)
1602 // Update notes for selection
1603 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1604 (*i)->on_channel_selection_change(mask);
1607 _last_channel_selection = mask;
1611 MidiRegionView::midi_patch_settings_changed(std::string model, std::string custom_device_mode)
1613 _model_name = model;
1614 _custom_device_mode = custom_device_mode;