]> git.sesse.net Git - nageru/blob - nageru/midi_mapper.h
Fix a comment typo.
[nageru] / nageru / midi_mapper.h
1 #ifndef _MIDI_MAPPER_H
2 #define _MIDI_MAPPER_H 1
3
4 // MIDIMapper is a class that gets incoming MIDI messages from mixer
5 // controllers (ie., it is not meant to be used with regular instruments)
6 // via MIDIDevice, interprets them according to a device-specific, user-defined
7 // mapping, and calls back into a receiver (typically the MainWindow).
8 // This way, it is possible to control audio functionality using physical
9 // pots and faders instead of the mouse.
10
11 #include <atomic>
12 #include <functional>
13 #include <map>
14 #include <memory>
15 #include <mutex>
16 #include <string>
17 #include <thread>
18
19 #include "defs.h"
20 #include "shared/midi_device.h"
21
22 class MIDIMappingProto;
23
24 // Interface for receiving interpreted controller messages.
25 class ControllerReceiver {
26 public:
27         virtual ~ControllerReceiver() {}
28
29         // All values are [0.0, 1.0].
30         virtual void set_locut(float value) = 0;
31         virtual void set_limiter_threshold(float value) = 0;
32         virtual void set_makeup_gain(float value) = 0;
33
34         virtual void set_stereo_width(unsigned bus_idx, float value) = 0;
35         virtual void set_treble(unsigned bus_idx, float value) = 0;
36         virtual void set_mid(unsigned bus_idx, float value) = 0;
37         virtual void set_bass(unsigned bus_idx, float value) = 0;
38         virtual void set_gain(unsigned bus_idx, float value) = 0;
39         virtual void set_compressor_threshold(unsigned bus_idx, float value) = 0;
40         virtual void set_fader(unsigned bus_idx, float value) = 0;
41
42         virtual void toggle_mute(unsigned bus_idx) = 0;
43         virtual void toggle_locut(unsigned bus_idx) = 0;
44         virtual void toggle_auto_gain_staging(unsigned bus_idx) = 0;
45         virtual void toggle_compressor(unsigned bus_idx) = 0;
46         virtual void clear_peak(unsigned bus_idx) = 0;
47         virtual void toggle_limiter() = 0;
48         virtual void toggle_auto_makeup_gain() = 0;
49
50         // Non-audio events.
51         virtual void switch_video_channel(int channel_number) = 0;
52         virtual void apply_transition(int transition_number) = 0;
53         virtual void prev_audio_view() = 0;
54         virtual void next_audio_view() = 0;
55         virtual void begin_new_segment() = 0;
56         virtual void exit() = 0;
57
58         // Signals to highlight controls to mark them to the user
59         // as MIDI-controllable (or not).
60         virtual void clear_all_highlights() = 0;
61
62         virtual void highlight_locut(bool highlight) = 0;
63         virtual void highlight_limiter_threshold(bool highlight) = 0;
64         virtual void highlight_makeup_gain(bool highlight) = 0;
65
66         virtual void highlight_stereo_width(unsigned bus_idx, bool highlight) = 0;
67         virtual void highlight_treble(unsigned bus_idx, bool highlight) = 0;
68         virtual void highlight_mid(unsigned bus_idx, bool highlight) = 0;
69         virtual void highlight_bass(unsigned bus_idx, bool highlight) = 0;
70         virtual void highlight_gain(unsigned bus_idx, bool highlight) = 0;
71         virtual void highlight_compressor_threshold(unsigned bus_idx, bool highlight) = 0;
72         virtual void highlight_fader(unsigned bus_idx, bool highlight) = 0;
73
74         virtual void highlight_mute(unsigned bus_idx, bool highlight) = 0;
75         virtual void highlight_toggle_locut(unsigned bus_idx, bool highlight) = 0;
76         virtual void highlight_toggle_auto_gain_staging(unsigned bus_idx, bool highlight) = 0;
77         virtual void highlight_toggle_compressor(unsigned bus_idx, bool highlight) = 0;
78         virtual void highlight_clear_peak(unsigned bus_idx, bool highlight) = 0;
79         virtual void highlight_toggle_limiter(bool highlight) = 0;
80         virtual void highlight_toggle_auto_makeup_gain(bool highlight) = 0;
81
82         // Raw events; used for the editor dialog only.
83         virtual void controller_changed(unsigned controller) = 0;
84         virtual void note_on(unsigned note) = 0;
85 };
86
87 class MIDIMapper : public MIDIReceiver {
88 public:
89         MIDIMapper(ControllerReceiver *receiver);
90         virtual ~MIDIMapper();
91         void set_midi_mapping(const MIDIMappingProto &new_mapping);
92         void start_thread();
93         const MIDIMappingProto &get_current_mapping() const;
94
95         // Overwrites and returns the previous value.
96         ControllerReceiver *set_receiver(ControllerReceiver *new_receiver);
97
98         void refresh_highlights();
99         void refresh_lights();
100
101         void set_has_peaked(unsigned bus_idx, bool has_peaked)
102         {
103                 this->has_peaked[bus_idx] = has_peaked;
104         }
105
106         // MIDIReceiver.
107         void controller_received(int controller, int value) override;
108         void note_on_received(int note) override;
109         void update_num_subscribers(unsigned num_subscribers) override;
110
111 private:
112         void match_controller(int controller, int field_number, int bank_field_number, float value, std::function<void(unsigned, float)> func);
113         void match_button(int note, int field_number, int bank_field_number, std::function<void(unsigned)> func);
114         bool has_active_controller(unsigned bus_idx, int field_number, int bank_field_number);  // Also works for buttons.
115         bool bank_mismatch(int bank_field_number);
116
117         void update_highlights();
118
119         void update_lights_lock_held();
120         void activate_lights_all_buses(int field_number, std::map<MIDIDevice::LightKey, uint8_t> *active_lights);
121
122         std::atomic<bool> should_quit{false};
123
124         std::atomic<bool> has_peaked[MAX_BUSES] {{ false }};
125
126         mutable std::mutex mu;
127         ControllerReceiver *receiver;  // Under <mu>.
128         std::unique_ptr<MIDIMappingProto> mapping_proto;  // Under <mu>.
129         int num_controller_banks;  // Under <mu>.
130         std::atomic<int> current_controller_bank{0};
131         std::atomic<int> num_subscribed_ports{0};
132
133         MIDIDevice midi_device;
134 };
135
136 bool load_midi_mapping_from_file(const std::string &filename, MIDIMappingProto *new_mapping);
137 bool save_midi_mapping_to_file(const MIDIMappingProto &mapping_proto, const std::string &filename);
138
139 #endif  // !defined(_MIDI_MAPPER_H)