]> git.sesse.net Git - nageru/blob - midi_mapper.h
Support audio-only FFmpeg inputs. Somewhat wonky, though.
[nageru] / midi_mapper.h
1 #ifndef _MIDI_MAPPER_H
2 #define _MIDI_MAPPER_H 1
3
4 // MIDIMapper is a class that listens for incoming MIDI messages from
5 // mixer controllers (ie., it is not meant to be used with regular
6 // instruments), 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 <set>
17 #include <string>
18 #include <thread>
19
20 #include "defs.h"
21
22 class MIDIMappingProto;
23 typedef struct snd_seq_addr snd_seq_addr_t;
24 typedef struct snd_seq_event snd_seq_event_t;
25 typedef struct _snd_seq snd_seq_t;
26
27 // Interface for receiving interpreted controller messages.
28 class ControllerReceiver {
29 public:
30         // All values are [0.0, 1.0].
31         virtual void set_locut(float value) = 0;
32         virtual void set_limiter_threshold(float value) = 0;
33         virtual void set_makeup_gain(float value) = 0;
34
35         virtual void set_stereo_width(unsigned bus_idx, float value) = 0;
36         virtual void set_treble(unsigned bus_idx, float value) = 0;
37         virtual void set_mid(unsigned bus_idx, float value) = 0;
38         virtual void set_bass(unsigned bus_idx, float value) = 0;
39         virtual void set_gain(unsigned bus_idx, float value) = 0;
40         virtual void set_compressor_threshold(unsigned bus_idx, float value) = 0;
41         virtual void set_fader(unsigned bus_idx, float value) = 0;
42
43         virtual void toggle_mute(unsigned bus_idx) = 0;
44         virtual void toggle_locut(unsigned bus_idx) = 0;
45         virtual void toggle_auto_gain_staging(unsigned bus_idx) = 0;
46         virtual void toggle_compressor(unsigned bus_idx) = 0;
47         virtual void clear_peak(unsigned bus_idx) = 0;
48         virtual void toggle_limiter() = 0;
49         virtual void toggle_auto_makeup_gain() = 0;
50
51         // Signals to highlight controls to mark them to the user
52         // as MIDI-controllable (or not).
53         virtual void clear_all_highlights() = 0;
54
55         virtual void highlight_locut(bool highlight) = 0;
56         virtual void highlight_limiter_threshold(bool highlight) = 0;
57         virtual void highlight_makeup_gain(bool highlight) = 0;
58
59         virtual void highlight_stereo_width(unsigned bus_idx, bool highlight) = 0;
60         virtual void highlight_treble(unsigned bus_idx, bool highlight) = 0;
61         virtual void highlight_mid(unsigned bus_idx, bool highlight) = 0;
62         virtual void highlight_bass(unsigned bus_idx, bool highlight) = 0;
63         virtual void highlight_gain(unsigned bus_idx, bool highlight) = 0;
64         virtual void highlight_compressor_threshold(unsigned bus_idx, bool highlight) = 0;
65         virtual void highlight_fader(unsigned bus_idx, bool highlight) = 0;
66
67         virtual void highlight_mute(unsigned bus_idx, bool highlight) = 0;
68         virtual void highlight_toggle_locut(unsigned bus_idx, bool highlight) = 0;
69         virtual void highlight_toggle_auto_gain_staging(unsigned bus_idx, bool highlight) = 0;
70         virtual void highlight_toggle_compressor(unsigned bus_idx, bool highlight) = 0;
71         virtual void highlight_clear_peak(unsigned bus_idx, bool highlight) = 0;
72         virtual void highlight_toggle_limiter(bool highlight) = 0;
73         virtual void highlight_toggle_auto_makeup_gain(bool highlight) = 0;
74
75         // Raw events; used for the editor dialog only.
76         virtual void controller_changed(unsigned controller) = 0;
77         virtual void note_on(unsigned note) = 0;
78 };
79
80 class MIDIMapper {
81 public:
82         MIDIMapper(ControllerReceiver *receiver);
83         virtual ~MIDIMapper();
84         void set_midi_mapping(const MIDIMappingProto &new_mapping);
85         void start_thread();
86         const MIDIMappingProto &get_current_mapping() const;
87
88         // Overwrites and returns the previous value.
89         ControllerReceiver *set_receiver(ControllerReceiver *new_receiver);
90
91         void refresh_highlights();
92         void refresh_lights();
93
94         void set_has_peaked(unsigned bus_idx, bool has_peaked)
95         {
96                 this->has_peaked[bus_idx] = has_peaked;
97         }
98
99 private:
100         void thread_func();
101         void handle_event(snd_seq_t *seq, snd_seq_event_t *event);
102         void subscribe_to_port_lock_held(snd_seq_t *seq, const snd_seq_addr_t &addr);
103         void match_controller(int controller, int field_number, int bank_field_number, float value, std::function<void(unsigned, float)> func);
104         void match_button(int note, int field_number, int bank_field_number, std::function<void(unsigned)> func);
105         bool has_active_controller(unsigned bus_idx, int field_number, int bank_field_number);  // Also works for buttons.
106         bool bank_mismatch(int bank_field_number);
107
108         void update_highlights();
109
110         void update_lights_lock_held();
111         void activate_lights(unsigned bus_idx, int field_number, std::set<unsigned> *active_lights);
112         void activate_lights_all_buses(int field_number, std::set<unsigned> *active_lights);
113
114         std::atomic<bool> should_quit{false};
115         int should_quit_fd;
116
117         std::atomic<bool> has_peaked[MAX_BUSES] {{ false }};
118
119         mutable std::mutex mu;
120         ControllerReceiver *receiver;  // Under <mu>.
121         std::unique_ptr<MIDIMappingProto> mapping_proto;  // Under <mu>.
122         int num_controller_banks;  // Under <mu>.
123         std::atomic<int> current_controller_bank{0};
124         std::atomic<int> num_subscribed_ports{0};
125
126         std::thread midi_thread;
127         std::map<unsigned, bool> current_light_status;  // Keyed by note number. Under <mu>.
128         snd_seq_t *alsa_seq{nullptr};  // Under <mu>.
129         int alsa_queue_id{-1};  // Under <mu>.
130 };
131
132 bool load_midi_mapping_from_file(const std::string &filename, MIDIMappingProto *new_mapping);
133 bool save_midi_mapping_to_file(const MIDIMappingProto &mapping_proto, const std::string &filename);
134
135 #endif  // !defined(_MIDI_MAPPER_H)