hide patch changes if they are too wide for the region at a given zoom level
[ardour.git] / gtk2_ardour / midi_region_view.cc
index 25ef2dfc339d1e95fa16feb9f1cd1db6be6f84f1..5d8bca9d776aeedec877445318fb91482ad3de81 100644 (file)
@@ -110,14 +110,11 @@ MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &
        , _last_event_y (0)
        , pre_enter_cursor (0)
        , pre_press_cursor (0)
+       , _note_player (0)
 {
        _note_group->raise_to_top();
        PublicEditor::DropDownKeys.connect (sigc::mem_fun (*this, &MidiRegionView::drop_down_keys));
 
-       /* Look up MIDNAM details from our MidiTimeAxisView */
-       MidiTimeAxisView& mtv = dynamic_cast<MidiTimeAxisView&> (tv);
-       midi_patch_settings_changed (mtv.midi_patch_model (), mtv.midi_patch_custom_device_node ());
-
        Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&MidiRegionView::parameter_changed, this, _1), gui_context());
        connect_to_diskstream ();
 
@@ -150,14 +147,11 @@ MidiRegionView::MidiRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &
        , _last_event_y (0)
        , pre_enter_cursor (0)
        , pre_press_cursor (0)
+       , _note_player (0)
 {
        _note_group->raise_to_top();
        PublicEditor::DropDownKeys.connect (sigc::mem_fun (*this, &MidiRegionView::drop_down_keys));
 
-       /* Look up MIDNAM details from our MidiTimeAxisView */
-       MidiTimeAxisView& mtv = dynamic_cast<MidiTimeAxisView&> (tv);
-       midi_patch_settings_changed (mtv.midi_patch_model (), mtv.midi_patch_custom_device_node ());
-       
        connect_to_diskstream ();
 
        SelectionCleared.connect (_selection_cleared_connection, invalidator (*this), boost::bind (&MidiRegionView::selection_cleared, this, _1), gui_context ());
@@ -198,6 +192,7 @@ MidiRegionView::MidiRegionView (const MidiRegionView& other)
        , _last_event_y (0)
        , pre_enter_cursor (0)
        , pre_press_cursor (0)
+       , _note_player (0)
 {
        Gdk::Color c;
        int r,g,b,a;
@@ -232,6 +227,7 @@ MidiRegionView::MidiRegionView (const MidiRegionView& other, boost::shared_ptr<M
        , _last_event_y (0)
        , pre_enter_cursor (0)
        , pre_press_cursor (0)
+       , _note_player (0)
 {
        Gdk::Color c;
        int r,g,b,a;
@@ -287,8 +283,8 @@ MidiRegionView::init (Gdk::Color const & basic_color, bool wfd)
        midi_view()->signal_channel_mode_changed().connect(
                sigc::mem_fun(this, &MidiRegionView::midi_channel_mode_changed));
 
-       midi_view()->signal_midi_patch_settings_changed().connect(
-               sigc::mem_fun(this, &MidiRegionView::midi_patch_settings_changed));
+       instrument_info().Changed.connect (_instrument_changed_connection, invalidator (*this),
+                                          boost::bind (&MidiRegionView::instrument_settings_changed, this), gui_context());
 
        trackview.editor().SnapChanged.connect(snap_changed_connection, invalidator(*this),
                                               boost::bind (&MidiRegionView::snap_changed, this),
@@ -300,6 +296,13 @@ MidiRegionView::init (Gdk::Color const & basic_color, bool wfd)
        SelectionCleared.connect (_selection_cleared_connection, invalidator (*this), boost::bind (&MidiRegionView::selection_cleared, this, _1), gui_context ());
 }
 
+InstrumentInfo&
+MidiRegionView::instrument_info () const
+{
+       RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
+       return route_ui->route()->instrument_info();
+}
+
 const boost::shared_ptr<ARDOUR::MidiRegion>
 MidiRegionView::midi_region() const
 {
@@ -318,6 +321,8 @@ MidiRegionView::connect_to_diskstream ()
 bool
 MidiRegionView::canvas_event(GdkEvent* ev)
 {
+       bool r;
+       
        switch (ev->type) {
        case GDK_ENTER_NOTIFY:
        case GDK_LEAVE_NOTIFY:
@@ -357,7 +362,10 @@ MidiRegionView::canvas_event(GdkEvent* ev)
                return button_press (&ev->button);
 
        case GDK_BUTTON_RELEASE:
-               return button_release (&ev->button);
+               r = button_release (&ev->button);
+               delete _note_player;
+               _note_player = 0;
+               return r;
 
        case GDK_ENTER_NOTIFY:
                return enter_notify (&ev->crossing);
@@ -1200,20 +1208,8 @@ MidiRegionView::display_patch_changes_on_channel (uint8_t channel, bool active_c
                        continue;
                }
 
-               MIDI::Name::PatchPrimaryKey patch_key ((*i)->bank_msb (), (*i)->bank_lsb (), (*i)->program ());
-
-               boost::shared_ptr<MIDI::Name::Patch> patch =
-                       MIDI::Name::MidiPatchManager::instance().find_patch(
-                               _model_name, _custom_device_mode, channel, patch_key);
-
-               if (patch != 0) {
-                       add_canvas_patch_change (*i, patch->name(), active_channel);
-               } else {
-                       char buf[16];
-                       /* program and bank numbers are zero-based: convert to one-based: MIDI_BP_ZERO */
-                       snprintf (buf, 16, "%d %d", (*i)->program() + MIDI_BP_ZERO , (*i)->bank() + MIDI_BP_ZERO);
-                       add_canvas_patch_change (*i, buf, active_channel);
-               }
+               string patch_name = instrument_info().get_patch_name ((*i)->bank(), (*i)->program(), channel);
+               add_canvas_patch_change (*i, patch_name, active_channel);
        }
 }
 
@@ -1351,6 +1347,14 @@ MidiRegionView::reset_width_dependent_items (double pixel_width)
                redisplay_model();
        }
 
+       for (PatchChanges::iterator x = _patch_changes.begin(); x != _patch_changes.end(); ++x) {
+               if ((*x)->width() >= _pixel_width) {
+                       (*x)->hide();
+               } else {
+                       (*x)->show();
+               }
+       }
+
        move_step_edit_cursor (_step_edit_cursor_position);
        set_step_edit_cursor_width (_step_edit_cursor_width);
 }
@@ -1539,13 +1543,34 @@ MidiRegionView::play_midi_note(boost::shared_ptr<NoteType> note)
                return;
        }
 
-       NotePlayer* np = new NotePlayer (route_ui->midi_track());
+       NotePlayer* np = new NotePlayer (route_ui->midi_track ());
        np->add (note);
        np->play ();
+
+       /* NotePlayer deletes itself */
+}
+
+void
+MidiRegionView::start_playing_midi_note(boost::shared_ptr<NoteType> note)
+{
+       if (_no_sound_notes || !Config->get_sound_midi_notes()) {
+               return;
+       }
+
+       RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
+
+       if (!route_ui || !route_ui->midi_track()) {
+               return;
+       }
+
+       delete _note_player;
+       _note_player = new NotePlayer (route_ui->midi_track ());
+       _note_player->add (note);
+       _note_player->on ();
 }
 
 void
-MidiRegionView::play_midi_chord (vector<boost::shared_ptr<NoteType> > notes)
+MidiRegionView::start_playing_midi_chord (vector<boost::shared_ptr<NoteType> > notes)
 {
        if (_no_sound_notes || !Config->get_sound_midi_notes()) {
                return;
@@ -1557,13 +1582,14 @@ MidiRegionView::play_midi_chord (vector<boost::shared_ptr<NoteType> > notes)
                return;
        }
 
-       NotePlayer* np = new NotePlayer (route_ui->midi_track());
+       delete _note_player;
+       _note_player = new NotePlayer (route_ui->midi_track());
 
        for (vector<boost::shared_ptr<NoteType> >::iterator n = notes.begin(); n != notes.end(); ++n) {
-               np->add (*n);
+               _note_player->add (*n);
        }
 
-       np->play ();
+       _note_player->on ();
 }
 
 
@@ -1776,8 +1802,7 @@ MidiRegionView::add_canvas_patch_change (MidiModel::PatchChangePtr patch, const
                                      displaytext,
                                      height,
                                      x, 1.0,
-                                     _model_name,
-                                     _custom_device_mode,
+                                     instrument_info(),
                                      patch,
                                      active_channel)
                          );
