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;
449 MidiModel::NoteDiffCommand* cmd;
450 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
451 boost::shared_ptr<NoteType> note;
452 boost::shared_ptr<NoteType> copy;
454 switch (ev->keyval) {
456 /* add a new note to the model, based on the note at the cursor
459 view.get_cursor (path, col);
460 iter = model->get_iter (path);
461 cmd = m->new_note_diff_command (_("insert new note"));
462 note = (*iter)[columns._note];
463 copy.reset (new NoteType (*note.get()));
465 m->apply_command (*_session, cmd);
466 /* model has been redisplayed by now */
468 /* select, start editing column 2 (note) */
469 col = view.get_column (2);
470 view.set_cursor (path, *col, true);
475 if (edit_column < 0) {
476 delete_selected_note ();
482 if (_session && Gtkmm2ext::Keyboard::modifier_state_contains (ev->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
489 if (_session && Gtkmm2ext::Keyboard::modifier_state_contains (ev->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
503 MidiListEditor::delete_selected_note ()
505 Glib::RefPtr<TreeSelection> selection = view.get_selection();
506 TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
512 typedef vector<boost::shared_ptr<NoteType> > Notes;
515 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
518 if ((iter = model->get_iter (*i))) {
519 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
520 to_delete.push_back (note);
524 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
525 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (_("delete notes (from list)"));
527 for (Notes::iterator i = to_delete.begin(); i != to_delete.end(); ++i) {
531 m->apply_command (*_session, cmd);
535 MidiListEditor::stop_editing (bool cancelled)
538 if (editing_editable) {
539 editing_editable->editing_done ();
542 if (editing_renderer) {
543 editing_renderer->stop_editing (cancelled);
549 MidiListEditor::editing_started (CellEditable* ed, const string& path, int colno)
551 edit_path = TreePath (path);
553 editing_renderer = dynamic_cast<CellRendererText*>(view.get_column_cell_renderer (colno));
554 editing_editable = ed;
557 Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ed);
559 e->signal_key_press_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_press), false);
560 e->signal_key_release_event().connect (sigc::mem_fun (*this, &MidiListEditor::key_release), false);
566 MidiListEditor::editing_canceled ()
570 editing_renderer = 0;
571 editing_editable = 0;
575 MidiListEditor::edited (const std::string& path, const std::string& text)
577 TreeModel::iterator iter = model->get_iter (path);
579 if (!iter || text.empty()) {
583 boost::shared_ptr<NoteType> note = (*iter)[columns._note];
584 MidiModel::NoteDiffCommand::Property prop (MidiModel::NoteDiffCommand::NoteNumber);
592 switch (edit_column) {
596 // correct ival for zero-based counting after scan
597 if (sscanf (text.c_str(), "%d", &ival) == 1 && --ival != note->channel()) {
598 idelta = ival - note->channel();
599 prop = MidiModel::NoteDiffCommand::Channel;
600 opname = _("change note channel");
605 if (sscanf (text.c_str(), "%d", &ival) == 1 && ival != note->note()) {
606 idelta = ival - note->note();
607 prop = MidiModel::NoteDiffCommand::NoteNumber;
608 opname = _("change note number");
615 if (sscanf (text.c_str(), "%d", &ival) == 1 && ival != note->velocity()) {
616 idelta = ival - note->velocity();
617 prop = MidiModel::NoteDiffCommand::Velocity;
618 opname = _("change note velocity");
624 if (sscanf (text.c_str(), "%lf", &fval) == 1) {
626 /* numeric value entered */
628 if (text.find ('.') == string::npos && text.find (',') == string::npos) {
629 /* integral => units are ticks */
630 fval = fval / BBT_Time::ticks_per_beat;
632 /* non-integral => beats, so use as-is */
637 /* assume its text from the combo. look for the map
638 * entry for the actual note ticks
641 int len_ticks = lrint (note->length() * Timecode::BBT_Time::ticks_per_beat);
642 std::map<int,string>::iterator x = note_length_map.find (len_ticks);
644 if (x == note_length_map.end()) {
646 /* tick length not in map - was
647 * displaying numeric value ... use new value
648 * from note length map, and convert to beats.
651 for (x = note_length_map.begin(); x != note_length_map.end(); ++x) {
652 if (x->second == text) {
657 if (x != note_length_map.end()) {
658 fval = x->first / BBT_Time::ticks_per_beat;
665 if (text != x->second) {
667 /* get ticks for the newly selected
671 for (x = note_length_map.begin(); x != note_length_map.end(); ++x) {
672 if (x->second == text) {
677 if (x != note_length_map.end()) {
678 /* convert to beats */
679 fval = (double) x->first / BBT_Time::ticks_per_beat;
686 fdelta = fval - note->length();
687 prop = MidiModel::NoteDiffCommand::Length;
688 opname = _("change note length");
699 boost::shared_ptr<MidiModel> m (region->midi_source(0)->model());
700 MidiModel::NoteDiffCommand* cmd = m->new_note_diff_command (opname);
702 TreeView::Selection::ListHandle_Path rows = view.get_selection()->get_selected_rows ();
704 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
705 if (iter = model->get_iter (*i)) {
707 note = (*iter)[columns._note];
710 case MidiModel::NoteDiffCommand::Velocity:
711 cmd->change (note, prop, (uint8_t) (note->velocity() + idelta));
713 case MidiModel::NoteDiffCommand::Length:
714 cmd->change (note, prop, note->length() + fdelta);
716 case MidiModel::NoteDiffCommand::Channel:
717 cmd->change (note, prop, (uint8_t) (note->channel() + idelta));
719 case MidiModel::NoteDiffCommand::NoteNumber:
720 cmd->change (note, prop, (uint8_t) (note->note() + idelta));
728 m->apply_command (*_session, cmd);
730 /* model has been redisplayed by now */
731 /* keep selected row(s), move cursor there, don't continue editing */
733 TreeViewColumn* col = view.get_column (edit_column);
734 view.set_cursor (edit_path, *col, 0);
736 /* reset edit info, since we're done */
740 editing_renderer = 0;
741 editing_editable = 0;
746 MidiListEditor::redisplay_model ()
748 view.set_model (Glib::RefPtr<Gtk::ListStore>(0));
753 BeatsFramesConverter conv (_session->tempo_map(), region->position());
754 MidiModel::Notes notes = region->midi_source(0)->model()->notes();
758 for (MidiModel::Notes::iterator i = notes.begin(); i != notes.end(); ++i) {
759 row = *(model->append());
760 row[columns.channel] = (*i)->channel() + 1;
761 row[columns.note_name] = Evoral::midi_note_name ((*i)->note());
762 row[columns.note] = (*i)->note();
763 row[columns.velocity] = (*i)->velocity();
765 Timecode::BBT_Time bbt;
768 _session->tempo_map().bbt_time (conv.to ((*i)->time()), bbt);
772 row[columns.start] = ss.str();
775 dur = (*i)->end_time() - (*i)->time();
776 bbt.beats = floor (dur);
777 bbt.ticks = (uint32_t) lrint (fmod (dur, 1.0) * Timecode::BBT_Time::ticks_per_beat);
779 int len_ticks = lrint ((*i)->length() * Timecode::BBT_Time::ticks_per_beat);
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];