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"
45 using namespace Gtkmm2ext;
47 using namespace ARDOUR;
48 using Timecode::BBT_Time;
50 static map<int,std::string> note_length_map;
53 fill_note_length_map ()
55 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat, _("Whole")));
56 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/2, _("Half")));
57 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/3, _("Triplet")));
58 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/4, _("Quarter")));
59 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/8, _("Eighth")));
60 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/16, _("Sixteenth")));
61 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/32, _("Thirty-second")));
62 note_length_map.insert (make_pair<int,string> (BBT_Time::ticks_per_beat/64, _("Sixty-fourth")));
65 MidiListEditor::MidiListEditor (Session* s, boost::shared_ptr<MidiRegion> r, boost::shared_ptr<MidiTrack> tr)
66 : ArdourWindow (r->name())
71 if (note_length_map.empty()) {
72 fill_note_length_map ();
75 /* We do not handle nested sources/regions. Caller should have tackled this */
77 if (r->max_source_level() > 0) {
78 throw failed_constructor();
87 model = ListStore::create (columns);
88 view.set_model (model);
90 note_length_model = ListStore::create (note_length_columns);
93 for (std::map<int,string>::iterator i = note_length_map.begin(); i != note_length_map.end(); ++i) {
94 row = *(note_length_model->append());
95 row[note_length_columns.ticks] = i->first;
96 row[note_length_columns.name] = i->second;
99 view.signal_key_press_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_press), false);
100 view.signal_key_release_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_release), false);
101 view.signal_scroll_event().connect (sigc::mem_fun (*this, &MidiListEditor::scroll_event), false);
103 view.append_column (_("Start"), columns.start);
104 view.append_column (_("Channel"), columns.channel);
105 view.append_column (_("Num"), columns.note);
106 view.append_column (_("Name"), columns.note_name);
107 view.append_column (_("Vel"), columns.velocity);
109 /* use a combo renderer for length, so that we can offer a selection
110 of pre-defined note lengths. we still allow edited values with
111 arbitrary length (in ticks).
114 Gtk::TreeViewColumn* lenCol = Gtk::manage (new Gtk::TreeViewColumn (_("Length")));
115 Gtk::CellRendererCombo* comboCell = Gtk::manage(new Gtk::CellRendererCombo);
116 lenCol->pack_start(*comboCell);
117 lenCol->add_attribute (comboCell->property_text(), columns.length);
119 comboCell->property_model() = note_length_model;
120 comboCell->property_text_column() = 1;
121 comboCell->property_has_entry() = false;
123 view.append_column (*lenCol);
125 view.set_headers_visible (true);
126 view.set_rules_hint (true);
127 view.get_selection()->set_mode (SELECTION_MULTIPLE);
128 view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &MidiListEditor::selection_changed));
130 for (int i = 0; i < 6; ++i) {
131 CellRendererText* renderer = dynamic_cast<CellRendererText*>(view.get_column_cell_renderer (i));
133 TreeViewColumn* col = view.get_column (i);
134 col->set_data (X_("colnum"), GUINT_TO_POINTER(i));
136 renderer->property_editable() = true;
138 renderer->signal_editing_started().connect (sigc::bind (sigc::mem_fun (*this, &MidiListEditor::editing_started), i));
139 renderer->signal_editing_canceled().connect (sigc::mem_fun (*this, &MidiListEditor::editing_canceled));
140 renderer->signal_edited().connect (sigc::mem_fun (*this, &MidiListEditor::edited));
144 scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
148 region->midi_source(0)->model()->ContentsChanged.connect (content_connection, invalidator (*this),
149 boost::bind (&MidiListEditor::redisplay_model, this), gui_context());
151 buttons.attach (sound_notes_button, 0, 1, 0, 1);
152 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action ("Editor", "sound-midi-notes");
154 gtk_activatable_set_related_action (GTK_ACTIVATABLE (sound_notes_button.gobj()), act->gobj());
161 sound_notes_button.show ();
163 vbox.set_spacing (6);
164 vbox.set_border_width (6);
165 vbox.pack_start (buttons, false, false);
166 vbox.pack_start (scroller, true, true);
169 set_size_request (-1, 400);
172 MidiListEditor::~MidiListEditor ()
177 MidiListEditor::scroll_event (GdkEventScroll* ev)
179 TreeModel::Path path;
185 MidiModel::NoteDiffCommand::Property prop (MidiModel::NoteDiffCommand::NoteNumber);
187 bool was_selected = false;
190 if (!view.get_path_at_pos (ev->x, ev->y, path, col, cellx, celly)) {
194 if (view.get_selection()->count_selected_rows() == 0) {
195 was_selected = false;
196 } else if (view.get_selection()->is_selected (path)) {
199 was_selected = false;
202 int colnum = GPOINTER_TO_UINT (col->get_data (X_("colnum")));
206 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
211 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
214 prop = MidiModel::NoteDiffCommand::StartTime;
215 opname = _("edit note start");
220 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
223 prop = MidiModel::NoteDiffCommand::Channel;
224 opname = _("edit note channel");
230 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
233 prop = MidiModel::NoteDiffCommand::NoteNumber;
234 opname = _("edit note number");
240 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
243 prop = MidiModel::NoteDiffCommand::Velocity;
244 opname = _("edit note velocity");
249 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
254 if (ev->direction == GDK_SCROLL_DOWN || ev->direction == GDK_SCROLL_LEFT) {
257 prop = MidiModel::NoteDiffCommand::Length;
258 opname = _("edit note length");
269 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
270 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (opname);
271 vector<TreeModel::Path> previous_selection;
277 TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
278 TreeModel::iterator iter;
279 boost::shared_ptr<NoteType> note;
281 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
283 previous_selection.push_back (*i);
285 if ((iter = model->get_iter (*i))) {
287 note = (*iter)[columns._note];
290 case MidiModel::NoteDiffCommand::StartTime:
291 if (note->time() + fdelta >= 0) {
292 cmd->change (note, prop, note->time() + fdelta);
294 cmd->change (note, prop, Evoral::MusicalTime());
297 case MidiModel::NoteDiffCommand::Velocity:
298 cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
300 case MidiModel::NoteDiffCommand::Length:
301 if (note->length().to_double() + fdelta >=
302 Evoral::MusicalTime::tick().to_double()) {
303 cmd->change (note, prop, note->length() + fdelta);
305 cmd->change (note, prop, Evoral::MusicalTime::tick());
308 case MidiModel::NoteDiffCommand::Channel:
309 cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
311 case MidiModel::NoteDiffCommand::NoteNumber:
312 cmd->change (note, prop, (uint8_t) (note->note() + idelta));
324 TreeModel::iterator iter;
325 iter = model->get_iter (path);
327 previous_selection.push_back (path);
330 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
333 case MidiModel::NoteDiffCommand::StartTime:
334 if (note->time() + fdelta >= 0) {
335 cmd->change (note, prop, note->time() + fdelta);
337 cmd->change (note, prop, Evoral::MusicalTime());
340 case MidiModel::NoteDiffCommand::Velocity:
341 cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
343 case MidiModel::NoteDiffCommand::Length:
344 if (note->length() + fdelta >=
345 Evoral::MusicalTime::tick().to_double()) {
346 cmd->change (note, prop, note->length() + fdelta);
348 cmd->change (note, prop, Evoral::MusicalTime::tick());
351 case MidiModel::NoteDiffCommand::Channel:
352 cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
354 case MidiModel::NoteDiffCommand::NoteNumber:
355 cmd->change (note, prop, (uint8_t) (note->note() + idelta));
363 m->apply_command (*_session, cmd);
365 /* reset selection to be as it was before we rebuilt */
367 for (vector<TreeModel::Path>::iterator i = previous_selection.begin(); i != previous_selection.end(); ++i) {
368 view.get_selection()->select (*i);
376 MidiListEditor::key_press (GdkEventKey* ev)
379 TreeModel::Path path;
383 switch (ev->keyval) {
385 if (edit_column > 0) {
386 colnum = edit_column;
388 if (editing_editable) {
389 editing_editable->editing_done ();
392 /* wrap to next line */
398 col = view.get_column (colnum);
399 view.set_cursor (path, *col, true);
406 if (edit_column > 0) {
407 colnum = edit_column;
409 if (editing_editable) {
410 editing_editable->editing_done ();
413 col = view.get_column (colnum);
414 view.set_cursor (path, *col, true);
421 if (edit_column > 0) {
422 colnum = edit_column;
424 if (editing_editable) {
425 editing_editable->editing_done ();
428 col = view.get_column (colnum);
429 view.set_cursor (path, *col, true);
444 MidiListEditor::key_release (GdkEventKey* ev)
447 TreeModel::Path path;
449 TreeModel::iterator iter;
450 MidiModel::NoteDiffCommand* cmd;
451 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
452 boost::shared_ptr<NoteType> note;
453 boost::shared_ptr<NoteType> copy;
455 switch (ev->keyval) {
457 /* add a new note to the model, based on the note at the cursor
460 view.get_cursor (path, col);
461 iter = model->get_iter (path);
462 cmd = m->new_note_diff_command (_("insert new note"));
463 note = (*iter)[columns._note];
464 copy.reset (new NoteType (*note.get()));
466 m->apply_command (*_session, cmd);
467 /* model has been redisplayed by now */
469 /* select, start editing column 2 (note) */
470 col = view.get_column (2);
471 view.set_cursor (path, *col, true);
476 if (edit_column < 0) {
477 delete_selected_note ();
483 if (_session && Gtkmm2ext::Keyboard::modifier_state_contains (ev->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
490 if (_session && Gtkmm2ext::Keyboard::modifier_state_contains (ev->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
504 MidiListEditor::delete_selected_note ()
506 Glib::RefPtr<TreeSelection> selection = view.get_selection();
507 TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
513 typedef vector<boost::shared_ptr<NoteType> > Notes;
516 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
519 if ((iter = model->get_iter (*i))) {
520 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
521 to_delete.push_back (note);
525 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
526 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (_("delete notes (from list)"));
528 for (Notes::iterator i = to_delete.begin(); i != to_delete.end(); ++i) {
532 m->apply_command (*_session, cmd);
536 MidiListEditor::stop_editing (bool cancelled)
539 if (editing_editable) {
540 editing_editable->editing_done ();
543 if (editing_renderer) {
544 editing_renderer->stop_editing (cancelled);
550 MidiListEditor::editing_started (CellEditable* ed, const string& path, int colno)
552 edit_path = TreePath (path);
554 editing_renderer = dynamic_cast<CellRendererText*>(view.get_column_cell_renderer (colno));
555 editing_editable = ed;
558 Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ed);
560 e->signal_key_press_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_press), false);
561 e->signal_key_release_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_release), false);
567 MidiListEditor::editing_canceled ()
571 editing_renderer = 0;
572 editing_editable = 0;
576 MidiListEditor::edited (const std::string& path, const std::string& text)
578 TreeModel::iterator iter = model->get_iter (path);
580 if (!iter || text.empty()) {
584 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
585 MidiModel::NoteDiffCommand::Property prop (MidiModel::NoteDiffCommand::NoteNumber);
593 switch (edit_column) {
597 // correct ival for zero-based counting after scan
598 if (sscanf (text.c_str(), "%d", &ival) == 1 && --ival != note->channel()) {
599 idelta = ival - note->channel();
600 prop = MidiModel::NoteDiffCommand::Channel;
601 opname = _("change note channel");
606 if (sscanf (text.c_str(), "%d", &ival) == 1 && ival != note->note()) {
607 idelta = ival - note->note();
608 prop = MidiModel::NoteDiffCommand::NoteNumber;
609 opname = _("change note number");
616 if (sscanf (text.c_str(), "%d", &ival) == 1 && ival != note->velocity()) {
617 idelta = ival - note->velocity();
618 prop = MidiModel::NoteDiffCommand::Velocity;
619 opname = _("change note velocity");
625 if (sscanf (text.c_str(), "%lf", &fval) == 1) {
627 /* numeric value entered */
629 if (text.find ('.') == string::npos && text.find (',') == string::npos) {
630 /* integral => units are ticks */
631 fval = fval / BBT_Time::ticks_per_beat;
633 /* non-integral => beats, so use as-is */
638 /* assume its text from the combo. look for the map
639 * entry for the actual note ticks
642 uint64_t len_ticks = note->length().to_ticks();
643 std::map<int,string>::iterator x = note_length_map.find (len_ticks);
645 if (x == note_length_map.end()) {
647 /* tick length not in map - was
648 * displaying numeric value ... use new value
649 * from note length map, and convert to beats.
652 for (x = note_length_map.begin(); x != note_length_map.end(); ++x) {
653 if (x->second == text) {
658 if (x != note_length_map.end()) {
659 fval = x->first / BBT_Time::ticks_per_beat;
666 if (text != x->second) {
668 /* get ticks for the newly selected
672 for (x = note_length_map.begin(); x != note_length_map.end(); ++x) {
673 if (x->second == text) {
678 if (x != note_length_map.end()) {
679 /* convert to beats */
680 fval = (double) x->first / BBT_Time::ticks_per_beat;
687 fdelta = fval - note->length().to_double();
688 prop = MidiModel::NoteDiffCommand::Length;
689 opname = _("change note length");
700 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
701 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (opname);
703 TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
705 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
706 if ((iter = model->get_iter (*i))) {
708 note = (*iter)[columns._note];
711 case MidiModel::NoteDiffCommand::Velocity:
712 cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
714 case MidiModel::NoteDiffCommand::Length:
715 cmd->change (note, prop, note->length() + fdelta);
717 case MidiModel::NoteDiffCommand::Channel:
718 cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
720 case MidiModel::NoteDiffCommand::NoteNumber:
721 cmd->change (note, prop, (uint8_t) (note->note() + idelta));
729 m->apply_command (*_session, cmd);
731 /* model has been redisplayed by now */
732 /* keep selected row(s), move cursor there, don't continue editing */
734 TreeViewColumn* col = view.get_column (edit_column);
735 view.set_cursor (edit_path, *col, 0);
737 /* reset edit info, since we're done */
741 editing_renderer = 0;
742 editing_editable = 0;
747 MidiListEditor::redisplay_model ()
749 view.set_model (Glib::RefPtr<Gtk::ListStore>(0));
754 BeatsFramesConverter conv (_session->tempo_map(), region->position());
755 MidiModel::Notes notes = region->midi_source(0)->model()->notes();
759 for (MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
760 row = *(model->append());
761 row[columns.channel] = (*i)->channel() + 1;
762 row[columns.note_name] = Evoral::midi_note_name ((*i)->note());
763 row[columns.note] = (*i)->note();
764 row[columns.velocity] = (*i)->velocity();
766 Timecode::BBT_Time bbt;
768 _session->tempo_map().bbt_time (conv.to ((*i)->time()), bbt);
772 row[columns.start] = ss.str();
775 const Evoral::MusicalTime dur = (*i)->end_time() - (*i)->time();
776 bbt.beats = dur.get_beats ();
777 bbt.ticks = dur.get_ticks ();
779 int len_ticks = (*i)->length().to_ticks();
780 std::map<int,string>::iterator x = note_length_map.find (len_ticks);
782 if (x != note_length_map.end()) {
783 row[columns.length] = x->second;
787 row[columns.length] = ss.str();
790 row[columns._note] = (*i);
794 view.set_model (model);
798 MidiListEditor::selection_changed ()
800 if (!Config->get_sound_midi_notes()) {
804 TreeModel::Path path;
805 TreeModel::iterator iter;
806 boost::shared_ptr<NoteType> note;
807 TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
809 NotePlayer* player = new NotePlayer (track);
811 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
812 if ((iter = model->get_iter (*i))) {
813 note = (*iter)[columns._note];