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 env;
33  double velocity;
34  double freq;
35  double pitch_bend_freq;
36  double pitch_bend_factor;
37  int pitch_bend_steps;
38  int note_id;
39 
40  Voice() :
41  mp_voice (NULL),
42  state (STATE_IDLE),
43  pedal (false)
44  {
45  }
46  ~Voice()
47  {
48  mp_voice = NULL;
49  }
50  };
51 
52  MorphPlanSynth morph_plan_synth;
53 
54  std::vector<Voice> voices;
55  std::vector<Voice *> idle_voices;
56  std::vector<Voice *> active_voices;
57  double mix_freq;
58  bool pedal_down;
59  size_t audio_time_stamp;
60  bool mono_enabled;
61  float portamento_glide;
62  int portamento_note_id;
63  int next_note_id;
64 
65  float control[2];
66 
67  Voice *alloc_voice();
68  void free_unused_voices();
69  bool update_mono_voice();
70  float freq_from_note (float note);
71 
72  void set_mono_enabled (bool new_value);
73  void process_audio (float *output, size_t n_values);
74  void process_note_on (int channel, int midi_note, int midi_velocity);
75  void process_note_off (int midi_note);
76  void process_midi_controller (int controller, int value);
77  void process_pitch_bend (int channel, double semi_tones);
78  void start_pitch_bend (Voice *voice, double dest_freq, double time_ms);
79 
80  struct MidiEvent
81  {
82  unsigned int offset;
83  char midi_data[3];
84 
85  bool is_note_on() const;
86  bool is_note_off() const;
87  bool is_controller() const;
88  bool is_pitch_bend() const;
89  int channel() const;
90  };
91  std::vector<MidiEvent> midi_events;
92 
93 public:
94  MidiSynth (double mix_freq, size_t n_voices);
95 
96  void add_midi_event (size_t offset, const unsigned char *midi_data);
97  void process (float *output, size_t n_values);
98 
99  void set_control_input (int i, float value);
100  void update_plan (MorphPlanPtr new_plan);
101 
102  size_t active_voice_count() const;
103 };
104 
105 }
106 
107 #endif /* SPECTMORPH_MIDI_SYNTH_HH */
Definition: smmorphplansynth.hh:15
Definition: smmorphplanvoice.hh:14
Definition: smalignedarray.cc:7
Definition: smmidisynth.hh:10