Amuse
Voice.hpp
1 #ifndef __AMUSE_VOICE_HPP__
2 #define __AMUSE_VOICE_HPP__
3 
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <memory>
7 #include <list>
8 #include "SoundMacroState.hpp"
9 #include "Entity.hpp"
10 #include "AudioGroupSampleDirectory.hpp"
11 #include "AudioGroup.hpp"
12 #include "Envelope.hpp"
13 
14 namespace amuse
15 {
16 class IBackendVoice;
17 class Submix;
18 struct Keymap;
19 struct LayerMapping;
20 
22 enum class VoiceState
23 {
24  Playing,
25  KeyOff,
26  Dead
27 };
28 
30 class Voice : public Entity
31 {
32  friend class Engine;
33  friend class Sequencer;
34  friend class SoundMacroState;
35  int m_vid;
36  bool m_emitter;
37  Submix* m_submix = nullptr;
38  std::list<std::shared_ptr<Voice>>::iterator m_engineIt;
40  std::unique_ptr<IBackendVoice> m_backendVoice;
41  SoundMacroState m_state;
42  SoundMacroState::EventTrap m_keyoffTrap;
43  SoundMacroState::EventTrap m_sampleEndTrap;
44  SoundMacroState::EventTrap m_messageTrap;
45  std::list<int32_t> m_messageQueue;
46  std::list<std::shared_ptr<Voice>> m_childVoices;
47  uint8_t m_keygroup = 0;
49  enum class SampleFormat : uint8_t
50  {
51  DSP,
52  IMA,
53  PCM
54  };
55  const Sample* m_curSample = nullptr;
56  const unsigned char* m_curSampleData = nullptr;
57  SampleFormat m_curFormat;
58  uint32_t m_curSamplePos = 0;
59  uint32_t m_lastSamplePos = 0;
60  int16_t m_prev1 = 0;
61  int16_t m_prev2 = 0;
62  double m_sampleRate = 32000.0;
63  double m_voiceTime;
65  VoiceState m_voxState = VoiceState::Dead;
66  bool m_sustained = false;
67  bool m_sustainKeyOff = false;
68  uint8_t m_curAftertouch = 0;
70  float m_userVol = 1.f;
71  float m_curVol;
72  float m_curReverbVol;
73  float m_curPan;
74  float m_curSpan;
75  float m_curPitchWheel = 0.f;
76  int32_t m_pitchWheelUp;
77  int32_t m_pitchWheelDown;
78  int32_t m_pitchWheelVal;
79  int32_t m_curPitch;
80  bool m_pitchDirty;
82  Envelope m_volAdsr;
83  double m_envelopeTime;
84  double m_envelopeDur;
85  float m_envelopeStart;
86  float m_envelopeEnd;
87  const Curve* m_envelopeCurve;
89  bool m_pitchEnv = false;
90  Envelope m_pitchAdsr;
91  int32_t m_pitchEnvRange;
93  uint32_t m_pitchSweep1;
94  uint32_t m_pitchSweep2;
95  int16_t m_pitchSweep1Add;
96  int16_t m_pitchSweep2Add;
97  uint8_t m_pitchSweep1Times;
98  uint8_t m_pitchSweep2Times;
99  uint8_t m_pitchSweep1It;
100  uint8_t m_pitchSweep2It;
102  float m_panningTime;
103  float m_panningDur;
104  uint8_t m_panPos;
105  uint8_t m_panWidth;
107  float m_spanningTime;
108  float m_spanningDur;
109  uint8_t m_spanPos;
110  uint8_t m_spanWidth;
112  int32_t m_vibratoLevel;
113  int32_t m_vibratoModLevel;
114  float m_vibratoPeriod;
115  bool m_vibratoModWheel;
117  float m_tremoloScale;
118  float m_tremoloModScale;
120  float m_lfoPeriods[2];
121  std::unique_ptr<int8_t[]> m_ctrlValsSelf;
122  int8_t* m_extCtrlVals = nullptr;
124  void _destroy();
125  void _reset();
126  bool _checkSamplePos();
127  void _doKeyOff();
128  void _macroKeyOff();
129  void _macroSampleEnd();
130  bool _advanceSample(int16_t& samp, int32_t& curPitch);
131  void _setTotalPitch(int32_t cents, bool slew);
132  bool _isRecursivelyDead();
133  void _bringOutYourDead();
134  std::shared_ptr<Voice> _findVoice(int vid, std::weak_ptr<Voice> thisPtr);
135  std::unique_ptr<int8_t[]>& _ensureCtrlVals();
136 
137  std::shared_ptr<Voice> _allocateVoice(double sampleRate, bool dynamicPitch);
138  std::list<std::shared_ptr<Voice>>::iterator _destroyVoice(Voice* voice);
139 
140  bool _loadSoundMacro(const unsigned char* macroData, int macroStep, double ticksPerSec,
141  uint8_t midiKey, uint8_t midiVel, uint8_t midiMod, bool pushPc=false);
142  bool _loadKeymap(const Keymap* keymap, int macroStep, double ticksPerSec,
143  uint8_t midiKey, uint8_t midiVel, uint8_t midiMod, bool pushPc=false);
144  bool _loadLayer(const std::vector<const LayerMapping*>& layer, int macroStep, double ticksPerSec,
145  uint8_t midiKey, uint8_t midiVel, uint8_t midiMod, bool pushPc=false);
146  std::shared_ptr<Voice> _startChildMacro(ObjectId macroId, int macroStep, double ticksPerSec,
147  uint8_t midiKey, uint8_t midiVel, uint8_t midiMod, bool pushPc=false);
148 public:
149  ~Voice();
150  Voice(Engine& engine, const AudioGroup& group, int groupId, int vid, bool emitter, Submix* smx);
151  Voice(Engine& engine, const AudioGroup& group, int groupId, ObjectId oid, int vid, bool emitter, Submix* smx);
152 
155  size_t supplyAudio(size_t frames, int16_t* data);
156 
158  Submix* getSubmix() {return m_submix;}
159 
161  VoiceState state() const {return m_voxState;}
162 
164  int vid() const {return m_vid;}
165 
167  int maxVid() const;
168 
170  std::shared_ptr<Voice> startChildMacro(int8_t addNote, ObjectId macroId, int macroStep);
171 
173  bool loadSoundObject(ObjectId objectId, int macroStep, double ticksPerSec,
174  uint8_t midiKey, uint8_t midiVel, uint8_t midiMod,
175  bool pushPc=false);
176 
178  void keyOff();
179 
181  void message(int32_t val);
182 
184  void startSample(int16_t sampId, int32_t offset);
185 
187  void stopSample();
188 
190  void setVolume(float vol);
191 
193  void setPan(float pan);
194 
196  void setSurroundPan(float span);
197 
199  void startEnvelope(double dur, float vol, const Curve* envCurve);
200 
202  void startFadeIn(double dur, float vol, const Curve* envCurve);
203 
205  void startPanning(double dur, uint8_t panPos, uint8_t panWidth);
206 
208  void startSpanning(double dur, uint8_t spanPos, uint8_t spanWidth);
209 
211  void setPitchKey(int32_t cents);
212 
214  void setPedal(bool pedal);
215 
217  void setDoppler(float doppler);
218 
220  void setVibrato(int32_t level, int32_t modLevel, float period);
221 
223  void setMod2VibratoRange(int32_t modLevel);
224 
226  void setTremolo(float tremoloScale, float tremoloModScale);
227 
229  void setLFO1Period(float period) {m_lfoPeriods[0] = period;}
230 
232  void setLFO2Period(float period) {m_lfoPeriods[1] = period;}
233 
235  void setPitchSweep1(uint8_t times, int16_t add);
236 
238  void setPitchSweep2(uint8_t times, int16_t add);
239 
241  void setReverbVol(float rvol);
242 
244  void setAdsr(ObjectId adsrId, bool dls);
245 
247  void setPitchFrequency(uint32_t hz, uint16_t fine);
248 
250  void setPitchAdsr(ObjectId adsrId, int32_t cents);
251 
253  void setPitchWheel(float pitchWheel);
254 
256  void setPitchWheelRange(int8_t up, int8_t down);
257 
259  void setAftertouch(uint8_t aftertouch);
260 
262  void setKeygroup(uint8_t kg) {m_keygroup = kg;}
263 
265  uint8_t getLastNote() const {return m_state.m_initKey;}
266 
267  void notifyCtrlChange(uint8_t ctrl, int8_t val);
268 
270  int8_t getCtrlValue(uint8_t ctrl) const
271  {
272  if (!m_extCtrlVals)
273  {
274  if (m_ctrlValsSelf)
275  m_ctrlValsSelf[ctrl];
276  return 0;
277  }
278  return m_extCtrlVals[ctrl];
279  }
280 
282  void setCtrlValue(uint8_t ctrl, int8_t val)
283  {
284  if (!m_extCtrlVals)
285  {
286  std::unique_ptr<int8_t[]>& vals = _ensureCtrlVals();
287  vals[ctrl] = val;
288  }
289  else
290  m_extCtrlVals[ctrl] = val;
291  notifyCtrlChange(ctrl, val);
292  }
293 
295  int8_t getModWheel() const {return getCtrlValue(1);}
296 
298  void installCtrlValues(int8_t* cvs)
299  {
300  m_ctrlValsSelf.reset();
301  m_extCtrlVals = cvs;
302  }
303 
305  int8_t getPitchWheel() const {return m_curPitchWheel * 127;}
306 
308  int8_t getAftertouch() const {return m_curAftertouch;}
309 
311  size_t getTotalVoices() const;
312 
313 };
314 
315 }
316 
317 #endif // __AMUSE_VOICE_HPP__
int8_t getAftertouch() const
Definition: Voice.hpp:308
int8_t getModWheel() const
Definition: Voice.hpp:295
int8_t getPitchWheel() const
Definition: Voice.hpp:305
void setLFO2Period(float period)
Definition: Voice.hpp:232
void setKeygroup(uint8_t kg)
Definition: Voice.hpp:262
void installCtrlValues(int8_t *cvs)
Definition: Voice.hpp:298
uint8_t getLastNote() const
Definition: Voice.hpp:265
Submix * getSubmix()
Definition: Voice.hpp:158
void setCtrlValue(uint8_t ctrl, int8_t val)
Definition: Voice.hpp:282
int vid() const
Definition: Voice.hpp:164
void setLFO1Period(float period)
Definition: Voice.hpp:229
int8_t getCtrlValue(uint8_t ctrl) const
Definition: Voice.hpp:270
VoiceState state() const
Definition: Voice.hpp:161