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.
27 #include <gtkmm2ext/gtk_ui.h>
29 #include <sigc++/signal.h>
31 #include "ardour/playlist.h"
32 #include "ardour/tempo.h"
33 #include "ardour/midi_region.h"
34 #include "ardour/midi_source.h"
35 #include "ardour/midi_diskstream.h"
36 #include "ardour/midi_model.h"
37 #include "ardour/midi_patch_manager.h"
39 #include "evoral/Parameter.hpp"
40 #include "evoral/Control.hpp"
42 #include "streamview.h"
43 #include "midi_region_view.h"
44 #include "midi_streamview.h"
45 #include "midi_time_axis.h"
46 #include "simpleline.h"
47 #include "canvas-hit.h"
48 #include "canvas-note.h"
49 #include "canvas-program-change.h"
50 #include "public_editor.h"
51 #include "ghostregion.h"
52 #include "midi_time_axis.h"
53 #include "automation_time_axis.h"
54 #include "automation_region_view.h"
56 #include "midi_util.h"
57 #include "gui_thread.h"
63 using namespace ARDOUR;
65 using namespace Editing;
66 using namespace ArdourCanvas;
68 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv,
69 boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color)
70 : RegionView (parent, tv, r, spu, basic_color)
72 , _last_channel_selection(0xFFFF)
73 , _default_note_length(1.0)
74 , _current_range_min(0)
75 , _current_range_max(0)
76 , _model_name(string())
77 , _custom_device_mode(string())
79 , _note_group(new ArdourCanvas::Group(*parent))
80 , _delta_command(NULL)
84 _note_group->raise_to_top();
87 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv,
88 boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color,
89 TimeAxisViewItem::Visibility visibility)
90 : RegionView (parent, tv, r, spu, basic_color, false, visibility)
92 , _last_channel_selection(0xFFFF)
93 , _default_note_length(1.0)
94 , _model_name(string())
95 , _custom_device_mode(string())
97 , _note_group(new ArdourCanvas::Group(*parent))
98 , _delta_command(NULL)
103 _note_group->raise_to_top();
107 MidiRegionView::MidiRegionView (const MidiRegionView& other)
110 , _last_channel_selection(0xFFFF)
111 , _default_note_length(1.0)
112 , _model_name(string())
113 , _custom_device_mode(string())
115 , _note_group(new ArdourCanvas::Group(*get_canvas_group()))
116 , _delta_command(NULL)
123 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
124 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
129 MidiRegionView::MidiRegionView (const MidiRegionView& other, boost::shared_ptr<MidiRegion> region)
130 : RegionView (other, boost::shared_ptr<Region> (region))
132 , _last_channel_selection(0xFFFF)
133 , _default_note_length(1.0)
134 , _model_name(string())
135 , _custom_device_mode(string())
137 , _note_group(new ArdourCanvas::Group(*get_canvas_group()))
138 , _delta_command(NULL)
145 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
146 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
152 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
155 midi_region()->midi_source(0)->load_model();
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);
176 _enable_display = true;
181 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
184 group->raise_to_top();
185 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
187 midi_view()->signal_channel_mode_changed().connect(
188 mem_fun(this, &MidiRegionView::midi_channel_mode_changed));
190 midi_view()->signal_midi_patch_settings_changed().connect(
191 mem_fun(this, &MidiRegionView::midi_patch_settings_changed));
195 MidiRegionView::canvas_event(GdkEvent* ev)
197 static bool delete_mod = false;
198 static Editing::MidiEditMode original_mode;
200 static double drag_start_x, drag_start_y;
201 static double last_x, last_y;
202 double event_x, event_y;
203 nframes64_t event_frame = 0;
205 static ArdourCanvas::SimpleRect* drag_rect = NULL;
207 if (trackview.editor().current_mouse_mode() != MouseNote)
210 const Editing::MidiEditMode midi_edit_mode = trackview.editor().current_midi_edit_mode();
214 if (ev->key.keyval == GDK_Delete && !delete_mod) {
216 original_mode = midi_edit_mode;
217 trackview.editor().set_midi_edit_mode(MidiEditErase);
218 start_delta_command(_("erase notes"));
219 _mouse_state = EraseTouchDragging;
221 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
222 _mouse_state = SelectTouchDragging;
224 } else if (ev->key.keyval == GDK_Escape) {
230 case GDK_KEY_RELEASE:
231 if (ev->key.keyval == GDK_Delete) {
232 if (_mouse_state == EraseTouchDragging) {
237 trackview.editor().set_midi_edit_mode(original_mode);
242 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
248 case GDK_BUTTON_PRESS:
249 if (_mouse_state != SelectTouchDragging &&
250 _mouse_state != EraseTouchDragging &&
251 ev->button.button == 1) {
252 _pressed_button = ev->button.button;
253 _mouse_state = Pressed;
256 _pressed_button = ev->button.button;
259 case GDK_2BUTTON_PRESS:
262 case GDK_ENTER_NOTIFY:
263 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
264 Keyboard::magic_widget_grab_focus();
268 case GDK_MOTION_NOTIFY:
269 event_x = ev->motion.x;
270 event_y = ev->motion.y;
271 group->w2i(event_x, event_y);
273 // convert event_x to global frame
274 event_frame = trackview.editor().pixel_to_frame(event_x) + _region->position();
275 trackview.editor().snap_to(event_frame);
276 // convert event_frame back to local coordinates relative to position
277 event_frame -= _region->position();
279 switch (_mouse_state) {
280 case Pressed: // Drag start
283 if (_pressed_button == 1 && midi_edit_mode == MidiEditSelect) {
284 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
285 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
288 drag_start_x = event_x;
289 drag_start_y = event_y;
291 drag_rect = new ArdourCanvas::SimpleRect(*group);
292 drag_rect->property_x1() = event_x;
293 drag_rect->property_y1() = event_y;
294 drag_rect->property_x2() = event_x;
295 drag_rect->property_y2() = event_y;
296 drag_rect->property_outline_what() = 0xFF;
297 drag_rect->property_outline_color_rgba()
298 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
299 drag_rect->property_fill_color_rgba()
300 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
302 _mouse_state = SelectRectDragging;
305 // Add note drag start
306 } else if (midi_edit_mode == MidiEditPencil) {
307 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
308 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
311 drag_start_x = event_x;
312 drag_start_y = event_y;
314 drag_rect = new ArdourCanvas::SimpleRect(*group);
315 drag_rect->property_x1() = trackview.editor().frame_to_pixel(event_frame);
317 drag_rect->property_y1() = midi_stream_view()->note_to_y(
318 midi_stream_view()->y_to_note(event_y));
319 drag_rect->property_x2() = event_x;
320 drag_rect->property_y2() = drag_rect->property_y1()
321 + floor(midi_stream_view()->note_height());
322 drag_rect->property_outline_what() = 0xFF;
323 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 (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(), frames_to_beats(length));
425 /** Add a note to the model, and the view, at a canvas (click) coordinate.
426 * \param x horizontal position in pixels
427 * \param y vertical position in pixels
428 * \param length duration of the note in beats */
430 MidiRegionView::create_note_at(double x, double y, double length)
432 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
433 MidiStreamView* const view = mtv->midi_view();
435 double note = midi_stream_view()->y_to_note(y);
438 assert(note <= 127.0);
440 // Start of note in frames relative to region start
441 nframes64_t start_frames = snap_to_frame(trackview.editor().pixel_to_frame(x));
442 assert(start_frames >= 0);
445 length = frames_to_beats(
446 snap_to_frame(start_frames + beats_to_frames(length)) - start_frames);
448 const boost::shared_ptr<NoteType> new_note(new NoteType(0,
449 frames_to_beats(start_frames + _region->start()), length,
450 (uint8_t)note, 0x40));
452 view->update_note_range(new_note->note());
454 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
456 _model->apply_command(trackview.session(), cmd);
461 MidiRegionView::clear_events()
466 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
467 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
472 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
477 _pgm_changes.clear();
483 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
486 if (_enable_display) {
493 MidiRegionView::start_delta_command(string name)
495 if (!_delta_command) {
496 _delta_command = _model->new_delta_command(name);
501 MidiRegionView::command_add_note(const boost::shared_ptr<NoteType> note, bool selected)
503 if (_delta_command) {
504 _delta_command->add(note);
507 _marked_for_selection.insert(note);
512 MidiRegionView::command_remove_note(ArdourCanvas::CanvasNoteEvent* ev)
514 if (_delta_command && ev->note()) {
515 _delta_command->remove(ev->note());
520 MidiRegionView::apply_command()
522 if (!_delta_command) {
526 // Mark all selected notes for selection when model reloads
527 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
528 _marked_for_selection.insert((*i)->note());
531 _model->apply_command(trackview.session(), _delta_command);
532 _delta_command = NULL;
533 midi_view()->midi_track()->diskstream()->playlist_modified();
535 _marked_for_selection.clear();
540 MidiRegionView::abort_command()
542 delete _delta_command;
543 _delta_command = NULL;
549 MidiRegionView::redisplay_model()
551 // Don't redisplay the model if we're currently recording and displaying that
560 MidiModel::Notes notes = _model->notes();
562 for (size_t i = 0; i < _model->n_notes(); ++i) {
563 add_note(_model->note_at(i));
568 display_program_changes();
570 _model->read_unlock();
573 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
578 MidiRegionView::display_program_changes()
580 boost::shared_ptr<Evoral::Control> control = _model->control(MidiPgmChangeAutomation);
585 Glib::Mutex::Lock lock (control->list()->lock());
587 uint8_t channel = control->parameter().channel();
589 for (AutomationList::const_iterator event = control->list()->begin();
590 event != control->list()->end(); ++event) {
591 double event_time = (*event)->when;
592 double program_number = floor((*event)->value + 0.5);
594 // Get current value of bank select MSB at time of the program change
595 Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
596 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
598 if (msb_control != 0) {
599 msb = uint8_t(floor(msb_control->get_float(true, event_time) + 0.5));
602 // Get current value of bank select LSB at time of the program change
603 Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
604 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
606 if (lsb_control != 0) {
607 lsb = uint8_t(floor(lsb_control->get_float(true, event_time) + 0.5));
610 MIDI::Name::PatchPrimaryKey patch_key(msb, lsb, program_number);
612 boost::shared_ptr<MIDI::Name::Patch> patch =
613 MIDI::Name::MidiPatchManager::instance().find_patch(
614 _model_name, _custom_device_mode, channel, patch_key);
616 PCEvent program_change(event_time, uint8_t(program_number), channel);
619 add_pgm_change(program_change, patch->name());
622 snprintf(buf, 4, "%d", int(program_number));
623 add_pgm_change(program_change, buf);
629 MidiRegionView::display_sysexes()
631 for (MidiModel::SysExes::const_iterator i = _model->sysexes().begin(); i != _model->sysexes().end(); ++i) {
632 ARDOUR::MidiModel::TimeType time = (*i)->time();
637 for (uint32_t b = 0; b < (*i)->size(); ++b) {
638 str << int((*i)->buffer()[b]);
639 if (b != (*i)->size() -1) {
643 string text = str.str();
645 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
646 const double x = trackview.editor().frame_to_pixel(beats_to_frames(time));
648 double height = midi_stream_view()->contents_height();
650 boost::shared_ptr<CanvasSysEx> sysex = boost::shared_ptr<CanvasSysEx>(
651 new CanvasSysEx(*this, *group, text, height, x, 1.0));
653 // Show unless program change is beyond the region bounds
654 if (time - _region->start() >= _region->length() || time < _region->start()) {
660 _sys_exes.push_back(sysex);
665 MidiRegionView::~MidiRegionView ()
667 in_destructor = true;
669 RegionViewGoingAway (this); /* EMIT_SIGNAL */
678 delete _delta_command;
683 MidiRegionView::region_resized (Change what_changed)
685 RegionView::region_resized(what_changed);
687 if (what_changed & ARDOUR::PositionChanged) {
688 set_duration(_region->length(), 0);
689 if (_enable_display) {
696 MidiRegionView::reset_width_dependent_items (double pixel_width)
698 RegionView::reset_width_dependent_items(pixel_width);
699 assert(_pixel_width == pixel_width);
701 if (_enable_display) {
707 MidiRegionView::set_height (double height)
709 static const double FUDGE = 2.0;
710 const double old_height = _height;
711 RegionView::set_height(height);
712 _height = height - FUDGE;
714 apply_note_range(midi_stream_view()->lowest_note(),
715 midi_stream_view()->highest_note(),
716 height != old_height + FUDGE);
719 name_text->raise_to_top();
724 /** Apply the current note range from the stream view
725 * by repositioning/hiding notes as necessary
728 MidiRegionView::apply_note_range (uint8_t min, uint8_t max, bool force)
730 if (!_enable_display) {
734 if (!force && _current_range_min == min && _current_range_max == max) {
738 _current_range_min = min;
739 _current_range_max = max;
741 for (Events::const_iterator i = _events.begin(); i != _events.end(); ++i) {
742 CanvasNoteEvent* event = *i;
743 Item* item = dynamic_cast<Item*>(event);
745 if (event && event->note()) {
746 if (event->note()->note() < _current_range_min
747 || event->note()->note() > _current_range_max) {
748 if (canvas_item_visible(item)) {
752 if (!canvas_item_visible(item)) {
756 event->hide_velocity();
757 if (CanvasNote* note = dynamic_cast<CanvasNote*>(event)) {
758 const double y1 = midi_stream_view()->note_to_y(event->note()->note());
759 const double y2 = y1 + floor(midi_stream_view()->note_height());
761 note->property_y1() = y1;
762 note->property_y2() = y2;
763 } else if (CanvasHit* hit = dynamic_cast<CanvasHit*>(event)) {
764 double x = trackview.editor().frame_to_pixel(
765 beats_to_frames(event->note()->time()) - _region->start());
766 const double diamond_size = midi_stream_view()->note_height() / 2.0;
767 double y = midi_stream_view()->note_to_y(event->note()->note())
768 + ((diamond_size-2.0) / 4.0);
770 hit->set_height(diamond_size);
771 hit->move(x-hit->x1(), y-hit->y1());
774 if (event->selected()) {
775 event->show_velocity();
784 MidiRegionView::add_ghost (TimeAxisView& tv)
786 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
790 double unit_position = _region->position () / samples_per_unit;
791 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
792 MidiGhostRegion* ghost;
794 if (mtv && mtv->midi_view()) {
795 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group
796 to allow having midi notes on top of note lines and waveforms.
798 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
800 ghost = new MidiGhostRegion (tv, trackview, unit_position);
803 ghost->set_height ();
804 ghost->set_duration (_region->length() / samples_per_unit);
805 ghosts.push_back (ghost);
807 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
808 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
809 ghost->add_note(note);
813 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
819 /** Begin tracking note state for successive calls to add_event
822 MidiRegionView::begin_write()
824 assert(!_active_notes);
825 _active_notes = new CanvasNote*[128];
826 for (unsigned i=0; i < 128; ++i) {
827 _active_notes[i] = NULL;
832 /** Destroy note state for add_event
835 MidiRegionView::end_write()
837 delete[] _active_notes;
838 _active_notes = NULL;
839 _marked_for_selection.clear();
843 /** Resolve an active MIDI note (while recording).
846 MidiRegionView::resolve_note(uint8_t note, double end_time)
848 if (midi_view()->note_mode() != Sustained) {
852 if (_active_notes && _active_notes[note]) {
853 const nframes64_t end_time_frames = beats_to_frames(end_time);
854 _active_notes[note]->property_x2() = trackview.editor().frame_to_pixel(end_time_frames);
855 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
856 _active_notes[note] = NULL;
861 /** Extend active notes to rightmost edge of region (if length is changed)
864 MidiRegionView::extend_active_notes()
866 if (!_active_notes) {
870 for (unsigned i=0; i < 128; ++i) {
871 if (_active_notes[i]) {
872 _active_notes[i]->property_x2() = trackview.editor().frame_to_pixel(_region->length());
878 MidiRegionView::play_midi_note(boost::shared_ptr<NoteType> note)
880 if (!trackview.editor().sound_notes()) {
884 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
887 route_ui->midi_track()->write_immediate_event(
888 note->on_event().size(), note->on_event().buffer());
890 const double note_length_beats = (note->off_event().time() - note->on_event().time());
891 nframes_t note_length_ms = beats_to_frames(note_length_beats)
892 * (1000 / (double)route_ui->session().nominal_frame_rate());
893 Glib::signal_timeout().connect(bind(mem_fun(this, &MidiRegionView::play_midi_note_off), note),
894 note_length_ms, G_PRIORITY_DEFAULT);
898 MidiRegionView::play_midi_note_off(boost::shared_ptr<NoteType> note)
900 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
903 route_ui->midi_track()->write_immediate_event(
904 note->off_event().size(), note->off_event().buffer());
910 MidiRegionView::note_in_visible_range(const boost::shared_ptr<NoteType> note) const
912 const nframes64_t note_start_frames = beats_to_frames(note->time());
913 bool outside = (note_start_frames - _region->start() >= _region->length())
914 || (note_start_frames < _region->start())
915 || (note->note() < midi_stream_view()->lowest_note())
916 || (note->note() > midi_stream_view()->highest_note());
920 /** Add a MIDI note to the view (with length).
922 * If in sustained mode, notes with length 0 will be considered active
923 * notes, and resolve_note should be called when the corresponding note off
924 * event arrives, to properly display the note.
927 MidiRegionView::add_note(const boost::shared_ptr<NoteType> note)
929 assert(note->time() >= 0);
930 assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
932 const nframes64_t note_start_frames = beats_to_frames(note->time());
933 const nframes64_t note_end_frames = beats_to_frames(note->end_time());
935 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
937 CanvasNoteEvent* event = 0;
939 const double x = trackview.editor().frame_to_pixel(note_start_frames - _region->start());
941 if (midi_view()->note_mode() == Sustained) {
942 const double y1 = midi_stream_view()->note_to_y(note->note());
943 const double note_endpixel =
944 trackview.editor().frame_to_pixel(note_end_frames - _region->start());
946 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
947 ev_rect->property_x1() = x;
948 ev_rect->property_y1() = y1;
949 if (note->length() > 0) {
950 ev_rect->property_x2() = note_endpixel;
952 ev_rect->property_x2() = trackview.editor().frame_to_pixel(_region->length());
954 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
956 if (note->length() == 0) {
958 assert(note->note() < 128);
959 // If this note is already active there's a stuck note,
960 // finish the old note rectangle
961 if (_active_notes[note->note()]) {
962 CanvasNote* const old_rect = _active_notes[note->note()];
963 boost::shared_ptr<NoteType> old_note = old_rect->note();
964 old_rect->property_x2() = x;
965 old_rect->property_outline_what() = (guint32) 0xF;
967 _active_notes[note->note()] = ev_rect;
969 /* outline all but right edge */
970 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
972 /* outline all edges */
973 ev_rect->property_outline_what() = (guint32) 0xF;
979 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
980 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
981 gr->add_note(ev_rect);
985 } else if (midi_view()->note_mode() == Percussive) {
986 const double diamond_size = midi_stream_view()->note_height() / 2.0;
987 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
989 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size, note);
990 ev_diamond->move(x, y);
997 if (_marked_for_selection.find(note) != _marked_for_selection.end()) {
998 note_selected(event, true);
1000 event->on_channel_selection_change(_last_channel_selection);
1001 _events.push_back(event);
1002 if (note_in_visible_range(note)) {
1011 MidiRegionView::add_pgm_change(PCEvent& program, const string& displaytext)
1013 assert(program.time >= 0);
1015 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
1016 const double x = trackview.editor().frame_to_pixel(beats_to_frames(program.time));
1018 double height = midi_stream_view()->contents_height();
1020 boost::shared_ptr<CanvasProgramChange> pgm_change = boost::shared_ptr<CanvasProgramChange>(
1021 new CanvasProgramChange(*this, *group,
1026 _custom_device_mode,
1027 program.time, program.channel, program.value));
1029 // Show unless program change is beyond the region bounds
1030 if (program.time - _region->start() >= _region->length() || program.time < _region->start()) {
1036 _pgm_changes.push_back(pgm_change);
1040 MidiRegionView::get_patch_key_at(double time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key)
1042 cerr << "getting patch key at " << time << " for channel " << channel << endl;
1043 Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
1044 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
1046 if (msb_control != 0) {
1047 msb = int(msb_control->get_float(true, time));
1048 cerr << "got msb " << msb;
1051 Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
1052 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
1054 if (lsb_control != 0) {
1055 lsb = lsb_control->get_float(true, time);
1056 cerr << " got lsb " << lsb;
1059 Evoral::Parameter program_change(MidiPgmChangeAutomation, channel, 0);
1060 boost::shared_ptr<Evoral::Control> program_control = _model->control(program_change);
1061 float program_number = -1.0;
1062 if (program_control != 0) {
1063 program_number = program_control->get_float(true, time);
1064 cerr << " got program " << program_number << endl;
1067 key.msb = (int) floor(msb + 0.5);
1068 key.lsb = (int) floor(lsb + 0.5);
1069 key.program_number = (int) floor(program_number + 0.5);
1070 assert(key.is_sane());
1075 MidiRegionView::alter_program_change(PCEvent& old_program, const MIDI::Name::PatchPrimaryKey& new_patch)
1077 // TODO: Get the real event here and alter them at the original times
1078 Evoral::Parameter bank_select_msb(MidiCCAutomation, old_program.channel, MIDI_CTL_MSB_BANK);
1079 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
1080 if (msb_control != 0) {
1081 msb_control->set_float(float(new_patch.msb), true, old_program.time);
1084 // TODO: Get the real event here and alter them at the original times
1085 Evoral::Parameter bank_select_lsb(MidiCCAutomation, old_program.channel, MIDI_CTL_LSB_BANK);
1086 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
1087 if (lsb_control != 0) {
1088 lsb_control->set_float(float(new_patch.lsb), true, old_program.time);
1091 Evoral::Parameter program_change(MidiPgmChangeAutomation, old_program.channel, 0);
1092 boost::shared_ptr<Evoral::Control> program_control = _model->control(program_change);
1094 assert(program_control != 0);
1095 program_control->set_float(float(new_patch.program_number), true, old_program.time);
1101 MidiRegionView::program_selected(CanvasProgramChange& program, const MIDI::Name::PatchPrimaryKey& new_patch)
1103 PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1104 alter_program_change(program_change_event, new_patch);
1108 MidiRegionView::previous_program(CanvasProgramChange& program)
1110 MIDI::Name::PatchPrimaryKey key;
1111 get_patch_key_at(program.event_time(), program.channel(), key);
1113 boost::shared_ptr<MIDI::Name::Patch> patch =
1114 MIDI::Name::MidiPatchManager::instance().previous_patch(
1116 _custom_device_mode,
1120 PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1122 alter_program_change(program_change_event, patch->patch_primary_key());
1127 MidiRegionView::next_program(CanvasProgramChange& program)
1129 MIDI::Name::PatchPrimaryKey key;
1130 get_patch_key_at(program.event_time(), program.channel(), key);
1132 boost::shared_ptr<MIDI::Name::Patch> patch =
1133 MIDI::Name::MidiPatchManager::instance().next_patch(
1135 _custom_device_mode,
1139 PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1141 alter_program_change(program_change_event, patch->patch_primary_key());
1146 MidiRegionView::delete_selection()
1148 assert(_delta_command);
1150 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1151 if ((*i)->selected()) {
1152 _delta_command->remove((*i)->note());
1160 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev)
1162 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1163 if ((*i)->selected() && (*i) != ev) {
1164 (*i)->selected(false);
1172 MidiRegionView::unique_select(ArdourCanvas::CanvasNoteEvent* ev)
1174 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1176 (*i)->selected(false);
1181 _selection.insert(ev);
1183 if ( ! ev->selected()) {
1189 MidiRegionView::note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
1192 clear_selection_except(ev);
1195 if (_selection.insert(ev).second) {
1196 play_midi_note(ev->note());
1199 if ( ! ev->selected()) {
1206 MidiRegionView::note_deselected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
1209 clear_selection_except(ev);
1212 _selection.erase(ev);
1214 if (ev->selected()) {
1215 ev->selected(false);
1221 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
1223 const double last_y = std::min(y1, y2);
1224 const double y = std::max(y1, y2);
1226 // TODO: Make this faster by storing the last updated selection rect, and only
1227 // adjusting things that are in the area that appears/disappeared.
1228 // We probably need a tree to be able to find events in O(log(n)) time.
1231 double last_x1 = 0.0;
1235 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1237 // Events should always be sorted by increasing x1() here
1238 assert((*i)->x1() >= last_x1);
1239 last_x1 = (*i)->x1();
1242 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1243 if (!(*i)->selected()) {
1244 (*i)->selected(true);
1245 _selection.insert(*i);
1246 play_midi_note((*i)->note());
1248 // Not inside rectangle
1249 } else if ((*i)->selected()) {
1250 (*i)->selected(false);
1251 _selection.erase(*i);
1255 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1257 // Events should always be sorted by increasing x1() here
1258 assert((*i)->x1() >= last_x1);
1259 last_x1 = (*i)->x1();
1262 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1263 if (!(*i)->selected()) {
1264 (*i)->selected(true);
1265 _selection.insert(*i);
1266 play_midi_note((*i)->note());
1268 // Not inside rectangle
1269 } else if ((*i)->selected()) {
1270 (*i)->selected(false);
1271 _selection.erase(*i);
1279 MidiRegionView::move_selection(double dx, double dy)
1281 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
1282 (*i)->move_event(dx, dy);
1287 MidiRegionView::note_dropped(CanvasNoteEvent* ev, double dt, uint8_t dnote)
1289 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
1290 if (_selection.find(ev) == _selection.end()) {
1294 uint8_t lowest_note_in_selection = midi_stream_view()->lowest_note();
1295 uint8_t highest_note_in_selection = midi_stream_view()->highest_note();
1296 uint8_t highest_note_difference = 0;
1298 // find highest and lowest notes first
1299 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1300 uint8_t pitch = (*i)->note()->note();
1301 lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
1302 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
1306 cerr << "dnote: " << (int) dnote << endl;
1307 cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
1308 << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
1309 cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
1310 << int(highest_note_in_selection) << endl;
1311 cerr << "selection size: " << _selection.size() << endl;
1312 cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
1315 // Make sure the note pitch does not exceed the MIDI standard range
1316 if (dnote <= 127 && (highest_note_in_selection + dnote > 127)) {
1317 highest_note_difference = highest_note_in_selection - 127;
1320 start_delta_command(_("move notes"));
1322 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
1323 Selection::iterator next = i;
1326 const boost::shared_ptr<NoteType> copy(new NoteType(*(*i)->note().get()));
1328 // we need to snap here again in nframes64_t in order to be sample accurate
1329 double start_frames = snap_to_frame(beats_to_frames((*i)->note()->time()) + dt);
1331 // keep notes inside region if dragged beyond left region bound
1332 if (start_frames < _region->start()) {
1333 start_frames = _region->start();
1336 copy->set_time(frames_to_beats(start_frames));
1338 uint8_t original_pitch = (*i)->note()->note();
1339 uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
1341 // keep notes in standard midi range
1342 clamp_to_0_127(new_pitch);
1344 // keep original pitch if note is dragged outside valid midi range
1345 if ((original_pitch != 0 && new_pitch == 0)
1346 || (original_pitch != 127 && new_pitch == 127)) {
1347 new_pitch = original_pitch;
1350 lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
1351 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
1353 copy->set_note(new_pitch);
1355 command_remove_note(*i);
1356 command_add_note(copy, (*i)->selected());
1363 // care about notes being moved beyond the upper/lower bounds on the canvas
1364 if (lowest_note_in_selection < midi_stream_view()->lowest_note() ||
1365 highest_note_in_selection > midi_stream_view()->highest_note()) {
1366 midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
1371 MidiRegionView::snap_to_frame(double x)
1373 PublicEditor &editor = trackview.editor();
1374 // x is region relative, convert it to global absolute frames
1375 nframes64_t frame = editor.pixel_to_frame(x) + _region->position();
1376 editor.snap_to(frame);
1377 return frame - _region->position(); // convert back to region relative
1381 MidiRegionView::snap_to_frame(nframes64_t x)
1383 PublicEditor &editor = trackview.editor();
1384 // x is region relative
1385 // convert x to global frame
1386 nframes64_t frame = x + _region->position();
1387 editor.snap_to(frame);
1388 // convert event_frame back to local coordinates relative to position
1389 frame -= _region->position();
1394 MidiRegionView::snap_to_pixel(double x)
1396 return (double) trackview.editor().frame_to_pixel(snap_to_frame(x));
1400 MidiRegionView::get_position_pixels()
1402 nframes64_t region_frame = get_position();
1403 return trackview.editor().frame_to_pixel(region_frame);
1407 MidiRegionView::beats_to_frames(double beats) const
1409 return _time_converter.to(beats);
1413 MidiRegionView::frames_to_beats(nframes64_t frames) const
1415 return _time_converter.from(frames);
1419 MidiRegionView::begin_resizing(CanvasNote::NoteEnd note_end)
1421 _resize_data.clear();
1423 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1424 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
1426 // only insert CanvasNotes into the map
1428 NoteResizeData *resize_data = new NoteResizeData();
1429 resize_data->canvas_note = note;
1431 // create a new SimpleRect from the note which will be the resize preview
1432 SimpleRect *resize_rect = new SimpleRect(
1433 *group, note->x1(), note->y1(), note->x2(), note->y2());
1435 // calculate the colors: get the color settings
1436 uint32_t fill_color = UINT_RGBA_CHANGE_A(
1437 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(),
1440 // make the resize preview notes more transparent and bright
1441 fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
1443 // calculate color based on note velocity
1444 resize_rect->property_fill_color_rgba() = UINT_INTERPOLATE(
1445 CanvasNoteEvent::meter_style_fill_color(note->note()->velocity()),
1449 resize_rect->property_outline_color_rgba() = CanvasNoteEvent::calculate_outline(
1450 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get());
1452 resize_data->resize_rect = resize_rect;
1454 if (note_end == CanvasNote::NOTE_ON) {
1455 resize_data->current_x = note->x1();
1456 } else { // NOTE_OFF
1457 resize_data->current_x = note->x2();
1460 _resize_data.push_back(resize_data);
1466 MidiRegionView::update_resizing(CanvasNote::NoteEnd note_end, double x, bool relative)
1468 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1469 SimpleRect* resize_rect = (*i)->resize_rect;
1470 CanvasNote* canvas_note = (*i)->canvas_note;
1472 const double region_start = get_position_pixels();
1475 (*i)->current_x = (*i)->current_x + x;
1477 // x is in track relative, transform it to region relative
1478 (*i)->current_x = x - region_start;
1481 double current_x = (*i)->current_x;
1483 if (note_end == CanvasNote::NOTE_ON) {
1484 resize_rect->property_x1() = snap_to_pixel(current_x);
1485 resize_rect->property_x2() = canvas_note->x2();
1487 resize_rect->property_x2() = snap_to_pixel(current_x);
1488 resize_rect->property_x1() = canvas_note->x1();
1494 MidiRegionView::commit_resizing(CanvasNote::NoteEnd note_end, double event_x, bool relative)
1496 start_delta_command(_("resize notes"));
1498 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1499 CanvasNote* canvas_note = (*i)->canvas_note;
1500 SimpleRect* resize_rect = (*i)->resize_rect;
1501 double current_x = (*i)->current_x;
1502 const double position = get_position_pixels();
1505 // event_x is in track relative, transform it to region relative
1506 current_x = event_x - position;
1509 // because snapping works on world coordinates we have to transform current_x
1510 // to world coordinates before snapping and transform it back afterwards
1511 nframes64_t current_frame = snap_to_frame(current_x);
1512 // transform to region start relative
1513 current_frame += _region->start();
1515 const boost::shared_ptr<NoteType> copy(new NoteType(*(canvas_note->note().get())));
1517 // resize beginning of note
1518 if (note_end == CanvasNote::NOTE_ON && current_frame < copy->end_time()) {
1519 command_remove_note(canvas_note);
1520 copy->on_event().time() = current_frame;
1521 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1523 // resize end of note
1524 if (note_end == CanvasNote::NOTE_OFF && current_frame > copy->time()) {
1525 command_remove_note(canvas_note);
1526 copy->off_event().time() = current_frame;
1527 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1534 _resize_data.clear();
1539 MidiRegionView::change_note_velocity(CanvasNoteEvent* event, int8_t velocity, bool relative)
1541 const boost::shared_ptr<NoteType> copy(new NoteType(*(event->note().get())));
1544 uint8_t new_velocity = copy->velocity() + velocity;
1545 clamp_to_0_127(new_velocity);
1546 copy->set_velocity(new_velocity);
1548 copy->set_velocity(velocity);
1551 command_remove_note(event);
1552 command_add_note(copy, event->selected());
1556 MidiRegionView::change_velocity(CanvasNoteEvent* ev, int8_t velocity, bool relative)
1558 start_delta_command(_("change velocity"));
1560 change_note_velocity(ev, velocity, relative);
1562 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1563 Selection::iterator next = i;
1565 if ( !(*((*i)->note()) == *(ev->note())) ) {
1566 change_note_velocity(*i, velocity, relative);
1575 MidiRegionView::change_channel(uint8_t channel)
1577 start_delta_command(_("change channel"));
1578 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1579 Selection::iterator next = i;
1582 CanvasNoteEvent* event = *i;
1583 const boost::shared_ptr<NoteType> copy(new NoteType(*(event->note().get())));
1585 copy->set_channel(channel);
1587 command_remove_note(event);
1588 command_add_note(copy, event->selected());
1598 MidiRegionView::note_entered(ArdourCanvas::CanvasNoteEvent* ev)
1600 if (ev->note() && _mouse_state == EraseTouchDragging) {
1601 start_delta_command(_("note entered"));
1603 _delta_command->remove(ev->note());
1604 } else if (_mouse_state == SelectTouchDragging) {
1605 note_selected(ev, true);
1611 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
1613 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
1615 display_model(msrc->model());
1619 MidiRegionView::set_frame_color()
1622 if (_selected && should_show_selection) {
1623 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
1625 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
1631 MidiRegionView::midi_channel_mode_changed(ChannelMode mode, uint16_t mask)
1635 case FilterChannels:
1636 _force_channel = -1;
1639 _force_channel = mask;
1640 mask = 0xFFFF; // Show all notes as active (below)
1643 // Update notes for selection
1644 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1645 (*i)->on_channel_selection_change(mask);
1648 _last_channel_selection = mask;
1652 MidiRegionView::midi_patch_settings_changed(std::string model, std::string custom_device_mode)
1654 _model_name = model;
1655 _custom_device_mode = custom_device_mode;