2 * Copyright (C) 2009-2015 David Robillard <d@drobilla.net>
3 * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2014-2016 Robin Gareus <robin@gareus.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <gtkmm/cellrenderercombo.h>
26 #include "evoral/midi_util.h"
27 #include "evoral/Note.h"
29 #include "ardour/beats_samples_converter.h"
30 #include "ardour/midi_model.h"
31 #include "ardour/midi_region.h"
32 #include "ardour/midi_source.h"
33 #include "ardour/session.h"
34 #include "ardour/tempo.h"
36 #include "gtkmm2ext/gui_thread.h"
37 #include "gtkmm2ext/keyboard.h"
38 #include "gtkmm2ext/actions.h"
40 #include "midi_list_editor.h"
41 #include "note_player.h"
42 #include "ui_config.h"
48 using namespace Gtkmm2ext;
50 using namespace ARDOUR;
51 using Timecode::BBT_Time;
53 static map<int,std::string> note_length_map;
56 fill_note_length_map ()
58 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat, _("Whole")));
59 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/2, _("Half")));
60 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/3, _("Triplet")));
61 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/4, _("Quarter")));
62 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/8, _("Eighth")));
63 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/16, _("Sixteenth")));
64 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/32, _("Thirty-second")));
65 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/64, _("Sixty-fourth")));
68 MidiListEditor::MidiListEditor (Session* s, boost::shared_ptr<MidiRegion> r, boost::shared_ptr<MidiTrack> tr)
69 : ArdourWindow (r->name())
74 if (note_length_map.empty()) {
75 fill_note_length_map ();
78 /* We do not handle nested sources/regions. Caller should have tackled this */
80 if (r->max_source_level() > 0) {
81 throw failed_constructor();
90 model = ListStore::create (columns);
91 view.set_model (model);
93 note_length_model = ListStore::create (note_length_columns);
96 for (std::map<int,string>::iterator i = note_length_map.begin(); i != note_length_map.end(); ++i) {
97 row = *(note_length_model->append());
98 row[note_length_columns.ticks] = i->first;
99 row[note_length_columns.name] = i->second;
102 view.signal_key_press_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_press), false);
103 view.signal_key_release_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_release), false);
104 view.signal_scroll_event().connect (sigc::mem_fun (*this, &MidiListEditor::scroll_event), false);
106 view.append_column (_("Start"), columns.start);
107 view.append_column (_("Channel"), columns.channel);
108 view.append_column (_("Num"), columns.note);
109 view.append_column (_("Name"), columns.note_name);
110 view.append_column (_("Vel"), columns.velocity);
112 /* use a combo renderer for length, so that we can offer a selection
113 of pre-defined note lengths. we still allow edited values with
114 arbitrary length (in ticks).
117 Gtk::TreeViewColumn* lenCol = Gtk::manage (new Gtk::TreeViewColumn (_("Length")));
118 Gtk::CellRendererCombo* comboCell = Gtk::manage(new Gtk::CellRendererCombo);
119 lenCol->pack_start(*comboCell);
120 lenCol->add_attribute (comboCell->property_text(), columns.length);
122 comboCell->property_model() = note_length_model;
123 comboCell->property_text_column() = 1;
124 comboCell->property_has_entry() = false;
126 view.append_column (*lenCol);
128 view.set_headers_visible (true);
129 view.set_rules_hint (true);
130 view.get_selection()->set_mode (SELECTION_MULTIPLE);
131 view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &MidiListEditor::selection_changed));
133 for (int i = 0; i < 6; ++i) {
134 CellRendererText* renderer = dynamic_cast<CellRendererText*>(view.get_column_cell_renderer (i));
136 TreeViewColumn* col = view.get_column (i);
137 col->set_data (X_("colnum"), GUINT_TO_POINTER(i));
139 renderer->property_editable() = true;
141 renderer->signal_editing_started().connect (sigc::bind (sigc::mem_fun (*this, &MidiListEditor::editing_started), i));
142 renderer->signal_editing_canceled().connect (sigc::mem_fun (*this, &MidiListEditor::editing_canceled));
143 renderer->signal_edited().connect (sigc::mem_fun (*this, &MidiListEditor::edited));
147 scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
151 region->midi_source(0)->model()->ContentsChanged.connect (content_connection, invalidator (*this),
152 boost::bind (&MidiListEditor::redisplay_model, this), gui_context());
154 buttons.attach (sound_notes_button, 0, 1, 0, 1);
155 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action ("Editor", "sound-midi-notes");
157 gtk_activatable_set_related_action (GTK_ACTIVATABLE (sound_notes_button.gobj()), act->gobj());
164 sound_notes_button.show ();
166 vbox.set_spacing (6);
167 vbox.set_border_width (6);
168 vbox.pack_start (buttons, false, false);
169 vbox.pack_start (scroller, true, true);
172 set_size_request (-1, 400);
175 MidiListEditor::~MidiListEditor ()
180 MidiListEditor::scroll_event (GdkEventScroll* ev)
182 TreeModel::Path path;
188 MidiModel::NoteDiffCommand::Property prop (MidiModel::NoteDiffCommand::NoteNumber);
190 bool was_selected = false;
193 if (!view.get_path_at_pos (ev->x, ev->y, path, col, cellx, celly)) {
197 if (view.get_selection()->count_selected_rows() == 0) {
198 was_selected = false;
199 } else if (view.get_selection()->is_selected (path)) {
202 was_selected = false;
205 int colnum = GPOINTER_TO_UINT (col->get_data (X_("colnum")));
209 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
214 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
217 prop = MidiModel::NoteDiffCommand::StartTime;
218 opname = _("edit note start");
223 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
226 prop = MidiModel::NoteDiffCommand::Channel;
227 opname = _("edit note channel");
233 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
236 prop = MidiModel::NoteDiffCommand::NoteNumber;
237 opname = _("edit note number");
243 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
246 prop = MidiModel::NoteDiffCommand::Velocity;
247 opname = _("edit note velocity");
252 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
257 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
260 prop = MidiModel::NoteDiffCommand::Length;
261 opname = _("edit note length");
272 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
273 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (opname);
274 vector<TreeModel::Path> previous_selection;
280 TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
281 TreeModel::iterator iter;
282 boost::shared_ptr<NoteType> note;
284 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
286 previous_selection.push_back (*i);
288 if ((iter = model->get_iter (*i))) {
290 note = (*iter)[columns._note];
293 case MidiModel::NoteDiffCommand::StartTime:
294 if (note->time() + fdelta >= 0) {
295 cmd->change (note, prop, note->time() + fdelta);
297 cmd->change (note, prop, Temporal::Beats());
300 case MidiModel::NoteDiffCommand::Velocity:
301 cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
303 case MidiModel::NoteDiffCommand::Length:
304 if (note->length().to_double() + fdelta >=
305 Temporal::Beats::tick().to_double()) {
306 cmd->change (note, prop, note->length() + fdelta);
308 cmd->change (note, prop, Temporal::Beats::tick());
311 case MidiModel::NoteDiffCommand::Channel:
312 cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
314 case MidiModel::NoteDiffCommand::NoteNumber:
315 cmd->change (note, prop, (uint8_t) (note->note() + idelta));
327 TreeModel::iterator iter;
328 iter = model->get_iter (path);
330 previous_selection.push_back (path);
333 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
336 case MidiModel::NoteDiffCommand::StartTime:
337 if (note->time() + fdelta >= 0) {
338 cmd->change (note, prop, note->time() + fdelta);
340 cmd->change (note, prop, Temporal::Beats());
343 case MidiModel::NoteDiffCommand::Velocity:
344 cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
346 case MidiModel::NoteDiffCommand::Length:
347 if (note->length() + fdelta >=
348 Temporal::Beats::tick().to_double()) {
349 cmd->change (note, prop, note->length() + fdelta);
351 cmd->change (note, prop, Temporal::Beats::tick());
354 case MidiModel::NoteDiffCommand::Channel:
355 cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
357 case MidiModel::NoteDiffCommand::NoteNumber:
358 cmd->change (note, prop, (uint8_t) (note->note() + idelta));
366 m->apply_command (*_session, cmd);
368 /* reset selection to be as it was before we rebuilt */
370 for (vector<TreeModel::Path>::iterator i = previous_selection.begin(); i != previous_selection.end(); ++i) {
371 view.get_selection()->select (*i);
379 MidiListEditor::key_press (GdkEventKey* ev)
382 TreeModel::Path path;
386 switch (ev->keyval) {
388 if (edit_column > 0) {
389 colnum = edit_column;
391 if (editing_editable) {
392 editing_editable->editing_done ();
395 /* wrap to next line */
401 col = view.get_column (colnum);
402 view.set_cursor (path, *col, true);
409 if (edit_column > 0) {
410 colnum = edit_column;
412 if (editing_editable) {
413 editing_editable->editing_done ();
416 col = view.get_column (colnum);
417 view.set_cursor (path, *col, true);
424 if (edit_column > 0) {
425 colnum = edit_column;
427 if (editing_editable) {
428 editing_editable->editing_done ();
431 col = view.get_column (colnum);
432 view.set_cursor (path, *col, true);
447 MidiListEditor::key_release (GdkEventKey* ev)
450 TreeModel::Path path;
452 TreeModel::iterator iter;
453 MidiModel::NoteDiffCommand* cmd;
454 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
455 boost::shared_ptr<NoteType> note;
456 boost::shared_ptr<NoteType> copy;
458 switch (ev->keyval) {
460 /* add a new note to the model, based on the note at the cursor
463 view.get_cursor (path, col);
464 iter = model->get_iter (path);
465 cmd = m->new_note_diff_command (_("insert new note"));
466 note = (*iter)[columns._note];
467 copy.reset (new NoteType (*note.get()));
469 m->apply_command (*_session, cmd);
470 /* model has been redisplayed by now */
472 /* select, start editing column 2 (note) */
473 col = view.get_column (2);
474 view.set_cursor (path, *col, true);
479 if (edit_column < 0) {
480 delete_selected_note ();
486 if (_session && Gtkmm2ext::Keyboard::modifier_state_contains (ev->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
493 if (_session && Gtkmm2ext::Keyboard::modifier_state_contains (ev->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
507 MidiListEditor::delete_selected_note ()
509 Glib::RefPtr<TreeSelection> selection = view.get_selection();
510 TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
516 typedef vector<boost::shared_ptr<NoteType> > Notes;
519 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
522 if ((iter = model->get_iter (*i))) {
523 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
524 to_delete.push_back (note);
528 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
529 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (_("delete notes (from list)"));
531 for (Notes::iterator i = to_delete.begin(); i != to_delete.end(); ++i) {
535 m->apply_command (*_session, cmd);
539 MidiListEditor::stop_editing (bool cancelled)
542 if (editing_editable) {
543 editing_editable->editing_done ();
546 if (editing_renderer) {
547 editing_renderer->stop_editing (cancelled);
553 MidiListEditor::editing_started (CellEditable* ed, const string& path, int colno)
555 edit_path = TreePath (path);
557 editing_renderer = dynamic_cast<CellRendererText*>(view.get_column_cell_renderer (colno));
558 editing_editable = ed;
561 Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ed);
563 e->signal_key_press_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_press), false);
564 e->signal_key_release_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_release), false);
570 MidiListEditor::editing_canceled ()
574 editing_renderer = 0;
575 editing_editable = 0;
579 MidiListEditor::edited (const std::string& path, const std::string& text)
581 TreeModel::iterator iter = model->get_iter (path);
583 if (!iter || text.empty()) {
587 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
588 MidiModel::NoteDiffCommand::Property prop (MidiModel::NoteDiffCommand::NoteNumber);
596 switch (edit_column) {
600 // correct ival for zero-based counting after scan
601 if (sscanf (text.c_str(), "%d", &ival) == 1 && --ival != note->channel()) {
602 idelta = ival - note->channel();
603 prop = MidiModel::NoteDiffCommand::Channel;
604 opname = _("change note channel");
609 if (sscanf (text.c_str(), "%d", &ival) == 1 && ival != note->note()) {
610 idelta = ival - note->note();
611 prop = MidiModel::NoteDiffCommand::NoteNumber;
612 opname = _("change note number");
617 ival = ParameterDescriptor::midi_note_num (text);
619 idelta = ival - note->note();
620 prop = MidiModel::NoteDiffCommand::NoteNumber;
621 opname = _("change note number");
626 if (sscanf (text.c_str(), "%d", &ival) == 1 && ival != note->velocity()) {
627 idelta = ival - note->velocity();
628 prop = MidiModel::NoteDiffCommand::Velocity;
629 opname = _("change note velocity");
635 if (sscanf (text.c_str(), "%lf", &fval) == 1) {
637 /* numeric value entered */
639 if (text.find ('.') == string::npos && text.find (',') == string::npos) {
640 /* integral => units are ticks */
641 fval = fval / BBT_Time::ticks_per_beat;
643 /* non-integral => beats, so use as-is */
648 /* assume its text from the combo. look for the map
649 * entry for the actual note ticks
652 uint64_t len_ticks = note->length().to_ticks();
653 std::map<int,string>::iterator x = note_length_map.find (len_ticks);
655 if (x == note_length_map.end()) {
657 /* tick length not in map - was
658 * displaying numeric value ... use new value
659 * from note length map, and convert to beats.
662 for (x = note_length_map.begin(); x != note_length_map.end(); ++x) {
663 if (x->second == text) {
668 if (x != note_length_map.end()) {
669 fval = x->first / BBT_Time::ticks_per_beat;
676 if (text != x->second) {
678 /* get ticks for the newly selected
682 for (x = note_length_map.begin(); x != note_length_map.end(); ++x) {
683 if (x->second == text) {
688 if (x != note_length_map.end()) {
689 /* convert to beats */
690 fval = (double) x->first / BBT_Time::ticks_per_beat;
697 fdelta = fval - note->length().to_double();
698 prop = MidiModel::NoteDiffCommand::Length;
699 opname = _("change note length");
710 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
711 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (opname);
713 TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
715 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
716 if ((iter = model->get_iter (*i))) {
718 note = (*iter)[columns._note];
721 case MidiModel::NoteDiffCommand::Velocity:
722 cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
724 case MidiModel::NoteDiffCommand::Length:
725 cmd->change (note, prop, note->length() + fdelta);
727 case MidiModel::NoteDiffCommand::Channel:
728 cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
730 case MidiModel::NoteDiffCommand::NoteNumber:
731 cmd->change (note, prop, (uint8_t) (note->note() + idelta));
739 m->apply_command (*_session, cmd);
741 /* model has been redisplayed by now */
742 /* keep selected row(s), move cursor there, don't continue editing */
744 TreeViewColumn* col = view.get_column (edit_column);
745 view.set_cursor (edit_path, *col, 0);
747 /* reset edit info, since we're done */
751 editing_renderer = 0;
752 editing_editable = 0;
757 MidiListEditor::redisplay_model ()
759 view.set_model (Glib::RefPtr<Gtk::ListStore>(0));
764 BeatsSamplesConverter conv (_session->tempo_map(), region->position());
765 MidiModel::Notes notes = region->midi_source(0)->model()->notes();
769 for (MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
770 row = *(model->append());
771 row[columns.channel] = (*i)->channel() + 1;
772 row[columns.note_name] = ParameterDescriptor::midi_note_name ((*i)->note());
773 row[columns.note] = (*i)->note();
774 row[columns.velocity] = (*i)->velocity();
776 Timecode::BBT_Time bbt (_session->tempo_map().bbt_at_sample (region->position() + conv.to ((*i)->time())));
780 row[columns.start] = ss.str();
783 const Temporal::Beats dur = (*i)->end_time() - (*i)->time();
784 bbt.beats = dur.get_beats ();
785 bbt.ticks = dur.get_ticks ();
787 uint64_t len_ticks = (*i)->length().to_ticks();
788 std::map<int,string>::iterator x = note_length_map.find (len_ticks);
790 if (x != note_length_map.end()) {
791 row[columns.length] = x->second;
795 row[columns.length] = ss.str();
798 row[columns._note] = (*i);
802 view.set_model (model);
806 MidiListEditor::selection_changed ()
808 if (!UIConfiguration::instance().get_sound_midi_notes()) {
812 TreeModel::Path path;
813 TreeModel::iterator iter;
814 boost::shared_ptr<NoteType> note;
815 TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
817 NotePlayer* player = new NotePlayer (track);
819 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
820 if ((iter = model->get_iter (*i))) {
821 note = (*iter)[columns._note];