@@ -1792,8 +1817,14 @@ MidiRegionView::add_canvas_patch_change (MidiModel::PatchChangePtr patch, const
        _patch_changes.push_back (patch_change);
 }
 
-void
-MidiRegionView::get_patch_key_at (Evoral::MusicalTime time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key)
+MIDI::Name::PatchPrimaryKey
+MidiRegionView::patch_change_to_patch_key (MidiModel::PatchChangePtr p)
+{
+       return MIDI::Name::PatchPrimaryKey (p->program(), p->bank());
+}
+
+void 
+MidiRegionView::get_patch_key_at (double time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key)
 {
        MidiModel::PatchChanges::iterator i = _model->patch_change_lower_bound (time);
        while (i != _model->patch_changes().end() && (*i)->channel() != channel) {
@@ -1801,17 +1832,15 @@ MidiRegionView::get_patch_key_at (Evoral::MusicalTime time, uint8_t channel, MID
        }
 
        if (i != _model->patch_changes().end()) {
-               key.msb = (*i)->bank_msb ();
-               key.lsb = (*i)->bank_lsb ();
+               key.bank_number = (*i)->bank();
                key.program_number = (*i)->program ();
        } else {
-               key.msb = key.lsb = key.program_number = 0;
+               key.bank_number = key.program_number = 0;
        }
 
        assert (key.is_sane());
 }
 
-
 void
 MidiRegionView::change_patch_change (CanvasPatchChange& pc, const MIDI::Name::PatchPrimaryKey& new_patch)
 {
@@ -1821,7 +1850,7 @@ MidiRegionView::change_patch_change (CanvasPatchChange& pc, const MIDI::Name::Pa
                c->change_program (pc.patch (), new_patch.program_number);
        }
 
-       int const new_bank = (new_patch.msb << 7) | new_patch.lsb;
+       int const new_bank = new_patch.bank_number;
        if (pc.patch()->bank() != new_bank) {
                c->change_bank (pc.patch (), new_bank);
        }
@@ -1910,8 +1939,7 @@ void
 MidiRegionView::previous_patch (CanvasPatchChange& patch)
 {
        if (patch.patch()->program() < 127) {
-               MIDI::Name::PatchPrimaryKey key;
-               get_patch_key_at (patch.patch()->time(), patch.patch()->channel(), key);
+               MIDI::Name::PatchPrimaryKey key = patch_change_to_patch_key (patch.patch());
                key.program_number++;
                change_patch_change (patch, key);
        }
@@ -1921,8 +1949,7 @@ void
 MidiRegionView::next_patch (CanvasPatchChange& patch)
 {
        if (patch.patch()->program() > 0) {
-               MIDI::Name::PatchPrimaryKey key;
-               get_patch_key_at (patch.patch()->time(), patch.patch()->channel(), key);
+               MIDI::Name::PatchPrimaryKey key = patch_change_to_patch_key (patch.patch());
                key.program_number--;
                change_patch_change (patch, key);
        }
@@ -1932,17 +1959,10 @@ void
 MidiRegionView::previous_bank (CanvasPatchChange& patch)
 {
        if (patch.patch()->program() < 127) {
-               MIDI::Name::PatchPrimaryKey key;
-               get_patch_key_at (patch.patch()->time(), patch.patch()->channel(), key);
-               if (key.lsb > 0) {
-                       key.lsb--;
+               MIDI::Name::PatchPrimaryKey key = patch_change_to_patch_key (patch.patch());
+               if (key.bank_number > 0) {
+                       key.bank_number--;
                        change_patch_change (patch, key);
-               } else {
-                       if (key.msb > 0) {
-                               key.lsb = 127;
-                               key.msb--;
-                               change_patch_change (patch, key);
-                       }
                }
        }
 }
@@ -1951,17 +1971,10 @@ void
 MidiRegionView::next_bank (CanvasPatchChange& patch)
 {
        if (patch.patch()->program() > 0) {
-               MIDI::Name::PatchPrimaryKey key;
-               get_patch_key_at (patch.patch()->time(), patch.patch()->channel(), key);
-               if (key.lsb < 127) {
-                       key.lsb++;
+               MIDI::Name::PatchPrimaryKey key = patch_change_to_patch_key (patch.patch());
+               if (key.bank_number < 127) {
+                       key.bank_number++;
                        change_patch_change (patch, key);
-               } else {
-                       if (key.msb < 127) {
-                               key.lsb = 0;
-                               key.msb++;
-                               change_patch_change (patch, key);
-                       }
                }
        }
 }
@@ -2338,7 +2351,7 @@ MidiRegionView::add_to_selection (CanvasNoteEvent* ev)
 
        if (_selection.insert (ev).second) {
                ev->set_selected (true);
-               play_midi_note ((ev)->note());
+               start_playing_midi_note ((ev)->note());
        }
 
        if (add_mrv_selection) {
@@ -2379,13 +2392,13 @@ MidiRegionView::move_selection(double dx, double dy, double cumulative_dy)
                                shifted.push_back (moved_note);
                        }
 
-                       play_midi_chord (shifted);
+                       start_playing_midi_chord (shifted);
 
                } else if (!to_play.empty()) {
 
                        boost::shared_ptr<NoteType> moved_note (new NoteType (*to_play.front()));
                        moved_note->set_note (moved_note->note() + cumulative_dy);
-                       play_midi_note (moved_note);
+                       start_playing_midi_note (moved_note);
                }
        }
 }
@@ -3199,10 +3212,8 @@ MidiRegionView::midi_channel_mode_changed(ChannelMode mode, uint16_t mask)
 }
 
 void
-MidiRegionView::midi_patch_settings_changed(std::string model, std::string custom_device_mode)
+MidiRegionView::instrument_settings_changed ()
 {
-       _model_name         = model;
-       _custom_device_mode = custom_device_mode;
        redisplay_model();
 }
 
@@ -3708,7 +3719,7 @@ MidiRegionView::trim_front_ending ()
 void
 MidiRegionView::edit_patch_change (ArdourCanvas::CanvasPatchChange* pc)
 {
-       PatchChangeDialog d (&_source_relative_time_converter, trackview.session(), *pc->patch (), _model_name, _custom_device_mode, Gtk::Stock::APPLY);
+       PatchChangeDialog d (&_source_relative_time_converter, trackview.session(), *pc->patch (), instrument_info(), Gtk::Stock::APPLY);
        if (d.run () != Gtk::RESPONSE_ACCEPT) {
                return;
        }
@@ -3794,3 +3805,10 @@ MidiRegionView::selection_cleared (MidiRegionView* rv)
        /* Clear our selection in sympathy; but don't signal the fact */
        clear_selection (false);
 }
+
+void
+MidiRegionView::note_button_release ()
+{
+       delete _note_player;
+       _note_player = 0;
+}