]> git.sesse.net Git - nageru/blob - midi_mapping_dialog.h
Support audio-only FFmpeg inputs. Somewhat wonky, though.
[nageru] / midi_mapping_dialog.h
1 #ifndef _MIDI_MAPPING_DIALOG_H
2 #define _MIDI_MAPPING_DIALOG_H
3
4 #include <stdbool.h>
5 #include <QDialog>
6 #include <QString>
7 #include <map>
8 #include <memory>
9 #include <string>
10 #include <utility>
11 #include <vector>
12
13 #include "midi_mapper.h"
14
15 class QEvent;
16 class QObject;
17
18 namespace Ui {
19 class MIDIMappingDialog;
20 }  // namespace Ui
21
22 class MIDIMappingProto;
23 class QComboBox;
24 class QSpinBox;
25 class QTreeWidgetItem;
26
27 class MIDIMappingDialog : public QDialog, public ControllerReceiver
28 {
29         Q_OBJECT
30
31 public:
32         MIDIMappingDialog(MIDIMapper *mapper);
33         ~MIDIMappingDialog();
34
35         bool eventFilter(QObject *obj, QEvent *event) override;
36
37         // For use in midi_mapping_dialog.cpp only.
38         struct Control {
39                 std::string label;
40                 int field_number;  // In MIDIMappingBusProto.
41                 int bank_field_number;  // In MIDIMappingProto.
42         };
43
44         // ControllerReceiver interface. We only implement the raw events.
45         // All values are [0.0, 1.0].
46         void set_locut(float value) override {}
47         void set_limiter_threshold(float value) override {}
48         void set_makeup_gain(float value) override {}
49
50         void set_stereo_width(unsigned bus_idx, float value) override {}
51         void set_treble(unsigned bus_idx, float value) override {}
52         void set_mid(unsigned bus_idx, float value) override {}
53         void set_bass(unsigned bus_idx, float value) override {}
54         void set_gain(unsigned bus_idx, float value) override {}
55         void set_compressor_threshold(unsigned bus_idx, float value) override {}
56         void set_fader(unsigned bus_idx, float value) override {}
57
58         void toggle_mute(unsigned bus_idx) override {}
59         void toggle_locut(unsigned bus_idx) override {}
60         void toggle_auto_gain_staging(unsigned bus_idx) override {}
61         void toggle_compressor(unsigned bus_idx) override {}
62         void clear_peak(unsigned bus_idx) override {}
63         void toggle_limiter() override {}
64         void toggle_auto_makeup_gain() override {}
65
66         void clear_all_highlights() override {}
67
68         void highlight_locut(bool highlight) override {}
69         void highlight_limiter_threshold(bool highlight) override {}
70         void highlight_makeup_gain(bool highlight) override {}
71
72         void highlight_stereo_width(unsigned bus_idx, bool highlight) override {}
73         void highlight_treble(unsigned bus_idx, bool highlight) override {}
74         void highlight_mid(unsigned bus_idx, bool highlight) override {}
75         void highlight_bass(unsigned bus_idx, bool highlight) override {}
76         void highlight_gain(unsigned bus_idx, bool highlight) override {}
77         void highlight_compressor_threshold(unsigned bus_idx, bool highlight) override {}
78         void highlight_fader(unsigned bus_idx, bool highlight) override {}
79
80         void highlight_mute(unsigned bus_idx, bool highlight) override {}
81         void highlight_toggle_locut(unsigned bus_idx, bool highlight) override {}
82         void highlight_toggle_auto_gain_staging(unsigned bus_idx, bool highlight) override {}
83         void highlight_toggle_compressor(unsigned bus_idx, bool highlight) override {}
84         void highlight_clear_peak(unsigned bus_idx, bool highlight) override {}
85         void highlight_toggle_limiter(bool highlight) override {}
86         void highlight_toggle_auto_makeup_gain(bool highlight) override {}
87
88         // Raw events; used for the editor dialog only.
89         void controller_changed(unsigned controller) override;
90         void note_on(unsigned note) override;
91
92 public slots:
93         void guess_clicked(bool limit_to_group);
94         void ok_clicked();
95         void cancel_clicked();
96         void save_clicked();
97         void load_clicked();
98
99 private:
100         static constexpr unsigned num_buses = 8;
101
102         // Each spinner belongs to exactly one group, corresponding to the
103         // subheadings in the UI. This is so that we can extrapolate data
104         // across only single groups if need be.
105         enum class SpinnerGroup {
106                 ALL_GROUPS = -1,
107                 PER_BUS_CONTROLLERS,
108                 PER_BUS_BUTTONS,
109                 PER_BUS_LIGHTS,
110                 GLOBAL_CONTROLLERS,
111                 GLOBAL_BUTTONS,
112                 GLOBAL_LIGHTS
113         };
114
115         void add_bank_selector(QTreeWidgetItem *item, const MIDIMappingProto &mapping_proto, int bank_field_number);
116         
117         enum class ControlType { CONTROLLER, BUTTON, LIGHT };
118         void add_controls(const std::string &heading, ControlType control_type,
119                           SpinnerGroup spinner_group,
120                           const MIDIMappingProto &mapping_proto, const std::vector<Control> &controls);
121         void fill_controls_from_mapping(const MIDIMappingProto &mapping_proto);
122
123         // Tries to find a source bus and an offset to it that would give
124         // a consistent offset for the rest of the mappings in this bus.
125         // Returns -1 for the bus if no consistent offset can be found.
126         std::pair<int, int> guess_offset(unsigned bus_idx, SpinnerGroup spinner_group);
127         bool bus_is_empty(unsigned bus_idx, SpinnerGroup spinner_group);
128
129         void update_guess_button_state();
130         struct FocusInfo {
131                 int bus_idx;  // -1 for none.
132                 SpinnerGroup spinner_group;
133                 int field_number;
134         };
135         FocusInfo find_focus() const;
136
137         std::unique_ptr<MIDIMappingProto> construct_mapping_proto_from_ui();
138
139         Ui::MIDIMappingDialog *ui;
140         MIDIMapper *mapper;
141         ControllerReceiver *old_receiver;
142         FocusInfo last_focus{-1, SpinnerGroup::ALL_GROUPS, -1};
143
144         // All controllers actually laid out on the grid (we need to store them
145         // so that we can move values back and forth between the controls and
146         // the protobuf on save/load).
147         struct InstantiatedSpinner {
148                 QSpinBox *spinner;
149                 unsigned bus_idx;
150                 SpinnerGroup spinner_group;
151                 int field_number;  // In MIDIMappingBusProto.
152         };
153         struct InstantiatedComboBox {
154                 QComboBox *combo_box;
155                 int field_number;  // In MIDIMappingProto.
156         };
157         std::vector<InstantiatedSpinner> controller_spinners;
158         std::vector<InstantiatedSpinner> button_spinners;
159         std::vector<InstantiatedSpinner> light_spinners;
160         std::vector<InstantiatedComboBox> bank_combo_boxes;
161
162         // Keyed on bus index, then field number.
163         struct SpinnerAndGroup {
164                 QSpinBox *spinner;
165                 SpinnerGroup group;
166         };
167         std::map<unsigned, std::map<unsigned, SpinnerAndGroup>> spinners;
168 };
169
170 #endif  // !defined(_MIDI_MAPPING_DIALOG_H)