2 Copyright (C) 2001-2007 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <gtkmm2ext/gtk_ui.h>
28 #include <sigc++/signal.h>
30 #include <ardour/playlist.h>
31 #include <ardour/tempo.h>
32 #include <ardour/midi_region.h>
33 #include <ardour/midi_source.h>
34 #include <ardour/midi_diskstream.h>
35 #include <ardour/midi_model.h>
36 #include <ardour/midi_patch_manager.h>
38 #include <evoral/Parameter.hpp>
39 #include <evoral/Control.hpp>
41 #include "streamview.h"
42 #include "midi_region_view.h"
43 #include "midi_streamview.h"
44 #include "midi_time_axis.h"
45 #include "simpleline.h"
46 #include "canvas-hit.h"
47 #include "canvas-note.h"
48 #include "canvas-program-change.h"
49 #include "public_editor.h"
50 #include "ghostregion.h"
51 #include "midi_time_axis.h"
52 #include "automation_time_axis.h"
53 #include "automation_region_view.h"
55 #include "midi_util.h"
56 #include "gui_thread.h"
62 using namespace ARDOUR;
64 using namespace Editing;
65 using namespace ArdourCanvas;
67 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color)
68 : RegionView (parent, tv, r, spu, basic_color)
70 , _last_channel_selection(0xFFFF)
71 , _default_note_length(0.0)
72 , _current_range_min(0)
73 , _current_range_max(0)
74 , _model_name(string())
75 , _custom_device_mode(string())
77 , _note_group(new ArdourCanvas::Group(*parent))
78 , _delta_command(NULL)
82 _note_group->raise_to_top();
85 MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<MidiRegion> r, double spu, Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
86 : RegionView (parent, tv, r, spu, basic_color, false, visibility)
88 , _last_channel_selection(0xFFFF)
89 , _default_note_length(0.0)
90 , _model_name(string())
91 , _custom_device_mode(string())
93 , _note_group(new ArdourCanvas::Group(*parent))
94 , _delta_command(NULL)
99 _note_group->raise_to_top();
103 MidiRegionView::MidiRegionView (const MidiRegionView& other)
106 , _last_channel_selection(0xFFFF)
107 , _default_note_length(0.0)
108 , _model_name(string())
109 , _custom_device_mode(string())
111 , _note_group(new ArdourCanvas::Group(*get_canvas_group()))
112 , _delta_command(NULL)
119 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
120 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
125 MidiRegionView::MidiRegionView (const MidiRegionView& other, boost::shared_ptr<MidiRegion> other_region)
126 : RegionView (other, boost::shared_ptr<Region> (other_region))
128 , _last_channel_selection(0xFFFF)
129 , _default_note_length(0.0)
130 , _model_name(string())
131 , _custom_device_mode(string())
133 , _note_group(new ArdourCanvas::Group(*get_canvas_group()))
134 , _delta_command(NULL)
141 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
142 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
148 MidiRegionView::init (Gdk::Color& basic_color, bool wfd)
151 midi_region()->midi_source(0)->load_model();
154 const Meter& m = trackview.session().tempo_map().meter_at(_region->position());
155 const Tempo& t = trackview.session().tempo_map().tempo_at(_region->position());
156 _default_note_length = m.frames_per_bar(t, trackview.session().frame_rate())
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);
174 //reset_width_dependent_items ((double) _region->length() / samples_per_unit);
178 _enable_display = true;
183 _model->ContentsChanged.connect(sigc::mem_fun(this, &MidiRegionView::redisplay_model));
186 group->raise_to_top();
187 group->signal_event().connect (mem_fun (this, &MidiRegionView::canvas_event), false);
189 midi_view()->signal_channel_mode_changed().connect(
190 mem_fun(this, &MidiRegionView::midi_channel_mode_changed));
192 midi_view()->signal_midi_patch_settings_changed().connect(
193 mem_fun(this, &MidiRegionView::midi_patch_settings_changed));
197 MidiRegionView::canvas_event(GdkEvent* ev)
199 static bool delete_mod = false;
200 static Editing::MidiEditMode original_mode;
202 static double drag_start_x, drag_start_y;
203 static double last_x, last_y;
204 double event_x, event_y;
205 nframes64_t event_frame = 0;
207 static ArdourCanvas::SimpleRect* drag_rect = NULL;
209 if (trackview.editor().current_mouse_mode() != MouseNote)
216 if (ev->key.keyval == GDK_Delete && !delete_mod) {
218 original_mode = trackview.editor().current_midi_edit_mode();
219 trackview.editor().set_midi_edit_mode(MidiEditErase);
220 start_delta_command(_("erase notes"));
221 _mouse_state = EraseTouchDragging;
223 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
224 _mouse_state = SelectTouchDragging;
226 } else if (ev->key.keyval == GDK_Escape) {
232 case GDK_KEY_RELEASE:
233 if (ev->key.keyval == GDK_Delete) {
234 if (_mouse_state == EraseTouchDragging) {
239 trackview.editor().set_midi_edit_mode(original_mode);
244 } else if (ev->key.keyval == GDK_Shift_L || ev->key.keyval == GDK_Control_L) {
250 case GDK_BUTTON_PRESS:
251 if (_mouse_state != SelectTouchDragging &&
252 _mouse_state != EraseTouchDragging &&
253 ev->button.button == 1) {
254 _pressed_button = ev->button.button;
255 _mouse_state = Pressed;
258 _pressed_button = ev->button.button;
261 case GDK_2BUTTON_PRESS:
264 case GDK_ENTER_NOTIFY:
265 /* FIXME: do this on switch to note tool, too, if the pointer is already in */
266 Keyboard::magic_widget_grab_focus();
270 case GDK_MOTION_NOTIFY:
271 event_x = ev->motion.x;
272 event_y = ev->motion.y;
273 group->w2i(event_x, event_y);
275 // convert event_x to global frame
276 event_frame = trackview.editor().pixel_to_frame(event_x) + _region->position();
277 trackview.editor().snap_to(event_frame);
278 // convert event_frame back to local coordinates relative to position
279 event_frame -= _region->position();
281 switch (_mouse_state) {
282 case Pressed: // Drag start
285 if (_pressed_button == 1 && trackview.editor().current_midi_edit_mode() == MidiEditSelect) {
286 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
287 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
290 drag_start_x = event_x;
291 drag_start_y = event_y;
293 drag_rect = new ArdourCanvas::SimpleRect(*group);
294 drag_rect->property_x1() = event_x;
295 drag_rect->property_y1() = event_y;
296 drag_rect->property_x2() = event_x;
297 drag_rect->property_y2() = event_y;
298 drag_rect->property_outline_what() = 0xFF;
299 drag_rect->property_outline_color_rgba()
300 = ARDOUR_UI::config()->canvasvar_MidiSelectRectOutline.get();
301 drag_rect->property_fill_color_rgba()
302 = ARDOUR_UI::config()->canvasvar_MidiSelectRectFill.get();
304 _mouse_state = SelectRectDragging;
307 // Add note drag start
308 } else if (trackview.editor().current_midi_edit_mode() == MidiEditPencil) {
309 group->grab(GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
310 Gdk::Cursor(Gdk::FLEUR), ev->motion.time);
313 drag_start_x = event_x;
314 drag_start_y = event_y;
316 drag_rect = new ArdourCanvas::SimpleRect(*group);
317 drag_rect->property_x1() = trackview.editor().frame_to_pixel(event_frame);
319 drag_rect->property_y1() = midi_stream_view()->note_to_y(midi_stream_view()->y_to_note(event_y));
320 drag_rect->property_x2() = event_x;
321 drag_rect->property_y2() = drag_rect->property_y1() + floor(midi_stream_view()->note_height());
322 drag_rect->property_outline_what() = 0xFF;
323 drag_rect->property_outline_color_rgba() = 0xFFFFFF99;
325 drag_rect->property_fill_color_rgba() = 0xFFFFFF66;
327 _mouse_state = AddDragging;
333 case SelectRectDragging: // Select drag motion
334 case AddDragging: // Add note drag motion
335 if (ev->motion.is_hint) {
338 GdkModifierType state;
339 gdk_window_get_pointer(ev->motion.window, &t_x, &t_y, &state);
344 if (_mouse_state == AddDragging)
345 event_x = trackview.editor().frame_to_pixel(event_frame);
348 if (event_x > drag_start_x)
349 drag_rect->property_x2() = event_x;
351 drag_rect->property_x1() = event_x;
354 if (drag_rect && _mouse_state == SelectRectDragging) {
355 if (event_y > drag_start_y)
356 drag_rect->property_y2() = event_y;
358 drag_rect->property_y1() = event_y;
360 update_drag_selection(drag_start_x, event_x, drag_start_y, event_y);
366 case EraseTouchDragging:
367 case SelectTouchDragging:
375 case GDK_BUTTON_RELEASE:
376 event_x = ev->motion.x;
377 event_y = ev->motion.y;
378 group->w2i(event_x, event_y);
379 group->ungrab(ev->button.time);
380 event_frame = trackview.editor().pixel_to_frame(event_x);
382 if (_pressed_button != 1) {
386 switch (_mouse_state) {
387 case Pressed: // Clicked
388 switch (trackview.editor().current_midi_edit_mode()) {
394 create_note_at(event_x, event_y, _default_note_length);
399 case SelectRectDragging: // Select drag done
404 case AddDragging: // Add drag done
406 if (drag_rect->property_x2() > drag_rect->property_x1() + 2) {
407 const double x = drag_rect->property_x1();
408 const double length = trackview.editor().pixel_to_frame(
409 drag_rect->property_x2() - drag_rect->property_x1());
411 create_note_at(x, drag_rect->property_y1(), length);
426 /** Add a note to the model, and the view, at a canvas (click) coordinate */
428 MidiRegionView::create_note_at(double x, double y, double length)
430 MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
431 MidiStreamView* const view = mtv->midi_view();
433 double note = midi_stream_view()->y_to_note(y);
436 assert(note <= 127.0);
438 nframes64_t new_note_time = trackview.editor().pixel_to_frame (x);
439 assert(new_note_time >= 0);
440 new_note_time += _region->start();
443 const Meter& m = trackview.session().tempo_map().meter_at(new_note_time);
444 const Tempo& t = trackview.session().tempo_map().tempo_at(new_note_time);
445 double length = m.frames_per_bar(t, trackview.session().frame_rate()) / m.beats_per_bar();
448 // we need to snap here again in nframes64_t in order to be sample accurate
449 // since note time is region-absolute but snap_to_frame expects position-relative
450 // time we have to coordinate transform back and forth here.
451 nframes64_t new_note_time_position_relative = new_note_time - _region->start();
452 new_note_time = snap_to_frame(new_note_time_position_relative) + _region->start();
454 // we need to snap the length too to be sample accurate
455 nframes64_t new_note_length = nframes_t(length);
456 new_note_length = snap_to_frame(new_note_time_position_relative + new_note_length) + _region->start()
459 const boost::shared_ptr<Evoral::Note> new_note(new Evoral::Note(
460 0, new_note_time, new_note_length, (uint8_t)note, 0x40));
461 view->update_note_range(new_note->note());
463 MidiModel::DeltaCommand* cmd = _model->new_delta_command("add note");
465 _model->apply_command(trackview.session(), cmd);
470 MidiRegionView::clear_events()
475 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
476 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
481 for (Events::iterator i = _events.begin(); i != _events.end(); ++i)
485 _pgm_changes.clear();
490 MidiRegionView::display_model(boost::shared_ptr<MidiModel> model)
500 MidiRegionView::start_delta_command(string name)
503 _delta_command = _model->new_delta_command(name);
507 MidiRegionView::command_add_note(const boost::shared_ptr<Evoral::Note> note, bool selected)
510 _delta_command->add(note);
513 _marked_for_selection.insert(note);
517 MidiRegionView::command_remove_note(ArdourCanvas::CanvasNoteEvent* ev)
519 if (_delta_command && ev->note()) {
520 _delta_command->remove(ev->note());
525 MidiRegionView::apply_command()
527 if (!_delta_command) {
531 // Mark all selected notes for selection when model reloads
532 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
533 _marked_for_selection.insert((*i)->note());
536 _model->apply_command(trackview.session(), _delta_command);
537 _delta_command = NULL;
538 midi_view()->midi_track()->diskstream()->playlist_modified();
540 _marked_for_selection.clear();
545 MidiRegionView::abort_command()
547 delete _delta_command;
548 _delta_command = NULL;
554 MidiRegionView::redisplay_model()
556 // Don't redisplay the model if we're currently recording and displaying that
566 MidiModel::Notes notes = _model->notes();
568 cerr << endl << _model->midi_source()->name() << " : redisplaying " << notes.size() << " notes:" << endl;
569 for (MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
570 cerr << "NOTE time: " << (*i)->time()
571 << " pitch: " << int((*i)->note())
572 << " length: " << (*i)->length()
573 << " end-time: " << (*i)->end_time()
574 << " velocity: " << int((*i)->velocity())
579 for (size_t i = 0; i < _model->n_notes(); ++i) {
580 add_note(_model->note_at(i));
583 find_and_insert_program_change_flags();
585 // Is this necessary?
586 /*for (Automatable::Controls::const_iterator i = _model->controls().begin();
587 i != _model->controls().end(); ++i) {
591 boost::shared_ptr<AutomationTimeAxisView> at
592 = midi_view()->automation_child(i->second->parameter());
596 Gdk::Color col = midi_stream_view()->get_region_color();
598 boost::shared_ptr<AutomationRegionView> arv;
601 Glib::Mutex::Lock list_lock (i->second->list()->lock());
603 arv = boost::shared_ptr<AutomationRegionView>(
604 new AutomationRegionView(at->canvas_display,
605 *at.get(), _region, i->second->list(),
606 midi_stream_view()->get_samples_per_unit(), col));
609 arv->set_duration(_region->length(), this);
610 arv->init(col, true);
612 _automation_children.insert(std::make_pair(i->second->parameter(), arv));
614 _model->read_unlock();
617 cerr << "MidiRegionView::redisplay_model called without a model" << endmsg;
622 MidiRegionView::find_and_insert_program_change_flags()
624 // Draw program change 'flags'
625 for (Automatable::Controls::iterator control = _model->controls().begin();
626 control != _model->controls().end(); ++control) {
627 if (control->first.type() == MidiPgmChangeAutomation) {
628 Glib::Mutex::Lock list_lock (control->second->list()->lock());
630 uint8_t channel = control->first.channel();
632 for (AutomationList::const_iterator event = control->second->list()->begin();
633 event != control->second->list()->end(); ++event) {
634 double event_time = (*event)->when;
635 double program_number = floor((*event)->value + 0.5);
637 //cerr << " got program change on channel " << int(channel) << " time: " << event_time << " number: " << program_number << endl;
639 // find bank select msb and lsb for the program change
640 Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
641 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
643 if (msb_control != 0) {
644 msb = uint8_t(floor(msb_control->get_float(true, event_time) + 0.5));
647 Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
648 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
650 if (lsb_control != 0) {
651 lsb = uint8_t(floor(lsb_control->get_float(true, event_time) + 0.5));
654 //cerr << " got msb " << int(msb) << " and lsb " << int(lsb) << " thread_id: " << pthread_self() << endl;
656 MIDI::Name::PatchPrimaryKey patch_key(msb, lsb, program_number);
658 boost::shared_ptr<MIDI::Name::Patch> patch =
659 MIDI::Name::MidiPatchManager::instance().find_patch(
666 ControlEvent program_change(nframes_t(event_time), uint8_t(program_number), channel);
669 //cerr << " got patch with name " << patch->name() << " number " << patch->number() << endl;
670 add_pgm_change(program_change, patch->name());
673 snprintf(buf, 4, "%d", int(program_number));
674 add_pgm_change(program_change, buf);
678 } else if (control->first.type() == MidiCCAutomation) {
679 //cerr << " found CC Automation of channel " << int(control->first.channel()) << " and id " << control->first.id() << endl;
685 MidiRegionView::~MidiRegionView ()
687 in_destructor = true;
689 RegionViewGoingAway (this); /* EMIT_SIGNAL */
698 delete _delta_command;
703 MidiRegionView::region_resized (Change what_changed)
705 RegionView::region_resized(what_changed);
707 if (what_changed & ARDOUR::PositionChanged) {
714 MidiRegionView::reset_width_dependent_items (double pixel_width)
716 RegionView::reset_width_dependent_items(pixel_width);
717 assert(_pixel_width == pixel_width);
724 MidiRegionView::set_height (gdouble height)
726 static const double FUDGE = 2;
727 const double old_height = _height;
728 RegionView::set_height(height);
729 _height = height - FUDGE;
731 apply_note_range(midi_stream_view()->lowest_note(),
732 midi_stream_view()->highest_note(),
733 height != old_height + FUDGE);
736 name_text->raise_to_top();
741 /** Apply the current note range from the stream view
742 * by repositioning/hiding notes as necessary
745 MidiRegionView::apply_note_range (uint8_t min, uint8_t max, bool force)
747 if (_enable_display) {
748 if (!force && _current_range_min == min && _current_range_max == max) {
752 _current_range_min = min;
753 _current_range_max = max;
755 for (Events::const_iterator i = _events.begin(); i != _events.end(); ++i) {
756 CanvasNoteEvent* event = *i;
757 Item* item = dynamic_cast<Item*>(event);
759 if (event && event->note()) {
760 if (event->note()->note() < _current_range_min || event->note()->note() > _current_range_max) {
761 if (canvas_item_visible(item)) {
765 if (!canvas_item_visible(item)) {
769 event->hide_velocity();
770 if (CanvasNote* note = dynamic_cast<CanvasNote*>(event)) {
771 const double y1 = midi_stream_view()->note_to_y(event->note()->note());
772 const double y2 = y1 + floor(midi_stream_view()->note_height());
774 note->property_y1() = y1;
775 note->property_y2() = y2;
776 } else if (CanvasHit* hit = dynamic_cast<CanvasHit*>(event)) {
777 double x = trackview.editor().frame_to_pixel((nframes64_t)
778 event->note()->time() - _region->start());
779 const double diamond_size = midi_stream_view()->note_height() / 2.0;
780 double y = midi_stream_view()->note_to_y(event->note()->note())
781 + ((diamond_size-2.0) / 4.0);
783 hit->set_height(diamond_size);
784 hit->move(x-hit->x1(), y-hit->y1());
787 if (event->selected()) {
788 event->show_velocity();
798 MidiRegionView::add_ghost (TimeAxisView& tv)
800 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
804 double unit_position = _region->position () / samples_per_unit;
805 MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
806 MidiGhostRegion* ghost;
808 if (mtv && mtv->midi_view()) {
809 /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group.
810 this is because it's nice to have midi notes on top of the note lines and
811 audio waveforms under it.
813 ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
816 ghost = new MidiGhostRegion (tv, trackview, unit_position);
819 ghost->set_height ();
820 ghost->set_duration (_region->length() / samples_per_unit);
821 ghosts.push_back (ghost);
823 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
824 if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
825 ghost->add_note(note);
829 ghost->GoingAway.connect (mem_fun(*this, &MidiRegionView::remove_ghost));
835 /** Begin tracking note state for successive calls to add_event
838 MidiRegionView::begin_write()
840 assert(!_active_notes);
841 _active_notes = new CanvasNote*[128];
842 for (unsigned i=0; i < 128; ++i) {
843 _active_notes[i] = NULL;
848 /** Destroy note state for add_event
851 MidiRegionView::end_write()
853 delete[] _active_notes;
854 _active_notes = NULL;
855 _marked_for_selection.clear();
859 /** Resolve an active MIDI note (while recording).
862 MidiRegionView::resolve_note(uint8_t note, double end_time)
864 if (midi_view()->note_mode() != Sustained)
867 if (_active_notes && _active_notes[note]) {
868 _active_notes[note]->property_x2() = trackview.editor().frame_to_pixel((nframes64_t)end_time);
869 _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
870 _active_notes[note] = NULL;
875 /** Extend active notes to rightmost edge of region (if length is changed)
878 MidiRegionView::extend_active_notes()
880 if (!_active_notes) {
884 for (unsigned i=0; i < 128; ++i) {
885 if (_active_notes[i]) {
886 _active_notes[i]->property_x2() = trackview.editor().frame_to_pixel(_region->length());
892 MidiRegionView::play_midi_note(boost::shared_ptr<Evoral::Note> note)
894 if (!trackview.editor().sound_notes()) {
898 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
901 route_ui->midi_track()->write_immediate_event(note->on_event().size(), note->on_event().buffer());
903 nframes_t note_length_ms = (note->off_event().time() - note->on_event().time())
904 * (1000 / (double)route_ui->session().nominal_frame_rate());
905 Glib::signal_timeout().connect(bind(mem_fun(this, &MidiRegionView::play_midi_note_off), note),
906 note_length_ms, G_PRIORITY_DEFAULT);
910 MidiRegionView::play_midi_note_off(boost::shared_ptr<Evoral::Note> note)
912 RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
915 route_ui->midi_track()->write_immediate_event(note->off_event().size(), note->off_event().buffer());
921 /** Add a MIDI note to the view (with length).
923 * If in sustained mode, notes with length 0 will be considered active
924 * notes, and resolve_note should be called when the corresponding note off
925 * event arrives, to properly display the note.
928 MidiRegionView::add_note(const boost::shared_ptr<Evoral::Note> note)
930 assert(note->time() >= 0);
931 assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
933 // dont display notes beyond the region bounds
934 if ( note->time() - _region->start() >= _region->length() ||
935 note->time() < _region->start() ||
936 note->note() < midi_stream_view()->lowest_note() ||
937 note->note() > midi_stream_view()->highest_note() ) {
941 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
943 CanvasNoteEvent* event = 0;
945 const double x = trackview.editor().frame_to_pixel((nframes64_t)note->time() - _region->start());
947 if (midi_view()->note_mode() == Sustained) {
949 const double y1 = midi_stream_view()->note_to_y(note->note());
950 const double note_endpixel =
951 trackview.editor().frame_to_pixel((nframes64_t)note->end_time() - _region->start());
953 CanvasNote* ev_rect = new CanvasNote(*this, *group, note);
954 ev_rect->property_x1() = x;
955 ev_rect->property_y1() = y1;
956 if (note->length() > 0)
957 ev_rect->property_x2() = note_endpixel;
959 ev_rect->property_x2() = trackview.editor().frame_to_pixel(_region->length());
960 ev_rect->property_y2() = y1 + floor(midi_stream_view()->note_height());
962 if (note->length() == 0) {
965 assert(note->note() < 128);
966 // If this note is already active there's a stuck note,
967 // finish the old note rectangle
968 if (_active_notes[note->note()]) {
969 CanvasNote* const old_rect = _active_notes[note->note()];
970 boost::shared_ptr<Evoral::Note> old_note = old_rect->note();
971 cerr << "MidiModel: WARNING: Note has length 0: chan " << old_note->channel()
972 << "note " << (int)old_note->note() << " @ " << old_note->time() << endl;
973 /* FIXME: How large to make it? Make it a diamond? */
974 old_rect->property_x2() = old_rect->property_x1() + 2.0;
975 old_rect->property_outline_what() = (guint32) 0xF;
977 _active_notes[note->note()] = ev_rect;
979 /* outline all but right edge */
980 ev_rect->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
982 /* outline all edges */
983 ev_rect->property_outline_what() = (guint32) 0xF;
987 _events.push_back(ev_rect);
992 for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
993 if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
994 gr->add_note(ev_rect);
998 } else if (midi_view()->note_mode() == Percussive) {
1000 //cerr << "MRV::add_note percussive " << note->note() << " @ " << note->time()
1001 // << " .. " << note->end_time() << endl;
1003 const double diamond_size = midi_stream_view()->note_height() / 2.0;
1004 const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
1006 CanvasHit* ev_diamond = new CanvasHit(*this, *group, diamond_size, note);
1007 ev_diamond->move(x, y);
1009 _events.push_back(ev_diamond);
1016 if (_marked_for_selection.find(note) != _marked_for_selection.end()) {
1017 note_selected(event, true);
1019 event->on_channel_selection_change(_last_channel_selection);
1024 MidiRegionView::add_pgm_change(ControlEvent& program, string displaytext)
1026 assert(program.time >= 0);
1028 // dont display program changes beyond the region bounds
1029 if (program.time - _region->start() >= _region->length() || program.time < _region->start())
1032 ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
1033 const double x = trackview.editor().frame_to_pixel((nframes64_t)program.time - _region->start());
1035 double height = midi_stream_view()->contents_height();
1037 boost::shared_ptr<CanvasProgramChange> pgm_change = boost::shared_ptr<CanvasProgramChange>(
1038 new CanvasProgramChange(
1046 _custom_device_mode,
1051 _pgm_changes.push_back(pgm_change);
1055 MidiRegionView::get_patch_key_at(double time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key)
1058 cerr << "getting patch key at " << time << " for channel " << channel << endl;
1059 Evoral::Parameter bank_select_msb(MidiCCAutomation, channel, MIDI_CTL_MSB_BANK);
1060 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
1062 if (msb_control != 0) {
1063 msb = int(msb_control->get_float(true, time));
1064 cerr << "got msb " << msb;
1067 Evoral::Parameter bank_select_lsb(MidiCCAutomation, channel, MIDI_CTL_LSB_BANK);
1068 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
1070 if (lsb_control != 0) {
1071 lsb = lsb_control->get_float(true, time);
1072 cerr << " got lsb " << lsb;
1075 Evoral::Parameter program_change(MidiPgmChangeAutomation, channel, 0);
1076 boost::shared_ptr<Evoral::Control> program_control = _model->control(program_change);
1077 float program_number = -1.0;
1078 if (program_control != 0) {
1079 program_number = program_control->get_float(true, time);
1080 cerr << " got program " << program_number << endl;
1083 key.msb = (int) floor(msb + 0.5);
1084 key.lsb = (int) floor(lsb + 0.5);
1085 key.program_number = (int) floor(program_number + 0.5);
1086 assert(key.is_sane());
1091 MidiRegionView::alter_program_change(ControlEvent& old_program, const MIDI::Name::PatchPrimaryKey& new_patch)
1094 // TODO: Get the real event here and alter them at the original times
1095 Evoral::Parameter bank_select_msb(MidiCCAutomation, old_program.channel, MIDI_CTL_MSB_BANK);
1096 boost::shared_ptr<Evoral::Control> msb_control = _model->control(bank_select_msb);
1097 if (msb_control != 0) {
1098 msb_control->set_float(float(new_patch.msb), true, old_program.time);
1101 // TODO: Get the real event here and alter them at the original times
1102 Evoral::Parameter bank_select_lsb(MidiCCAutomation, old_program.channel, MIDI_CTL_LSB_BANK);
1103 boost::shared_ptr<Evoral::Control> lsb_control = _model->control(bank_select_lsb);
1104 if (lsb_control != 0) {
1105 lsb_control->set_float(float(new_patch.lsb), true, old_program.time);
1108 Evoral::Parameter program_change(MidiPgmChangeAutomation, old_program.channel, 0);
1109 boost::shared_ptr<Evoral::Control> program_control = _model->control(program_change);
1111 assert(program_control != 0);
1112 program_control->set_float(float(new_patch.program_number), true, old_program.time);
1118 MidiRegionView::program_selected(CanvasProgramChange& program, const MIDI::Name::PatchPrimaryKey& new_patch)
1120 ControlEvent program_change_event(program.event_time(), program.program(), program.channel());
1121 alter_program_change(program_change_event, new_patch);
1125 MidiRegionView::previous_program(CanvasProgramChange& program)
1127 MIDI::Name::PatchPrimaryKey key;
1128 get_patch_key_at(program.event_time(), program.channel(), key);
1130 boost::shared_ptr<MIDI::Name::Patch> patch =
1131 MIDI::Name::MidiPatchManager::instance().previous_patch(
1133 _custom_device_mode,
1138 ControlEvent program_change_event(program.event_time(), program.program(), program.channel());
1140 alter_program_change(program_change_event, patch->patch_primary_key());
1145 MidiRegionView::next_program(CanvasProgramChange& program)
1147 MIDI::Name::PatchPrimaryKey key;
1148 get_patch_key_at(program.event_time(), program.channel(), key);
1150 boost::shared_ptr<MIDI::Name::Patch> patch =
1151 MIDI::Name::MidiPatchManager::instance().next_patch(
1153 _custom_device_mode,
1157 ControlEvent program_change_event(program.event_time(), program.program(), program.channel());
1159 alter_program_change(program_change_event, patch->patch_primary_key());
1164 MidiRegionView::delete_selection()
1166 assert(_delta_command);
1168 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1169 if ((*i)->selected()) {
1170 _delta_command->remove((*i)->note());
1178 MidiRegionView::clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev)
1180 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1181 if ((*i)->selected() && (*i) != ev) {
1182 (*i)->selected(false);
1190 MidiRegionView::unique_select(ArdourCanvas::CanvasNoteEvent* ev)
1192 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1194 (*i)->selected(false);
1199 _selection.insert(ev);
1201 if ( ! ev->selected()) {
1207 MidiRegionView::note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
1210 clear_selection_except(ev);
1213 if (_selection.insert(ev).second) {
1214 play_midi_note(ev->note());
1217 if ( ! ev->selected()) {
1224 MidiRegionView::note_deselected(ArdourCanvas::CanvasNoteEvent* ev, bool add)
1227 clear_selection_except(ev);
1230 _selection.erase(ev);
1232 if (ev->selected()) {
1233 ev->selected(false);
1239 MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2)
1241 const double last_y = std::min(y1, y2);
1242 const double y = std::max(y1, y2);
1244 // TODO: Make this faster by storing the last updated selection rect, and only
1245 // adjusting things that are in the area that appears/disappeared.
1246 // We probably need a tree to be able to find events in O(log(n)) time.
1249 double last_x1 = 0.0;
1253 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1255 // Events should always be sorted by increasing x1() here
1256 assert((*i)->x1() >= last_x1);
1257 last_x1 = (*i)->x1();
1260 if ((*i)->x1() >= x1 && (*i)->x1() <= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1261 if (!(*i)->selected()) {
1262 (*i)->selected(true);
1263 _selection.insert(*i);
1264 play_midi_note((*i)->note());
1266 // Not inside rectangle
1267 } else if ((*i)->selected()) {
1268 (*i)->selected(false);
1269 _selection.erase(*i);
1273 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1275 // Events should always be sorted by increasing x1() here
1276 assert((*i)->x1() >= last_x1);
1277 last_x1 = (*i)->x1();
1280 if ((*i)->x2() <= x1 && (*i)->x2() >= x2 && (*i)->y1() >= last_y && (*i)->y1() <= y) {
1281 if (!(*i)->selected()) {
1282 (*i)->selected(true);
1283 _selection.insert(*i);
1284 play_midi_note((*i)->note());
1286 // Not inside rectangle
1287 } else if ((*i)->selected()) {
1288 (*i)->selected(false);
1289 _selection.erase(*i);
1297 MidiRegionView::move_selection(double dx, double dy)
1299 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i)
1300 (*i)->move_event(dx, dy);
1305 MidiRegionView::note_dropped(CanvasNoteEvent* ev, double dt, uint8_t dnote)
1307 // TODO: This would be faster/nicer with a MoveCommand that doesn't need to copy...
1308 if (_selection.find(ev) != _selection.end()) {
1309 uint8_t lowest_note_in_selection = midi_stream_view()->lowest_note();
1310 uint8_t highest_note_in_selection = midi_stream_view()->highest_note();
1311 uint8_t highest_note_difference = 0;
1313 // find highest and lowest notes first
1314 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1315 uint8_t pitch = (*i)->note()->note();
1316 lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
1317 highest_note_in_selection = std::max(highest_note_in_selection, pitch);
1321 cerr << "dnote: " << (int) dnote << endl;
1322 cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
1323 << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
1324 cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
1325 << int(highest_note_in_selection) << endl;
1326 cerr << "selection size: " << _selection.size() << endl;
1327 cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
1330 // Make sure the note pitch does not exceed the MIDI standard range
1331 if (dnote <= 127 && (highest_note_in_selection + dnote > 127)) {
1332 highest_note_difference = highest_note_in_selection - 127;
1335 start_delta_command(_("move notes"));
1337 for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ) {
1338 Selection::iterator next = i;
1341 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(*i)->note().get()));
1343 // we need to snap here again in nframes64_t in order to be sample accurate
1344 double new_note_time = (*i)->note()->time();
1345 new_note_time += dt;
1347 // keep notes inside region if dragged beyond left region bound
1348 if (new_note_time < _region->start()) {
1349 new_note_time = _region->start();
1352 // since note time is region-absolute but snap_to_frame expects position-relative
1353 // time we have to coordinate transform back and forth here.
1354 new_note_time = snap_to_frame(nframes64_t(new_note_time) - _region->start()) + _region->start();
1356 copy->set_time(new_note_time);
1358 uint8_t original_pitch = (*i)->note()->note();
1359 uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
1361 // keep notes in standard midi range
1362 clamp_0_to_127(new_pitch);
1364 //notes which are dragged beyond the standard midi range snap back to their original place
1365 if ((original_pitch != 0 && new_pitch == 0) || (original_pitch != 127 && new_pitch == 127)) {
1366 new_pitch = original_pitch;
1369 lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
1370 highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
1372 copy->set_note(new_pitch);
1374 command_remove_note(*i);
1375 command_add_note(copy, (*i)->selected());
1382 // care about notes being moved beyond the upper/lower bounds on the canvas
1383 if (lowest_note_in_selection < midi_stream_view()->lowest_note() ||
1384 highest_note_in_selection > midi_stream_view()->highest_note()) {
1385 midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
1391 MidiRegionView::snap_to_frame(double x)
1393 PublicEditor &editor = trackview.editor();
1394 // x is region relative
1395 // convert x to global frame
1396 nframes64_t frame = editor.pixel_to_frame(x) + _region->position();
1397 editor.snap_to(frame);
1398 // convert event_frame back to local coordinates relative to position
1399 frame -= _region->position();
1404 MidiRegionView::snap_to_frame(nframes64_t x)
1406 PublicEditor &editor = trackview.editor();
1407 // x is region relative
1408 // convert x to global frame
1409 nframes64_t frame = x + _region->position();
1410 editor.snap_to(frame);
1411 // convert event_frame back to local coordinates relative to position
1412 frame -= _region->position();
1417 MidiRegionView::snap_to_pixel(double x)
1419 return (double) trackview.editor().frame_to_pixel(snap_to_frame(x));
1423 MidiRegionView::get_position_pixels()
1425 nframes64_t region_frame = get_position();
1426 return trackview.editor().frame_to_pixel(region_frame);
1430 MidiRegionView::begin_resizing(CanvasNote::NoteEnd note_end)
1432 _resize_data.clear();
1434 for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
1435 CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
1437 // only insert CanvasNotes into the map
1439 NoteResizeData *resize_data = new NoteResizeData();
1440 resize_data->canvas_note = note;
1442 // create a new SimpleRect from the note which will be the resize preview
1443 SimpleRect *resize_rect =
1451 // calculate the colors: get the color settings
1452 uint32_t fill_color =
1454 ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(),
1457 // make the resize preview notes more transparent and bright
1458 fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
1460 // calculate color based on note velocity
1461 resize_rect->property_fill_color_rgba() =
1463 CanvasNoteEvent::meter_style_fill_color(note->note()->velocity()),
1467 resize_rect->property_outline_color_rgba() =
1468 CanvasNoteEvent::calculate_outline(ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get());
1470 resize_data->resize_rect = resize_rect;
1472 if (note_end == CanvasNote::NOTE_ON) {
1473 resize_data->current_x = note->x1();
1474 } else { // NOTE_OFF
1475 resize_data->current_x = note->x2();
1478 _resize_data.push_back(resize_data);
1484 MidiRegionView::update_resizing(CanvasNote::NoteEnd note_end, double x, bool relative)
1486 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1487 SimpleRect *resize_rect = (*i)->resize_rect;
1488 CanvasNote *canvas_note = (*i)->canvas_note;
1490 const double region_start = get_position_pixels();
1493 (*i)->current_x = (*i)->current_x + x;
1495 // x is in track relative, transform it to region relative
1496 (*i)->current_x = x - region_start;
1499 double current_x = (*i)->current_x;
1501 if (note_end == CanvasNote::NOTE_ON) {
1502 resize_rect->property_x1() = snap_to_pixel(current_x);
1503 resize_rect->property_x2() = canvas_note->x2();
1505 resize_rect->property_x2() = snap_to_pixel(current_x);
1506 resize_rect->property_x1() = canvas_note->x1();
1512 MidiRegionView::commit_resizing(CanvasNote::NoteEnd note_end, double event_x, bool relative)
1514 start_delta_command(_("resize notes"));
1516 for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
1517 CanvasNote* canvas_note = (*i)->canvas_note;
1518 SimpleRect* resize_rect = (*i)->resize_rect;
1519 double current_x = (*i)->current_x;
1520 const double position = get_position_pixels();
1523 // event_x is in track relative, transform it to region relative
1524 current_x = event_x - position;
1527 // because snapping works on world coordinates we have to transform current_x
1528 // to world coordinates before snapping and transform it back afterwards
1529 nframes64_t current_frame = snap_to_frame(current_x);
1530 // transform to region start relative
1531 current_frame += _region->start();
1533 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(canvas_note->note().get())));
1535 // resize beginning of note
1536 if (note_end == CanvasNote::NOTE_ON && current_frame < copy->end_time()) {
1537 command_remove_note(canvas_note);
1538 copy->on_event().time() = current_frame;
1539 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1541 // resize end of note
1542 if (note_end == CanvasNote::NOTE_OFF && current_frame > copy->time()) {
1543 command_remove_note(canvas_note);
1544 copy->off_event().time() = current_frame;
1545 command_add_note(copy, _selection.find(canvas_note) != _selection.end());
1552 _resize_data.clear();
1557 MidiRegionView::change_note_velocity(CanvasNoteEvent* event, int8_t velocity, bool relative)
1559 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(event->note().get())));
1562 uint8_t new_velocity = copy->velocity() + velocity;
1563 clamp_0_to_127(new_velocity);
1564 copy->set_velocity(new_velocity);
1566 copy->set_velocity(velocity);
1569 command_remove_note(event);
1570 command_add_note(copy, event->selected());
1574 MidiRegionView::change_velocity(CanvasNoteEvent* ev, int8_t velocity, bool relative)
1576 start_delta_command(_("change velocity"));
1578 change_note_velocity(ev, velocity, relative);
1580 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1581 Selection::iterator next = i;
1583 if ( !(*((*i)->note()) == *(ev->note())) ) {
1584 change_note_velocity(*i, velocity, relative);
1593 MidiRegionView::change_channel(uint8_t channel)
1595 start_delta_command(_("change channel"));
1596 for (Selection::iterator i = _selection.begin(); i != _selection.end();) {
1597 Selection::iterator next = i;
1600 CanvasNoteEvent* event = *i;
1601 const boost::shared_ptr<Evoral::Note> copy(new Evoral::Note(*(event->note().get())));
1603 copy->set_channel(channel);
1605 command_remove_note(event);
1606 command_add_note(copy, event->selected());
1616 MidiRegionView::note_entered(ArdourCanvas::CanvasNoteEvent* ev)
1618 if (ev->note() && _mouse_state == EraseTouchDragging) {
1619 start_delta_command(_("note entered"));
1621 _delta_command->remove(ev->note());
1622 } else if (_mouse_state == SelectTouchDragging) {
1623 note_selected(ev, true);
1629 MidiRegionView::switch_source(boost::shared_ptr<Source> src)
1631 boost::shared_ptr<MidiSource> msrc = boost::dynamic_pointer_cast<MidiSource>(src);
1633 display_model(msrc->model());
1637 MidiRegionView::set_frame_color()
1640 if (_selected && should_show_selection) {
1641 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
1643 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiFrameBase.get();
1649 MidiRegionView::midi_channel_mode_changed(ChannelMode mode, uint16_t mask)
1653 case FilterChannels:
1654 _force_channel = -1;
1657 _force_channel = mask;
1658 mask = 0xFFFF; // Show all notes as active (below)
1661 // Update notes for selection
1662 for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
1663 (*i)->on_channel_selection_change(mask);
1666 _last_channel_selection = mask;
1670 MidiRegionView::midi_patch_settings_changed(std::string model, std::string custom_device_mode)
1672 _model_name = model;
1673 _custom_device_mode = custom_device_mode;