Skip to content

Commit 3b85fb6

Browse files
committed
ResampleConfig
1 parent b1404eb commit 3b85fb6

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

src/AudioTools/Resample.h

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ class ResampleParameterEstimator {
291291
LOGI("div: %d, fact %d -> rate: %d, rate_eff: %f", div,fact,to_rate, to_rate_eff);
292292
}
293293
};
294+
/**
295+
* @brief Configuration for ResampleStream
296+
*
297+
*/
298+
struct ResampleConfig : public AudioBaseInfo {
299+
int sample_rate_from=0;
300+
};
294301

295302
/**
296303
* @brief Stream class which can be used to resample between different sample rates.
@@ -299,7 +306,7 @@ class ResampleParameterEstimator {
299306
* @tparam T data type of audio data
300307
*/
301308
template<typename T>
302-
class ResampleStream : public AudioStreamX {
309+
class ResampleStream : public AudioStreamX, AudioBaseInfoDependent {
303310
public:
304311
ResampleStream(Print &out, ResamplePrecision precision = Medium){
305312
this->precision = precision;
@@ -313,11 +320,51 @@ class ResampleStream : public AudioStreamX {
313320
down.setIn(in); // so that we can downsample to the requested rate
314321
}
315322

316-
/// Defines the channels and sample rates
317-
void begin(int channels, int fromRate, int toRate){
318-
calc.begin(fromRate, toRate, precision);
323+
// Recalculates the up and downsamplers
324+
bool begin() {
325+
if (channels==0){
326+
LOGE("channels are not defined")
327+
return false;
328+
}
329+
if (from_rate==0){
330+
LOGE("from_rate is not defined")
331+
return false;
332+
}
333+
if (to_rate==0){
334+
LOGE("to_rate is not defined")
335+
return false;
336+
}
337+
calc.begin(from_rate, to_rate, precision);
319338
up.begin(channels, calc.upsample());
320339
down.begin(channels, calc.downsample());
340+
return true;
341+
}
342+
343+
/// Defines the channels and sample rates
344+
bool begin(int channels, int fromRate, int toRate){
345+
this->channels = channels;
346+
this->from_rate = fromRate;
347+
this->to_rate = toRate;
348+
return begin();
349+
}
350+
351+
/// Handle Change of audio rate or channels
352+
void setAudioInfo(AudioBaseInfo info) override {
353+
from_rate = info.sample_rate;
354+
channels = info.channels;
355+
begin();
356+
}
357+
358+
/// Returns an empty configuration object
359+
ResampleConfig defaultConfig() {
360+
ResampleConfig cfg;
361+
return cfg;
362+
}
363+
364+
/// Defines the channels and sample rates from the AudioBaseInfo. Please call setFromInfo() or setFromSampleRate() before
365+
void begin(ResampleConfig info){
366+
from_rate = info.sample_rate_from;
367+
setAudioInfo(info);
321368
}
322369

323370
/// Determines the number of bytes which are available for write
@@ -336,6 +383,7 @@ class ResampleStream : public AudioStreamX {
336383
}
337384

338385
protected:
386+
int channels, from_rate, to_rate;
339387
ResampleParameterEstimator calc;
340388
Resample<T> up;
341389
Resample<T> down;

0 commit comments

Comments
 (0)