Skip to content

Commit d28bb08

Browse files
committed
FFTReal: PSRAM support
1 parent 5c1644f commit d28bb08

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

src/AudioTools/AudioLibs/AudioFFT.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ class AudioFFTBase : public AudioStream {
259259
l_magnitudes.resize(0);
260260
rfft_data.resize(0);
261261
rfft_add.resize(0);
262+
step_data.resize(0);
262263
}
263264

264265
/// Provide the audio data as FFT input
@@ -433,6 +434,7 @@ class AudioFFTBase : public AudioStream {
433434
unsigned long timestamp = 0l;
434435
RingBuffer<uint8_t> stride_buffer{0};
435436
Vector<float> l_magnitudes{0};
437+
Vector<float> step_data{0};
436438
int bins = 0;
437439
RingBuffer<uint8_t> rfft_data{0};
438440
FFTInverseOverlapAdder rfft_add{0};
@@ -506,25 +508,25 @@ class AudioFFTBase : public AudioStream {
506508
void rfftWriteData(BaseBuffer<uint8_t> &data) {
507509
int step_size = cfg.stride > 0 ? cfg.stride : cfg.length;
508510
// get data to result buffer
509-
float step_data[step_size];
511+
step_data.resize(step_size);
510512
for (int j = 0; j < step_size; j++) {
511513
step_data[j] = 0.0;
512514
}
513-
rfft_add.getStepData(step_data, step_size,
515+
rfft_add.getStepData(step_data.data(), step_size,
514516
NumberConverter::maxValue(cfg.bits_per_sample));
515517

516518
switch (cfg.bits_per_sample) {
517519
case 8:
518-
writeIFFT<int8_t>(step_data, step_size);
520+
writeIFFT<int8_t>(step_data.data(), step_size);
519521
break;
520522
case 16:
521-
writeIFFT<int16_t>(step_data, step_size);
523+
writeIFFT<int16_t>(step_data.data(), step_size);
522524
break;
523525
case 24:
524-
writeIFFT<int24_t>(step_data, step_size);
526+
writeIFFT<int24_t>(step_data.data(), step_size);
525527
break;
526528
case 32:
527-
writeIFFT<int32_t>(step_data, step_size);
529+
writeIFFT<int32_t>(step_data.data(), step_size);
528530
break;
529531
default:
530532
LOGE("Unsupported bits: %d", cfg.bits_per_sample);

src/AudioTools/AudioLibs/FFT/FFTReal.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ To Public License, Version 2, as published by Sam Hocevar. See
1414
*Tab=3***********************************************************************/
1515
#pragma GCC diagnostic ignored "-Wdeprecated-enum-enum-conversion"
1616

17+
// We use the DefaultAllocator which supports PSRAM
18+
#define FFT_CUSTOM_ALLOC DefaultAllocator
19+
1720

1821
#if ! defined (ffft_FFTReal_HEADER_INCLUDED)
1922
#define ffft_FFTReal_HEADER_INCLUDED
@@ -240,7 +243,11 @@ DynArray <T>::DynArray (long size)
240243
assert (size >= 0);
241244
if (size > 0)
242245
{
246+
#ifdef FFT_CUSTOM_ALLOC
247+
_data_ptr = FFT_CUSTOM_ALLOC.createArray<DataType>(size);
248+
#else
243249
_data_ptr = new DataType [size];
250+
#endif
244251
_len = size;
245252
}
246253
}
@@ -250,7 +257,11 @@ DynArray <T>::DynArray (long size)
250257
template <class T>
251258
DynArray <T>::~DynArray ()
252259
{
260+
#ifdef FFT_CUSTOM_ALLOC
261+
FFT_CUSTOM_ALLOC.removeArray<DataType>(_data_ptr, _len);
262+
#else
253263
delete [] _data_ptr;
264+
#endif
254265
_data_ptr = 0;
255266
_len = 0;
256267
}
@@ -272,12 +283,19 @@ void DynArray <T>::resize (long size)
272283
if (size > 0)
273284
{
274285
DataType * old_data_ptr = _data_ptr;
286+
#ifdef FFT_CUSTOM_ALLOC
287+
DataType * tmp_data_ptr = FFT_CUSTOM_ALLOC.createArray<DataType>(size);
288+
#else
275289
DataType * tmp_data_ptr = new DataType [size];
276-
290+
#endif
277291
_data_ptr = tmp_data_ptr;
278292
_len = size;
279293

294+
#ifdef FFT_CUSTOM_ALLOC
295+
FFT_CUSTOM_ALLOC.removeArray<DataType>(old_data_ptr, _len);
296+
#else
280297
delete [] old_data_ptr;
298+
#endif
281299
}
282300
}
283301

@@ -1973,7 +1991,11 @@ DynArray <T>::DynArray (long size)
19731991
assert (size >= 0);
19741992
if (size > 0)
19751993
{
1994+
#ifdef FFT_CUSTOM_ALLOC
1995+
_data_ptr = FFT_CUSTOM_ALLOC.createArray<DataType>(size);
1996+
#else
19761997
_data_ptr = new DataType [size];
1998+
#endif
19771999
_len = size;
19782000
}
19792001
}
@@ -1983,7 +2005,11 @@ DynArray <T>::DynArray (long size)
19832005
template <class T>
19842006
DynArray <T>::~DynArray ()
19852007
{
2008+
#ifdef FFT_CUSTOM_ALLOC
2009+
FFT_CUSTOM_ALLOC.removeArray<T>(_data_ptr, _len);
2010+
#else
19862011
delete [] _data_ptr;
2012+
#endif
19872013
_data_ptr = 0;
19882014
_len = 0;
19892015
}
@@ -2005,12 +2031,18 @@ void DynArray <T>::resize (long size)
20052031
if (size > 0)
20062032
{
20072033
DataType * old_data_ptr = _data_ptr;
2034+
#ifdef FFT_CUSTOM_ALLOC
2035+
#else
20082036
DataType * tmp_data_ptr = new DataType [size];
2037+
#endif
20092038

20102039
_data_ptr = tmp_data_ptr;
20112040
_len = size;
20122041

2042+
#ifdef FFT_CUSTOM_ALLOC
2043+
#else
20132044
delete [] old_data_ptr;
2045+
#endif
20142046
}
20152047
}
20162048

0 commit comments

Comments
 (0)