2 Copyright (C) 2009 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <gtkmm/cellrenderercombo.h>
24 #include "evoral/midi_util.h"
25 #include "evoral/Note.hpp"
27 #include "ardour/beats_frames_converter.h"
28 #include "ardour/midi_model.h"
29 #include "ardour/midi_region.h"
30 #include "ardour/midi_source.h"
31 #include "ardour/session.h"
32 #include "ardour/tempo.h"
34 #include "gtkmm2ext/gui_thread.h"
35 #include "gtkmm2ext/keyboard.h"
36 #include "gtkmm2ext/actions.h"
38 #include "midi_list_editor.h"
39 #include "note_player.h"
40 #include "ui_config.h"
46 using namespace Gtkmm2ext;
48 using namespace ARDOUR;
49 using Timecode::BBT_Time;
51 static map<int,std::string> note_length_map;
54 fill_note_length_map ()
56 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat, _("Whole")));
57 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/2, _("Half")));
58 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/3, _("Triplet")));
59 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/4, _("Quarter")));
60 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/8, _("Eighth")));
61 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/16, _("Sixteenth")));
62 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/32, _("Thirty-second")));
63 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/64, _("Sixty-fourth")));
66 MidiListEditor::MidiListEditor (Session* s, boost::shared_ptr<MidiRegion> r, boost::shared_ptr<MidiTrack> tr)
67 : ArdourWindow (r->name())
72 if (note_length_map.empty()) {
73 fill_note_length_map ();
76 /* We do not handle nested sources/regions. Caller should have tackled this */
78 if (r->max_source_level() > 0) {
79 throw failed_constructor();
88 model = ListStore::create (columns);
89 view.set_model (model);
91 note_length_model = ListStore::create (note_length_columns);
94 for (std::map<int,string>::iterator i = note_length_map.begin(); i != note_length_map.end(); ++i) {
95 row = *(note_length_model->append());
96 row[note_length_columns.ticks] = i->first;
97 row[note_length_columns.name] = i->second;
100 view.signal_key_press_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_press), false);
101 view.signal_key_release_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_release), false);
102 view.signal_scroll_event().connect (sigc::mem_fun (*this, &MidiListEditor::scroll_event), false);
104 view.append_column (_("Start"), columns.start);
105 view.append_column (_("Channel"), columns.channel);
106 view.append_column (_("Num"), columns.note);
107 view.append_column (_("Name"), columns.note_name);
108 view.append_column (_("Vel"), columns.velocity);
110 /* use a combo renderer for length, so that we can offer a selection
111 of pre-defined note lengths. we still allow edited values with
112 arbitrary length (in ticks).
115 Gtk::TreeViewColumn* lenCol = Gtk::manage (new Gtk::TreeViewColumn (_("Length")));
116 Gtk::CellRendererCombo* comboCell = Gtk::manage(new Gtk::CellRendererCombo);
117 lenCol->pack_start(*comboCell);
118 lenCol->add_attribute (comboCell->property_text(), columns.length);
120 comboCell->property_model() = note_length_model;
121 comboCell->property_text_column() = 1;
122 comboCell->property_has_entry() = false;
124 view.append_column (*lenCol);
126 view.set_headers_visible (true);
127 view.set_rules_hint (true);
128 view.get_selection()->set_mode (SELECTION_MULTIPLE);
129 view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &MidiListEditor::selection_changed));
131 for (int i = 0; i < 6; ++i) {
132 CellRendererText* renderer = dynamic_cast<CellRendererText*>(view.get_column_cell_renderer (i));
134 TreeViewColumn* col = view.get_column (i);
135 col->set_data (X_("colnum"), GUINT_TO_POINTER(i));
137 renderer->property_editable() = true;
139 renderer->signal_editing_started().connect (sigc::bind (sigc::mem_fun (*this, &MidiListEditor::editing_started), i));
140 renderer->signal_editing_canceled().connect (sigc::mem_fun (*this, &MidiListEditor::editing_canceled));
141 renderer->signal_edited().connect (sigc::mem_fun (*this, &MidiListEditor::edited));
145 scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
149 region->midi_source(0)->model()->ContentsChanged.connect (content_connection, invalidator (*this),
150 boost::bind (&MidiListEditor::redisplay_model, this), gui_context());
152 buttons.attach (sound_notes_button, 0, 1, 0, 1);
153 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action ("Editor", "sound-midi-notes");
155 gtk_activatable_set_related_action (GTK_ACTIVATABLE (sound_notes_button.gobj()), act->gobj());
162 sound_notes_button.show ();
164 vbox.set_spacing (6);
165 vbox.set_border_width (6);
166 vbox.pack_start (buttons, false, false);
167 vbox.pack_start (scroller, true, true);
170 set_size_request (-1, 400);
173 MidiListEditor::~MidiListEditor ()
178 MidiListEditor::scroll_event (GdkEventScroll* ev)
180 TreeModel::Path path;
186 MidiModel::NoteDiffCommand::Property prop (MidiModel::NoteDiffCommand::NoteNumber);
188 bool was_selected = false;
191 if (!view.get_path_at_pos (ev->x, ev->y, path, col, cellx, celly)) {
195 if (view.get_selection()->count_selected_rows() == 0) {
196 was_selected = false;
197 } else if (view.get_selection()->is_selected (path)) {
200 was_selected = false;
203 int colnum = GPOINTER_TO_UINT (col->get_data (X_("colnum")));
207 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
212 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
215 prop = MidiModel::NoteDiffCommand::StartTime;
216 opname = _("edit note start");
221 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
224 prop = MidiModel::NoteDiffCommand::Channel;
225 opname = _("edit note channel");
231 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
234 prop = MidiModel::NoteDiffCommand::NoteNumber;
235 opname = _("edit note number");
241 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
244 prop = MidiModel::NoteDiffCommand::Velocity;
245 opname = _("edit note velocity");
250 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
255 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
258 prop = MidiModel::NoteDiffCommand::Length;
259 opname = _("edit note length");
270 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
271 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (opname);
272 vector<TreeModel::Path> previous_selection;
278 TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
279 TreeModel::iterator iter;
280 boost::shared_ptr<NoteType> note;
282 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
284 previous_selection.push_back (*i);
286 if ((iter = model->get_iter (*i))) {
288 note = (*iter)[columns._note];
291 case MidiModel::NoteDiffCommand::StartTime:
292 if (note->time() + fdelta >= 0) {
293 cmd->change (note, prop, note->time() + fdelta);
295 cmd->change (note, prop, Evoral::Beats());
298 case MidiModel::NoteDiffCommand::Velocity:
299 cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
301 case MidiModel::NoteDiffCommand::Length:
302 if (note->length().to_double() + fdelta >=
303 Evoral::Beats::tick().to_double()) {
304 cmd->change (note, prop, note->length() + fdelta);
306 cmd->change (note, prop, Evoral::Beats::tick());
309 case MidiModel::NoteDiffCommand::Channel:
310 cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
312 case MidiModel::NoteDiffCommand::NoteNumber:
313 cmd->change (note, prop, (uint8_t) (note->note() + idelta));
325 TreeModel::iterator iter;
326 iter = model->get_iter (path);
328 previous_selection.push_back (path);
331 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
334 case MidiModel::NoteDiffCommand::StartTime:
335 if (note->time() + fdelta >= 0) {
336 cmd->change (note, prop, note->time() + fdelta);
338 cmd->change (note, prop, Evoral::Beats());
341 case MidiModel::NoteDiffCommand::Velocity:
342 cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
344 case MidiModel::NoteDiffCommand::Length:
345 if (note->length() + fdelta >=
346 Evoral::Beats::tick().to_double()) {
347 cmd->change (note, prop, note->length() + fdelta);
349 cmd->change (note, prop, Evoral::Beats::tick());
352 case MidiModel::NoteDiffCommand::Channel:
353 cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
355 case MidiModel::NoteDiffCommand::NoteNumber:
356 cmd->change (note, prop, (uint8_t) (note->note() + idelta));
364 m->apply_command (*_session, cmd);
366 /* reset selection to be as it was before we rebuilt */
368 for (vector<TreeModel::Path>::iterator i = previous_selection.begin(); i != previous_selection.end(); ++i) {
369 view.get_selection()->select (*i);
377 MidiListEditor::key_press (GdkEventKey* ev)
380 TreeModel::Path path;
384 switch (ev->keyval) {
386 if (edit_column > 0) {
387 colnum = edit_column;
389 if (editing_editable) {
390 editing_editable->editing_done ();
393 /* wrap to next line */
399 col = view.get_column (colnum);
400 view.set_cursor (path, *col, true);
407 if (edit_column > 0) {
408 colnum = edit_column;
410 if (editing_editable) {
411 editing_editable->editing_done ();
414 col = view.get_column (colnum);
415 view.set_cursor (path, *col, true);
422 if (edit_column > 0) {
423 colnum = edit_column;
425 if (editing_editable) {
426 editing_editable->editing_done ();
429 col = view.get_column (colnum);
430 view.set_cursor (path, *col, true);
445 MidiListEditor::key_release (GdkEventKey* ev)
448 TreeModel::Path path;
450 TreeModel::iterator iter;
451 MidiModel::NoteDiffCommand* cmd;
452 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
453 boost::shared_ptr<NoteType> note;
454 boost::shared_ptr<NoteType> copy;
456 switch (ev->keyval) {
458 /* add a new note to the model, based on the note at the cursor
461 view.get_cursor (path, col);
462 iter = model->get_iter (path);
463 cmd = m->new_note_diff_command (_("insert new note"));
464 note = (*iter)[columns._note];
465 copy.reset (new NoteType (*note.get()));
467 m->apply_command (*_session, cmd);
468 /* model has been redisplayed by now */
470 /* select, start editing column 2 (note) */
471 col = view.get_column (2);
472 view.set_cursor (path, *col, true);
477 if (edit_column < 0) {
478 delete_selected_note ();
484 if (_session && Gtkmm2ext::Keyboard::modifier_state_contains (ev->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
491 if (_session && Gtkmm2ext::Keyboard::modifier_state_contains (ev->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
505 MidiListEditor::delete_selected_note ()
507 Glib::RefPtr<TreeSelection> selection = view.get_selection();
508 TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
514 typedef vector<boost::shared_ptr<NoteType> > Notes;
517 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
520 if ((iter = model->get_iter (*i))) {
521 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
522 to_delete.push_back (note);
526 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
527 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (_("delete notes (from list)"));
529 for (Notes::iterator i = to_delete.begin(); i != to_delete.end(); ++i) {
533 m->apply_command (*_session, cmd);
537 MidiListEditor::stop_editing (bool cancelled)
540 if (editing_editable) {
541 editing_editable->editing_done ();
544 if (editing_renderer) {
545 editing_renderer->stop_editing (cancelled);
551 MidiListEditor::editing_started (CellEditable* ed, const string& path, int colno)
553 edit_path = TreePath (path);
555 editing_renderer = dynamic_cast<CellRendererText*>(view.get_column_cell_renderer (colno));
556 editing_editable = ed;
559 Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ed);
561 e->signal_key_press_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_press), false);
562 e->signal_key_release_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_release), false);
568 MidiListEditor::editing_canceled ()
572 editing_renderer = 0;
573 editing_editable = 0;
577 MidiListEditor::edited (const std::string& path, const std::string& text)
579 TreeModel::iterator iter = model->get_iter (path);
581 if (!iter || text.empty()) {
585 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
586 MidiModel::NoteDiffCommand::Property prop (MidiModel::NoteDiffCommand::NoteNumber);
594 switch (edit_column) {
598 // correct ival for zero-based counting after scan
599 if (sscanf (text.c_str(), "%d", &ival) == 1 && --ival != note->channel()) {
600 idelta = ival - note->channel();
601 prop = MidiModel::NoteDiffCommand::Channel;
602 opname = _("change note channel");
607 if (sscanf (text.c_str(), "%d", &ival) == 1 && ival != note->note()) {
608 idelta = ival - note->note();
609 prop = MidiModel::NoteDiffCommand::NoteNumber;
610 opname = _("change note number");
617 if (sscanf (text.c_str(), "%d", &ival) == 1 && ival != note->velocity()) {
618 idelta = ival - note->velocity();
619 prop = MidiModel::NoteDiffCommand::Velocity;
620 opname = _("change note velocity");
626 if (sscanf (text.c_str(), "%lf", &fval) == 1) {
628 /* numeric value entered */
630 if (text.find ('.') == string::npos && text.find (',') == string::npos) {
631 /* integral => units are ticks */
632 fval = fval / BBT_Time::ticks_per_beat;
634 /* non-integral => beats, so use as-is */
639 /* assume its text from the combo. look for the map
640 * entry for the actual note ticks
643 uint64_t len_ticks = note->length().to_ticks();
644 std::map<int,string>::iterator x = note_length_map.find (len_ticks);
646 if (x == note_length_map.end()) {
648 /* tick length not in map - was
649 * displaying numeric value ... use new value
650 * from note length map, and convert to beats.
653 for (x = note_length_map.begin(); x != note_length_map.end(); ++x) {
654 if (x->second == text) {
659 if (x != note_length_map.end()) {
660 fval = x->first / BBT_Time::ticks_per_beat;
667 if (text != x->second) {
669 /* get ticks for the newly selected
673 for (x = note_length_map.begin(); x != note_length_map.end(); ++x) {
674 if (x->second == text) {
679 if (x != note_length_map.end()) {
680 /* convert to beats */
681 fval = (double) x->first / BBT_Time::ticks_per_beat;
688 fdelta = fval - note->length().to_double();
689 prop = MidiModel::NoteDiffCommand::Length;
690 opname = _("change note length");
701 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
702 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (opname);
704 TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
706 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
707 if ((iter = model->get_iter (*i))) {
709 note = (*iter)[columns._note];
712 case MidiModel::NoteDiffCommand::Velocity:
713 cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
715 case MidiModel::NoteDiffCommand::Length:
716 cmd->change (note, prop, note->length() + fdelta);
718 case MidiModel::NoteDiffCommand::Channel:
719 cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
721 case MidiModel::NoteDiffCommand::NoteNumber:
722 cmd->change (note, prop, (uint8_t) (note->note() + idelta));
730 m->apply_command (*_session, cmd);
732 /* model has been redisplayed by now */
733 /* keep selected row(s), move cursor there, don't continue editing */
735 TreeViewColumn* col = view.get_column (edit_column);
736 view.set_cursor (edit_path, *col, 0);
738 /* reset edit info, since we're done */
742 editing_renderer = 0;
743 editing_editable = 0;
748 MidiListEditor::redisplay_model ()
750 view.set_model (Glib::RefPtr<Gtk::ListStore>(0));
755 BeatsFramesConverter conv (_session->tempo_map(), region->position());
756 MidiModel::Notes notes = region->midi_source(0)->model()->notes();
760 for (MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
761 row = *(model->append());
762 row[columns.channel] = (*i)->channel() + 1;
763 row[columns.note_name] = Evoral::midi_note_name ((*i)->note());
764 row[columns.note] = (*i)->note();
765 row[columns.velocity] = (*i)->velocity();
767 Timecode::BBT_Time bbt;
769 _session->tempo_map().bbt_time (conv.to ((*i)->time()), bbt);
773 row[columns.start] = ss.str();
776 const Evoral::Beats dur = (*i)->end_time() - (*i)->time();
777 bbt.beats = dur.get_beats ();
778 bbt.ticks = dur.get_ticks ();
780 uint64_t len_ticks = (*i)->length().to_ticks();
781 std::map<int,string>::iterator x = note_length_map.find (len_ticks);
783 if (x != note_length_map.end()) {
784 row[columns.length] = x->second;
788 row[columns.length] = ss.str();
791 row[columns._note] = (*i);
795 view.set_model (model);
799 MidiListEditor::selection_changed ()
801 if (!UIConfiguration::instance().get_sound_midi_notes()) {
805 TreeModel::Path path;
806 TreeModel::iterator iter;
807 boost::shared_ptr<NoteType> note;
808 TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
810 NotePlayer* player = new NotePlayer (track);
812 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
813 if ((iter = model->get_iter (*i))) {
814 note = (*iter)[columns._note];