partially-done (but compile-friendly) move of instrument info into a new backend...
[ardour.git] / gtk2_ardour / midi_region_view.cc
index 25ef2dfc339d1e95fa16feb9f1cd1db6be6f84f1..ca9cd4af717241a136df9a9167f618ad48b4cb71 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,11 @@ 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));
+       RouteUI* route_ui = dynamic_cast<RouteUI*> (&trackview);
+       if (route_ui) {
+               route_ui->route()->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),
@@ -318,6 +317,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 +358,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 +1204,10 @@ 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 ());
+               // MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
+               //string patch_name = mtv->get_patch_name ((*i)->bank(), (*i)->program(), channel);
 
-               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);
-               }
+               // add_canvas_patch_change (*i, patch_name, active_channel);
        }
 }
 
@@ -1539,13 +1533,15 @@ 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::play_midi_chord (vector<boost::shared_ptr<NoteType> > notes)
+MidiRegionView::start_playing_midi_note(boost::shared_ptr<NoteType> note)
 {
        if (_no_sound_notes || !Config->get_sound_midi_notes()) {
                return;
@@ -1557,13 +1553,33 @@ 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 ());
+       _note_player->add (note);
+       _note_player->on ();
+}
+
+void
+MidiRegionView::start_playing_midi_chord (vector<boost::shared_ptr<NoteType> > notes)
+{
+       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());
 
        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 ();
 }
 
 
@@ -1801,11 +1817,10 @@ 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());
@@ -1821,7 +1836,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);
        }
@@ -1934,15 +1949,9 @@ 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--;
+               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);
-                       }
                }
        }
 }
@@ -1953,15 +1962,9 @@ 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++;
+               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 +2341,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 +2382,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 +3202,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();
 }
 
@@ -3794,3 +3795,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;
+}