SpectMorph
smmidisynth.hh
1 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html
2 
3 #ifndef SPECTMORPH_MIDI_SYNTH_HH
4 #define SPECTMORPH_MIDI_SYNTH_HH
5 
6 #include "smmorphplansynth.hh"
7 
8 namespace SpectMorph {
9 
10 class MidiSynth
11 {
12  class Voice
13  {
14  public:
15  enum State {
16  STATE_IDLE,
17  STATE_ON,
18  STATE_RELEASE
19  };
20  enum class MonoType {
21  POLY,
22  MONO,
23  SHADOW
24  };
25  MorphPlanVoice *mp_voice;
26 
27  State state;
28  MonoType mono_type;
29  bool pedal;
30  int midi_note;
31  int channel;
32  double velocity;
33  double freq;
34  double pitch_bend_freq;
35  double pitch_bend_factor;
36  int pitch_bend_steps;
37  int note_id;
38 
39  Voice() :
40  mp_voice (NULL),
41  state (STATE_IDLE),
42  pedal (false)
43  {
44  }
45  ~Voice()
46  {
47  mp_voice = NULL;
48  }
49  };
50 
51  MorphPlanSynth morph_plan_synth;
52 
53  std::vector<Voice> voices;
54  std::vector<Voice *> idle_voices;
55  std::vector<Voice *> active_voices;
56  double mix_freq;
57  bool pedal_down;
58  size_t audio_time_stamp;
59  bool mono_enabled;
60  float portamento_glide;
61  int portamento_note_id;
62  int next_note_id;
63 
64  float control[2];
65 
66  Voice *alloc_voice();
67  void free_unused_voices();
68  bool update_mono_voice();
69  float freq_from_note (float note);
70 
71  void set_mono_enabled (bool new_value);
72  void process_audio (float *output, size_t n_values);
73  void process_note_on (int channel, int midi_note, int midi_velocity);
74  void process_note_off (int midi_note);
75  void process_midi_controller (int controller, int value);
76  void process_pitch_bend (int channel, double semi_tones);
77  void start_pitch_bend (Voice *voice, double dest_freq, double time_ms);
78 
79  struct MidiEvent
80  {
81  unsigned int offset;
82  char midi_data[3];
83 
84  bool is_note_on() const;
85  bool is_note_off() const;
86  bool is_controller() const;
87  bool is_pitch_bend() const;
88  int channel() const;
89  };
90  std::vector<MidiEvent> midi_events;
91 
92 public:
93  MidiSynth (double mix_freq, size_t n_voices);
94 
95  void add_midi_event (size_t offset, const unsigned char *midi_data);
96  void process (float *output, size_t n_values);
97 
98  void set_control_input (int i, float value);
99  void update_plan (MorphPlanPtr new_plan);
100 
101  size_t active_voice_count() const;
102 };
103 
104 }
105 
106 #endif /* SPECTMORPH_MIDI_SYNTH_HH */
Definition: smmorphplansynth.hh:15
Definition: smmorphplanvoice.hh:14
Definition: smadsrenvelope.hh:8
Definition: smmidisynth.hh:10