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  MorphPlanVoice *mp_voice;
21 
22  State state;
23  bool pedal;
24  int midi_note;
25  double env;
26  double velocity;
27 
28  Voice() :
29  mp_voice (NULL),
30  state (STATE_IDLE),
31  pedal (false)
32  {
33  }
34  ~Voice()
35  {
36  mp_voice = NULL;
37  }
38  };
39 
40  MorphPlanSynth morph_plan_synth;
41 
42  std::vector<Voice> voices;
43  std::vector<Voice *> idle_voices;
44  std::vector<Voice *> active_voices;
45  double mix_freq;
46  bool pedal_down;
47 
48  float control[2];
49 
50  Voice *alloc_voice();
51  void free_unused_voices();
52  float freq_from_note (float note);
53 
54  void process_audio (float *output, size_t n_values);
55  void process_note_on (int midi_note, int midi_velocity);
56  void process_note_off (int midi_note);
57  void process_midi_controller (int controller, int value);
58 
59  struct MidiEvent
60  {
61  unsigned int offset;
62  char midi_data[3];
63 
64  bool is_note_on() const;
65  bool is_note_off() const;
66  bool is_controller() const;
67  };
68  std::vector<MidiEvent> midi_events;
69 
70 public:
71  MidiSynth (double mix_freq, size_t n_voices);
72 
73  void add_midi_event (size_t offset, const unsigned char *midi_data);
74  void process (float *output, size_t n_values);
75 
76  void set_control_input (int i, float value);
77  void update_plan (MorphPlanPtr new_plan);
78 
79  size_t active_voice_count() const;
80 };
81 
82 }
83 
84 #endif /* SPECTMORPH_MIDI_SYNTH_HH */
Definition: smmorphplansynth.hh:15
Definition: smmorphplanvoice.hh:14
Definition: smaudio.hh:15
Definition: smmidisynth.hh:10