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
|
#include "opl_chip_base.h"
#include <cmath>
#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
#include <zita-resampler/vresampler.h>
#endif
/* OPLChipBase */
inline OPLChipBase::OPLChipBase() :
m_rate(44100)
{
}
inline OPLChipBase::~OPLChipBase()
{
}
/* OPLChipBaseT */
template <class T>
OPLChipBaseT<T>::OPLChipBaseT()
: OPLChipBase()
{
#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
m_resampler = new VResampler;
#endif
setupResampler(m_rate);
}
template <class T>
OPLChipBaseT<T>::~OPLChipBaseT()
{
#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
delete m_resampler;
#endif
}
template <class T>
void OPLChipBaseT<T>::setRate(uint32_t rate)
{
uint32_t oldRate = m_rate;
m_rate = rate;
if(rate != oldRate)
setupResampler(rate);
else
resetResampler();
}
template <class T>
void OPLChipBaseT<T>::reset()
{
resetResampler();
}
template <class T>
void OPLChipBaseT<T>::generate(int16_t *output, size_t frames)
{
static_cast<T *>(this)->nativePreGenerate();
for(size_t i = 0; i < frames; ++i)
{
int32_t frame[2];
static_cast<T *>(this)->resampledGenerate(frame);
for (unsigned c = 0; c < 2; ++c) {
int32_t temp = frame[c];
temp = (temp > -32768) ? temp : -32768;
temp = (temp < 32767) ? temp : 32767;
output[c] = temp;
}
}
static_cast<T *>(this)->nativePostGenerate();
}
template <class T>
void OPLChipBaseT<T>::generateAndMix(int16_t *output, size_t frames)
{
static_cast<T *>(this)->nativePreGenerate();
for(size_t i = 0; i < frames; ++i)
{
int32_t frame[2];
static_cast<T *>(this)->resampledGenerate(frame);
for (unsigned c = 0; c < 2; ++c) {
int32_t temp = (int32_t)output[c] + frame[c];
temp = (temp > -32768) ? temp : -32768;
temp = (temp < 32767) ? temp : 32767;
output[c] = temp;
}
}
static_cast<T *>(this)->nativePostGenerate();
}
template <class T>
void OPLChipBaseT<T>::generate32(int32_t *output, size_t frames)
{
static_cast<T *>(this)->nativePreGenerate();
for(size_t i = 0; i < frames; ++i)
{
static_cast<T *>(this)->resampledGenerate(output);
output += 2;
}
static_cast<T *>(this)->nativePostGenerate();
}
template <class T>
void OPLChipBaseT<T>::generateAndMix32(int32_t *output, size_t frames)
{
static_cast<T *>(this)->nativePreGenerate();
for(size_t i = 0; i < frames; ++i)
{
int32_t frame[2];
static_cast<T *>(this)->resampledGenerate(frame);
output[0] += frame[0];
output[1] += frame[1];
output += 2;
}
static_cast<T *>(this)->nativePostGenerate();
}
template <class T>
void OPLChipBaseT<T>::setupResampler(uint32_t rate)
{
#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
m_resampler->setup(rate * (1.0 / 49716), 2, 48);
#else
m_oldsamples[0] = m_oldsamples[1] = 0;
m_samples[0] = m_samples[1] = 0;
m_samplecnt = 0;
m_rateratio = (int32_t)((rate << rsm_frac) / 49716);
#endif
}
template <class T>
void OPLChipBaseT<T>::resetResampler()
{
#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
m_resampler->reset();
#else
m_oldsamples[0] = m_oldsamples[1] = 0;
m_samples[0] = m_samples[1] = 0;
m_samplecnt = 0;
#endif
}
#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
template <class T>
void OPLChipBaseT<T>::resampledGenerate(int32_t *output)
{
VResampler *rsm = m_resampler;
float scale = (float)T::resamplerPreAmplify /
(float)T::resamplerPostAttenuate;
float f_in[2];
float f_out[2];
rsm->inp_count = 0;
rsm->inp_data = f_in;
rsm->out_count = 1;
rsm->out_data = f_out;
while(rsm->process(), rsm->out_count != 0)
{
int16_t in[2];
static_cast<T *>(this)->nativeGenerate(in);
f_in[0] = scale * (float)in[0];
f_in[1] = scale * (float)in[1];
rsm->inp_count = 1;
rsm->inp_data = f_in;
rsm->out_count = 1;
rsm->out_data = f_out;
}
output[0] = std::lround(f_out[0]);
output[1] = std::lround(f_out[1]);
}
#else
template <class T>
void OPLChipBaseT<T>::resampledGenerate(int32_t *output)
{
int32_t samplecnt = m_samplecnt;
const int32_t rateratio = m_rateratio;
while(samplecnt >= rateratio)
{
m_oldsamples[0] = m_samples[0];
m_oldsamples[1] = m_samples[1];
int16_t buffer[2];
static_cast<T *>(this)->nativeGenerate(buffer);
m_samples[0] = buffer[0] * T::resamplerPreAmplify;
m_samples[1] = buffer[1] * T::resamplerPreAmplify;
samplecnt -= rateratio;
}
output[0] = (int32_t)(((m_oldsamples[0] * (rateratio - samplecnt)
+ m_samples[0] * samplecnt) / rateratio)/T::resamplerPostAttenuate);
output[1] = (int32_t)(((m_oldsamples[1] * (rateratio - samplecnt)
+ m_samples[1] * samplecnt) / rateratio)/T::resamplerPostAttenuate);
m_samplecnt = samplecnt + (1 << rsm_frac);
}
#endif
/* OPLChipBaseBufferedT */
template <class T, unsigned Buffer>
void OPLChipBaseBufferedT<T, Buffer>::reset()
{
OPLChipBaseT<T>::reset();
m_bufferIndex = 0;
}
template <class T, unsigned Buffer>
void OPLChipBaseBufferedT<T, Buffer>::nativeGenerate(int16_t *frame)
{
unsigned bufferIndex = m_bufferIndex;
if(bufferIndex == 0)
static_cast<T *>(this)->nativeGenerateN(m_buffer, Buffer);
frame[0] = m_buffer[2 * bufferIndex];
frame[1] = m_buffer[2 * bufferIndex + 1];
bufferIndex = (bufferIndex + 1 < Buffer) ? (bufferIndex + 1) : 0;
m_bufferIndex = bufferIndex;
}
|