move mode/scale/key definitions out of push2 code and into libardour; Aeolian is...
[ardour.git] / libs / surfaces / push2 / push2.cc
index 87d4c4d9c58fa664ebe465c71416e7d4c05d5263..73c871548b070d8effdf66993f9133739550d932 100644 (file)
@@ -30,6 +30,7 @@
 #include "timecode/time.h"
 #include "timecode/bbt_time.h"
 
+#include "ardour/amp.h"
 #include "ardour/async_midi_port.h"
 #include "ardour/audioengine.h"
 #include "ardour/debug.h"
@@ -74,50 +75,10 @@ register_enums ()
        vector<int> i;
        vector<string> s;
 
-       MusicalMode::Type mode;
 
 #define REGISTER(e) enum_writer.register_distinct (typeid(e).name(), i, s); i.clear(); s.clear()
 #define REGISTER_CLASS_ENUM(t,e) i.push_back (t::e); s.push_back (#e)
 
-       REGISTER_CLASS_ENUM (MusicalMode,Dorian);
-       REGISTER_CLASS_ENUM (MusicalMode, IonianMajor);
-       REGISTER_CLASS_ENUM (MusicalMode, Minor);
-       REGISTER_CLASS_ENUM (MusicalMode, HarmonicMinor);
-       REGISTER_CLASS_ENUM (MusicalMode, MelodicMinorAscending);
-       REGISTER_CLASS_ENUM (MusicalMode, MelodicMinorDescending);
-       REGISTER_CLASS_ENUM (MusicalMode, Phrygian);
-       REGISTER_CLASS_ENUM (MusicalMode, Lydian);
-       REGISTER_CLASS_ENUM (MusicalMode, Mixolydian);
-       REGISTER_CLASS_ENUM (MusicalMode, Aeolian);
-       REGISTER_CLASS_ENUM (MusicalMode, Locrian);
-       REGISTER_CLASS_ENUM (MusicalMode, PentatonicMajor);
-       REGISTER_CLASS_ENUM (MusicalMode, PentatonicMinor);
-       REGISTER_CLASS_ENUM (MusicalMode, Chromatic);
-       REGISTER_CLASS_ENUM (MusicalMode, BluesScale);
-       REGISTER_CLASS_ENUM (MusicalMode, NeapolitanMinor);
-       REGISTER_CLASS_ENUM (MusicalMode, NeapolitanMajor);
-       REGISTER_CLASS_ENUM (MusicalMode, Oriental);
-       REGISTER_CLASS_ENUM (MusicalMode, DoubleHarmonic);
-       REGISTER_CLASS_ENUM (MusicalMode, Enigmatic);
-       REGISTER_CLASS_ENUM (MusicalMode, Hirajoshi);
-       REGISTER_CLASS_ENUM (MusicalMode, HungarianMinor);
-       REGISTER_CLASS_ENUM (MusicalMode, HungarianMajor);
-       REGISTER_CLASS_ENUM (MusicalMode, Kumoi);
-       REGISTER_CLASS_ENUM (MusicalMode, Iwato);
-       REGISTER_CLASS_ENUM (MusicalMode, Hindu);
-       REGISTER_CLASS_ENUM (MusicalMode, Spanish8Tone);
-       REGISTER_CLASS_ENUM (MusicalMode, Pelog);
-       REGISTER_CLASS_ENUM (MusicalMode, HungarianGypsy);
-       REGISTER_CLASS_ENUM (MusicalMode, Overtone);
-       REGISTER_CLASS_ENUM (MusicalMode, LeadingWholeTone);
-       REGISTER_CLASS_ENUM (MusicalMode, Arabian);
-       REGISTER_CLASS_ENUM (MusicalMode, Balinese);
-       REGISTER_CLASS_ENUM (MusicalMode, Gypsy);
-       REGISTER_CLASS_ENUM (MusicalMode, Mohammedan);
-       REGISTER_CLASS_ENUM (MusicalMode, Javanese);
-       REGISTER_CLASS_ENUM (MusicalMode, Persian);
-       REGISTER_CLASS_ENUM (MusicalMode, Algerian);
-       REGISTER (mode);
 }
 
 Push2::Push2 (ARDOUR::Session& s)
