iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
VoiceAllocator.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9 */
10
11#pragma once
12
18#include <array>
19#include <vector>
20#include <stdint.h>
21#include <functional>
22#include <bitset>
23#include <climits>
24#include <memory>
25//#include <iostream>
26
27#include "IPlugLogger.h"
28#include "IPlugQueue.h"
29
30#include "SynthVoice.h"
31
32BEGIN_IPLUG_NAMESPACE
33
34using namespace voiceControlNames;
35
37{
38 uint8_t mZone;
39 uint8_t mChannel;
40 uint8_t mKey;
41 uint8_t mFlags;
42};
43
44const uint8_t kAllZones = UCHAR_MAX;
45const uint8_t kAllChannels = UCHAR_MAX;
46const uint8_t kAllKeys = UCHAR_MAX;
47
48// flags
49const uint8_t kVoicesBusy = 1 << 0;
50const uint8_t kVoicesMostRecent = 1 << 1;
51const uint8_t kVoicesAll = 1 << 2;
52
53enum EVoiceAction
54{
55 kNullAction = 0,
56 kNoteOnAction,
57 kNoteOffAction,
58 kPitchBendAction,
59 kPressureAction,
60 kTimbreAction,
61 kSustainAction,
62 kControllerAction,
63 kProgramChangeAction
64};
65
75{
76 VoiceAddress mAddress;
77 EVoiceAction mAction;
78 int mControllerNumber;
79 float mValue;
80 int mSampleOffset;
81 bool mImmediate;
82};
83
84#pragma mark - VoiceAllocator class
85
86class VoiceAllocator final
87{
88public:
89
90 enum EATMode
91 {
92 kATModeChannel = 0,
93 kATModePoly,
94 kNumATModes
95 };
96
97 enum EPolyMode
98 {
99 kPolyModePoly = 0,
100 kPolyModeMono,
101 kNumPolyModes
102 };
103
104 static constexpr int kVoiceMostRecent = 1 << 7;
105
106 // one voice worth of ramp generators
107 using VoiceControlRamps = ControlRampProcessor::ProcessorArray<kNumVoiceControlRamps>;
108
111
112 VoiceAllocator(const VoiceAllocator&) = delete;
113 VoiceAllocator& operator=(const VoiceAllocator&) = delete;
114
115 void Clear();
116
117 void SetSampleRateAndBlockSize(double sampleRate, int blockSize) { mSampleRate = sampleRate; CalcGlideTimesInSamples(); }
118 void SetNoteGlideTime(double t) { mNoteGlideTime = t; CalcGlideTimesInSamples(); }
119 void SetControlGlideTime(double t) { mControlGlideTime = t; CalcGlideTimesInSamples(); }
120
124 void AddVoice(SynthVoice* pv, uint8_t zone);
125
127 void AddEvent(VoiceInputEvent e) { mInputQueue.Push(e); }
128
130 void ProcessEvents(int samples, int64_t sampleTime);
131
133 void SoftKillAllVoices();
134
136 void HardKillAllVoices();
137
138 void SetKeyToPitchFunction(const std::function<float(int)>& fn) {mKeyToPitchFn = fn;}
139
142
143 void ProcessVoices(sample** inputs, sample** outputs, int nInputs, int nOutputs, int startIndex, int blockSize);
144
145 size_t GetNVoices() const {return mVoicePtrs.size();}
146 SynthVoice* GetVoice(int voiceIndex) const {return mVoicePtrs[voiceIndex];}
147 void SetPitchOffset(float offset) { mPitchOffset = offset; }
148
149private:
150 using VoiceBitsArray = std::bitset<UCHAR_MAX>;
151
152 VoiceBitsArray VoicesMatchingAddress(VoiceAddress va);
153
154 void SendControlToVoiceInputs(VoiceBitsArray v, int ctlIdx, float val, int glideSamples, int startOffset = 0);
155 void SendExpressionToVoiceInputs(VoiceBitsArray v, int ctlIdx, const VoiceInputEvent& event);
156 void SendControlToVoicesDirect(VoiceBitsArray v, int ctlIdx, float val);
157 void SendProgramChangeToVoices(VoiceBitsArray v, int pgm);
158
159 void StartVoice(int voiceIdx, int channel, int key, float pitch, float velocity, int sampleOffset, int64_t sampleTime, bool retrig);
160 void StartVoices(VoiceBitsArray voices, int channel, int key, float pitch, float velocity, int sampleOffset, int64_t sampleTime, bool retrig);
161
162 void StopVoice(int voiceIdx, int sampleOffset);
163 void StopVoices(VoiceBitsArray voices, int sampleOffset);
164
165 void CalcGlideTimesInSamples();
166 void ClearVoiceInputs(SynthVoice* pVoice);
167 int FindFreeVoiceIndex(int startIndex, uint8_t zone) const;
168 int FindVoiceIndexToSteal(int64_t sampleTime, uint8_t zone) const;
169
170 void NoteOn(VoiceInputEvent e, int64_t sampleTime);
171 void NoteOff(VoiceInputEvent e, int64_t sampleTime);
172
173 IPlugQueue<VoiceInputEvent> mInputQueue{1024};
174
175 std::vector<SynthVoice*> mVoicePtrs;
176 std::vector<std::unique_ptr<VoiceControlRamps>> mVoiceGlides;
177 std::vector<int> mHeldKeys; // The currently physically held keys on the keyboard
178 std::vector<int> mSustainedNotes; // Any notes that are sustained, including those that are physically held
179 VoiceBitsArray mNoteOnVoices; // The voices started by the last note on
180
181 std::function<float(int)> mKeyToPitchFn;
182 double mPitchOffset{0.};
183
184 double mNoteGlideTime{0.};
185 double mControlGlideTime{0.01};
186 int mNoteGlideSamples{0}; // glide for note-to-note portamento
187 int mControlGlideSamples{0}; // glide for controls including pitch bend
188 double mSampleRate;
189 int mBlockSize;
190
191 bool mRotateVoices{true};
192 int mVoiceRotateIndex{0};
193 bool mSustainPedalDown{false};
194 float mModWheel{0.f};
195 float mMinHeldVelocity{1.f};
196
197public:
198 EPolyMode mPolyMode {kPolyModePoly};
199 EATMode mATMode {kATModeChannel};
200};
201
202END_IPLUG_NAMESPACE
IPlug logging a.k.a tracing functionality.
A VoiceInputEvent describes a change in input to be applied to one more more voices.
A lock-free SPSC queue used to transfer data between threads based on MLQueue.h by Randy Jones based ...
Definition: IPlugQueue.h:32
bool Push(const T &item)
Adds an item to the queue.
Definition: IPlugQueue.h:57
void HardKillAllVoices()
Stop all voices from making sound immdiately.
void AddEvent(VoiceInputEvent e)
Add a single event to the input queue for the current processing block.
void SendEventToVoices(VoiceInputEvent event)
Send the event to the voices matching its address.
void SoftKillAllVoices()
Turn all voice gates off, allowing any voice envelopes to finish.
void ProcessEvents(int samples, int64_t sampleTime)
Process all input events and generate voice outputs.
void AddVoice(SynthVoice *pv, uint8_t zone)
Add a synth voice to the allocator.
A generic synthesizer voice to be controlled by a voice allocator.
Definition: SynthVoice.h:35