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)
108 : sigc::trackable(other)
111 , _last_channel_selection(0xFFFF)
112 , _default_note_length(1.0)
113 , _model_name(string())
114 , _custom_device_mode(string())
116 , _note_group(new ArdourCanvas::Group(*get_canvas_group()))
117 , _delta_command(NULL)
124 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
125 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
130 MidiRegionView::MidiRegionView (const MidiRegionView& other, boost::shared_ptr<MidiRegion> region)
131 : RegionView (other, boost::shared_ptr<Region> (region))
133 , _last_channel_selection(0xFFFF)
134 , _default_note_length(1.0)
135 , _model_name(string())
136 , _custom_device_mode(string())
138 , _note_group(new ArdourCanvas::Group(*get_canvas_group()))
139 , _delta_command(NULL)
146 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
147 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
153 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
156 midi_region()->midi_source(0)->load_model();
159 _model = midi_region()->midi_source(0)->model();
160 _enable_display = false;
162 RegionView::init (basic_color, false);
164 compute_colors (basic_color);
166 set_height (trackview.current_height());
169 region_sync_changed ();
170 region_resized (BoundsChanged);
173 reset_width_dependent_items (_pixel_width);
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 double drag_start_x, drag_start_y;
199 static double last_x, last_y;
200 double event_x, event_y;
201 nframes64_t event_frame = 0;
203 static ArdourCanvas::SimpleRect* drag_rect = NULL;
205 if (trackview.editor().current_mouse_mode() != MouseNote)
208 const Editing::MidiEditMode midi_edit_mode = trackview.editor().current_midi_edit_mode();
212 if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
213 _mouse_state = SelectTouchDragging;
215 } else if (ev->key.keyval == GDK_Escape) {
221 case GDK_KEY_RELEASE:
222 if (ev->key.keyval == GDK_Delete) {
226 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
232 case GDK_BUTTON_PRESS:
233 if (_mouse_state != SelectTouchDragging && ev->button.button == 1) {
234 _pressed_button = ev->button.button;
235 _mouse_state = Pressed;
238 _pressed_button = ev->button.button;
241 case GDK_2BUTTON_PRESS:
244 case GDK_ENTER_NOTIFY:
245 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
246 Keyboard::magic_widget_grab_focus();
250 case GDK_MOTION_NOTIFY:
251 event_x = ev->motion.x;
252 event_y = ev->motion.y;
253 group->w2i(event_x, event_y);
255 // convert event_x to global frame
256 event_frame = trackview.editor().pixel_to_frame(event_x) + _region->position();
257 trackview.editor().snap_to(event_frame);
258 // convert event_frame back to local coordinates relative to position
259 event_frame -= _region->position();
261 switch (_mouse_state) {
262 case Pressed: // Drag start
265 if (_pressed_button == 1 && midi_edit_mode == MidiEditSelect) {
266 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
267 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
270 drag_start_x = event_x;
271 drag_start_y = event_y;
273 drag_rect = new ArdourCanvas::SimpleRect(*group);
274 drag_rect->property_x1() = event_x;
275 drag_rect->property_y1() = event_y;
276 drag_rect->property_x2() = event_x;
277 drag_rect->property_y2() = event_y;
278 drag_rect->property_outline_what() = 0xFF;
279 drag_rect->property_outline_color_rgba()
280 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
281 drag_rect->property_fill_color_rgba()
282 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
284 _mouse_state = SelectRectDragging;
287 // Add note drag start
288 } else if (midi_edit_mode == MidiEditPencil) {
289 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
290 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
293 drag_start_x = event_x;
294 drag_start_y = event_y;
296 drag_rect = new ArdourCanvas::SimpleRect(*group);
297 drag_rect->property_x1() = trackview.editor().frame_to_pixel(event_frame);
299 drag_rect->property_y1() = midi_stream_view()->note_to_y(
300 midi_stream_view()->y_to_note(event_y));
301 drag_rect->property_x2() = event_x;
302 drag_rect->property_y2() = drag_rect->property_y1()
303 + floor(midi_stream_view()->note_height());
304 drag_rect->property_outline_what() = 0xFF;
305 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
306 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
308 _mouse_state = AddDragging;
314 case SelectRectDragging: // Select drag motion
315 case AddDragging: // Add note drag motion
316 if (ev->motion.is_hint) {
319 GdkModifierType state;
320 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
325 if (_mouse_state == AddDragging)
326 event_x = trackview.editor().frame_to_pixel(event_frame);
329 if (event_x > drag_start_x)
330 drag_rect->property_x2() = event_x;
332 drag_rect->property_x1() = event_x;
335 if (drag_rect && _mouse_state == SelectRectDragging) {
336 if (event_y > drag_start_y)
337 drag_rect->property_y2() = event_y;
339 drag_rect->property_y1() = event_y;
341 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
347 case SelectTouchDragging:
355 case GDK_BUTTON_RELEASE:
356 event_x = ev->motion.x;
357 event_y = ev->motion.y;
358 group->w2i(event_x, event_y);
359 group->ungrab(ev->button.time);
360 event_frame = trackview.editor().pixel_to_frame(event_x);
362 if (_pressed_button != 1) {
366 switch (_mouse_state) {
367 case Pressed: // Clicked
368 switch (midi_edit_mode) {
374 create_note_at(event_x, event_y, _default_note_length);
379 case SelectRectDragging: // Select drag done
384 case AddDragging: // Add drag done
386 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
387 const double x = drag_rect->property_x1();
388 const double length = trackview.editor().pixel_to_frame(
389 drag_rect->property_x2() - drag_rect->property_x1());
391 create_note_at(x, drag_rect->property_y1(), frames_to_beats(length));
406 /** Add a note to the model, and the view, at a canvas (click) coordinate.
407 * \param x horizontal position in pixels
408 * \param y vertical position in pixels
409 * \param length duration of the note in beats */
411 MidiRegionView::create_note_at(double x, double y, double length)
413 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
414 MidiStreamView* const view = mtv->midi_view();
416 double note = midi_stream_view()->y_to_note(y);
419 assert(note <= 127.0);
421 // Start of note in frames relative to region start
422 nframes64_t start_frames = snap_frame_to_frame(trackview.editor().pixel_to_frame(x));
423 assert(start_frames >= 0);
426 length = frames_to_beats(
427 snap_frame_to_frame(start_frames + beats_to_frames(length)) - start_frames);
429 const boost::shared_ptr<NoteType> new_note(new NoteType(0,
430 frames_to_beats(start_frames + _region->start()), length,
431 (uint8_t)note, 0x40));
433 view->update_note_range(new_note->note());
435 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
437 _model->apply_command(trackview.session(), cmd);
442 MidiRegionView::clear_events()
447 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
448 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
453 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
458 _pgm_changes.clear();
464 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
467 if (_enable_display) {
474 MidiRegionView::start_delta_command(string name)
476 if (!_delta_command) {
477 _delta_command = _model->new_delta_command(name);
482 MidiRegionView::command_add_note(const boost::shared_ptr<NoteType> note, bool selected, bool show_velocity)
484 if (_delta_command) {
485 _delta_command->add(note);
488 _marked_for_selection.insert(note);
491 _marked_for_velocity.insert(note);
496 MidiRegionView::command_remove_note(ArdourCanvas::CanvasNoteEvent* ev)
498 if (_delta_command && ev->note()) {
499 _delta_command->remove(ev->note());
504 MidiRegionView::apply_command()
506 if (!_delta_command) {
510 // Mark all selected notes for selection when model reloads
511 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
512 _marked_for_selection.insert((*i)->note());
515 _model->apply_command(trackview.session(), _delta_command);
516 _delta_command = NULL;
517 midi_view()->midi_track()->diskstream()->playlist_modified();
519 _marked_for_selection.clear();
520 _marked_for_velocity.clear();
525 MidiRegionView::abort_command()
527 delete _delta_command;
528 _delta_command = NULL;
534 MidiRegionView::redisplay_model()
536 // Don't redisplay the model if we're currently recording and displaying that
545 MidiModel::Notes notes = _model->notes();
547 for (size_t i = 0; i < _model->n_notes(); ++i) {
548 add_note(_model->note_at(i));
553 display_program_changes();
555 _model->read_unlock();
558 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
563 MidiRegionView::display_program_changes()
565 boost::shared_ptr<Evoral::Control> control = _model->control(MidiPgmChangeAutomation);
570 Glib::Mutex::Lock lock (control->list()->lock());
572 uint8_t channel = control->parameter().channel();
574 for (AutomationList::const_iterator event = control->list()->begin();
575 event != control->list()->end(); ++event) {
576 double event_time = (*event)->when;
577 double program_number = floor((*event)->value + 0.5);
579 // Get current value of bank select MSB at time of the program change
580 Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
581 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
583 if (msb_control != 0) {
584 msb = uint8_t(floor(msb_control->get_float(true, event_time) + 0.5));
587 // Get current value of bank select LSB at time of the program change
588 Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
589 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
591 if (lsb_control != 0) {
592 lsb = uint8_t(floor(lsb_control->get_float(true, event_time) + 0.5));
595 MIDI::Name::PatchPrimaryKey patch_key(msb, lsb, program_number);
597 boost::shared_ptr<MIDI::Name::Patch> patch =
598 MIDI::Name::MidiPatchManager::instance().find_patch(
599 _model_name, _custom_device_mode, channel, patch_key);
601 PCEvent program_change(event_time, uint8_t(program_number), channel);
604 add_pgm_change(program_change, patch->name());
607 snprintf(buf, 4, "%d", int(program_number));
608 add_pgm_change(program_change, buf);
614 MidiRegionView::display_sysexes()
616 for (MidiModel::SysExes::const_iterator i = _model->sysexes().begin(); i != _model->sysexes().end(); ++i) {
617 ARDOUR::MidiModel::TimeType time = (*i)->time();
622 for (uint32_t b = 0; b < (*i)->size(); ++b) {
623 str << int((*i)->buffer()[b]);
624 if (b != (*i)->size() -1) {
628 string text = str.str();
630 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
631 const double x = trackview.editor().frame_to_pixel(beats_to_frames(time));
633 double height = midi_stream_view()->contents_height();
635 boost::shared_ptr<CanvasSysEx> sysex = boost::shared_ptr<CanvasSysEx>(
636 new CanvasSysEx(*this, *group, text, height, x, 1.0));
638 // Show unless program change is beyond the region bounds
639 if (time - _region->start() >= _region->length() || time < _region->start()) {
645 _sys_exes.push_back(sysex);
650 MidiRegionView::~MidiRegionView ()
652 in_destructor = true;
654 RegionViewGoingAway (this); /* EMIT_SIGNAL */
663 delete _delta_command;
668 MidiRegionView::region_resized (Change what_changed)
670 RegionView::region_resized(what_changed);
672 if (what_changed & ARDOUR::PositionChanged) {
673 set_duration(_region->length(), 0);
674 if (_enable_display) {
681 MidiRegionView::reset_width_dependent_items (double pixel_width)
683 RegionView::reset_width_dependent_items(pixel_width);
684 assert(_pixel_width == pixel_width);
686 if (_enable_display) {
692 MidiRegionView::set_height (double height)
694 static const double FUDGE = 2.0;
695 const double old_height = _height;
696 RegionView::set_height(height);
697 _height = height - FUDGE;
699 apply_note_range(midi_stream_view()->lowest_note(),
700 midi_stream_view()->highest_note(),
701 height != old_height + FUDGE);
704 name_text->raise_to_top();
709 /** Apply the current note range from the stream view
710 * by repositioning/hiding notes as necessary
713 MidiRegionView::apply_note_range (uint8_t min, uint8_t max, bool force)
715 if (!_enable_display) {
719 if (!force && _current_range_min == min && _current_range_max == max) {
723 _current_range_min = min;
724 _current_range_max = max;
726 for (Events::const_iterator i = _events.begin(); i != _events.end(); ++i) {
727 CanvasNoteEvent* event = *i;
728 Item* item = dynamic_cast<Item*>(event);
730 if (event && event->note()) {
731 if (event->note()->note() < _current_range_min
732 || event->note()->note() > _current_range_max) {
733 if (canvas_item_visible(item)) {
737 if (!canvas_item_visible(item)) {
741 if (CanvasNote* note = dynamic_cast<CanvasNote*>(event)) {
742 const double y1 = midi_stream_view()->note_to_y(event->note()->note());
743 const double y2 = y1 + floor(midi_stream_view()->note_height());
745 note->property_y1() = y1;
746 note->property_y2() = y2;
747 } else if (CanvasHit* hit = dynamic_cast<CanvasHit*>(event)) {
748 double x = trackview.editor().frame_to_pixel(
749 beats_to_frames(event->note()->time()) - _region->start());
750 const double diamond_size = midi_stream_view()->note_height() / 2.0;
751 double y = midi_stream_view()->note_to_y(event->note()->note())
752 + ((diamond_size-2.0) / 4.0);
754 hit->set_height(diamond_size);
755 hit->move(x-hit->x1(), y-hit->y1());
765 MidiRegionView::add_ghost (TimeAxisView& tv)
767 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
771 double unit_position = _region->position () / samples_per_unit;
772 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
773 MidiGhostRegion* ghost;
775 if (mtv && mtv->midi_view()) {
776 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group
777 to allow having midi notes on top of note lines and waveforms.
779 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
781 ghost = new MidiGhostRegion (tv, trackview, unit_position);
784 ghost->set_height ();
785 ghost->set_duration (_region->length() / samples_per_unit);
786 ghosts.push_back (ghost);
788 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
789 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
790 ghost->add_note(note);
794 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
800 /** Begin tracking note state for successive calls to add_event
803 MidiRegionView::begin_write()
805 assert(!_active_notes);
806 _active_notes = new CanvasNote*[128];
807 for (unsigned i=0; i < 128; ++i) {
808 _active_notes[i] = NULL;
813 /** Destroy note state for add_event
816 MidiRegionView::end_write()
818 delete[] _active_notes;
819 _active_notes = NULL;
820 _marked_for_selection.clear();
821 _marked_for_velocity.clear();
825 /** Resolve an active MIDI note (while recording).
828 MidiRegionView::resolve_note(uint8_t note, double end_time)
830 if (midi_view()->note_mode() != Sustained) {
834 if (_active_notes && _active_notes[note]) {
835 const nframes64_t end_time_frames = beats_to_frames(end_time);
836 _active_notes[note]->property_x2() = trackview.editor().frame_to_pixel(end_time_frames);
837 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
838 _active_notes[note] = NULL;
843 /** Extend active notes to rightmost edge of region (if length is changed)
846 MidiRegionView::extend_active_notes()
848 if (!_active_notes) {
852 for (unsigned i=0; i < 128; ++i) {
853 if (_active_notes[i]) {
854 _active_notes[i]->property_x2() = trackview.editor().frame_to_pixel(_region->length());
860 MidiRegionView::play_midi_note(boost::shared_ptr<NoteType> note)
862 if (!trackview.editor().sound_notes()) {
866 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
869 route_ui->midi_track()->write_immediate_event(
870 note->on_event().size(), note->on_event().buffer());
872 const double note_length_beats = (note->off_event().time() - note->on_event().time());
873 nframes_t note_length_ms = beats_to_frames(note_length_beats)
874 * (1000 / (double)route_ui->session().nominal_frame_rate());
875 Glib::signal_timeout().connect(bind(mem_fun(this, &MidiRegionView::play_midi_note_off), note),
876 note_length_ms, G_PRIORITY_DEFAULT);
880 MidiRegionView::play_midi_note_off(boost::shared_ptr<NoteType> note)
882 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
885 route_ui->midi_track()->write_immediate_event(
886 note->off_event().size(), note->off_event().buffer());
892 MidiRegionView::note_in_visible_range(const boost::shared_ptr<NoteType> note) const
894 const nframes64_t note_start_frames = beats_to_frames(note->time());
895 bool outside = (note_start_frames - _region->start() >= _region->length())
896 || (note_start_frames < _region->start())
897 || (note->note() < midi_stream_view()->lowest_note())
898 || (note->note() > midi_stream_view()->highest_note());
902 /** Add a MIDI note to the view (with length).
904 * If in sustained mode, notes with length 0 will be considered active
905 * notes, and resolve_note should be called when the corresponding note off
906 * event arrives, to properly display the note.
909 MidiRegionView::add_note(const boost::shared_ptr<NoteType> note)
911 assert(note->time() >= 0);
912 assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
914 const nframes64_t note_start_frames = beats_to_frames(note->time());
915 const nframes64_t note_end_frames = beats_to_frames(note->end_time());
917 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
919 CanvasNoteEvent* event = 0;
921 const double x = trackview.editor().frame_to_pixel(note_start_frames - _region->start());
923 if (midi_view()->note_mode() == Sustained) {
924 const double y1 = midi_stream_view()->note_to_y(note->note());
925 const double note_endpixel =
926 trackview.editor().frame_to_pixel(note_end_frames - _region->start());
928 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
929 ev_rect->property_x1() = x;
930 ev_rect->property_y1() = y1;
931 if (note->length() > 0) {
932 ev_rect->property_x2() = note_endpixel;
934 ev_rect->property_x2() = trackview.editor().frame_to_pixel(_region->length());
936 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
938 if (note->length() == 0) {
940 assert(note->note() < 128);
941 // If this note is already active there's a stuck note,
942 // finish the old note rectangle
943 if (_active_notes[note->note()]) {
944 CanvasNote* const old_rect = _active_notes[note->note()];
945 boost::shared_ptr<NoteType> old_note = old_rect->note();
946 old_rect->property_x2() = x;
947 old_rect->property_outline_what() = (guint32) 0xF;
949 _active_notes[note->note()] = ev_rect;
951 /* outline all but right edge */
952 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
954 /* outline all edges */
955 ev_rect->property_outline_what() = (guint32) 0xF;
961 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
962 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
963 gr->add_note(ev_rect);
967 } else if (midi_view()->note_mode() == Percussive) {
968 const double diamond_size = midi_stream_view()->note_height() / 2.0;
969 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
971 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size, note);
972 ev_diamond->move(x, y);
979 if (_marked_for_selection.find(note) != _marked_for_selection.end()) {
980 note_selected(event, true);
982 if (_marked_for_velocity.find(note) != _marked_for_velocity.end()) {
983 event->show_velocity();
985 event->on_channel_selection_change(_last_channel_selection);
986 _events.push_back(event);
987 if (note_in_visible_range(note)) {
996 MidiRegionView::add_pgm_change(PCEvent& program, const string& displaytext)
998 assert(program.time >= 0);
1000 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
1001 const double x = trackview.editor().frame_to_pixel(beats_to_frames(program.time));
1003 double height = midi_stream_view()->contents_height();
1005 boost::shared_ptr<CanvasProgramChange> pgm_change = boost::shared_ptr<CanvasProgramChange>(
1006 new CanvasProgramChange(*this, *group,
1011 _custom_device_mode,
1012 program.time, program.channel, program.value));
1014 // Show unless program change is beyond the region bounds
1015 if (program.time - _region->start() >= _region->length() || program.time < _region->start()) {
1021 _pgm_changes.push_back(pgm_change);
1025 MidiRegionView::get_patch_key_at(double time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key)
1027 cerr << "getting patch key at " << time << " for channel " << channel << endl;
1028 Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
1029 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
1031 if (msb_control != 0) {
1032 msb = int(msb_control->get_float(true, time));
1033 cerr << "got msb " << msb;
1036 Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
1037 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
1039 if (lsb_control != 0) {
1040 lsb = lsb_control->get_float(true, time);
1041 cerr << " got lsb " << lsb;
1044 Evoral::Parameter program_change(MidiPgmChangeAutomation, channel, 0);
1045 boost::shared_ptr<Evoral::Control> program_control = _model->control(program_change);
1046 float program_number = -1.0;
1047 if (program_control != 0) {
1048 program_number = program_control->get_float(true, time);
1049 cerr << " got program " << program_number << endl;
1052 key.msb = (int) floor(msb + 0.5);
1053 key.lsb = (int) floor(lsb + 0.5);
1054 key.program_number = (int) floor(program_number + 0.5);
1055 assert(key.is_sane());
1060 MidiRegionView::alter_program_change(PCEvent& old_program, const MIDI::Name::PatchPrimaryKey& new_patch)
1062 // TODO: Get the real event here and alter them at the original times
1063 Evoral::Parameter bank_select_msb(MidiCCAutomation, old_program.channel, MIDI_CTL_MSB_BANK);
1064 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
1065 if (msb_control != 0) {
1066 msb_control->set_float(float(new_patch.msb), true, old_program.time);
1069 // TODO: Get the real event here and alter them at the original times
1070 Evoral::Parameter bank_select_lsb(MidiCCAutomation, old_program.channel, MIDI_CTL_LSB_BANK);
1071 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
1072 if (lsb_control != 0) {
1073 lsb_control->set_float(float(new_patch.lsb), true, old_program.time);
1076 Evoral::Parameter program_change(MidiPgmChangeAutomation, old_program.channel, 0);
1077 boost::shared_ptr<Evoral::Control> program_control = _model->control(program_change);
1079 assert(program_control != 0);
1080 program_control->set_float(float(new_patch.program_number), true, old_program.time);
1086 MidiRegionView::program_selected(CanvasProgramChange& program, const MIDI::Name::PatchPrimaryKey& new_patch)
1088 PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1089 alter_program_change(program_change_event, new_patch);
1093 MidiRegionView::previous_program(CanvasProgramChange& program)
1095 MIDI::Name::PatchPrimaryKey key;
1096 get_patch_key_at(program.event_time(), program.channel(), key);
1098 boost::shared_ptr<MIDI::Name::Patch> patch =
1099 MIDI::Name::MidiPatchManager::instance().previous_patch(
1101 _custom_device_mode,
1105 PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1107 alter_program_change(program_change_event, patch->patch_primary_key());
1112 MidiRegionView::next_program(CanvasProgramChange& program)
1114 MIDI::Name::PatchPrimaryKey key;
1115 get_patch_key_at(program.event_time(), program.channel(), key);
1117 boost::shared_ptr<MIDI::Name::Patch> patch =
1118 MIDI::Name::MidiPatchManager::instance().next_patch(
1120 _custom_device_mode,
1124 PCEvent program_change_event(program.event_time(), program.program(), program.channel());
1126 alter_program_change(program_change_event, patch->patch_primary_key());
1131 MidiRegionView::delete_selection()
1133 if (_selection.empty()) {
1137 if (!_delta_command) {
1138 _delta_command = _model->new_delta_command("delete selection");
1141 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1142 if ((*i)->selected()) {
1143 _delta_command->remove((*i)->note());
1151 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev)
1153 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1154 if ((*i)->selected() && (*i) != ev) {
1155 (*i)->selected(false);
1156 (*i)->hide_velocity();
1164 MidiRegionView::unique_select(ArdourCanvas::CanvasNoteEvent* ev)
1166 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1168 (*i)->selected(false);
1169 (*i)->hide_velocity();
1174 _selection.insert(ev);
1176 if ( ! ev->selected()) {
1182 MidiRegionView::note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
1185 clear_selection_except(ev);
1188 if (_selection.insert(ev).second) {
1189 play_midi_note(ev->note());
1192 if ( ! ev->selected()) {
1199 MidiRegionView::note_deselected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
1202 clear_selection_except(ev);
1205 _selection.erase(ev);
1207 if (ev->selected()) {
1208 ev->selected(false);
1214 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
1216 const double last_y = std::min(y1, y2);
1217 const double y = std::max(y1, y2);
1219 // TODO: Make this faster by storing the last updated selection rect, and only
1220 // adjusting things that are in the area that appears/disappeared.
1221 // We probably need a tree to be able to find events in O(log(n)) time.
1224 double last_x1 = 0.0;
1228 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1230 // Events should always be sorted by increasing x1() here
1231 assert((*i)->x1() >= last_x1);
1232 last_x1 = (*i)->x1();
1235 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1236 if (!(*i)->selected()) {
1237 (*i)->selected(true);
1238 _selection.insert(*i);
1239 play_midi_note((*i)->note());
1241 // Not inside rectangle
1242 } else if ((*i)->selected()) {
1243 (*i)->selected(false);
1244 _selection.erase(*i);
1248 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1250 // Events should always be sorted by increasing x1() here
1251 assert((*i)->x1() >= last_x1);
1252 last_x1 = (*i)->x1();
1255 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1256 if (!(*i)->selected()) {
1257 (*i)->selected(true);
1258 _selection.insert(*i);
1259 play_midi_note((*i)->note());
1261 // Not inside rectangle
1262 } else if ((*i)->selected()) {
1263 (*i)->selected(false);
1264 _selection.erase(*i);
1272 MidiRegionView::move_selection(double dx, double dy)
1274 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1275 (*i)->move_event(dx, dy);
1281 MidiRegionView::note_dropped(CanvasNoteEvent* ev, double dt, uint8_t dnote)
1283 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
1284 if (_selection.find(ev) == _selection.end()) {
1288 uint8_t lowest_note_in_selection = midi_stream_view()->lowest_note();
1289 uint8_t highest_note_in_selection = midi_stream_view()->highest_note();
1290 uint8_t highest_note_difference = 0;
1292 // find highest and lowest notes first
1293 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1294 uint8_t pitch = (*i)->note()->note();
1295 lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
1296 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
1300 cerr << "dnote: " << (int) dnote << endl;
1301 cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
1302 << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
1303 cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
1304 << int(highest_note_in_selection) << endl;
1305 cerr << "selection size: " << _selection.size() << endl;
1306 cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
1309 // Make sure the note pitch does not exceed the MIDI standard range
1310 if (dnote <= 127 && (highest_note_in_selection + dnote > 127)) {
1311 highest_note_difference = highest_note_in_selection - 127;
1314 start_delta_command(_("move notes"));
1316 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
1317 Selection::iterator next = i;
1320 const boost::shared_ptr<NoteType> copy(new NoteType(*(*i)->note().get()));
1322 nframes64_t start_frames = beats_to_frames((*i)->note()->time());
1324 start_frames += snap_frame_to_frame(trackview.editor().pixel_to_frame(dt));
1326 start_frames -= snap_frame_to_frame(trackview.editor().pixel_to_frame(-dt));
1329 copy->set_time(frames_to_beats(start_frames));
1331 uint8_t original_pitch = (*i)->note()->note();
1332 uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
1334 // keep notes in standard midi range
1335 clamp_to_0_127(new_pitch);
1337 // keep original pitch if note is dragged outside valid midi range
1338 if ((original_pitch != 0 && new_pitch == 0)
1339 || (original_pitch != 127 && new_pitch == 127)) {
1340 new_pitch = original_pitch;
1343 lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
1344 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
1346 copy->set_note(new_pitch);
1348 command_remove_note(*i);
1349 command_add_note(copy, (*i)->selected());
1356 // care about notes being moved beyond the upper/lower bounds on the canvas
1357 if (lowest_note_in_selection < midi_stream_view()->lowest_note() ||
1358 highest_note_in_selection > midi_stream_view()->highest_note()) {
1359 midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
1364 MidiRegionView::snap_pixel_to_frame(double x)
1366 PublicEditor& editor = trackview.editor();
1367 // x is region relative, convert it to global absolute frames
1368 nframes64_t frame = editor.pixel_to_frame(x) + _region->position();
1369 editor.snap_to(frame);
1370 return frame - _region->position(); // convert back to region relative
1374 MidiRegionView::snap_frame_to_frame(nframes64_t x)
1376 PublicEditor& editor = trackview.editor();
1377 // x is region relative, convert it to global absolute frames
1378 nframes64_t frame = x + _region->position();
1379 editor.snap_to(frame);
1380 return frame - _region->position(); // convert back to region relative
1384 MidiRegionView::snap_to_pixel(double x)
1386 return (double) trackview.editor().frame_to_pixel(snap_pixel_to_frame(x));
1390 MidiRegionView::get_position_pixels()
1392 nframes64_t region_frame = get_position();
1393 return trackview.editor().frame_to_pixel(region_frame);
1397 MidiRegionView::beats_to_frames(double beats) const
1399 return _time_converter.to(beats);
1403 MidiRegionView::frames_to_beats(nframes64_t frames) const
1405 return _time_converter.from(frames);
1409 MidiRegionView::begin_resizing(CanvasNote::NoteEnd note_end)
1411 _resize_data.clear();
1413 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1414 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
1416 // only insert CanvasNotes into the map
1418 NoteResizeData *resize_data = new NoteResizeData();
1419 resize_data->canvas_note = note;
1421 // create a new SimpleRect from the note which will be the resize preview
1422 SimpleRect *resize_rect = new SimpleRect(
1423 *group, note->x1(), note->y1(), note->x2(), note->y2());
1425 // calculate the colors: get the color settings
1426 uint32_t fill_color = UINT_RGBA_CHANGE_A(
1427 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(),
1430 // make the resize preview notes more transparent and bright
1431 fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
1433 // calculate color based on note velocity
1434 resize_rect->property_fill_color_rgba() = UINT_INTERPOLATE(
1435 CanvasNoteEvent::meter_style_fill_color(note->note()->velocity()),
1439 resize_rect->property_outline_color_rgba() = CanvasNoteEvent::calculate_outline(
1440 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get());
1442 resize_data->resize_rect = resize_rect;
1444 if (note_end == CanvasNote::NOTE_ON) {
1445 resize_data->current_x = note->x1();
1446 } else { // NOTE_OFF
1447 resize_data->current_x = note->x2();
1450 _resize_data.push_back(resize_data);
1456 MidiRegionView::update_resizing(CanvasNote::NoteEnd note_end, double x, bool relative)
1458 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1459 SimpleRect* resize_rect = (*i)->resize_rect;
1460 CanvasNote* canvas_note = (*i)->canvas_note;
1462 const double region_start = get_position_pixels();
1465 (*i)->current_x = (*i)->current_x + x;
1467 // x is in track relative, transform it to region relative
1468 (*i)->current_x = x - region_start;
1471 double current_x = (*i)->current_x;
1473 if (note_end == CanvasNote::NOTE_ON) {
1474 resize_rect->property_x1() = snap_to_pixel(current_x);
1475 resize_rect->property_x2() = canvas_note->x2();
1477 resize_rect->property_x2() = snap_to_pixel(current_x);
1478 resize_rect->property_x1() = canvas_note->x1();
1484 MidiRegionView::commit_resizing(CanvasNote::NoteEnd note_end, double event_x, bool relative)
1486 start_delta_command(_("resize notes"));
1488 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1489 CanvasNote* canvas_note = (*i)->canvas_note;
1490 SimpleRect* resize_rect = (*i)->resize_rect;
1491 double current_x = (*i)->current_x;
1492 const double position = get_position_pixels();
1495 // event_x is in track relative, transform it to region relative
1496 current_x = event_x - position;
1499 // because snapping works on world coordinates we have to transform current_x
1500 // to world coordinates before snapping and transform it back afterwards
1501 nframes64_t current_frame = snap_pixel_to_frame(current_x);
1502 // transform to region start relative
1503 current_frame += _region->start();
1505 const boost::shared_ptr<NoteType> copy(new NoteType(*(canvas_note->note().get())));
1507 // resize beginning of note
1508 if (note_end == CanvasNote::NOTE_ON && current_frame < copy->end_time()) {
1509 command_remove_note(canvas_note);
1510 copy->on_event().time() = current_frame;
1511 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1513 // resize end of note
1514 if (note_end == CanvasNote::NOTE_OFF && current_frame > copy->time()) {
1515 command_remove_note(canvas_note);
1516 copy->off_event().time() = current_frame;
1517 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1524 _resize_data.clear();
1529 MidiRegionView::change_note_velocity(CanvasNoteEvent* event, int8_t velocity, bool relative)
1531 const boost::shared_ptr<NoteType> copy(new NoteType(*(event->note().get())));
1534 uint8_t new_velocity = copy->velocity() + velocity;
1535 clamp_to_0_127(new_velocity);
1536 copy->set_velocity(new_velocity);
1538 copy->set_velocity(velocity);
1541 command_remove_note(event);
1542 command_add_note(copy, event->selected(), true);
1546 MidiRegionView::change_velocity(CanvasNoteEvent* ev, int8_t velocity, bool relative)
1548 start_delta_command(_("change velocity"));
1550 change_note_velocity(ev, velocity, relative);
1552 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1553 Selection::iterator next = i;
1555 if ( !(*((*i)->note()) == *(ev->note())) ) {
1556 change_note_velocity(*i, velocity, relative);
1565 MidiRegionView::change_channel(uint8_t channel)
1567 start_delta_command(_("change channel"));
1568 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1569 Selection::iterator next = i;
1572 CanvasNoteEvent* event = *i;
1573 const boost::shared_ptr<NoteType> copy(new NoteType(*(event->note().get())));
1575 copy->set_channel(channel);
1577 command_remove_note(event);
1578 command_add_note(copy, event->selected());
1588 MidiRegionView::note_entered(ArdourCanvas::CanvasNoteEvent* ev)
1590 if (_mouse_state == SelectTouchDragging) {
1591 note_selected(ev, true);
1597 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
1599 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
1601 display_model(msrc->model());
1605 MidiRegionView::set_frame_color()
1608 if (_selected && should_show_selection) {
1609 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
1611 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
1617 MidiRegionView::midi_channel_mode_changed(ChannelMode mode, uint16_t mask)
1621 case FilterChannels:
1622 _force_channel = -1;
1625 _force_channel = mask;
1626 mask = 0xFFFF; // Show all notes as active (below)
1629 // Update notes for selection
1630 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1631 (*i)->on_channel_selection_change(mask);
1634 _last_channel_selection = mask;
1638 MidiRegionView::midi_patch_settings_changed(std::string model, std::string custom_device_mode)
1640 _model_name = model;
1641 _custom_device_mode = custom_device_mode;