@@ -232,14 +193,15 @@ Push2::open ()
 
        try {
                _canvas = new Push2Canvas (*this, 960, 160);
-               mix_layout = new MixLayout (*this, *session);
-               scale_layout = new ScaleLayout (*this, *session);
-               track_mix_layout = new TrackMixLayout (*this, *session);
-               splash_layout = new SplashLayout (*this, *session);
+               mix_layout = new MixLayout (*this, *session, "globalmix");
+               scale_layout = new ScaleLayout (*this, *session, "scale");
+               track_mix_layout = new TrackMixLayout (*this, *session, "trackmix");
+               splash_layout = new SplashLayout (*this, *session, "splash");
        } catch (...) {
                error << _("Cannot construct Canvas for display") << endmsg;
                libusb_release_interface (handle, 0x00);
                libusb_close (handle);
+               handle = 0;
                return -1;
        }
 
@@ -365,7 +327,7 @@ Push2::init_buttons (bool startup)
        */
 
        ButtonID buttons[] = { Mute, Solo, Master, Up, Right, Left, Down, Note, Session, Mix, AddTrack, Delete, Undo,
-                              Metronome, Shift, Select, Play, RecordEnable, Automate, Repeat, Note, Session, DoubleLoop,
+                              Metronome, Shift, Select, Play, RecordEnable, Automate, Repeat, Note, Session,
                               Quantize, Duplicate, Browse, PageRight, PageLeft, OctaveUp, OctaveDown, Layout, Scale
        };
 
@@ -479,6 +441,11 @@ Push2::vblank ()
                }
        }
 
+       if (_current_layout) {
+               _current_layout->update_meters ();
+               _current_layout->update_clocks ();
+       }
+
        _canvas->vblank();
 
        return true;
