+ if (!_note_diff_command) {
+ _note_diff_command = _model->new_note_diff_command (name);
+ }
+}
+
+void
+MidiRegionView::note_diff_add_note (const boost::shared_ptr<NoteType> note, bool selected, bool show_velocity)
+{
+ if (_note_diff_command) {
+ _note_diff_command->add (note);
+ }
+ if (selected) {
+ _marked_for_selection.insert(note);
+ }
+ if (show_velocity) {
+ _marked_for_velocity.insert(note);
+ }
+}
+
+void
+MidiRegionView::note_diff_remove_note (ArdourCanvas::CanvasNoteEvent* ev)
+{
+ if (_note_diff_command && ev->note()) {
+ _note_diff_command->remove(ev->note());
+ }
+}
+
+void
+MidiRegionView::note_diff_add_change (ArdourCanvas::CanvasNoteEvent* ev,
+ MidiModel::NoteDiffCommand::Property property,
+ uint8_t val)
+{
+ if (_note_diff_command) {
+ _note_diff_command->change (ev->note(), property, val);
+ }
+}
+
+void
+MidiRegionView::note_diff_add_change (ArdourCanvas::CanvasNoteEvent* ev,
+ MidiModel::NoteDiffCommand::Property property,
+ Evoral::MusicalTime val)
+{
+ if (_note_diff_command) {
+ _note_diff_command->change (ev->note(), property, val);
+ }
+}
+
+void
+MidiRegionView::apply_diff (bool as_subcommand)
+{
+ bool add_or_remove;
+
+ if (!_note_diff_command) {
+ return;
+ }
+
+ if ((add_or_remove = _note_diff_command->adds_or_removes())) {
+ // Mark all selected notes for selection when model reloads
+ for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
+ _marked_for_selection.insert((*i)->note());
+ }
+ }
+
+ if (as_subcommand) {
+ _model->apply_command_as_subcommand (*trackview.session(), _note_diff_command);
+ } else {
+ _model->apply_command (*trackview.session(), _note_diff_command);
+ }
+
+ _note_diff_command = 0;
+ midi_view()->midi_track()->playlist_modified();
+
+ if (add_or_remove) {
+ _marked_for_selection.clear();
+ }
+
+ _marked_for_velocity.clear();
+}
+
+void
+MidiRegionView::abort_command()
+{
+ delete _note_diff_command;
+ _note_diff_command = 0;
+ clear_selection();
+}
+
+CanvasNoteEvent*
+MidiRegionView::find_canvas_note (boost::shared_ptr<NoteType> note)
+{
+ if (_optimization_iterator != _events.end()) {
+ ++_optimization_iterator;
+ }
+
+ if (_optimization_iterator != _events.end() && (*_optimization_iterator)->note() == note) {
+ return *_optimization_iterator;
+ }
+
+ for (_optimization_iterator = _events.begin(); _optimization_iterator != _events.end(); ++_optimization_iterator) {
+ if ((*_optimization_iterator)->note() == note) {
+ return *_optimization_iterator;
+ }
+ }
+
+ return 0;
+}
+
+void
+MidiRegionView::get_events (Events& e, Evoral::Sequence<Evoral::MusicalTime>::NoteOperator op, uint8_t val, int chan_mask)
+{
+ MidiModel::Notes notes;
+ _model->get_notes (notes, op, val, chan_mask);
+
+ for (MidiModel::Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
+ CanvasNoteEvent* cne = find_canvas_note (*n);
+ if (cne) {
+ e.push_back (cne);
+ }
+ }
+}
+
+void
+MidiRegionView::redisplay_model()
+{
+ // Don't redisplay the model if we're currently recording and displaying that
+ if (_active_notes) {
+ return;
+ }
+
+ if (!_model) {
+ return;
+ }
+
+ for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
+ (*i)->invalidate ();
+ }
+
+ MidiModel::ReadLock lock(_model->read_lock());
+
+ MidiModel::Notes& notes (_model->notes());
+ _optimization_iterator = _events.begin();
+
+ for (MidiModel::Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
+
+ boost::shared_ptr<NoteType> note (*n);
+ CanvasNoteEvent* cne;
+ bool visible;
+
+ if (note_in_region_range (note, visible)) {
+
+ if ((cne = find_canvas_note (note)) != 0) {
+
+ cne->validate ();
+
+ CanvasNote* cn;
+ CanvasHit* ch;
+
+ if ((cn = dynamic_cast<CanvasNote*>(cne)) != 0) {
+ update_note (cn);
+ } else if ((ch = dynamic_cast<CanvasHit*>(cne)) != 0) {
+ update_hit (ch);
+ }
+
+ if (visible) {
+ cne->show ();
+ } else {
+ cne->hide ();
+ }
+
+ } else {
+
+ add_note (note, visible);
+ }
+
+ } else {
+
+ if ((cne = find_canvas_note (note)) != 0) {
+ cne->validate ();
+ cne->hide ();
+ }
+ }
+ }
+
+
+ /* remove note items that are no longer valid */
+
+ for (Events::iterator i = _events.begin(); i != _events.end(); ) {
+ if (!(*i)->valid ()) {
+
+ for (vector<GhostRegion*>::iterator j = ghosts.begin(); j != ghosts.end(); ++j) {
+ MidiGhostRegion* gr = dynamic_cast<MidiGhostRegion*> (*j);
+ if (gr) {
+ gr->remove_note (*i);
+ }
+ }
+
+ delete *i;
+ i = _events.erase (i);
+
+ } else {
+ ++i;
+ }
+ }
+
+ _patch_changes.clear();
+ _sys_exes.clear();
+
+ display_sysexes();
+ display_patch_changes ();
+
+ _marked_for_selection.clear ();
+ _marked_for_velocity.clear ();
+
+ /* we may have caused _events to contain things out of order (e.g. if a note
+ moved earlier or later). we don't generally need them in time order, but
+ make a note that a sort is required for those cases that require it.
+ */
+
+ _sort_needed = true;
+}
+
+void
+MidiRegionView::display_patch_changes ()
+{
+ MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
+ uint16_t chn_mask = mtv->channel_selector().get_selected_channels();
+
+ for (uint8_t i = 0; i < 16; ++i) {
+ if (chn_mask & (1<<i)) {
+ display_patch_changes_on_channel (i);
+ }
+ /* TODO gray-out patch instad of not displaying it */
+ }
+}
+
+void
+MidiRegionView::display_patch_changes_on_channel (uint8_t channel)
+{
+ for (MidiModel::PatchChanges::const_iterator i = _model->patch_changes().begin(); i != _model->patch_changes().end(); ++i) {
+
+ if ((*i)->channel() != channel) {
+ continue;
+ }
+
+ MIDI::Name::PatchPrimaryKey patch_key ((*i)->bank_msb (), (*i)->bank_lsb (), (*i)->program ());
+
+ boost::shared_ptr<MIDI::Name::Patch> patch =
+ MIDI::Name::MidiPatchManager::instance().find_patch(
+ _model_name, _custom_device_mode, channel, patch_key);
+
+ if (patch != 0) {
+ add_canvas_patch_change (*i, patch->name());
+ } else {
+ char buf[16];
+ /* program and bank numbers are zero-based: convert to one-based: MIDI_BP_ZERO */
+ snprintf (buf, 16, "%d %d", (*i)->program() + MIDI_BP_ZERO , (*i)->bank() + MIDI_BP_ZERO);
+ add_canvas_patch_change (*i, buf);
+ }
+ }
+}
+
+void
+MidiRegionView::display_sysexes()
+{
+ for (MidiModel::SysExes::const_iterator i = _model->sysexes().begin(); i != _model->sysexes().end(); ++i) {
+ Evoral::MusicalTime time = (*i)->time();
+ assert(time >= 0);
+
+ ostringstream str;
+ str << hex;
+ for (uint32_t b = 0; b < (*i)->size(); ++b) {
+ str << int((*i)->buffer()[b]);
+ if (b != (*i)->size() -1) {
+ str << " ";
+ }
+ }
+ string text = str.str();
+
+ const double x = trackview.editor().frame_to_pixel(source_beats_to_absolute_frames(time));
+
+ double height = midi_stream_view()->contents_height();
+
+ boost::shared_ptr<CanvasSysEx> sysex = boost::shared_ptr<CanvasSysEx>(
+ new CanvasSysEx(*this, *_note_group, text, height, x, 1.0));
+
+ // Show unless patch change is beyond the region bounds
+ if (time - _region->start() >= _region->length() || time < _region->start()) {
+ sysex->hide();
+ } else {
+ sysex->show();
+ }
+
+ _sys_exes.push_back(sysex);
+ }
+}
+
+
+MidiRegionView::~MidiRegionView ()
+{
+ in_destructor = true;
+
+ trackview.editor().verbose_cursor()->hide ();
+
+ note_delete_connection.disconnect ();
+
+ delete _list_editor;
+
+ RegionViewGoingAway (this); /* EMIT_SIGNAL */
+
+ if (_active_notes) {
+ end_write();
+ }
+
+ _selection.clear();
+ clear_events();
+
+ delete _note_group;
+ delete _note_diff_command;
+ delete _step_edit_cursor;
+ delete _temporary_note_group;
+}
+
+void
+MidiRegionView::region_resized (const PropertyChange& what_changed)
+{
+ RegionView::region_resized(what_changed);
+
+ if (what_changed.contains (ARDOUR::Properties::position)) {
+ set_duration(_region->length(), 0);
+ if (_enable_display) {
+ redisplay_model();
+ }
+ }
+}
+
+void
+MidiRegionView::reset_width_dependent_items (double pixel_width)
+{
+ RegionView::reset_width_dependent_items(pixel_width);
+ assert(_pixel_width == pixel_width);
+
+ if (_enable_display) {
+ redisplay_model();
+ }
+
+ move_step_edit_cursor (_step_edit_cursor_position);
+ set_step_edit_cursor_width (_step_edit_cursor_width);
+}
+
+void
+MidiRegionView::set_height (double height)
+{
+ static const double FUDGE = 2.0;
+ const double old_height = _height;
+ RegionView::set_height(height);
+ _height = height - FUDGE;
+
+ apply_note_range(midi_stream_view()->lowest_note(),
+ midi_stream_view()->highest_note(),
+ height != old_height + FUDGE);
+
+ if (name_pixbuf) {
+ name_pixbuf->raise_to_top();
+ }
+
+ for (PatchChanges::iterator x = _patch_changes.begin(); x != _patch_changes.end(); ++x) {
+ (*x)->set_height (midi_stream_view()->contents_height());
+ }
+
+ if (_step_edit_cursor) {
+ _step_edit_cursor->property_y2() = midi_stream_view()->contents_height();
+ }
+}
+
+
+/** Apply the current note range from the stream view
+ * by repositioning/hiding notes as necessary
+ */
+void
+MidiRegionView::apply_note_range (uint8_t min, uint8_t max, bool force)
+{
+ if (!_enable_display) {
+ return;
+ }
+
+ if (!force && _current_range_min == min && _current_range_max == max) {
+ return;
+ }
+
+ _current_range_min = min;
+ _current_range_max = max;
+
+ for (Events::const_iterator i = _events.begin(); i != _events.end(); ++i) {
+ CanvasNoteEvent* event = *i;
+ boost::shared_ptr<NoteType> note (event->note());
+
+ if (note->note() < _current_range_min ||
+ note->note() > _current_range_max) {
+ event->hide();
+ } else {
+ event->show();
+ }
+
+ if (CanvasNote* cnote = dynamic_cast<CanvasNote*>(event)) {
+
+ const double y1 = midi_stream_view()->note_to_y(note->note());
+ const double y2 = y1 + floor(midi_stream_view()->note_height());
+
+ cnote->property_y1() = y1;
+ cnote->property_y2() = y2;
+
+ } else if (CanvasHit* chit = dynamic_cast<CanvasHit*>(event)) {
+
+ const double diamond_size = update_hit (chit);
+
+ chit->set_height (diamond_size);
+ }
+ }
+}
+
+GhostRegion*
+MidiRegionView::add_ghost (TimeAxisView& tv)
+{
+ CanvasNote* note;
+
+ double unit_position = _region->position () / samples_per_unit;
+ MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&tv);
+ MidiGhostRegion* ghost;
+
+ if (mtv && mtv->midi_view()) {
+ /* if ghost is inserted into midi track, use a dedicated midi ghost canvas group
+ to allow having midi notes on top of note lines and waveforms.
+ */
+ ghost = new MidiGhostRegion (*mtv->midi_view(), trackview, unit_position);
+ } else {
+ ghost = new MidiGhostRegion (tv, trackview, unit_position);
+ }
+
+ for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
+ if ((note = dynamic_cast<CanvasNote*>(*i)) != 0) {
+ ghost->add_note(note);
+ }
+ }
+
+ ghost->set_height ();
+ ghost->set_duration (_region->length() / samples_per_unit);
+ ghosts.push_back (ghost);
+
+ GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
+
+ return ghost;
+}
+
+
+/** Begin tracking note state for successive calls to add_event
+ */
+void
+MidiRegionView::begin_write()
+{
+ assert(!_active_notes);
+ _active_notes = new CanvasNote*[128];
+ for (unsigned i=0; i < 128; ++i) {
+ _active_notes[i] = 0;
+ }
+}
+
+
+/** Destroy note state for add_event
+ */
+void
+MidiRegionView::end_write()
+{
+ delete[] _active_notes;
+ _active_notes = 0;
+ _marked_for_selection.clear();
+ _marked_for_velocity.clear();
+}
+
+
+/** Resolve an active MIDI note (while recording).
+ */
+void
+MidiRegionView::resolve_note(uint8_t note, double end_time)
+{
+ if (midi_view()->note_mode() != Sustained) {
+ return;
+ }
+
+ if (_active_notes && _active_notes[note]) {
+
+ /* XXX is end_time really region-centric? I think so, because
+ this is a new region that we're recording, so source zero is
+ the same as region zero
+ */
+ const framepos_t end_time_frames = region_beats_to_region_frames(end_time);
+
+ _active_notes[note]->property_x2() = trackview.editor().frame_to_pixel(end_time_frames);
+ _active_notes[note]->property_outline_what() = (guint32) 0xF; // all edges
+ _active_notes[note] = 0;
+ }
+}
+
+
+/** Extend active notes to rightmost edge of region (if length is changed)
+ */
+void
+MidiRegionView::extend_active_notes()
+{
+ if (!_active_notes) {
+ return;
+ }
+
+ for (unsigned i=0; i < 128; ++i) {
+ if (_active_notes[i]) {
+ _active_notes[i]->property_x2() = trackview.editor().frame_to_pixel(_region->length());
+ }
+ }
+}
+
+
+void
+MidiRegionView::play_midi_note(boost::shared_ptr<NoteType> note)
+{
+ if (_no_sound_notes || !trackview.editor().sound_notes()) {
+ return;
+ }
+
+ RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
+
+ if (!route_ui || !route_ui->midi_track()) {
+ return;
+ }
+
+ NotePlayer* np = new NotePlayer (route_ui->midi_track());
+ np->add (note);
+ np->play ();
+}
+
+void
+MidiRegionView::play_midi_chord (vector<boost::shared_ptr<NoteType> > notes)
+{
+ if (_no_sound_notes || !trackview.editor().sound_notes()) {
+ return;
+ }
+
+ RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
+
+ if (!route_ui || !route_ui->midi_track()) {
+ return;
+ }
+
+ NotePlayer* np = new NotePlayer (route_ui->midi_track());
+
+ for (vector<boost::shared_ptr<NoteType> >::iterator n = notes.begin(); n != notes.end(); ++n) {
+ np->add (*n);
+ }
+
+ np->play ();
+}
+
+
+bool
+MidiRegionView::note_in_region_range (const boost::shared_ptr<NoteType> note, bool& visible) const
+{
+ const framepos_t note_start_frames = source_beats_to_region_frames (note->time());
+ bool outside = (note_start_frames < 0) || (note_start_frames > _region->last_frame());
+
+ visible = (note->note() >= midi_stream_view()->lowest_note()) &&
+ (note->note() <= midi_stream_view()->highest_note());
+
+ return !outside;
+}
+
+/** Update a canvas note's size from its model note.
+ * @param ev Canvas note to update.
+ * @param update_ghost_regions true to update the note in any ghost regions that we have, otherwise false.
+ */
+void
+MidiRegionView::update_note (CanvasNote* ev, bool update_ghost_regions)
+{
+ boost::shared_ptr<NoteType> note = ev->note();
+
+ const double x = trackview.editor().frame_to_pixel (source_beats_to_region_frames (note->time()));
+ const double y1 = midi_stream_view()->note_to_y(note->note());
+
+ ev->property_x1() = x;
+ ev->property_y1() = y1;
+
+ /* trim note display to not overlap the end of its region */
+
+ if (note->length() > 0) {
+ const framepos_t note_end_frames = min (source_beats_to_region_frames (note->end_time()), _region->length());
+ ev->property_x2() = trackview.editor().frame_to_pixel (note_end_frames);
+ } else {
+ ev->property_x2() = trackview.editor().frame_to_pixel (_region->length());
+ }
+
+ ev->property_y2() = y1 + floor(midi_stream_view()->note_height());
+
+ if (note->length() == 0) {
+ if (_active_notes) {
+ assert(note->note() < 128);
+ // If this note is already active there's a stuck note,
+ // finish the old note rectangle
+ if (_active_notes[note->note()]) {
+ CanvasNote* const old_rect = _active_notes[note->note()];
+ boost::shared_ptr<NoteType> old_note = old_rect->note();
+ old_rect->property_x2() = x;
+ old_rect->property_outline_what() = (guint32) 0xF;
+ }
+ _active_notes[note->note()] = ev;
+ }
+ /* outline all but right edge */
+ ev->property_outline_what() = (guint32) (0x1 & 0x4 & 0x8);
+ } else {
+ /* outline all edges */
+ ev->property_outline_what() = (guint32) 0xF;
+ }
+
+ if (update_ghost_regions) {
+ for (std::vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
+ MidiGhostRegion* gr = dynamic_cast<MidiGhostRegion*> (*i);
+ if (gr) {
+ gr->update_note (ev);
+ }
+ }
+ }
+}
+
+double
+MidiRegionView::update_hit (CanvasHit* ev)
+{
+ boost::shared_ptr<NoteType> note = ev->note();
+
+ const framepos_t note_start_frames = source_beats_to_region_frames(note->time());
+ const double x = trackview.editor().frame_to_pixel(note_start_frames);
+ const double diamond_size = midi_stream_view()->note_height() / 2.0;
+ const double y = midi_stream_view()->note_to_y(note->note()) + ((diamond_size-2) / 4.0);
+
+ ev->move_to (x, y);
+
+ return diamond_size;
+}
+
+/** Add a MIDI note to the view (with length).
+ *
+ * If in sustained mode, notes with length 0 will be considered active
+ * notes, and resolve_note should be called when the corresponding note off
+ * event arrives, to properly display the note.
+ */
+void
+MidiRegionView::add_note(const boost::shared_ptr<NoteType> note, bool visible)
+{
+ CanvasNoteEvent* event = 0;
+
+ assert(note->time() >= 0);
+ assert(midi_view()->note_mode() == Sustained || midi_view()->note_mode() == Percussive);
+
+ //ArdourCanvas::Group* const group = (ArdourCanvas::Group*) get_canvas_group();
+
+ if (midi_view()->note_mode() == Sustained) {
+
+ CanvasNote* ev_rect = new CanvasNote(*this, *_note_group, note);
+
+ update_note (ev_rect);
+
+ event = ev_rect;
+
+ MidiGhostRegion* gr;
+
+ for (std::vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
+ if ((gr = dynamic_cast<MidiGhostRegion*>(*g)) != 0) {
+ gr->add_note(ev_rect);
+ }
+ }
+
+ } else if (midi_view()->note_mode() == Percussive) {
+
+ const double diamond_size = midi_stream_view()->note_height() / 2.0;
+
+ CanvasHit* ev_diamond = new CanvasHit (*this, *_note_group, diamond_size, note);
+
+ update_hit (ev_diamond);
+
+ event = ev_diamond;
+
+ } else {
+ event = 0;
+ }
+
+ if (event) {
+ if (_marked_for_selection.find(note) != _marked_for_selection.end()) {
+ note_selected(event, true);
+ }
+
+ if (_marked_for_velocity.find(note) != _marked_for_velocity.end()) {
+ event->show_velocity();
+ }
+
+ event->on_channel_selection_change(_last_channel_selection);
+ _events.push_back(event);
+
+ if (visible) {
+ event->show();
+ } else {
+ event->hide ();
+ }
+ }
+
+ MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
+ MidiStreamView* const view = mtv->midi_view();
+
+ view->update_note_range (note->note());
+}
+
+void
+MidiRegionView::step_add_note (uint8_t channel, uint8_t number, uint8_t velocity,
+ Evoral::MusicalTime pos, Evoral::MusicalTime len)
+{
+ boost::shared_ptr<NoteType> new_note (new NoteType (channel, pos, len, number, velocity));
+
+ /* potentially extend region to hold new note */
+
+ framepos_t end_frame = source_beats_to_absolute_frames (new_note->end_time());
+ framepos_t region_end = _region->last_frame();
+
+ if (end_frame > region_end) {
+ _region->set_length (end_frame - _region->position());
+ }
+
+ MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
+ MidiStreamView* const view = mtv->midi_view();
+
+ view->update_note_range(new_note->note());
+
+ _marked_for_selection.clear ();
+ clear_selection ();
+
+ start_note_diff_command (_("step add"));
+ note_diff_add_note (new_note, true, false);
+ apply_diff();
+
+ // last_step_edit_note = new_note;
+}
+
+void
+MidiRegionView::step_sustain (Evoral::MusicalTime beats)
+{
+ change_note_lengths (false, false, beats, false, true);
+}
+
+void
+MidiRegionView::add_canvas_patch_change (MidiModel::PatchChangePtr patch, const string& displaytext)
+{
+ assert (patch->time() >= 0);
+
+ const double x = trackview.editor().frame_to_pixel (source_beats_to_region_frames (patch->time()));
+
+ double const height = midi_stream_view()->contents_height();
+
+ boost::shared_ptr<CanvasPatchChange> patch_change = boost::shared_ptr<CanvasPatchChange>(
+ new CanvasPatchChange(*this, *_note_group,
+ displaytext,
+ height,
+ x, 1.0,
+ _model_name,
+ _custom_device_mode,
+ patch)
+ );
+
+ // Show unless patch change is beyond the region bounds
+ if (patch->time() - _region->start() >= _region->length() || patch->time() < _region->start()) {
+ patch_change->hide();
+ } else {
+ patch_change->show();
+ }
+
+ _patch_changes.push_back (patch_change);
+}
+
+void
+MidiRegionView::get_patch_key_at (Evoral::MusicalTime time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key)
+{
+ MidiModel::PatchChanges::iterator i = _model->patch_change_lower_bound (time);
+ while (i != _model->patch_changes().end() && (*i)->channel() != channel) {
+ ++i;
+ }
+
+ if (i != _model->patch_changes().end()) {
+ key.msb = (*i)->bank_msb ();
+ key.lsb = (*i)->bank_lsb ();
+ key.program_number = (*i)->program ();
+ } else {
+ key.msb = key.lsb = key.program_number = 0;
+ }
+
+ assert (key.is_sane());
+}
+
+
+void
+MidiRegionView::change_patch_change (CanvasPatchChange& pc, const MIDI::Name::PatchPrimaryKey& new_patch)
+{
+ MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (_("alter patch change"));
+
+ if (pc.patch()->program() != new_patch.program_number) {
+ c->change_program (pc.patch (), new_patch.program_number);
+ }
+
+ int const new_bank = (new_patch.msb << 7) | new_patch.lsb;
+ if (pc.patch()->bank() != new_bank) {
+ c->change_bank (pc.patch (), new_bank);
+ }
+
+ _model->apply_command (*trackview.session(), c);
+
+ _patch_changes.clear ();
+ display_patch_changes ();
+}
+
+void
+MidiRegionView::change_patch_change (MidiModel::PatchChangePtr old_change, const Evoral::PatchChange<Evoral::MusicalTime> & new_change)
+{
+ MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (_("alter patch change"));
+
+ if (old_change->time() != new_change.time()) {
+ c->change_time (old_change, new_change.time());
+ }
+
+ if (old_change->channel() != new_change.channel()) {
+ c->change_channel (old_change, new_change.channel());
+ }
+
+ if (old_change->program() != new_change.program()) {
+ c->change_program (old_change, new_change.program());
+ }
+
+ if (old_change->bank() != new_change.bank()) {
+ c->change_bank (old_change, new_change.bank());
+ }
+
+ _model->apply_command (*trackview.session(), c);
+
+ _patch_changes.clear ();
+ display_patch_changes ();
+}
+
+/** Add a patch change to the region.
+ * @param t Time in frames relative to region position
+ * @param patch Patch to add; time and channel are ignored (time is converted from t, and channel comes from
+ * MidiTimeAxisView::get_channel_for_add())
+ */
+void
+MidiRegionView::add_patch_change (framecnt_t t, Evoral::PatchChange<Evoral::MusicalTime> const & patch)
+{
+ MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
+
+ MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (_("add patch change"));
+ c->add (MidiModel::PatchChangePtr (
+ new Evoral::PatchChange<Evoral::MusicalTime> (
+ absolute_frames_to_source_beats (_region->position() + t),
+ mtv->get_channel_for_add(), patch.program(), patch.bank()
+ )
+ )
+ );
+
+ _model->apply_command (*trackview.session(), c);
+
+ _patch_changes.clear ();
+ display_patch_changes ();
+}
+
+void
+MidiRegionView::move_patch_change (CanvasPatchChange& pc, Evoral::MusicalTime t)
+{
+ MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (_("move patch change"));
+ c->change_time (pc.patch (), t);
+ _model->apply_command (*trackview.session(), c);
+
+ _patch_changes.clear ();
+ display_patch_changes ();
+}
+
+void
+MidiRegionView::delete_patch_change (CanvasPatchChange* pc)
+{
+ MidiModel::PatchChangeDiffCommand* c = _model->new_patch_change_diff_command (_("delete patch change"));
+ c->remove (pc->patch ());
+ _model->apply_command (*trackview.session(), c);
+
+ _patch_changes.clear ();
+ display_patch_changes ();
+}
+
+void
+MidiRegionView::previous_patch (CanvasPatchChange& patch)
+{
+ if (patch.patch()->program() < 127) {
+ MIDI::Name::PatchPrimaryKey key;
+ get_patch_key_at (patch.patch()->time(), patch.patch()->channel(), key);
+ key.program_number++;
+ change_patch_change (patch, key);
+ }
+}
+
+void
+MidiRegionView::next_patch (CanvasPatchChange& patch)
+{
+ if (patch.patch()->program() > 0) {
+ MIDI::Name::PatchPrimaryKey key;
+ get_patch_key_at (patch.patch()->time(), patch.patch()->channel(), key);
+ key.program_number--;
+ change_patch_change (patch, key);
+ }
+}
+
+void
+MidiRegionView::previous_bank (CanvasPatchChange& patch)
+{
+ if (patch.patch()->program() < 127) {
+ MIDI::Name::PatchPrimaryKey key;
+ get_patch_key_at (patch.patch()->time(), patch.patch()->channel(), key);
+ if (key.lsb > 0) {
+ key.lsb--;
+ change_patch_change (patch, key);
+ } else {
+ if (key.msb > 0) {
+ key.lsb = 127;
+ key.msb--;
+ change_patch_change (patch, key);
+ }
+ }
+ }
+}
+
+void
+MidiRegionView::next_bank (CanvasPatchChange& patch)
+{
+ if (patch.patch()->program() > 0) {
+ MIDI::Name::PatchPrimaryKey key;
+ get_patch_key_at (patch.patch()->time(), patch.patch()->channel(), key);
+ if (key.lsb < 127) {
+ key.lsb++;
+ change_patch_change (patch, key);
+ } else {
+ if (key.msb < 127) {
+ key.lsb = 0;
+ key.msb++;
+ change_patch_change (patch, key);
+ }
+ }
+ }
+}
+
+void
+MidiRegionView::maybe_remove_deleted_note_from_selection (CanvasNoteEvent* cne)
+{
+ if (_selection.empty()) {
+ return;
+ }
+
+ _selection.erase (cne);
+}
+
+void
+MidiRegionView::delete_selection()
+{
+ if (_selection.empty()) {
+ return;
+ }
+
+ start_note_diff_command (_("delete selection"));
+
+ for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
+ if ((*i)->selected()) {
+ _note_diff_command->remove((*i)->note());
+ }
+ }
+
+ _selection.clear();
+
+ apply_diff ();
+}
+
+void
+MidiRegionView::delete_note (boost::shared_ptr<NoteType> n)
+{
+ start_note_diff_command (_("delete note"));
+ _note_diff_command->remove (n);
+ apply_diff ();
+
+ trackview.editor().verbose_cursor()->hide ();
+}
+
+void
+MidiRegionView::clear_selection_except (ArdourCanvas::CanvasNoteEvent* ev, bool signal)
+{
+ for (Selection::iterator i = _selection.begin(); i != _selection.end(); ) {
+ if ((*i) != ev) {
+ Selection::iterator tmp = i;
+ ++tmp;
+
+ (*i)->set_selected (false);
+ (*i)->hide_velocity ();
+ _selection.erase (i);
+
+ i = tmp;
+ } else {
+ ++i;
+ }
+ }
+
+ /* this does not change the status of this regionview w.r.t the editor
+ selection.
+ */
+
+ if (signal) {
+ SelectionCleared (this); /* EMIT SIGNAL */
+ }
+}
+
+void
+MidiRegionView::unique_select(ArdourCanvas::CanvasNoteEvent* ev)
+{
+ clear_selection_except (ev);
+
+ /* don't bother with checking to see if we should remove this
+ regionview from the editor selection, since we're about to add
+ another note, and thus put/keep this regionview in the editor
+ selection anyway.
+ */
+
+ if (!ev->selected()) {
+ add_to_selection (ev);
+ }
+}
+
+void
+MidiRegionView::select_all_notes ()
+{
+ clear_selection ();
+
+ for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
+ add_to_selection (*i);
+ }
+}
+
+void
+MidiRegionView::select_range (framepos_t start, framepos_t end)
+{
+ clear_selection ();
+
+ for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
+ framepos_t t = source_beats_to_absolute_frames((*i)->note()->time());
+ if (t >= start && t <= end) {
+ add_to_selection (*i);
+ }
+ }
+}
+
+void
+MidiRegionView::invert_selection ()
+{
+ for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
+ if ((*i)->selected()) {
+ remove_from_selection(*i);
+ } else {
+ add_to_selection (*i);
+ }
+ }
+}
+
+void
+MidiRegionView::select_matching_notes (uint8_t notenum, uint16_t channel_mask, bool add, bool extend)
+{
+ uint8_t low_note = 127;
+ uint8_t high_note = 0;
+ MidiModel::Notes& notes (_model->notes());
+ _optimization_iterator = _events.begin();
+
+ if (!add) {
+ clear_selection ();
+ }
+
+ if (extend && _selection.empty()) {
+ extend = false;
+ }
+
+ if (extend) {
+
+ /* scan existing selection to get note range */
+
+ for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
+ if ((*i)->note()->note() < low_note) {
+ low_note = (*i)->note()->note();
+ }
+ if ((*i)->note()->note() > high_note) {
+ high_note = (*i)->note()->note();
+ }
+ }
+
+ low_note = min (low_note, notenum);
+ high_note = max (high_note, notenum);
+ }
+
+ _no_sound_notes = true;
+
+ for (MidiModel::Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
+
+ boost::shared_ptr<NoteType> note (*n);
+ CanvasNoteEvent* cne;
+ bool select = false;
+
+ if (((1 << note->channel()) & channel_mask) != 0) {
+ if (extend) {
+ if ((note->note() >= low_note && note->note() <= high_note)) {
+ select = true;
+ }
+ } else if (note->note() == notenum) {
+ select = true;
+ }
+ }
+
+ if (select) {
+ if ((cne = find_canvas_note (note)) != 0) {
+ // extend is false because we've taken care of it,
+ // since it extends by time range, not pitch.
+ note_selected (cne, add, false);
+ }
+ }
+
+ add = true; // we need to add all remaining matching notes, even if the passed in value was false (for "set")
+
+ }
+
+ _no_sound_notes = false;
+}
+
+void
+MidiRegionView::toggle_matching_notes (uint8_t notenum, uint16_t channel_mask)
+{
+ MidiModel::Notes& notes (_model->notes());
+ _optimization_iterator = _events.begin();
+
+ for (MidiModel::Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
+
+ boost::shared_ptr<NoteType> note (*n);
+ CanvasNoteEvent* cne;
+
+ if (note->note() == notenum && (((0x0001 << note->channel()) & channel_mask) != 0)) {
+ if ((cne = find_canvas_note (note)) != 0) {
+ if (cne->selected()) {
+ note_deselected (cne);
+ } else {
+ note_selected (cne, true, false);
+ }
+ }
+ }
+ }
+}
+
+void
+MidiRegionView::note_selected (ArdourCanvas::CanvasNoteEvent* ev, bool add, bool extend)
+{
+ if (!add) {
+ clear_selection_except (ev);
+ if (!_selection.empty()) {
+ PublicEditor& editor (trackview.editor());
+ editor.get_selection().add (this);
+ }
+ }
+
+ if (!extend) {
+
+ if (!ev->selected()) {
+ add_to_selection (ev);
+ }
+
+ } else {
+ /* find end of latest note selected, select all between that and the start of "ev" */
+
+ Evoral::MusicalTime earliest = Evoral::MaxMusicalTime;
+ Evoral::MusicalTime latest = 0;
+
+ for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
+ if ((*i)->note()->end_time() > latest) {
+ latest = (*i)->note()->end_time();
+ }
+ if ((*i)->note()->time() < earliest) {
+ earliest = (*i)->note()->time();
+ }
+ }
+
+ if (ev->note()->end_time() > latest) {
+ latest = ev->note()->end_time();
+ }
+
+ if (ev->note()->time() < earliest) {
+ earliest = ev->note()->time();
+ }
+
+ for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
+
+ /* find notes entirely within OR spanning the earliest..latest range */
+
+ if (((*i)->note()->time() >= earliest && (*i)->note()->end_time() <= latest) ||
+ ((*i)->note()->time() <= earliest && (*i)->note()->end_time() >= latest)) {
+ add_to_selection (*i);
+ }
+
+ }
+ }
+}
+
+void
+MidiRegionView::note_deselected(ArdourCanvas::CanvasNoteEvent* ev)
+{
+ remove_from_selection (ev);
+}
+
+void
+MidiRegionView::update_drag_selection(double x1, double x2, double y1, double y2, bool extend)
+{
+ if (x1 > x2) {
+ swap (x1, x2);
+ }
+
+ if (y1 > y2) {
+ swap (y1, y2);
+ }
+
+ // TODO: Make this faster by storing the last updated selection rect, and only
+ // adjusting things that are in the area that appears/disappeared.
+ // We probably need a tree to be able to find events in O(log(n)) time.
+
+ for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
+
+ /* check if any corner of the note is inside the rect
+
+ Notes:
+ 1) this is computing "touched by", not "contained by" the rect.
+ 2) this does not require that events be sorted in time.
+ */
+
+ const double ix1 = (*i)->x1();
+ const double ix2 = (*i)->x2();
+ const double iy1 = (*i)->y1();
+ const double iy2 = (*i)->y2();
+
+ if ((ix1 >= x1 && ix1 <= x2 && iy1 >= y1 && iy1 <= y2) ||
+ (ix1 >= x1 && ix1 <= x2 && iy2 >= y1 && iy2 <= y2) ||
+ (ix2 >= x1 && ix2 <= x2 && iy1 >= y1 && iy1 <= y2) ||
+ (ix2 >= x1 && ix2 <= x2 && iy2 >= y1 && iy2 <= y2)) {
+
+ // Inside rectangle
+ if (!(*i)->selected()) {
+ add_to_selection (*i);
+ }
+ } else if ((*i)->selected() && !extend) {
+ // Not inside rectangle
+ remove_from_selection (*i);
+ }
+ }
+}
+
+void
+MidiRegionView::remove_from_selection (CanvasNoteEvent* ev)
+{
+ Selection::iterator i = _selection.find (ev);
+
+ if (i != _selection.end()) {
+ _selection.erase (i);
+ }
+
+ ev->set_selected (false);
+ ev->hide_velocity ();
+
+ if (_selection.empty()) {
+ PublicEditor& editor (trackview.editor());
+ editor.get_selection().remove (this);
+ }
+}
+
+void
+MidiRegionView::add_to_selection (CanvasNoteEvent* ev)
+{
+ bool add_mrv_selection = false;
+
+ if (_selection.empty()) {
+ add_mrv_selection = true;
+ }
+
+ if (_selection.insert (ev).second) {
+ ev->set_selected (true);
+ play_midi_note ((ev)->note());
+ }
+
+ if (add_mrv_selection) {
+ PublicEditor& editor (trackview.editor());
+ editor.get_selection().add (this);
+ }
+}
+
+void
+MidiRegionView::move_selection(double dx, double dy, double cumulative_dy)
+{
+ typedef vector<boost::shared_ptr<NoteType> > PossibleChord;
+ PossibleChord to_play;
+ Evoral::MusicalTime earliest = Evoral::MaxMusicalTime;
+
+ for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
+ if ((*i)->note()->time() < earliest) {
+ earliest = (*i)->note()->time();
+ }
+ }
+
+ for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
+ if (Evoral::musical_time_equal ((*i)->note()->time(), earliest)) {
+ to_play.push_back ((*i)->note());
+ }
+ (*i)->move_event(dx, dy);
+ }
+
+ if (dy && !_selection.empty() && !_no_sound_notes && trackview.editor().sound_notes()) {
+
+ if (to_play.size() > 1) {
+
+ PossibleChord shifted;
+
+ for (PossibleChord::iterator n = to_play.begin(); n != to_play.end(); ++n) {
+ boost::shared_ptr<NoteType> moved_note (new NoteType (**n));
+ moved_note->set_note (moved_note->note() + cumulative_dy);
+ shifted.push_back (moved_note);
+ }
+
+ play_midi_chord (shifted);
+
+ } else if (!to_play.empty()) {
+
+ boost::shared_ptr<NoteType> moved_note (new NoteType (*to_play.front()));
+ moved_note->set_note (moved_note->note() + cumulative_dy);
+ play_midi_note (moved_note);
+ }
+ }
+}
+
+void
+MidiRegionView::note_dropped(CanvasNoteEvent *, frameoffset_t dt, int8_t dnote)
+{
+ assert (!_selection.empty());
+
+ uint8_t lowest_note_in_selection = 127;
+ uint8_t highest_note_in_selection = 0;
+ uint8_t highest_note_difference = 0;
+
+ // find highest and lowest notes first
+
+ for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
+ uint8_t pitch = (*i)->note()->note();
+ lowest_note_in_selection = std::min(lowest_note_in_selection, pitch);
+ highest_note_in_selection = std::max(highest_note_in_selection, pitch);
+ }
+
+ /*
+ cerr << "dnote: " << (int) dnote << endl;
+ cerr << "lowest note (streamview): " << int(midi_stream_view()->lowest_note())
+ << " highest note (streamview): " << int(midi_stream_view()->highest_note()) << endl;
+ cerr << "lowest note (selection): " << int(lowest_note_in_selection) << " highest note(selection): "
+ << int(highest_note_in_selection) << endl;
+ cerr << "selection size: " << _selection.size() << endl;
+ cerr << "Highest note in selection: " << (int) highest_note_in_selection << endl;
+ */
+
+ // Make sure the note pitch does not exceed the MIDI standard range
+ if (highest_note_in_selection + dnote > 127) {
+ highest_note_difference = highest_note_in_selection - 127;
+ }
+
+ start_note_diff_command (_("move notes"));
+
+ for (Selection::iterator i = _selection.begin(); i != _selection.end() ; ++i) {
+
+ Evoral::MusicalTime new_time = absolute_frames_to_source_beats (source_beats_to_absolute_frames ((*i)->note()->time()) + dt);
+
+ if (new_time < 0) {
+ continue;
+ }
+
+ note_diff_add_change (*i, MidiModel::NoteDiffCommand::StartTime, new_time);
+
+ uint8_t original_pitch = (*i)->note()->note();
+ uint8_t new_pitch = original_pitch + dnote - highest_note_difference;
+
+ // keep notes in standard midi range
+ clamp_to_0_127(new_pitch);
+
+ lowest_note_in_selection = std::min(lowest_note_in_selection, new_pitch);
+ highest_note_in_selection = std::max(highest_note_in_selection, new_pitch);
+
+ note_diff_add_change (*i, MidiModel::NoteDiffCommand::NoteNumber, new_pitch);
+ }
+
+ apply_diff();
+
+ // care about notes being moved beyond the upper/lower bounds on the canvas
+ if (lowest_note_in_selection < midi_stream_view()->lowest_note() ||
+ highest_note_in_selection > midi_stream_view()->highest_note()) {
+ midi_stream_view()->set_note_range(MidiStreamView::ContentsRange);
+ }
+}
+
+/** @param x Pixel relative to the region position.
+ * @return Snapped frame relative to the region position.
+ */
+framepos_t
+MidiRegionView::snap_pixel_to_frame(double x)
+{
+ PublicEditor& editor (trackview.editor());
+ return snap_frame_to_frame (editor.pixel_to_frame (x));
+}
+
+/** @param x Pixel relative to the region position.
+ * @return Snapped pixel relative to the region position.
+ */
+double
+MidiRegionView::snap_to_pixel(double x)
+{
+ return (double) trackview.editor().frame_to_pixel(snap_pixel_to_frame(x));
+}
+
+double
+MidiRegionView::get_position_pixels()
+{
+ framepos_t region_frame = get_position();
+ return trackview.editor().frame_to_pixel(region_frame);
+}
+
+double
+MidiRegionView::get_end_position_pixels()
+{
+ framepos_t frame = get_position() + get_duration ();
+ return trackview.editor().frame_to_pixel(frame);
+}
+
+framepos_t
+MidiRegionView::source_beats_to_absolute_frames(double beats) const
+{
+ /* the time converter will return the frame corresponding to `beats'
+ relative to the start of the source. The start of the source
+ is an implied position given by region->position - region->start
+ */
+ const framepos_t source_start = _region->position() - _region->start();
+ return source_start + _source_relative_time_converter.to (beats);
+}
+
+double
+MidiRegionView::absolute_frames_to_source_beats(framepos_t frames) const
+{
+ /* the `frames' argument needs to be converted into a frame count
+ relative to the start of the source before being passed in to the
+ converter.
+ */
+ const framepos_t source_start = _region->position() - _region->start();
+ return _source_relative_time_converter.from (frames - source_start);
+}
+
+framepos_t
+MidiRegionView::region_beats_to_region_frames(double beats) const
+{
+ return _region_relative_time_converter.to(beats);
+}
+
+double
+MidiRegionView::region_frames_to_region_beats(framepos_t frames) const
+{
+ return _region_relative_time_converter.from(frames);
+}
+
+void
+MidiRegionView::begin_resizing (bool /*at_front*/)
+{
+ _resize_data.clear();
+
+ for (Selection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
+ CanvasNote *note = dynamic_cast<CanvasNote *> (*i);
+
+ // only insert CanvasNotes into the map
+ if (note) {
+ NoteResizeData *resize_data = new NoteResizeData();
+ resize_data->canvas_note = note;
+
+ // create a new SimpleRect from the note which will be the resize preview
+ SimpleRect *resize_rect = new SimpleRect(
+ *_note_group, note->x1(), note->y1(), note->x2(), note->y2());
+
+ // calculate the colors: get the color settings
+ uint32_t fill_color = UINT_RGBA_CHANGE_A(
+ ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get(),
+ 128);
+
+ // make the resize preview notes more transparent and bright
+ fill_color = UINT_INTERPOLATE(fill_color, 0xFFFFFF40, 0.5);
+
+ // calculate color based on note velocity
+ resize_rect->property_fill_color_rgba() = UINT_INTERPOLATE(
+ CanvasNoteEvent::meter_style_fill_color(note->note()->velocity(), note->selected()),
+ fill_color,
+ 0.85);
+
+ resize_rect->property_outline_color_rgba() = CanvasNoteEvent::calculate_outline(
+ ARDOUR_UI::config()->canvasvar_MidiNoteSelected.get());
+
+ resize_data->resize_rect = resize_rect;
+ _resize_data.push_back(resize_data);
+ }