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
|
/*
* Echo.cpp
* --------
* Purpose: Implementation of the DMO Echo DSP (for non-Windows platforms)
* Notes : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#ifndef NO_PLUGINS
#include "../../Sndfile.h"
#include "Echo.h"
#endif // !NO_PLUGINS
OPENMPT_NAMESPACE_BEGIN
#ifndef NO_PLUGINS
namespace DMO
{
IMixPlugin* Echo::Create(VSTPluginLib &factory, CSoundFile &sndFile, SNDMIXPLUGIN *mixStruct)
{
return new (std::nothrow) Echo(factory, sndFile, mixStruct);
}
Echo::Echo(VSTPluginLib &factory, CSoundFile &sndFile, SNDMIXPLUGIN *mixStruct)
: IMixPlugin(factory, sndFile, mixStruct)
, m_bufferSize(0)
, m_writePos(0)
, m_sampleRate(sndFile.GetSampleRate())
, m_initialFeedback(0.0f)
{
m_param[kEchoWetDry] = 0.5f;
m_param[kEchoFeedback] = 0.5f;
m_param[kEchoLeftDelay] = (500.0f - 1.0f) / 1999.0f;
m_param[kEchoRightDelay] = (500.0f - 1.0f) / 1999.0f;
m_param[kEchoPanDelay] = 0.0f;
m_mixBuffer.Initialize(2, 2);
InsertIntoFactoryList();
}
void Echo::Process(float *pOutL, float *pOutR, uint32 numFrames)
{
if(!m_bufferSize || !m_mixBuffer.Ok())
return;
const float wetMix = m_param[kEchoWetDry], dryMix = 1 - wetMix;
const float *in[2] = { m_mixBuffer.GetInputBuffer(0), m_mixBuffer.GetInputBuffer(1) };
float *out[2] = { m_mixBuffer.GetOutputBuffer(0), m_mixBuffer.GetOutputBuffer(1) };
for(uint32 i = numFrames; i != 0; i--)
{
for(uint8 channel = 0; channel < 2; channel++)
{
const uint8 readChannel = (m_crossEcho ? (1 - channel) : channel);
int readPos = m_writePos - m_delayTime[readChannel];
if(readPos < 0)
readPos += m_bufferSize;
float chnInput = *(in[channel])++;
float chnDelay = m_delayLine[readPos * 2 + readChannel];
// Calculate the delay
float chnOutput = chnInput * m_initialFeedback;
chnOutput += chnDelay * m_param[kEchoFeedback];
// Prevent denormals
if(std::abs(chnOutput) < 1e-24f)
chnOutput = 0.0f;
m_delayLine[m_writePos * 2 + channel] = chnOutput;
// Output samples now
*(out[channel])++ = (chnInput * dryMix + chnDelay * wetMix);
}
m_writePos++;
if(m_writePos == m_bufferSize)
m_writePos = 0;
}
ProcessMixOps(pOutL, pOutR, m_mixBuffer.GetOutputBuffer(0), m_mixBuffer.GetOutputBuffer(1), numFrames);
}
PlugParamValue Echo::GetParameter(PlugParamIndex index)
{
if(index < kEchoNumParameters)
{
return m_param[index];
}
return 0;
}
void Echo::SetParameter(PlugParamIndex index, PlugParamValue value)
{
if(index < kEchoNumParameters)
{
value = mpt::safe_clamp(value, 0.0f, 1.0f);
if(index == kEchoPanDelay)
value = mpt::round(value);
m_param[index] = value;
RecalculateEchoParams();
}
}
void Echo::Resume()
{
m_isResumed = true;
m_sampleRate = m_SndFile.GetSampleRate();
RecalculateEchoParams();
PositionChanged();
}
void Echo::PositionChanged()
{
m_bufferSize = m_sampleRate * 2u;
try
{
m_delayLine.assign(m_bufferSize * 2, 0);
} catch(mpt::out_of_memory e)
{
mpt::delete_out_of_memory(e);
m_bufferSize = 0;
}
m_writePos = 0;
}
#ifdef MODPLUG_TRACKER
CString Echo::GetParamName(PlugParamIndex param)
{
switch(param)
{
case kEchoWetDry: return _T("WetDryMix");
case kEchoFeedback: return _T("Feedback");
case kEchoLeftDelay: return _T("LeftDelay");
case kEchoRightDelay: return _T("RightDelay");
case kEchoPanDelay: return _T("PanDelay");
}
return CString();
}
CString Echo::GetParamLabel(PlugParamIndex param)
{
switch(param)
{
case kEchoFeedback:
return _T("%");
case kEchoLeftDelay:
case kEchoRightDelay:
return _T("ms");
default:
return CString{};
}
}
CString Echo::GetParamDisplay(PlugParamIndex param)
{
CString s;
switch(param)
{
case kEchoWetDry:
s.Format(_T("%.1f : %.1f"), m_param[param] * 100.0f, 100.0f - m_param[param] * 100.0f);
break;
case kEchoFeedback:
s.Format(_T("%.2f"), m_param[param] * 100.0f);
break;
case kEchoLeftDelay:
case kEchoRightDelay:
s.Format(_T("%.2f"), 1.0f + m_param[param] * 1999.0f);
break;
case kEchoPanDelay:
s = (m_param[param] <= 0.5) ? _T("No") : _T("Yes");
}
return s;
}
#endif // MODPLUG_TRACKER
void Echo::RecalculateEchoParams()
{
m_initialFeedback = std::sqrt(1.0f - (m_param[kEchoFeedback] * m_param[kEchoFeedback]));
m_delayTime[0] = static_cast<uint32>((1.0f + m_param[kEchoLeftDelay] * 1999.0f) / 1000.0f * m_sampleRate);
m_delayTime[1] = static_cast<uint32>((1.0f + m_param[kEchoRightDelay] * 1999.0f) / 1000.0f * m_sampleRate);
m_crossEcho = (m_param[kEchoPanDelay]) > 0.5f;
}
} // namespace DMO
#else
MPT_MSVC_WORKAROUND_LNK4221(Echo)
#endif // !NO_PLUGINS
OPENMPT_NAMESPACE_END
|