SpectMorph
smlivedecoder.hh
1 // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl-2.1.html
2 
3 #ifndef SPECTMORPH_LIVEDECODER_HH
4 #define SPECTMORPH_LIVEDECODER_HH
5 
6 #include "smwavset.hh"
7 #include "smsinedecoder.hh"
8 #include "smnoisedecoder.hh"
9 #include "smlivedecodersource.hh"
10 #include "smpolyphaseinter.hh"
11 #include "smalignedarray.hh"
12 #include <vector>
13 #include <functional>
14 
15 namespace SpectMorph {
16 
17 class LiveDecoderFilter;
19 {
20  static constexpr size_t PARTIAL_STATE_RESERVE = 2048; // maximum number of partials to expect
21  static constexpr size_t MAX_N_VALUES = 64; // maximum number of values to process at once
22  static constexpr size_t MAX_UNISON_VOICES = 7; // maximum number of unison voices
23 
24  struct PartialState
25  {
26  float freq;
27  float phase;
28  };
29  std::vector<PartialState> pstate[2], *last_pstate;
30 
31  struct PortamentoState {
32  std::vector<float> buffer;
33  double pos;
34  bool active;
35 
36  enum { DELTA = 32 };
37  } portamento_state;
38 
39  WavSet *smset;
40  Audio *audio;
41 
42  size_t block_size;
43  IFFTSynth ifft_synth;
44  NoiseDecoder noise_decoder;
45  LiveDecoderSource *source;
46  PolyPhaseInter *pp_inter;
47  RTMemoryArea *rt_memory_area = nullptr;
48  LiveDecoderFilter *filter = nullptr;
49  bool filter_latency_compensation;
50 
51  bool sines_enabled;
52  bool noise_enabled;
53  bool debug_fft_perf_enabled;
54  bool original_samples_enabled;
55  bool loop_enabled;
56  bool start_skip_enabled;
57 
58  double frame_step;
59  size_t zero_values_at_start_scaled;
60  size_t loop_start_scaled;
61  size_t loop_end_scaled;
62  int loop_point;
63  float current_freq;
64  float mix_freq;
65 
66  size_t have_samples;
67  size_t pos;
68  double env_pos;
69  size_t frame_idx;
70  double original_sample_pos;
71  double original_samples_norm_factor;
72 
73  int noise_seed;
74 
75  AlignedArray<float,16> sse_samples;
76 
77  // unison
78  int unison_voices;
79  std::vector<float> unison_phases[2];
80  std::vector<float> unison_freq_factor;
81  float unison_gain;
82  Random unison_phase_random_gen;
83 
84  // vibrato
85  bool vibrato_enabled;
86  float vibrato_depth;
87  float vibrato_frequency;
88  float vibrato_attack;
89  float vibrato_phase; // state
90  float vibrato_env; // state
91 
92  // timing related
93  double start_env_pos = 0;
94  bool in_process = false;
95 
96  // active/done
97  enum class DoneState {
98  DONE,
99  ACTIVE,
100  ALMOST_DONE
101  };
102  DoneState done_state = DoneState::DONE;
103 
104  Audio::LoopType get_loop_type();
105 
106  void process_internal (size_t n_values,
107  float *audio_out,
108  float portamento_stretch);
109 
110  void portamento_grow (double end_pos, float portamento_stretch);
111  void portamento_shrink();
112 
113  void process_portamento (size_t n_values,
114  const float *freq_in,
115  float *audio_out);
116  void process_vibrato (size_t n_values,
117  const float *freq_in,
118  float *audio_out);
119  void process_with_filter (size_t n_values,
120  const float *freq_in,
121  float *audio_out,
122  bool ramp);
123 
124 public:
125  LiveDecoder (float mix_freq);
126  LiveDecoder (WavSet *smset, float mix_freq);
127  LiveDecoder (LiveDecoderSource *source, float mix_freq);
128  ~LiveDecoder();
129 
130  void enable_noise (bool ne);
131  void enable_sines (bool se);
132  void enable_debug_fft_perf (bool dfp);
133  void enable_original_samples (bool eos);
134  void enable_loop (bool eloop);
135  void enable_start_skip (bool ess);
136  void set_noise_seed (int seed);
137  void set_unison_voices (int voices, float detune);
138  void set_vibrato (bool enable_vibrato, float depth, float frequency, float attack);
139  void set_filter (LiveDecoderFilter *filter);
140  void set_source (LiveDecoderSource *source);
141 
142  static void precompute_tables (float mix_freq);
143  void retrigger (int channel, float freq, int midi_velocity);
144  void process (RTMemoryArea& rt_memory_area,
145  size_t n_values,
146  const float *freq_in,
147  float *audio_out);
148 
149  double current_pos() const;
150  double fundamental_note() const;
151 
152  static size_t compute_loop_frame_index (size_t index, Audio *audio);
153  bool done() const;
154 
155  double time_offset_ms() const;
156 };
157 
158 }
159 #endif
Audio sample containing many blocks.
Definition: smaudio.hh:80
Definition: smifftsynth.hh:16
Definition: smlivedecoderfilter.hh:19
Definition: smlivedecodersource.hh:12
Definition: smlivedecoder.hh:19
Decoder for the noise component (stochastic component) of the signal.
Definition: smnoisedecoder.hh:17
Definition: smpolyphaseinter.hh:13
Definition: smrtmemory.hh:16
Definition: smrandom.hh:15
Definition: smwavset.hh:29