Skip to content

Commit 8d81700

Browse files
committed
plugin: Directly output resampled frames to host within Resample_f32()
In my resample procedure, Resample_f32() will finally output the same amount of frames as the buffer size. So why not directly specify host audio buffer as Resample_f32()'s argument "output"? By directly writing to final audio buffer, intermediate buffers are no longer required. So I dropped `buffer_after_resample(l|r)`. Time for copying sample frames is also saved, as the final for-loop is also no longer needed. For sanity check, in Resample_f32(), I added a new parameter `maxOutputSize`, which acts like strncpy() and snprintf(). Just in case the output buffer size is larger than audio buffer size.
1 parent fd07b44 commit 8d81700

File tree

4 files changed

+7
-19
lines changed

4 files changed

+7
-19
lines changed

plugin/MinatonPlugin.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,8 @@ void MinatonPlugin::run(const float** inputs, float** outputs, uint32_t frames,
131131
// Process each channel respectively.
132132
// Note: The resampler functions are originally designed for interleaved WAV files.
133133
const int channels = 1;
134-
Resample_f32(buffer_before_resample_l, buffer_after_resample_l, 44100, int(fSampleRate), expect_input_size / channels, channels);
135-
Resample_f32(buffer_before_resample_r, buffer_after_resample_r, 44100, int(fSampleRate), expect_input_size / channels, channels);
136-
137-
// NOTICE: Do not use memcpy(). Use for-loop.
138-
// Otherwise the output samples will not be continuous!
139-
for (uint32_t x = 0; x < frames; x++) {
140-
outputs[0][x] = buffer_after_resample_l[x];
141-
outputs[1][x] = buffer_after_resample_r[x];
142-
}
134+
Resample_f32(buffer_before_resample_l, outputs[0], 44100, int(fSampleRate), expect_input_size / channels, channels, frames);
135+
Resample_f32(buffer_before_resample_r, outputs[1], 44100, int(fSampleRate), expect_input_size / channels, channels, frames);
143136
}
144137
}
145138

@@ -171,25 +164,18 @@ void MinatonPlugin::initResampler(uint32_t bufferSize)
171164
{
172165
buffer_before_resample_l = (float*)malloc(sizeof(float) * bufferSize);
173166
buffer_before_resample_r = (float*)malloc(sizeof(float) * bufferSize);
174-
buffer_after_resample_l = (float*)malloc(sizeof(float) * bufferSize);
175-
buffer_after_resample_r = (float*)malloc(sizeof(float) * bufferSize);
176167
}
177168

178169
void MinatonPlugin::reinitResampler(uint32_t bufferSize, uint32_t sampleRate)
179170
{
180171
buffer_before_resample_l = (float*)realloc(buffer_before_resample_l, sizeof(float) * bufferSize);
181172
buffer_before_resample_r = (float*)realloc(buffer_before_resample_r, sizeof(float) * bufferSize);
182-
183-
buffer_after_resample_l = (float*)realloc(buffer_after_resample_l, sizeof(float) * bufferSize);
184-
buffer_after_resample_r = (float*)realloc(buffer_after_resample_r, sizeof(float) * bufferSize);
185173
}
186174

187175
void MinatonPlugin::cleanupResampler()
188176
{
189177
free(buffer_before_resample_l);
190178
free(buffer_before_resample_r);
191-
free(buffer_after_resample_l);
192-
free(buffer_after_resample_r);
193179
}
194180

195181
Plugin* createPlugin()

plugin/MinatonPlugin.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class MinatonPlugin : public Plugin {
2424

2525
// Resampler data
2626
float *buffer_before_resample_l, *buffer_before_resample_r;
27-
float *buffer_after_resample_l, *buffer_after_resample_r;
2827

2928
public:
3029
MinatonPlugin();

vendor/resampler/resampler.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ uint64_t Resample_f32(const float* input, float* output, int inSampleRate, int o
3535
}
3636

3737
uint64_t Resample_f32(const float* input, float* output, int inSampleRate, int outSampleRate, double inputSize,
38-
uint32_t channels)
38+
uint32_t channels, uint32_t maxOutputSize)
3939
{
4040
if (input == NULL)
4141
return 0;
@@ -57,6 +57,9 @@ uint64_t Resample_f32(const float* input, float* output, int inSampleRate, int o
5757
if (output == NULL)
5858
return outputSize;
5959

60+
// Prevent out-of-range exceptions
61+
outputSize = (outputSize > maxOutputSize) ? maxOutputSize : outputSize;
62+
6063
double stepDist = ((double)inSampleRate / (double)outSampleRate);
6164
const uint64_t fixedFraction = (1LL << 32);
6265
const double normFixed = (1.0 / (1LL << 32));

vendor/resampler/resampler.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ uint64_t Resample_f32(const float* input, float* output, int inSampleRate, int o
2020
uint32_t channels);
2121

2222
uint64_t Resample_f32(const float* input, float* output, int inSampleRate, int outSampleRate, double inputSize,
23-
uint32_t channels);
23+
uint32_t channels, uint32_t maxOutputSize);
2424

2525
uint64_t Resample_s16(const int16_t* input, int16_t* output, int inSampleRate, int outSampleRate, uint64_t inputSize,
2626
uint32_t channels);

0 commit comments

Comments
 (0)