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);
172 //reset_width_dependent_items ((double) _region->length() / samples_per_unit);
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();
482 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
485 if (_enable_display) {
492 MidiRegionView::start_delta_command(string name)
494 if (!_delta_command) {
495 _delta_command = _model->new_delta_command(name);
500 MidiRegionView::command_add_note(const boost::shared_ptr<NoteType> note, bool selected)
502 if (_delta_command) {
503 _delta_command->add(note);
506 _marked_for_selection.insert(note);
511 MidiRegionView::command_remove_note(ArdourCanvas::CanvasNoteEvent* ev)
513 if (_delta_command && ev->note()) {
514 _delta_command->remove(ev->note());
519 MidiRegionView::apply_command()
521 if (!_delta_command) {
525 // Mark all selected notes for selection when model reloads
526 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
527 _marked_for_selection.insert((*i)->note());
530 _model->apply_command(trackview.session(), _delta_command);
531 _delta_command = NULL;
532 midi_view()->midi_track()->diskstream()->playlist_modified();
534 _marked_for_selection.clear();
539 MidiRegionView::abort_command()
541 delete _delta_command;
542 _delta_command = NULL;
548 MidiRegionView::redisplay_model()
550 // Don't redisplay the model if we're currently recording and displaying that
559 MidiModel::Notes notes = _model->notes();
561 cerr << _model->midi_source()->name() << " : redisplaying " << notes.size() << endl;
562 for (MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
563 cerr << "NOTE time: " << (*i)->time()
564 << " pitch: " << int((*i)->note())
565 << " length: " << (*i)->length()
566 << " end-time: " << (*i)->end_time()
567 << " velocity: " << int((*i)->velocity())
572 for (size_t i = 0; i < _model->n_notes(); ++i) {
573 add_note(_model->note_at(i));
576 display_program_change_flags();
578 _model->read_unlock();
581 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
586 MidiRegionView::display_program_change_flags()
588 for (Automatable::Controls::iterator control = _model->controls().begin();
589 control != _model->controls().end(); ++control) {
590 if (control->first.type() == MidiPgmChangeAutomation) {
591 Glib::Mutex::Lock list_lock (control->second->list()->lock());
593 uint8_t channel = control->first.channel();
595 for (AutomationList::const_iterator event = control->second->list()->begin();
596 event != control->second->list()->end(); ++event) {
597 double event_time = (*event)->when;
598 double program_number = floor((*event)->value + 0.5);
600 //cerr << " got program change on channel " << int(channel)
601 // << " time: " << event_time << " number: " << program_number << endl;
603 // find bank select msb and lsb for the program change
604 Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
605 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
607 if (msb_control != 0) {
608 msb = uint8_t(floor(msb_control->get_float(true, event_time) + 0.5));
611 Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
612 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
614 if (lsb_control != 0) {
615 lsb = uint8_t(floor(lsb_control->get_float(true, event_time) + 0.5));
618 //cerr << " got msb " << int(msb) << " and lsb " << int(lsb)
619 // << " thread_id: " << pthread_self() << endl;
621 MIDI::Name::PatchPrimaryKey patch_key(msb, lsb, program_number);
623 boost::shared_ptr<MIDI::Name::Patch> patch =
624 MIDI::Name::MidiPatchManager::instance().find_patch(
630 ControlEvent program_change(beats_to_frames(event_time),
631 uint8_t(program_number), channel);
634 //cerr << " got patch with name " << patch->name()
635 // << " number " << patch->number() << endl;
636 add_pgm_change(program_change, patch->name());
639 snprintf(buf, 4, "%d", int(program_number));
640 add_pgm_change(program_change, buf);
649 MidiRegionView::~MidiRegionView ()
651 in_destructor = true;
653 RegionViewGoingAway (this); /* EMIT_SIGNAL */
662 delete _delta_command;
667 MidiRegionView::region_resized (Change what_changed)
669 RegionView::region_resized(what_changed);
671 if (what_changed & ARDOUR::PositionChanged) {
672 if (_enable_display) {
679 MidiRegionView::reset_width_dependent_items (double pixel_width)
681 RegionView::reset_width_dependent_items(pixel_width);
682 assert(_pixel_width == pixel_width);
684 if (_enable_display) {
690 MidiRegionView::set_height (double height)
692 static const double FUDGE = 2.0;
693 const double old_height = _height;
694 RegionView::set_height(height);
695 _height = height - FUDGE;
697 apply_note_range(midi_stream_view()->lowest_note(),
698 midi_stream_view()->highest_note(),
699 height != old_height + FUDGE);
702 name_text->raise_to_top();
707 /** Apply the current note range from the stream view
708 * by repositioning/hiding notes as necessary
711 MidiRegionView::apply_note_range (uint8_t min, uint8_t max, bool force)
713 if (_enable_display) {
714 if (!force && _current_range_min == min && _current_range_max == max) {
718 _current_range_min = min;
719 _current_range_max = max;
721 for (Events::const_iterator i = _events.begin(); i != _events.end(); ++i) {
722 CanvasNoteEvent* event = *i;
723 Item* item = dynamic_cast<Item*>(event);
725 if (event && event->note()) {
726 if (event->note()->note() < _current_range_min
727 || event->note()->note() > _current_range_max) {
728 if (canvas_item_visible(item)) {
732 if (!canvas_item_visible(item)) {
736 event->hide_velocity();
737 if (CanvasNote* note = dynamic_cast<CanvasNote*>(event)) {
738 const double y1 = midi_stream_view()->note_to_y(event->note()->note());
739 const double y2 = y1 + floor(midi_stream_view()->note_height());
741 note->property_y1() = y1;
742 note->property_y2() = y2;
743 } else if (CanvasHit* hit = dynamic_cast<CanvasHit*>(event)) {
744 double x = trackview.editor().frame_to_pixel(
745 beats_to_frames(event->note()->time()) - _region->start());
746 const double diamond_size = midi_stream_view()->note_height() / 2.0;
747 double y = midi_stream_view()->note_to_y(event->note()->note())
748 + ((diamond_size-2.0) / 4.0);
750 hit->set_height(diamond_size);
751 hit->move(x-hit->x1(), y-hit->y1());
754 if (event->selected()) {
755 event->show_velocity();
764 MidiRegionView::add_ghost (TimeAxisView& tv)
766 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
770 double unit_position = _region->position () / samples_per_unit;
771 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
772 MidiGhostRegion* ghost;
774 if (mtv && mtv->midi_view()) {
775 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group.
776 this is because it's nice to have midi notes on top of the note lines and
777 audio waveforms under it.
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();
824 /** Resolve an active MIDI note (while recording).
827 MidiRegionView::resolve_note(uint8_t note, double end_time)
829 if (midi_view()->note_mode() != Sustained) {
833 if (_active_notes && _active_notes[note]) {
834 _active_notes[note]->property_x2() = trackview.editor().frame_to_pixel((nframes64_t)end_time);
835 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
836 _active_notes[note] = NULL;
841 /** Extend active notes to rightmost edge of region (if length is changed)
844 MidiRegionView::extend_active_notes()
846 if (!_active_notes) {
850 for (unsigned i=0; i < 128; ++i) {
851 if (_active_notes[i]) {
852 _active_notes[i]->property_x2() = trackview.editor().frame_to_pixel(_region->length());
858 MidiRegionView::play_midi_note(boost::shared_ptr<NoteType> note)
860 if (!trackview.editor().sound_notes()) {
864 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
867 route_ui->midi_track()->write_immediate_event(
868 note->on_event().size(), note->on_event().buffer());
870 const double note_length_beats = (note->off_event().time() - note->on_event().time());
871 nframes_t note_length_ms = beats_to_frames(note_length_beats)
872 * (1000 / (double)route_ui->session().nominal_frame_rate());
873 Glib::signal_timeout().connect(bind(mem_fun(this, &MidiRegionView::play_midi_note_off), note),
874 note_length_ms, G_PRIORITY_DEFAULT);
878 MidiRegionView::play_midi_note_off(boost::shared_ptr<NoteType> note)
880 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
883 route_ui->midi_track()->write_immediate_event(
884 note->off_event().size(), note->off_event().buffer());
890 MidiRegionView::note_in_visible_range(const boost::shared_ptr<NoteType> note) const
892 const nframes64_t note_start_frames = beats_to_frames(note->time());
893 bool outside = (note_start_frames - _region->start() >= _region->length())
894 || (note_start_frames < _region->start())
895 || (note->note() < midi_stream_view()->lowest_note())
896 || (note->note() > midi_stream_view()->highest_note());
900 /** Add a MIDI note to the view (with length).
902 * If in sustained mode, notes with length 0 will be considered active
903 * notes, and resolve_note should be called when the corresponding note off
904 * event arrives, to properly display the note.
907 MidiRegionView::add_note(const boost::shared_ptr<NoteType> note)
909 assert(note->time() >= 0);
910 assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
912 const nframes64_t note_start_frames = beats_to_frames(note->time());
913 const nframes64_t note_end_frames = beats_to_frames(note->end_time());
915 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
917 CanvasNoteEvent* event = 0;
919 const double x = trackview.editor().frame_to_pixel(note_start_frames - _region->start());
921 if (midi_view()->note_mode() == Sustained) {
922 const double y1 = midi_stream_view()->note_to_y(note->note());
923 const double note_endpixel =
924 trackview.editor().frame_to_pixel(note_end_frames - _region->start());
926 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
927 ev_rect->property_x1() = x;
928 ev_rect->property_y1() = y1;
929 if (note->length() > 0) {
930 ev_rect->property_x2() = note_endpixel;
932 ev_rect->property_x2() = trackview.editor().frame_to_pixel(_region->length());
934 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
936 if (note->length() == 0) {
938 assert(note->note() < 128);
939 // If this note is already active there's a stuck note,
940 // finish the old note rectangle
941 if (_active_notes[note->note()]) {
942 CanvasNote* const old_rect = _active_notes[note->note()];
943 boost::shared_ptr<NoteType> old_note = old_rect->note();
944 cerr << "MidiModel: WARNING: Note has length 0: chan " << old_note->channel()
945 << "note " << (int)old_note->note() << " @ " << old_note->time() << endl;
946 /* FIXME: How large to make it? Make it a diamond? */
947 old_rect->property_x2() = old_rect->property_x1() + 2.0;
948 old_rect->property_outline_what() = (guint32) 0xF;
950 _active_notes[note->note()] = ev_rect;
952 /* outline all but right edge */
953 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
955 /* outline all edges */
956 ev_rect->property_outline_what() = (guint32) 0xF;
962 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
963 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
964 gr->add_note(ev_rect);
968 } else if (midi_view()->note_mode() == Percussive) {
969 const double diamond_size = midi_stream_view()->note_height() / 2.0;
970 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
972 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size, note);
973 ev_diamond->move(x, y);
980 if (_marked_for_selection.find(note) != _marked_for_selection.end()) {
981 note_selected(event, true);
983 event->on_channel_selection_change(_last_channel_selection);
984 _events.push_back(event);
985 if (note_in_visible_range(note)) {
994 MidiRegionView::add_pgm_change(ControlEvent& program, string displaytext)
996 assert(program.time >= 0);
998 // dont display program changes beyond the region bounds
999 if (program.time - _region->start() >= _region->length() || program.time < _region->start())
1002 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
1003 const double x = trackview.editor().frame_to_pixel((nframes64_t)program.time - _region->start());
1005 double height = midi_stream_view()->contents_height();
1007 boost::shared_ptr<CanvasProgramChange> pgm_change = boost::shared_ptr<CanvasProgramChange>(
1008 new CanvasProgramChange(
1016 _custom_device_mode,
1021 _pgm_changes.push_back(pgm_change);
1025 MidiRegionView::get_patch_key_at(double time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key)
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(ControlEvent& 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 ControlEvent 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 ControlEvent 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,
1123 ControlEvent program_change_event(program.event_time(), program.program(), program.channel());
1125 alter_program_change(program_change_event, patch->patch_primary_key());
1130 MidiRegionView::delete_selection()
1132 assert(_delta_command);
1134 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1135 if ((*i)->selected()) {
1136 _delta_command->remove((*i)->note());
1144 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev)
1146 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1147 if ((*i)->selected() && (*i) != ev) {
1148 (*i)->selected(false);
1156 MidiRegionView::unique_select(ArdourCanvas::CanvasNoteEvent* ev)
1158 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1160 (*i)->selected(false);
1165 _selection.insert(ev);
1167 if ( ! ev->selected()) {
1173 MidiRegionView::note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
1176 clear_selection_except(ev);
1179 if (_selection.insert(ev).second) {
1180 play_midi_note(ev->note());
1183 if ( ! ev->selected()) {
1190 MidiRegionView::note_deselected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
1193 clear_selection_except(ev);
1196 _selection.erase(ev);
1198 if (ev->selected()) {
1199 ev->selected(false);
1205 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
1207 const double last_y = std::min(y1, y2);
1208 const double y = std::max(y1, y2);
1210 // TODO: Make this faster by storing the last updated selection rect, and only
1211 // adjusting things that are in the area that appears/disappeared.
1212 // We probably need a tree to be able to find events in O(log(n)) time.
1215 double last_x1 = 0.0;
1219 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1221 // Events should always be sorted by increasing x1() here
1222 assert((*i)->x1() >= last_x1);
1223 last_x1 = (*i)->x1();
1226 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1227 if (!(*i)->selected()) {
1228 (*i)->selected(true);
1229 _selection.insert(*i);
1230 play_midi_note((*i)->note());
1232 // Not inside rectangle
1233 } else if ((*i)->selected()) {
1234 (*i)->selected(false);
1235 _selection.erase(*i);
1239 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1241 // Events should always be sorted by increasing x1() here
1242 assert((*i)->x1() >= last_x1);
1243 last_x1 = (*i)->x1();
1246 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1247 if (!(*i)->selected()) {
1248 (*i)->selected(true);
1249 _selection.insert(*i);
1250 play_midi_note((*i)->note());
1252 // Not inside rectangle
1253 } else if ((*i)->selected()) {
1254 (*i)->selected(false);
1255 _selection.erase(*i);
1263 MidiRegionView::move_selection(double dx, double dy)
1265 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
1266 (*i)->move_event(dx, dy);
1271 MidiRegionView::note_dropped(CanvasNoteEvent* ev, double dt, uint8_t dnote)
1273 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
1274 if (_selection.find(ev) != _selection.end()) {
1275 uint8_t lowest_note_in_selection = midi_stream_view()->lowest_note();
1276 uint8_t highest_note_in_selection = midi_stream_view()->highest_note();
1277 uint8_t highest_note_difference = 0;
1279 // find highest and lowest notes first
1280 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1281 uint8_t pitch = (*i)->note()->note();
1282 lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
1283 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
1287 cerr << "dnote: " << (int) dnote << endl;
1288 cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
1289 << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
1290 cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
1291 << int(highest_note_in_selection) << endl;
1292 cerr << "selection size: " << _selection.size() << endl;
1293 cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
1296 // Make sure the note pitch does not exceed the MIDI standard range
1297 if (dnote <= 127 && (highest_note_in_selection + dnote > 127)) {
1298 highest_note_difference = highest_note_in_selection - 127;
1301 start_delta_command(_("move notes"));
1303 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
1304 Selection::iterator next = i;
1307 const boost::shared_ptr<NoteType> copy(new NoteType(*(*i)->note().get()));
1309 // we need to snap here again in nframes64_t in order to be sample accurate
1310 double start_frames = snap_to_frame(beats_to_frames((*i)->note()->time()) + dt);
1312 // keep notes inside region if dragged beyond left region bound
1313 if (start_frames < _region->start()) {
1314 start_frames = _region->start();
1317 copy->set_time(frames_to_beats(start_frames));
1319 uint8_t original_pitch = (*i)->note()->note();
1320 uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
1322 // keep notes in standard midi range
1323 clamp_to_0_127(new_pitch);
1325 // keep original pitch if note is dragged outside valid midi range
1326 if ((original_pitch != 0 && new_pitch == 0)
1327 || (original_pitch != 127 && new_pitch == 127)) {
1328 new_pitch = original_pitch;
1331 lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
1332 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
1334 copy->set_note(new_pitch);
1336 command_remove_note(*i);
1337 command_add_note(copy, (*i)->selected());
1344 // care about notes being moved beyond the upper/lower bounds on the canvas
1345 if (lowest_note_in_selection < midi_stream_view()->lowest_note() ||
1346 highest_note_in_selection > midi_stream_view()->highest_note()) {
1347 midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
1353 MidiRegionView::snap_to_frame(double x)
1355 PublicEditor &editor = trackview.editor();
1356 // x is region relative, convert it to global absolute frames
1357 nframes64_t frame = editor.pixel_to_frame(x) + _region->position();
1358 editor.snap_to(frame);
1359 return frame - _region->position(); // convert back to region relative
1363 MidiRegionView::snap_to_frame(nframes64_t x)
1365 PublicEditor &editor = trackview.editor();
1366 // x is region relative
1367 // convert x to global frame
1368 nframes64_t frame = x + _region->position();
1369 editor.snap_to(frame);
1370 // convert event_frame back to local coordinates relative to position
1371 frame -= _region->position();
1376 MidiRegionView::snap_to_pixel(double x)
1378 return (double) trackview.editor().frame_to_pixel(snap_to_frame(x));
1382 MidiRegionView::get_position_pixels()
1384 nframes64_t region_frame = get_position();
1385 return trackview.editor().frame_to_pixel(region_frame);
1389 MidiRegionView::beats_to_frames(double beats) const
1391 return midi_region()->midi_source()->time_converter().to(beats);
1395 MidiRegionView::frames_to_beats(nframes64_t frames) const
1397 return midi_region()->midi_source()->time_converter().from(frames);
1401 MidiRegionView::begin_resizing(CanvasNote::NoteEnd note_end)
1403 _resize_data.clear();
1405 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1406 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
1408 // only insert CanvasNotes into the map
1410 NoteResizeData *resize_data = new NoteResizeData();
1411 resize_data->canvas_note = note;
1413 // create a new SimpleRect from the note which will be the resize preview
1414 SimpleRect *resize_rect = new SimpleRect(
1415 *group, note->x1(), note->y1(), note->x2(), note->y2());
1417 // calculate the colors: get the color settings
1418 uint32_t fill_color = UINT_RGBA_CHANGE_A(
1419 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(),
1422 // make the resize preview notes more transparent and bright
1423 fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
1425 // calculate color based on note velocity
1426 resize_rect->property_fill_color_rgba() = UINT_INTERPOLATE(
1427 CanvasNoteEvent::meter_style_fill_color(note->note()->velocity()),
1431 resize_rect->property_outline_color_rgba() = CanvasNoteEvent::calculate_outline(
1432 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get());
1434 resize_data->resize_rect = resize_rect;
1436 if (note_end == CanvasNote::NOTE_ON) {
1437 resize_data->current_x = note->x1();
1438 } else { // NOTE_OFF
1439 resize_data->current_x = note->x2();
1442 _resize_data.push_back(resize_data);
1448 MidiRegionView::update_resizing(CanvasNote::NoteEnd note_end, double x, bool relative)
1450 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1451 SimpleRect* resize_rect = (*i)->resize_rect;
1452 CanvasNote* canvas_note = (*i)->canvas_note;
1454 const double region_start = get_position_pixels();
1457 (*i)->current_x = (*i)->current_x + x;
1459 // x is in track relative, transform it to region relative
1460 (*i)->current_x = x - region_start;
1463 double current_x = (*i)->current_x;
1465 if (note_end == CanvasNote::NOTE_ON) {
1466 resize_rect->property_x1() = snap_to_pixel(current_x);
1467 resize_rect->property_x2() = canvas_note->x2();
1469 resize_rect->property_x2() = snap_to_pixel(current_x);
1470 resize_rect->property_x1() = canvas_note->x1();
1476 MidiRegionView::commit_resizing(CanvasNote::NoteEnd note_end, double event_x, bool relative)
1478 start_delta_command(_("resize notes"));
1480 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1481 CanvasNote* canvas_note = (*i)->canvas_note;
1482 SimpleRect* resize_rect = (*i)->resize_rect;
1483 double current_x = (*i)->current_x;
1484 const double position = get_position_pixels();
1487 // event_x is in track relative, transform it to region relative
1488 current_x = event_x - position;
1491 // because snapping works on world coordinates we have to transform current_x
1492 // to world coordinates before snapping and transform it back afterwards
1493 nframes64_t current_frame = snap_to_frame(current_x);
1494 // transform to region start relative
1495 current_frame += _region->start();
1497 const boost::shared_ptr<NoteType> copy(new NoteType(*(canvas_note->note().get())));
1499 // resize beginning of note
1500 if (note_end == CanvasNote::NOTE_ON && current_frame < copy->end_time()) {
1501 command_remove_note(canvas_note);
1502 copy->on_event().time() = current_frame;
1503 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1505 // resize end of note
1506 if (note_end == CanvasNote::NOTE_OFF && current_frame > copy->time()) {
1507 command_remove_note(canvas_note);
1508 copy->off_event().time() = current_frame;
1509 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1516 _resize_data.clear();
1521 MidiRegionView::change_note_velocity(CanvasNoteEvent* event, int8_t velocity, bool relative)
1523 const boost::shared_ptr<NoteType> copy(new NoteType(*(event->note().get())));
1526 uint8_t new_velocity = copy->velocity() + velocity;
1527 clamp_to_0_127(new_velocity);
1528 copy->set_velocity(new_velocity);
1530 copy->set_velocity(velocity);
1533 command_remove_note(event);
1534 command_add_note(copy, event->selected());
1538 MidiRegionView::change_velocity(CanvasNoteEvent* ev, int8_t velocity, bool relative)
1540 start_delta_command(_("change velocity"));
1542 change_note_velocity(ev, velocity, relative);
1544 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1545 Selection::iterator next = i;
1547 if ( !(*((*i)->note()) == *(ev->note())) ) {
1548 change_note_velocity(*i, velocity, relative);
1557 MidiRegionView::change_channel(uint8_t channel)
1559 start_delta_command(_("change channel"));
1560 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1561 Selection::iterator next = i;
1564 CanvasNoteEvent* event = *i;
1565 const boost::shared_ptr<NoteType> copy(new NoteType(*(event->note().get())));
1567 copy->set_channel(channel);
1569 command_remove_note(event);
1570 command_add_note(copy, event->selected());
1580 MidiRegionView::note_entered(ArdourCanvas::CanvasNoteEvent* ev)
1582 if (ev->note() && _mouse_state == EraseTouchDragging) {
1583 start_delta_command(_("note entered"));
1585 _delta_command->remove(ev->note());
1586 } else if (_mouse_state == SelectTouchDragging) {
1587 note_selected(ev, true);
1593 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
1595 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
1597 display_model(msrc->model());
1601 MidiRegionView::set_frame_color()
1604 if (_selected && should_show_selection) {
1605 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
1607 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
1613 MidiRegionView::midi_channel_mode_changed(ChannelMode mode, uint16_t mask)
1617 case FilterChannels:
1618 _force_channel = -1;
1621 _force_channel = mask;
1622 mask = 0xFFFF; // Show all notes as active (below)
1625 // Update notes for selection
1626 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1627 (*i)->on_channel_selection_change(mask);
1630 _last_channel_selection = mask;
1634 MidiRegionView::midi_patch_settings_changed(std::string model, std::string custom_device_mode)
1636 _model_name = model;
1637 _custom_device_mode = custom_device_mode;