aboutsummaryrefslogtreecommitdiff
path: root/buffer_override/bufferoverride.hpp
blob: 513a678f2245e10caf3d700e0322693d231721e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*------------------- by Marc Poirier  ][  March 2001 -------------------*/


#ifndef __bufferoverride
#define __bufferoverride

#include "dfxmisc.h"
#include "lfo.h"
#include "TempoRateTable.h"

#include "flext.h"

//----------------------------------------------------------------------------- 
// these are the plugin parameters:
enum
{
	kDivisor,
	kBuffer,
	kBufferTempoSync,
	kBufferInterrupt,

	kDivisorLFOrate,
	kDivisorLFOdepth,
	kDivisorLFOshape,
	kDivisorLFOtempoSync,
	kBufferLFOrate,
	kBufferLFOdepth,
	kBufferLFOshape,
	kBufferLFOtempoSync,

	kSmooth,
	kDryWetMix,

	kPitchbend,
	kMidiMode,

	kTempo,

	NUM_PARAMETERS
};

//----------------------------------------------------------------------------- 
// constants & macros

#define DIVISOR_MIN 1.92f
#define DIVISOR_MAX 222.0f
#define bufferDivisorScaled(A) ( paramRangeSquaredScaled((A), DIVISOR_MIN, DIVISOR_MAX) )
#define bufferDivisorUnscaled(A) ( paramRangeSquaredUnscaled((A), DIVISOR_MIN, DIVISOR_MAX) )

#define BUFFER_MIN 1.0f
#define BUFFER_MAX 999.0f
#define forcedBufferSizeScaled(A) ( paramRangeSquaredScaled((1.0f-(A)), BUFFER_MIN, BUFFER_MAX) )
#define forcedBufferSizeUnscaled(A) ( 1.0f - paramRangeSquaredUnscaled((A), BUFFER_MIN, BUFFER_MAX) )
#define forcedBufferSizeSamples(A) ( (long)(forcedBufferSizeScaled((A)) * SAMPLERATE * 0.001f) )

#define TEMPO_MIN 57.0f
#define TEMPO_MAX 480.0f
#define tempoScaled(A)   ( paramRangeScaled((A), TEMPO_MIN, TEMPO_MAX) )
#define tempoUnscaled(A)   ( paramRangeUnscaled((A), TEMPO_MIN, TEMPO_MAX) )

#define LFO_RATE_MIN 0.03f
#define LFO_RATE_MAX 21.0f
#define LFOrateScaled(A)   ( paramRangeSquaredScaled((A), LFO_RATE_MIN, LFO_RATE_MAX) )
#define LFOrateUnscaled(A)   ( paramRangeSquaredUnscaled((A), LFO_RATE_MIN, LFO_RATE_MAX) )

#define normalize(f,a,b)	((((f < b) ? f : b) > a) ? f : a)


// you need this stuff to get some maximum buffer size & allocate for that
// this is 42 bpm - should be sufficient
#define MIN_ALLOWABLE_BPS 0.7f


//----------------------------------------------------------------------------- 
class bufferoverrideProgram
{
friend class bufferoverride;
public:
	bufferoverrideProgram();
	~bufferoverrideProgram();
private:
	float *param;
	char *name;
};


//----------------------------------------------------------------------------- 

class bufferoverride : public flext_dsp  {

	FLEXT_HEADER(bufferoverride, flext_dsp)

public:
	bufferoverride(int argc, t_atom *argv);
	~bufferoverride();
	
	virtual void m_signal(int, t_sample *const *, t_sample *const *);
	virtual void m_help();

protected:
	void doTheProcess(float **inputs, float **outputs, long sampleFrames, bool replacing);
	void updateBuffer(long samplePos);

	void heedbufferoverrideEvents(long samplePos);
	float getDivisorParameterFromNote(int currentNote);
	float getDivisorParameterFromPitchbend(int pitchbendByte);

	void initPresets();
	void createAudioBuffers();

	// the parameters
	float fDivisor, fBuffer, fBufferTempoSync, fBufferInterrupt, fSmooth, 
	fDryWetMix, fPitchbend, fMidiMode, fTempo;

	long currentForcedBufferSize;	// the size of the larger, imposed buffer
	// these store the forced buffer
	float *buffer1;

	long writePos;	// the current sample position within the forced buffer

	long minibufferSize;	// the current size of the divided "mini" buffer
	long prevMinibufferSize;	// the previous size
	long readPos;	// the current sample position within the minibuffer
	float currentBufferDivisor;	// the current value of the divisor with LFO possibly applied

	float numLFOpointsDivSR;	// the number of LFO table points divided by the sampling rate

	float currentTempoBPS;	// tempo in beats per second
	TempoRateTable *tempoRateTable;	// a table of tempo rate values
	long hostCanDoTempo;	// my semi-booly dude who knows something about the host's VstTimeInfo implementation
	bool needResync;

	long SUPER_MAX_BUFFER;
	float SAMPLERATE;

	long smoothDur, smoothcount;	// total duration & sample counter for the minibuffer transition smoothing period
	float smoothStep;	// the gain increment for each sample "step" during the smoothing period
	float sqrtFadeIn, sqrtFadeOut;	// square root of the smoothing gains, for equal power crossfading
	float smoothFract;

	bool divisorWasChangedByHand;	// for MIDI trigger mode - tells us to respect the fDivisor value
	bool divisorWasChangedByMIDI;	// tells the GUI that the divisor displays need updating

	LFO *divisorLFO, *bufferLFO;

	float fadeOutGain, fadeInGain, realFadePart, imaginaryFadePart;	// for trig crossfading







	virtual void suspend();

	// flext
	
	FLEXT_CALLBACK_F(setDivisor)
	void setDivisor(float f) {
		fDivisor = bufferDivisorUnscaled(normalize(f,DIVISOR_MIN,DIVISOR_MAX));
	}
	
	FLEXT_CALLBACK_F(setBuffer)
	void setBuffer(float f) {
		fBuffer = forcedBufferSizeUnscaled(normalize(f,BUFFER_MIN,BUFFER_MAX));
	}
	
/*	FLEXT_CALLBACK_F(setBufferTempoSync)
	void setBufferTempoSync(float f) {
		fBufferTempoSync = normalize(f,0,1);
	}
	
	FLEXT_CALLBACK_F(setBufferInterrupt)
	void setBufferInterrupt(float f) {
		fBufferInterrupt = normalize(f,0,1);
	}
	
	FLEXT_CALLBACK_F(setSmooth)
	void setSmooth(float f) {
		fSmooth = normalize(f,0,1);
	}
	
	FLEXT_CALLBACK_F(setDryWetMix)
	void setDryWetMix(float f) {
		fDryWetMix = normalize(f,0,1);
	}
*/	
	FLEXT_CALLBACK_F(setTempo)
	void setTempo(float f) {
		fTempo = normalize(f,0,1);
	}
	
	FLEXT_CALLBACK_F(setDivisorLFOrate)
	void setDivisorLFOrate(float f) {
		divisorLFO->fRate = paramSteppedUnscaled(f, NUM_TEMPO_RATES);
	}
	
	FLEXT_CALLBACK_F(setDivisorLFOdepth)
	void setDivisorLFOdepth(float f) {
		divisorLFO->fDepth = normalize(f,0,1);
	}
	
	FLEXT_CALLBACK_F(setDivisorLFOshape)
	void setDivisorLFOshape(float f) {
		divisorLFO->fShape = LFOshapeUnscaled(normalize(f,0,7));
	}
	
	
	FLEXT_CALLBACK_F(setBufferLFOrate)
	void setBufferLFOrate(float f) {
		bufferLFO->fRate = paramSteppedUnscaled(f, NUM_TEMPO_RATES);
	}
	
	FLEXT_CALLBACK_F(setBufferLFOdepth)
	void setBufferLFOdepth(float f) {
		bufferLFO->fDepth = normalize(f,0,1);
	}
	
	FLEXT_CALLBACK_F(setBufferLFOshape)
	void setBufferLFOshape(float f) {
		bufferLFO->fShape = LFOshapeUnscaled(normalize(f,0,7));
	}


};

#endif