@@ -622,18 +589,30 @@ void
 Push2::handle_midi_sysex (MIDI::Parser&, MIDI::byte* raw_bytes, size_t sz)
 {
        DEBUG_TRACE (DEBUG::Push2, string_compose ("Sysex, %1 bytes\n", sz));
+
+       if (sz < 8) {
+               return;
+       }
+
        MidiByteArray msg (sz, raw_bytes);
-       MidiByteArray aftertouch_mode_response (9, 0xF0, 0x00, 0x21, 0x1D, 0x01, 0x01, 0x1F, 0x0, 0xF7);
-       MidiByteArray polypress_mode_response (9, 0xF0, 0x00, 0x21, 0x1D, 0x01, 0x01, 0x1F, 0x1, 0xF7);
-
-       if (msg == aftertouch_mode_response) {
-               _pressure_mode = AfterTouch;
-               PressureModeChange (AfterTouch);
-               cerr << "Pressure mod eis after\n";
-       } else if (msg == polypress_mode_response) {
-               _pressure_mode = PolyPressure;
-               PressureModeChange (PolyPressure);
-               cerr << "Pressure mod eis poly\n";
+       MidiByteArray push2_sysex_header (6, 0xF0, 0x00, 0x21, 0x1D, 0x01, 0x01);
+
+       if (!push2_sysex_header.compare_n (msg, 6)) {
+               return;
+       }
+
+       switch (msg[6]) {
+       case 0x1f: /* pressure mode */
+               if (msg[7] == 0x0) {
+                       _pressure_mode = AfterTouch;
+                       PressureModeChange (AfterTouch);
+                       cerr << "Pressure mode is after\n";
+               } else {
+                       _pressure_mode = PolyPressure;
+                       PressureModeChange (PolyPressure);
+                       cerr << "Pressure mode is poly\n";
+               }
+               break;
        }
 }
 
@@ -1071,17 +1050,31 @@ Push2::set_state (const XMLNode & node, int version)
 void
 Push2::other_vpot (int n, int delta)
 {
+       boost::shared_ptr<Amp> click_gain;
        switch (n) {
        case 0:
+               /* tempo control */
                break;
        case 1:
+               /* metronome gain control */
+               click_gain = session->click_gain();
+               if (click_gain) {
+                       boost::shared_ptr<AutomationControl> ac = click_gain->gain_control();
+                       if (ac) {
+                               ac->set_value (ac->interface_to_internal (
+                                                      min (ac->upper(), max (ac->lower(), ac->internal_to_interface (ac->get_value()) + (delta/256.0)))),
+                                              PBD::Controllable::UseGroup);
+                       }
+               }
                break;
        case 2:
                /* master gain control */
                if (master) {
                        boost::shared_ptr<AutomationControl> ac = master->gain_control();
                        if (ac) {
-                               ac->set_value (ac->get_value() + ((2.0/64.0) * delta), PBD::Controllable::UseGroup);
+                               ac->set_value (ac->interface_to_internal (
+                                                      min (ac->upper(), max (ac->lower(), ac->internal_to_interface (ac->get_value()) + (delta/256.0)))),
+                                              PBD::Controllable::UseGroup);
                        }
                }
                break;
@@ -1262,6 +1255,21 @@ Push2::pad_note (int row, int col) const
        return 0;
 }
 
+void
+Push2::update_selection_color ()
+{
+       boost::shared_ptr<MidiTrack> current_midi_track = current_pad_target.lock();
+
+       if (!current_midi_track) {
+               return;
+       }
+
+       selection_color = get_color_index (current_midi_track->presentation_info().color());
+       contrast_color = get_color_index (ArdourCanvas::HSV (current_midi_track->presentation_info().color()).opposite().color());
+
+       reset_pad_colors ();
+}
+
 void
 Push2::reset_pad_colors ()
 {
@@ -1443,6 +1451,8 @@ Push2::set_percussive_mode (bool yn)
 
        int drum_note = 36;
 
+       fn_pad_map.clear ();
+
        for (int row = 0; row < 8; ++row) {
 
                for (int col = 0; col < 4; ++col) {
@@ -1508,6 +1518,14 @@ Push2::stripable_selection_change (StripableNotificationListPtr selected)
        /* disconnect from pad port, if appropriate */
 
        if (current_midi_track && pad_port) {
+
+               /* XXX this could possibly leave dangling MIDI notes.
+                *
+                * A general libardour fix is required. It isn't obvious
+                * how note resolution can be done unless disconnecting
+                * becomes "slow" (i.e. deferred for as long as it takes
+                * to resolve notes).
+                */
                current_midi_track->input()->disconnect (current_midi_track->input()->nth(0), pad_port->name(), this);
        }
 
@@ -1654,20 +1672,26 @@ Push2::get_color (ColorName name)
 void
 Push2::set_current_layout (Push2Layout* layout)
 {
-       if (_current_layout) {
-               _current_layout->hide ();
-               _canvas->root()->remove (_current_layout);
-               _previous_layout = _current_layout;
-       }
+       if (layout && layout == _current_layout) {
+               _current_layout->show ();
+       } else {
 
-       _current_layout = layout;
+               if (_current_layout) {
+                       _current_layout->hide ();
+                       _canvas->root()->remove (_current_layout);
+                       _previous_layout = _current_layout;
+               }
 
-       if (_current_layout) {
-               _canvas->root()->add (_current_layout);
-               _current_layout->show ();
-       }
+               _current_layout = layout;
+
+               if (_current_layout) {
+                       _canvas->root()->add (_current_layout);
+                       _current_layout->show ();
+               }
 
-       _canvas->request_redraw ();
+
+               _canvas->request_redraw ();
+       }
 }
 
 void