]> git.sesse.net Git - nageru/blob - midi_mapper.h
Make sure MIDI mapping extrapolation doesn't give out-of-range values.
[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 <memory>
14 #include <mutex>
15 #include <string>
16 #include <thread>
17
18 class MIDIMappingProto;
19 typedef struct snd_seq_addr snd_seq_addr_t;
20 typedef struct snd_seq_event snd_seq_event_t;
21 typedef struct _snd_seq snd_seq_t;
22
23 // Interface for receiving interpreted controller messages.
24 class ControllerReceiver {
25 public:
26         // All values are [0.0, 1.0].
27         virtual void set_locut(float value) = 0;
28         virtual void set_limiter_threshold(float value) = 0;
29         virtual void set_makeup_gain(float value) = 0;
30
31         virtual void set_treble(unsigned bus_idx, float value) = 0;
32         virtual void set_mid(unsigned bus_idx, float value) = 0;
33         virtual void set_bass(unsigned bus_idx, float value) = 0;
34         virtual void set_gain(unsigned bus_idx, float value) = 0;
35         virtual void set_compressor_threshold(unsigned bus_idx, float value) = 0;
36         virtual void set_fader(unsigned bus_idx, float value) = 0;
37
38         virtual void toggle_locut(unsigned bus_idx) = 0;
39         virtual void toggle_auto_gain_staging(unsigned bus_idx) = 0;
40         virtual void toggle_compressor(unsigned bus_idx) = 0;
41         virtual void clear_peak(unsigned bus_idx) = 0;
42
43         // Raw events; used for the editor dialog only.
44         virtual void controller_changed(unsigned controller) = 0;
45         virtual void note_on(unsigned note) = 0;
46 };
47
48 class MIDIMapper {
49 public:
50         MIDIMapper(ControllerReceiver *receiver);
51         virtual ~MIDIMapper();
52         void set_midi_mapping(const MIDIMappingProto &new_mapping);
53         void start_thread();
54         const MIDIMappingProto &get_current_mapping() const;
55
56         // Overwrites and returns the previous value.
57         ControllerReceiver *set_receiver(ControllerReceiver *new_receiver);
58
59 private:
60         void thread_func();
61         void handle_event(snd_seq_t *seq, snd_seq_event_t *event);
62         void subscribe_to_port(snd_seq_t *seq, const snd_seq_addr_t &addr);
63         void match_controller(int controller, int field_number, int bank_field_number, float value, std::function<void(unsigned, float)> func);
64         void match_button(int note, int field_number, int bank_field_number, std::function<void(unsigned)> func);
65         bool bank_mismatch(int bank_field_number);
66
67         std::atomic<bool> should_quit{false};
68         int should_quit_fd;
69
70         mutable std::mutex mapping_mu;
71         ControllerReceiver *receiver;  // Under <mapping_mu>.
72         std::unique_ptr<MIDIMappingProto> mapping_proto;  // Under <mapping_mu>.
73         int num_controller_banks;  // Under <mapping_mu>.
74         std::atomic<int> current_controller_bank{0};
75
76         std::thread midi_thread;
77 };
78
79 bool load_midi_mapping_from_file(const std::string &filename, MIDIMappingProto *new_mapping);
80 bool save_midi_mapping_to_file(const MIDIMappingProto &mapping_proto, const std::string &filename);
81
82 #endif  // !defined(_MIDI_MAPPER_H)