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, 0.0);
297 case MidiModel::NoteDiffCommand::Velocity:
298 cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
300 case MidiModel::NoteDiffCommand::Length:
301 if (note->length() + fdelta >= 1.0/BBT_Time::ticks_per_beat) {
302 cmd->change (note, prop, note->length() + fdelta);
304 cmd->change (note, prop, 1.0/BBT_Time::ticks_per_beat);
307 case MidiModel::NoteDiffCommand::Channel:
308 cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
310 case MidiModel::NoteDiffCommand::NoteNumber:
311 cmd->change (note, prop, (uint8_t) (note->note() + idelta));
323 TreeModel::iterator iter;
324 iter = model->get_iter (path);
326 previous_selection.push_back (path);
329 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
332 case MidiModel::NoteDiffCommand::StartTime:
333 if (note->time() + fdelta >= 0) {
334 cmd->change (note, prop, note->time() + fdelta);
336 cmd->change (note, prop, 0.0);
339 case MidiModel::NoteDiffCommand::Velocity:
340 cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
342 case MidiModel::NoteDiffCommand::Length:
343 if (note->length() + fdelta >= 1.0/BBT_Time::ticks_per_beat) {
344 cmd->change (note, prop, note->length() + fdelta);
346 cmd->change (note, prop, 1.0/BBT_Time::ticks_per_beat);
349 case MidiModel::NoteDiffCommand::Channel:
350 cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
352 case MidiModel::NoteDiffCommand::NoteNumber:
353 cmd->change (note, prop, (uint8_t) (note->note() + idelta));
361 m->apply_command (*_session, cmd);
363 /* reset selection to be as it was before we rebuilt */
365 for (vector<TreeModel::Path>::iterator i = previous_selection.begin(); i != previous_selection.end(); ++i) {
366 view.get_selection()->select (*i);
374 MidiListEditor::key_press (GdkEventKey* ev)
377 TreeModel::Path path;
381 switch (ev->keyval) {
383 if (edit_column > 0) {
384 colnum = edit_column;
386 if (editing_editable) {
387 editing_editable->editing_done ();
390 /* wrap to next line */
396 col = view.get_column (colnum);
397 view.set_cursor (path, *col, true);
404 if (edit_column > 0) {
405 colnum = edit_column;
407 if (editing_editable) {
408 editing_editable->editing_done ();
411 col = view.get_column (colnum);
412 view.set_cursor (path, *col, true);
419 if (edit_column > 0) {
420 colnum = edit_column;
422 if (editing_editable) {
423 editing_editable->editing_done ();
426 col = view.get_column (colnum);
427 view.set_cursor (path, *col, true);
442 MidiListEditor::key_release (GdkEventKey* ev)
445 TreeModel::Path path;
447 TreeModel::iterator iter;
448 MidiModel::NoteDiffCommand* cmd;
449 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
450 boost::shared_ptr<NoteType> note;
451 boost::shared_ptr<NoteType> copy;
453 switch (ev->keyval) {
455 /* add a new note to the model, based on the note at the cursor
458 view.get_cursor (path, col);
459 iter = model->get_iter (path);
460 cmd = m->new_note_diff_command (_("insert new note"));
461 note = (*iter)[columns._note];
462 copy.reset (new NoteType (*note.get()));
464 m->apply_command (*_session, cmd);
465 /* model has been redisplayed by now */
467 /* select, start editing column 2 (note) */
468 col = view.get_column (2);
469 view.set_cursor (path, *col, true);
474 if (edit_column < 0) {
475 delete_selected_note ();
481 if (_session && Gtkmm2ext::Keyboard::modifier_state_contains (ev->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
488 if (_session && Gtkmm2ext::Keyboard::modifier_state_contains (ev->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
502 MidiListEditor::delete_selected_note ()
504 Glib::RefPtr<TreeSelection> selection = view.get_selection();
505 TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
511 typedef vector<boost::shared_ptr<NoteType> > Notes;
514 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
517 if ((iter = model->get_iter (*i))) {
518 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
519 to_delete.push_back (note);
523 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
524 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (_("delete notes (from list)"));
526 for (Notes::iterator i = to_delete.begin(); i != to_delete.end(); ++i) {
530 m->apply_command (*_session, cmd);
534 MidiListEditor::stop_editing (bool cancelled)
537 if (editing_editable) {
538 editing_editable->editing_done ();
541 if (editing_renderer) {
542 editing_renderer->stop_editing (cancelled);
548 MidiListEditor::editing_started (CellEditable* ed, const string& path, int colno)
550 edit_path = TreePath (path);
552 editing_renderer = dynamic_cast<CellRendererText*>(view.get_column_cell_renderer (colno));
553 editing_editable = ed;
556 Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ed);
558 e->signal_key_press_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_press), false);
559 e->signal_key_release_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_release), false);
565 MidiListEditor::editing_canceled ()
569 editing_renderer = 0;
570 editing_editable = 0;
574 MidiListEditor::edited (const std::string& path, const std::string& text)
576 TreeModel::iterator iter = model->get_iter (path);
578 if (!iter || text.empty()) {
582 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
583 MidiModel::NoteDiffCommand::Property prop (MidiModel::NoteDiffCommand::NoteNumber);
591 switch (edit_column) {
595 // correct ival for zero-based counting after scan
596 if (sscanf (text.c_str(), "%d", &ival) == 1 && --ival != note->channel()) {
597 idelta = ival - note->channel();
598 prop = MidiModel::NoteDiffCommand::Channel;
599 opname = _("change note channel");
604 if (sscanf (text.c_str(), "%d", &ival) == 1 && ival != note->note()) {
605 idelta = ival - note->note();
606 prop = MidiModel::NoteDiffCommand::NoteNumber;
607 opname = _("change note number");
614 if (sscanf (text.c_str(), "%d", &ival) == 1 && ival != note->velocity()) {
615 idelta = ival - note->velocity();
616 prop = MidiModel::NoteDiffCommand::Velocity;
617 opname = _("change note velocity");
623 if (sscanf (text.c_str(), "%lf", &fval) == 1) {
625 /* numeric value entered */
627 if (text.find ('.') == string::npos && text.find (',') == string::npos) {
628 /* integral => units are ticks */
629 fval = fval / BBT_Time::ticks_per_beat;
631 /* non-integral => beats, so use as-is */
636 /* assume its text from the combo. look for the map
637 * entry for the actual note ticks
640 int len_ticks = lrint (note->length() * Timecode::BBT_Time::ticks_per_beat);
641 std::map<int,string>::iterator x = note_length_map.find (len_ticks);
643 if (x == note_length_map.end()) {
645 /* tick length not in map - was
646 * displaying numeric value ... use new value
647 * from note length map, and convert to beats.
650 for (x = note_length_map.begin(); x != note_length_map.end(); ++x) {
651 if (x->second == text) {
656 if (x != note_length_map.end()) {
657 fval = x->first / BBT_Time::ticks_per_beat;
664 if (text != x->second) {
666 /* get ticks for the newly selected
670 for (x = note_length_map.begin(); x != note_length_map.end(); ++x) {
671 if (x->second == text) {
676 if (x != note_length_map.end()) {
677 /* convert to beats */
678 fval = (double) x->first / BBT_Time::ticks_per_beat;
685 fdelta = fval - note->length();
686 prop = MidiModel::NoteDiffCommand::Length;
687 opname = _("change note length");
698 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
699 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (opname);
701 TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
703 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
704 if ((iter = model->get_iter (*i))) {
706 note = (*iter)[columns._note];
709 case MidiModel::NoteDiffCommand::Velocity:
710 cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
712 case MidiModel::NoteDiffCommand::Length:
713 cmd->change (note, prop, note->length() + fdelta);
715 case MidiModel::NoteDiffCommand::Channel:
716 cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
718 case MidiModel::NoteDiffCommand::NoteNumber:
719 cmd->change (note, prop, (uint8_t) (note->note() + idelta));
727 m->apply_command (*_session, cmd);
729 /* model has been redisplayed by now */
730 /* keep selected row(s), move cursor there, don't continue editing */
732 TreeViewColumn* col = view.get_column (edit_column);
733 view.set_cursor (edit_path, *col, 0);
735 /* reset edit info, since we're done */
739 editing_renderer = 0;
740 editing_editable = 0;
745 MidiListEditor::redisplay_model ()
747 view.set_model (Glib::RefPtr<Gtk::ListStore>(0));
752 BeatsFramesConverter conv (_session->tempo_map(), region->position());
753 MidiModel::Notes notes = region->midi_source(0)->model()->notes();
757 for (MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
758 row = *(model->append());
759 row[columns.channel] = (*i)->channel() + 1;
760 row[columns.note_name] = Evoral::midi_note_name ((*i)->note());
761 row[columns.note] = (*i)->note();
762 row[columns.velocity] = (*i)->velocity();
764 Timecode::BBT_Time bbt;
767 _session->tempo_map().bbt_time (conv.to ((*i)->time()), bbt);
771 row[columns.start] = ss.str();
774 dur = (*i)->end_time() - (*i)->time();
775 bbt.beats = floor (dur);
776 bbt.ticks = (uint32_t) lrint (fmod (dur, 1.0) * Timecode::BBT_Time::ticks_per_beat);
778 int len_ticks = lrint ((*i)->length() * Timecode::BBT_Time::ticks_per_beat);
779 std::map<int,string>::iterator x = note_length_map.find (len_ticks);
781 if (x != note_length_map.end()) {
782 row[columns.length] = x->second;
786 row[columns.length] = ss.str();
789 row[columns._note] = (*i);
793 view.set_model (model);
797 MidiListEditor::selection_changed ()
799 if (!Config->get_sound_midi_notes()) {
803 TreeModel::Path path;
804 TreeModel::iterator iter;
805 boost::shared_ptr<NoteType> note;
806 TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
808 NotePlayer* player = new NotePlayer (track);
810 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
811 if ((iter = model->get_iter (*i))) {
812 note = (*iter)[columns._note];