@@ -291,6 +291,13 @@ class ResampleParameterEstimator {
291
291
LOGI (" div: %d, fact %d -> rate: %d, rate_eff: %f" , div,fact,to_rate, to_rate_eff);
292
292
}
293
293
};
294
+ /* *
295
+ * @brief Configuration for ResampleStream
296
+ *
297
+ */
298
+ struct ResampleConfig : public AudioBaseInfo {
299
+ int sample_rate_from=0 ;
300
+ };
294
301
295
302
/* *
296
303
* @brief Stream class which can be used to resample between different sample rates.
@@ -299,7 +306,7 @@ class ResampleParameterEstimator {
299
306
* @tparam T data type of audio data
300
307
*/
301
308
template <typename T>
302
- class ResampleStream : public AudioStreamX {
309
+ class ResampleStream : public AudioStreamX , AudioBaseInfoDependent {
303
310
public:
304
311
ResampleStream (Print &out, ResamplePrecision precision = Medium){
305
312
this ->precision = precision;
@@ -313,11 +320,51 @@ class ResampleStream : public AudioStreamX {
313
320
down.setIn (in); // so that we can downsample to the requested rate
314
321
}
315
322
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);
319
338
up.begin (channels, calc.upsample ());
320
339
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);
321
368
}
322
369
323
370
// / Determines the number of bytes which are available for write
@@ -336,6 +383,7 @@ class ResampleStream : public AudioStreamX {
336
383
}
337
384
338
385
protected:
386
+ int channels, from_rate, to_rate;
339
387
ResampleParameterEstimator calc;
340
388
Resample<T> up;
341
389
Resample<T> down;
0 commit comments