1 /*
2  *  mdaDetuneProcessor.cpp
3  *  mda-vst3
4  *
5  *  Created by Arne Scheffler on 6/14/08.
6  *
7  *  mda VST Plug-ins
8  *
9  *  Copyright (c) 2008 Paul Kellett
10  *
11  *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14  *
15  */
16 
17 #include "mdaDetuneProcessor.h"
18 #include "mdaDetuneController.h"
19 
20 #include <math.h>
21 
22 namespace Steinberg {
23 namespace Vst {
24 namespace mda {
25 
26 #define BUFMAX   4096
27 
28 //-----------------------------------------------------------------------------
29 FUID DetuneProcessor::uid (0x4CCBED11, 0xE28346A6, 0xA91EC86C, 0x9E85EDF8);
30 
31 //-----------------------------------------------------------------------------
DetuneProcessor()32 DetuneProcessor::DetuneProcessor ()
33 {
34 	setControllerClass (DetuneController::uid);
35 	allocParameters (4);
36 	buf=win=0;
37 }
38 
39 //-----------------------------------------------------------------------------
~DetuneProcessor()40 DetuneProcessor::~DetuneProcessor ()
41 {
42 }
43 
44 //-----------------------------------------------------------------------------
initialize(FUnknown * context)45 tresult PLUGIN_API DetuneProcessor::initialize (FUnknown* context)
46 {
47 	tresult res = BaseProcessor::initialize (context);
48 	if (res == kResultTrue)
49 	{
50 		addAudioInput (USTRING("Stereo In"), SpeakerArr::kStereo);
51 		addAudioOutput (USTRING("Stereo Out"), SpeakerArr::kStereo);
52 
53 		params[0] = 0.40f;  //fine
54 		params[1] = 0.40f;  //mix
55 		params[2] = 0.50f;  //output
56 		params[3] = 0.50f;  //chunksize
57 
58 		buf = new float[BUFMAX];
59 		win = new float[BUFMAX];
60 		buflen=0;
61 
62 		recalculate ();
63 	}
64 	return res;
65 }
66 
67 //-----------------------------------------------------------------------------
terminate()68 tresult PLUGIN_API DetuneProcessor::terminate ()
69 {
70 	if (buf) delete [] buf;
71 	if (win) delete [] win;
72 	buf=win=0;
73 	return BaseProcessor::terminate ();
74 }
75 
76 //-----------------------------------------------------------------------------
setActive(TBool state)77 tresult PLUGIN_API DetuneProcessor::setActive (TBool state)
78 {
79 	if (state)
80 	{
81 		memset (buf, 0, BUFMAX * sizeof (float));
82 		pos0 = 0; pos1 = pos2 = 0.0f;
83 	}
84 	return BaseProcessor::setActive (state);
85 }
86 
87 //-----------------------------------------------------------------------------
doProcessing(ProcessData & data)88 void DetuneProcessor::doProcessing (ProcessData& data)
89 {
90 	int32 sampleFrames = data.numSamples;
91 
92 	float* in1 = data.inputs[0].channelBuffers32[0];
93 	float* in2 = data.inputs[0].channelBuffers32[1];
94 	float* out1 = data.outputs[0].channelBuffers32[0];
95 	float* out2 = data.outputs[0].channelBuffers32[1];
96 
97 	float a, b, c, d;
98 	float x, w=wet, y =dry, p1=pos1, p1f, d1=dpos1;
99 	float                  p2=pos2,      d2=dpos2;
100 	int32  p0=pos0, p1i, p2i;
101 	int32  l=buflen-1, lh=buflen>>1;
102 	float lf = (float)buflen;
103 
104 	--in1;
105 	--in2;
106 	--out1;
107 	--out2;
108 	while (--sampleFrames >= 0)  //had to disable optimization /Og in MSVC++5!
109 	{
110 		a = *++in1;
111 		b = *++in2;
112 
113 		c = y * a;
114 		d = y * b;
115 
116 		--p0 &= l;
117 		*(buf + p0) = w * (a + b);      //input
118 
119 		p1 -= d1;
120 		if (p1<0.0f) p1 += lf;           //output
121 		p1i = (int32)p1;
122 		p1f = p1 - (float)p1i;
123 		a = *(buf + p1i);
124 		++p1i &= l;
125 		a += p1f * (*(buf + p1i) - a);  //linear interpolation
126 
127 		p2i = (p1i + lh) & l;           //180-degree ouptut
128 		b = *(buf + p2i);
129 		++p2i &= l;
130 		b += p1f * (*(buf + p2i) - b);  //linear interpolation
131 
132 		p2i = (p1i - p0) & l;           //crossfade
133 		x = *(win + p2i);
134 		//++p2i &= l;
135 		//x += p1f * (*(win + p2i) - x); //linear interpolation (doesn't do much)
136 		c += b + x * (a - b);
137 
138 		p2 -= d2;  //repeat for downwards shift - can't see a more efficient way?
139 		if (p2<0.0f) p2 += lf;           //output
140 		p1i = (int32)p2;
141 		p1f = p2 - (float)p1i;
142 		a = *(buf + p1i);
143 		++p1i &= l;
144 		a += p1f * (*(buf + p1i) - a);  //linear interpolation
145 
146 		p2i = (p1i + lh) & l;           //180-degree ouptut
147 		b = *(buf + p2i);
148 		++p2i &= l;
149 		b += p1f * (*(buf + p2i) - b);  //linear interpolation
150 
151 		p2i = (p1i - p0) & l;           //crossfade
152 		x = *(win + p2i);
153 		//++p2i &= l;
154 		//x += p1f * (*(win + p2i) - x); //linear interpolation (doesn't do much)
155 		d += b + x * (a - b);
156 
157 		*++out1 = c;
158 		*++out2 = d;
159 	}
160 	pos0=p0; pos1=p1; pos2=p2;
161 }
162 
163 //-----------------------------------------------------------------------------
recalculate()164 void DetuneProcessor::recalculate ()
165 {
166 	int32 tmp;
167 
168 	semi = 3.0f * params[0] * params[0] * params[0];
169 	dpos2 = (float)pow (1.0594631f, semi);
170 	dpos1 = 1.0f / dpos2;
171 
172 	wet = (float)pow (10.0f, (float)(2.0f * params[2] - 1.0f));
173 	dry = wet - wet * params[1] * params[1];
174 	wet = (wet + wet - wet * params[1]) * params[1];
175 
176 	tmp = 1 << (8 + (int32)(4.9f * params[3]));
177 
178 	if (tmp!=buflen) //recalculate crossfade window
179 	{
180 		buflen = tmp;
181 		bufres = 1000.0f * (float)buflen / getSampleRate ();
182 
183 		int32 i; //hanning half-overlap-and-add
184 		double p=0.0, dp=6.28318530718/buflen;
185 		for(i = 0;i<buflen;i++) { win[i] = (float)(0.5 - 0.5 * cos(p)); p+=dp; }
186 	}
187 }
188 
189 }}} // namespaces
190 
191