]> git.sesse.net Git - nageru/blob - midi_mapper.h
Rewrite the ALSA sequencer input loop.
[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 <string>
15 #include <thread>
16
17 class MIDIMappingProto;
18 typedef struct snd_seq_event snd_seq_event_t;
19 typedef struct _snd_seq snd_seq_t;
20
21 // Interface for receiving interpreted controller messages.
22 class ControllerReceiver {
23 public:
24         // All values are [0.0, 1.0].
25         virtual void set_locut(float value) = 0;
26         virtual void set_limiter_threshold(float value) = 0;
27         virtual void set_makeup_gain(float value) = 0;
28
29         virtual void set_treble(unsigned bus_idx, float value) = 0;
30         virtual void set_mid(unsigned bus_idx, float value) = 0;
31         virtual void set_bass(unsigned bus_idx, float value) = 0;
32         virtual void set_gain(unsigned bus_idx, float value) = 0;
33         virtual void set_compressor_threshold(unsigned bus_idx, float value) = 0;
34         virtual void set_fader(unsigned bus_idx, float value) = 0;
35
36         virtual void toggle_locut(unsigned bus_idx) = 0;
37         virtual void toggle_auto_gain_staging(unsigned bus_idx) = 0;
38         virtual void toggle_compressor(unsigned bus_idx) = 0;
39         virtual void clear_peak(unsigned bus_idx) = 0;
40 };
41
42 class MIDIMapper {
43 public:
44         MIDIMapper(ControllerReceiver *receiver);
45         virtual ~MIDIMapper();
46         void set_midi_mapping(const MIDIMappingProto &new_mapping);
47         void start_thread();
48         const MIDIMappingProto &get_current_mapping() const { return *mapping_proto; }
49
50 private:
51         void thread_func();
52         void handle_event(snd_seq_t *seq, snd_seq_event_t *event);
53         void match_controller(int controller, int field_number, int bank_field_number, float value, std::function<void(unsigned, float)> func);
54         void match_button(int note, int field_number, int bank_field_number, std::function<void(unsigned)> func);
55         bool bank_mismatch(int bank_field_number);
56
57         ControllerReceiver *receiver;
58         std::atomic<bool> should_quit{false};
59         int should_quit_fd;
60
61         std::unique_ptr<MIDIMappingProto> mapping_proto;
62         int num_controller_banks;
63         int current_controller_bank = 0;
64
65         std::thread midi_thread;
66 };
67
68 bool load_midi_mapping_from_file(const std::string &filename, MIDIMappingProto *new_mapping);
69
70 #endif  // !defined(_MIDI_MAPPER_H)